Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for no_std compilation through an "std" feature #22

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ documentation = "https://doc.servo.org/fnv/"
[lib]
name = "fnv"
path = "lib.rs"

[features]
default = ["std"]
std = []
94 changes: 56 additions & 38 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,66 @@
//! denial-of-service attacks, and can assume that its inputs are going to be
//! small—a perfect use case for FNV.
//!
//!
//! ## Using FNV in a `HashMap`
//!
//! The `FnvHashMap` type alias is the easiest way to use the standard library’s
//! `HashMap` with FNV.
//!
//! ```rust
//! use fnv::FnvHashMap;
//!
//! let mut map = FnvHashMap::default();
//! map.insert(1, "one");
//! map.insert(2, "two");
//!
//! map = FnvHashMap::with_capacity_and_hasher(10, Default::default());
//! map.insert(1, "one");
//! map.insert(2, "two");
//! ```
//!
//! Note, the standard library’s `HashMap::new` and `HashMap::with_capacity`
//! are only implemented for the `RandomState` hasher, so using `Default` to
//! get the hasher is the next best option.
//!
//! ## Using FNV in a `HashSet`
//!
//! Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet`
//! with FNV.
//!
//! ```rust
//! use fnv::FnvHashSet;
//!
//! let mut set = FnvHashSet::default();
//! set.insert(1);
//! set.insert(2);
//!
//! set = FnvHashSet::with_capacity_and_hasher(10, Default::default());
//! set.insert(1);
//! set.insert(2);
//! ```
#![cfg_attr(feature = "std", doc = r#"

## Using FNV in a `HashMap`

The `FnvHashMap` type alias is the easiest way to use the standard library’s
`HashMap` with FNV.

```rust
use fnv::FnvHashMap;

let mut map = FnvHashMap::default();
map.insert(1, "one");
map.insert(2, "two");

map = FnvHashMap::with_capacity_and_hasher(10, Default::default());
map.insert(1, "one");
map.insert(2, "two");
```

Note, the standard library’s `HashMap::new` and `HashMap::with_capacity`
are only implemented for the `RandomState` hasher, so using `Default` to
get the hasher is the next best option.

## Using FNV in a `HashSet`

Similarly, `FnvHashSet` is a type alias for the standard library’s `HashSet`
with FNV.

```rust
use fnv::FnvHashSet;

let mut set = FnvHashSet::default();
set.insert(1);
set.insert(2);

set = FnvHashSet::with_capacity_and_hasher(10, Default::default());
set.insert(1);
set.insert(2);
```
"#)]
//!
//! [chongo]: http://www.isthe.com/chongo/tech/comp/fnv/index.html
//! [faq]: https://www.rust-lang.org/en-US/faq.html#why-are-rusts-hashmaps-slow
//! [graphs]: https://cglab.ca/~abeinges/blah/hash-rs/

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(all(not(feature = "std"), test))]
extern crate alloc;

#[cfg(feature = "std")]
use std::default::Default;
#[cfg(feature = "std")]
use std::hash::{Hasher, BuildHasherDefault};
#[cfg(feature = "std")]
use std::collections::{HashMap, HashSet};
#[cfg(not(feature = "std"))]
use core::default::Default;
#[cfg(not(feature = "std"))]
use core::hash::{Hasher, BuildHasherDefault};

/// An implementation of the Fowler–Noll–Vo hash function.
///
Expand Down Expand Up @@ -115,16 +128,21 @@ impl Hasher for FnvHasher {
pub type FnvBuildHasher = BuildHasherDefault<FnvHasher>;

/// A `HashMap` using a default FNV hasher.
#[cfg(feature = "std")]
pub type FnvHashMap<K, V> = HashMap<K, V, FnvBuildHasher>;

/// A `HashSet` using a default FNV hasher.
#[cfg(feature = "std")]
pub type FnvHashSet<T> = HashSet<T, FnvBuildHasher>;


#[cfg(test)]
mod test {
use super::*;
#[cfg(feature = "std")]
use std::hash::Hasher;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

fn fnv1a(bytes: &[u8]) -> u64 {
let mut hasher = FnvHasher::default();
Expand Down