You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Supporting HashMap<K, V, S> where S: BuildHasher + Default will allow into_group_map to interoperate with hash maps that use other hash algorithms, e.g. FnvHashMap or FxHashMap.
Implementation issues
The problem is that naively adding an S: BuildHasher + Default type parameter like this:
src/group_map.rs
#![cfg(feature = "use_std")]
use std::collections::HashMap;
-use std::hash::Hash;+use std::hash::{BuildHasher, Hash};
use std::iter::Iterator;
/// Return a `HashMap` of keys mapped to a list of their corresponding values.
///
/// See [`.into_group_map()`](../trait.Itertools.html#method.into_group_map)
/// for more information.
-pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>>+pub fn into_group_map<I, K, V, S>(iter: I) -> HashMap<K, Vec<V>, S>
where I: Iterator<Item=(K, V)>,
K: Hash + Eq,
+ S: BuildHasher + Default,
{
- let mut lookup = HashMap::new();+ let mut lookup = HashMap::default();
for (key, val) in iter {
lookup.entry(key).or_insert(Vec::new()).push(val);
}
lookup
}
So I guess the only way out is to keep into_group_map as is (to support the default case) and add a new combinator which differs from the original as shown in the diff above.
The text was updated successfully, but these errors were encountered:
Motivation
Currently
Itertools::into_group_map
returnsHashMap<K, V>
which equals toHashMap<K, V, RandomState>
withRandomState
being https://doc.rust-lang.org/std/collections/hash_map/struct.RandomState.html.Supporting
HashMap<K, V, S>
whereS: BuildHasher + Default
will allowinto_group_map
to interoperate with hash maps that use other hash algorithms, e.g.FnvHashMap
orFxHashMap
.Implementation issues
The problem is that naively adding an
S: BuildHasher + Default
type parameter like this:src/group_map.rs
src/lib.rs
will mess with type checking if the code relied on
S = RandomState
to be inferred.Specifically, the above change breaks compilation of
tests/quick.rs
:What's more, default type parameters are only supported on type definitions, but not on functions or methods: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=c8030e99c0e0f5908b130dbfe3e8c26d.
So I guess the only way out is to keep
into_group_map
as is (to support the default case) and add a new combinator which differs from the original as shown in the diff above.The text was updated successfully, but these errors were encountered: