diff --git a/Cargo.toml b/Cargo.toml index fb0ec67..c1fe4a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] diff --git a/README.md b/README.md index a672999..e33057a 100644 --- a/README.md +++ b/README.md @@ -24,3 +24,15 @@ use rustc_hash::FxHashMap; let mut map: FxHashMap = 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. diff --git a/src/lib.rs b/src/lib.rs index d8b4795..ee9ad31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,22 +13,37 @@ //! # Example //! //! ```rust +//! # #[cfg(feature = "std")] +//! # fn main() { //! use rustc_hash::FxHashMap; //! let mut map: FxHashMap = 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 = HashMap>; /// Type alias for a hashmap using the `fx` hash algorithm. +#[cfg(feature = "std")] pub type FxHashSet = HashSet>; /// A speedy hash algorithm for use within rustc. The hashmap in liballoc @@ -43,7 +58,7 @@ pub type FxHashSet = HashSet>; /// 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")]