indexmap-rs/indexmap

Using new seems impossible with FxBuildHasher

Closed this issue · 2 comments

I'm not able to get this line of code to work, even though I want to use std and have the default crate features:

fn main() {
    let _ = indexmap::IndexMap::<u64, u64, fxhash::FxBuildHasher>::new();
}

This is unfortunate because it means indexmap::IndexMap<K, V, FxBuildHasher> isn't a drop-in replacement for std::collections::HashMap<K, V>. I'm currently using IndexMap::default() as a workaround, which does work.

Am I doing something wrong? I'm on Rust v1.72.0-nightly if that matters, and this is fxhash 0.2.1 and indexmap 1.9.3.

This is unfortunate because it means indexmap::IndexMap<K, V, FxBuildHasher> isn't a drop-in replacement for std::collections::HashMap<K, V>. I'm currently using IndexMap::default() as a workaround, which does work.

It's not a drop-in with your own hasher, but IndexMap<K, V> (default S = RandomState) is compatible.

This is the exact same behavior as std, where fn new is only implemented for HashMap<K, V, RandomState>:
https://doc.rust-lang.org/std/collections/hash_map/struct.HashMap.html#method.new

... and if you want a different hasher, you have to use something like HashMap::default().

Part 1 of this article explains the effects on type inference -- the reason new forces RandomState:
https://faultlore.com/blah/defaults-affect-inference/