Implement no_std mode

This only exports the hash itself, omitting the HashMap re-exports.

Essentially a rebase of #7.
This commit is contained in:
Mark Rousskov 2020-02-08 08:17:43 -05:00
parent 18411e8d6e
commit 89ce6783ea
3 changed files with 37 additions and 6 deletions

View File

@ -7,3 +7,7 @@ license = "Apache-2.0/MIT"
readme = "README.md"
keywords = ["hash", "fxhash", "rustc"]
repository = "https://github.com/rust-lang-nursery/rustc-hash"
[features]
std = []
default = ["std"]

View File

@ -24,3 +24,15 @@ use rustc_hash::FxHashMap;
let mut map: FxHashMap<u32, u32> = FxHashMap::default();
map.insert(22, 44);
```
### `no_std`
This crate can be used as a `no_std` crate by disabling the `std`
feature, which is on by default, as follows:
```toml
rustc-hash = { version = "1.0", default-features = false }
```
In this configuration, `FxHasher` is the only export, and the
`FxHashMap`/`FxHashSet` type aliases are omitted.

View File

@ -13,22 +13,37 @@
//! # Example
//!
//! ```rust
//! # #[cfg(feature = "std")]
//! # fn main() {
//! use rustc_hash::FxHashMap;
//! let mut map: FxHashMap<u32, u32> = FxHashMap::default();
//! map.insert(22, 44);
//! # }
//! # #[cfg(not(feature = "std"))]
//! # fn main() { }
//! ```
#![no_std]
#[cfg(feature = "std")]
extern crate std;
use core::convert::TryInto;
use core::default::Default;
#[cfg(feature = "std")]
use core::hash::BuildHasherDefault;
use core::hash::Hasher;
use core::mem::size_of;
use core::ops::BitXor;
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::default::Default;
use std::hash::{Hasher, BuildHasherDefault};
use std::ops::BitXor;
use std::mem::size_of;
/// Type alias for a hashmap using the `fx` hash algorithm.
#[cfg(feature = "std")]
pub type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
/// Type alias for a hashmap using the `fx` hash algorithm.
#[cfg(feature = "std")]
pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
/// A speedy hash algorithm for use within rustc. The hashmap in liballoc
@ -43,7 +58,7 @@ pub type FxHashSet<V> = HashSet<V, BuildHasherDefault<FxHasher>>;
/// similar or slightly worse than FNV, but the speed of the hash function
/// itself is much higher because it works on up to 8 bytes at a time.
pub struct FxHasher {
hash: usize
hash: usize,
}
#[cfg(target_pointer_width = "32")]