Skip to content

Commit 4f1cfb0

Browse files
committed
Prepare hashbrown for inclusion in the standard library
1 parent 15ed229 commit 4f1cfb0

File tree

8 files changed

+1019
-391
lines changed

8 files changed

+1019
-391
lines changed

Cargo.toml

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ keywords = ["hash", "no_std", "hashmap", "swisstable"]
1010
categories = ["data-structures", "no-std"]
1111

1212
[dependencies]
13-
byteorder = { version = "1.0", default-features = false }
14-
scopeguard = { version = "0.3", default-features = false }
15-
1613
# For external trait impls
1714
rayon = { version = "1.0", optional = true }
1815
serde = { version = "1.0", default-features = false, optional = true }
1916

17+
# When built as part of libstd
18+
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" }
19+
compiler_builtins = { version = "0.1.2", optional = true }
20+
alloc = { version = "1.0.0", optional = true, package = "rustc-std-workspace-alloc" }
21+
2022
[dev-dependencies]
2123
lazy_static = "~1.2"
2224
rand = "0.5.1"
@@ -26,3 +28,4 @@ serde_test = "1.0"
2628

2729
[features]
2830
nightly = []
31+
rustc-dep-of-std = ["nightly", "core", "compiler_builtins", "alloc"]

src/fx.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use core::hash::{BuildHasherDefault, Hasher};
55
use core::mem::size_of;
66
use core::ops::BitXor;
77

8-
use byteorder::{ByteOrder, NativeEndian};
9-
108
/// Type alias for a `HashBuilder` using the `fx` hash algorithm.
119
pub type FxHashBuilder = BuildHasherDefault<FxHasher>;
1210

@@ -47,23 +45,29 @@ impl FxHasher {
4745
impl Hasher for FxHasher {
4846
#[inline]
4947
fn write(&mut self, mut bytes: &[u8]) {
50-
#[cfg(target_pointer_width = "32")]
51-
let read_usize = |bytes| NativeEndian::read_u32(bytes);
52-
#[cfg(target_pointer_width = "64")]
53-
let read_usize = |bytes| NativeEndian::read_u64(bytes);
48+
macro_rules! read_bytes {
49+
($ty:ty, $src:expr) => {{
50+
assert!(size_of::<$ty>() <= $src.len());
51+
let mut data: $ty = 0;
52+
unsafe {
53+
$src.as_ptr().copy_to_nonoverlapping(&mut data as *mut $ty as *mut u8, size_of::<$ty>());
54+
}
55+
data
56+
}};
57+
}
5458

5559
let mut hash = FxHasher { hash: self.hash };
5660
assert!(size_of::<usize>() <= 8);
5761
while bytes.len() >= size_of::<usize>() {
58-
hash.add_to_hash(read_usize(bytes) as usize);
62+
hash.add_to_hash(read_bytes!(usize, bytes) as usize);
5963
bytes = &bytes[size_of::<usize>()..];
6064
}
6165
if (size_of::<usize>() > 4) && (bytes.len() >= 4) {
62-
hash.add_to_hash(NativeEndian::read_u32(bytes) as usize);
66+
hash.add_to_hash(read_bytes!(u32, bytes) as usize);
6367
bytes = &bytes[4..];
6468
}
6569
if (size_of::<usize>() > 2) && bytes.len() >= 2 {
66-
hash.add_to_hash(NativeEndian::read_u16(bytes) as usize);
70+
hash.add_to_hash(read_bytes!(u16, bytes) as usize);
6771
bytes = &bytes[2..];
6872
}
6973
if (size_of::<usize>() > 1) && bytes.len() >= 1 {

src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@
2525
#![warn(missing_docs)]
2626

2727
#[cfg(test)]
28-
#[macro_use]
28+
#[cfg_attr(feature = "rayon", macro_use)]
2929
extern crate std;
3030
#[cfg(test)]
3131
extern crate rand;
3232

3333
#[cfg(feature = "nightly")]
3434
#[cfg_attr(test, macro_use)]
3535
extern crate alloc;
36-
extern crate byteorder;
3736
#[cfg(feature = "rayon")]
3837
extern crate rayon;
39-
extern crate scopeguard;
4038
#[cfg(feature = "serde")]
4139
extern crate serde;
4240
#[cfg(not(feature = "nightly"))]
@@ -48,11 +46,16 @@ mod fx;
4846
mod map;
4947
mod raw;
5048
mod set;
49+
#[cfg(feature = "rustc-dep-of-std")]
50+
mod rustc_entry;
5151

5252
pub mod hash_map {
5353
//! A hash map implemented with quadratic probing and SIMD lookup.
5454
pub use map::*;
5555

56+
#[cfg(feature = "rustc-dep-of-std")]
57+
pub use rustc_entry::*;
58+
5659
#[cfg(feature = "rayon")]
5760
/// [rayon]-based parallel iterator types for hash maps.
5861
/// You will rarely need to interact with it directly unless you have need

0 commit comments

Comments
 (0)