问题
Since SipHasher is too slow for my use case, I'm trying to implement a custom hash function. I found an example which I used as base to get everything compiling.
My current code looks like this:
use std::collections::hash_state::{DefaultState};
use std::collections::{HashMap, HashSet};
use std::default::Default;
use std::hash::{Hash, Hasher, SipHasher};
use std::marker;
pub struct FnvHasher(u64);
impl Default for FnvHasher {
fn default() -> FnvHasher { FnvHasher(0xcbf29ce484222325) }
}
impl Hasher for FnvHasher {
fn write(&mut self, bytes: &[u8]) {
let FnvHasher(mut hash) = *self;
for byte in bytes {
hash = hash ^ (*byte as u64);
hash = hash * 0x100000001b3;
}
*self = FnvHasher(hash);
}
fn finish(&self) -> u64 { self.0 }
}
fn main() {
let mut set:HashSet<i64, DefaultState<FnvHasher>> = HashSet::with_hash_state(DefaultState::<FnvHasher>);
}
When I compile I get the following error message:
$ rustc -V
rustc 1.0.0-nightly (522d09dfe 2015-02-19) (built 2015-02-19)
$ rustc hash.rs
hash.rs:26:86: 26:111 error: mismatched types:
expected `std::collections::hash::state::DefaultState<FnvHasher>`,
found `fn(core::marker::PhantomData<FnvHasher>) -> std::collections::hash::state::DefaultState<FnvHasher> {std::collections::hash::state::DefaultState}`
(expected struct `std::collections::hash::state::DefaultState`,
found fn item) [E0308]
hash.rs:26 let mut set:HashSet<i64, DefaultState<FnvHasher>> = HashSet::with_hash_state(DefaultState::<FnvHasher>);
^~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
How can I get the above example to compile or use a custom hash function in a HashSet/HashMap?
回答1:
Take a look at the definition of DefaultState
:
pub struct DefaultState<H>(marker::PhantomData<H>);
You are treating it as though it were as it used to be:
pub struct DefaultState<H>;
This change is recent; generic parameters must now be expressly used.
For this you must now use Default::default()
, for that field is private, preventing literal expressions from being written. Thus, DefaultState::<FnvHasher>
becomes Default::default()
.
And once you’re using Default::default()
for that, you might as well go the whole hog and replace HashSet::with_hash_state(Default::default())
with Default::default()
:
let mut set: HashSet<i64, DefaultState<FnvHasher>> = Default::default();
回答2:
Rust 1.7 stabilized the use of custom hashers. For example, you can use FnvHasher
from the fnv crate
extern crate fnv;
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use fnv::FnvHasher;
type HashMapFnv<K, V> = HashMap<K, V, BuildHasherDefault<FnvHasher>>;
fn main() {
let mut map = HashMapFnv::default();
map.insert(1, "Hello");
map.insert(2, ", world!");
println!("{:?}", map);
}
来源:https://stackoverflow.com/questions/28644070/how-can-i-use-a-custom-hash-function-in-a-hashset-or-hashmap