Skip to content

Commit 89ce678

Browse files
Implement no_std mode
This only exports the hash itself, omitting the HashMap re-exports. Essentially a rebase of rust-lang#7.
1 parent 18411e8 commit 89ce678

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ license = "Apache-2.0/MIT"
77
readme = "README.md"
88
keywords = ["hash", "fxhash", "rustc"]
99
repository = "https://github.com/rust-lang-nursery/rustc-hash"
10+
11+
[features]
12+
std = []
13+
default = ["std"]

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,15 @@ use rustc_hash::FxHashMap;
2424
let mut map: FxHashMap<u32, u32> = FxHashMap::default();
2525
map.insert(22, 44);
2626
```
27+
28+
### `no_std`
29+
30+
This crate can be used as a `no_std` crate by disabling the `std`
31+
feature, which is on by default, as follows:
32+
33+
```toml
34+
rustc-hash = { version = "1.0", default-features = false }
35+
```
36+
37+
In this configuration, `FxHasher` is the only export, and the
38+
`FxHashMap`/`FxHashSet` type aliases are omitted.

src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,37 @@
1313
//! # Example
1414
//!
1515
//! ```rust
16+
//! # #[cfg(feature = "std")]
17+
//! # fn main() {
1618
//! use rustc_hash::FxHashMap;
1719
//! let mut map: FxHashMap<u32, u32> = FxHashMap::default();
1820
//! map.insert(22, 44);
21+
//! # }
22+
//! # #[cfg(not(feature = "std"))]
23+
//! # fn main() { }
1924
//! ```
2025
26+
#![no_std]
27+
28+
#[cfg(feature = "std")]
29+
extern crate std;
30+
31+
use core::convert::TryInto;
32+
use core::default::Default;
33+
#[cfg(feature = "std")]
34+
use core::hash::BuildHasherDefault;
35+
use core::hash::Hasher;
36+
use core::mem::size_of;
37+
use core::ops::BitXor;
38+
#[cfg(feature = "std")]
2139
use std::collections::{HashMap, HashSet};
22-
use std::convert::TryInto;
23-
use std::default::Default;
24-
use std::hash::{Hasher, BuildHasherDefault};
25-
use std::ops::BitXor;
26-
use std::mem::size_of;
2740

2841
/// Type alias for a hashmap using the `fx` hash algorithm.
42+
#[cfg(feature = "std")]
2943
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
3044

3145
/// Type alias for a hashmap using the `fx` hash algorithm.
46+
#[cfg(feature = "std")]
3247
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
3348

3449
/// A speedy hash algorithm for use within rustc. The hashmap in liballoc
@@ -43,7 +58,7 @@ pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
4358
/// similar or slightly worse than FNV, but the speed of the hash function
4459
/// itself is much higher because it works on up to 8 bytes at a time.
4560
pub struct FxHasher {
46-
hash: usize
61+
hash: usize,
4762
}
4863

4964
#[cfg(target_pointer_width = "32")]

0 commit comments

Comments
 (0)