Skip to content

Commit 6facc9c

Browse files
committed
Prepare hashbrown for inclusion in the standard library
1 parent d40f49e commit 6facc9c

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
@@ -11,13 +11,15 @@ categories = ["data-structures", "no-std"]
1111
exclude = [".travis.yml", "bors.toml"]
1212

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

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

2830
[features]
2931
nightly = []
32+
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"))]
@@ -51,11 +49,16 @@ mod fx;
5149
mod map;
5250
mod raw;
5351
mod set;
52+
#[cfg(feature = "rustc-dep-of-std")]
53+
mod rustc_entry;
5454

5555
pub mod hash_map {
5656
//! A hash map implemented with quadratic probing and SIMD lookup.
5757
pub use map::*;
5858

59+
#[cfg(feature = "rustc-dep-of-std")]
60+
pub use rustc_entry::*;
61+
5962
#[cfg(feature = "rayon")]
6063
/// [rayon]-based parallel iterator types for hash maps.
6164
/// You will rarely need to interact with it directly unless you have need

0 commit comments

Comments
 (0)