A suite of non-cryptographic hash functions for Rust, base on a forked smhasher.
To use fasthash
, first add this to your Cargo.toml
:
[dependencies]
fasthash = "0.2"
When use fasthash
128bit supports in Rust 1.15 (beta) or earlier version, we need enable i128
feature.
[dependencies.fasthash]
version = "0.2"
features = ["i128", "sse42"]
Then, add this to your crate root
extern crate fasthash;
use fasthash::*;
And then, use hash function with module or hasher
let h = city::hash64("hello world");
Or work with std::hash::Hash
use std::hash::Hash;
use fasthash::MetroHasher;
fn hash<T: Hash>(t: &T) -> u64 {
let mut s = MetroHasher::new();
t.hash(&mut s);
s.finish()
}
hash(&"hello world");
It also works with HashMap
or HashSet
, act as the hash function
use std::collections::HashSet;
use fasthash::spooky::SpookyHash128;
let mut set = HashSet::with_hasher(SpookyHash128 {});
set.insert(2);
Or use RandomState with a random seed.
use std::collections::HashMap;
use fasthash::RandomState;
use fasthash::city::CityHash64;
let s = RandomState::<CityHash64>::new();
let mut map = HashMap::with_hasher(s);
assert_eq!(map.insert(37, "a"), None);
assert_eq!(map.is_empty(), false);
map.insert(37, "b");
assert_eq!(map.insert(37, "c"), Some("b"));
assert_eq!(map[&37], "c");
- High performance
- Zero cost binding
- Compatibility with libstd/libcollection
- Modern Hash Functions
- Compatibility
To bench the hash function, we need nighly rust
$ rustup run nightly cargo bench
Please check smhasher reports for more details.