-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: add support for custom random state #62
Conversation
@zhu-he if you rebase this PR now it should green up the checks |
I was wondering about these new bounds and trying to understand why they are needed:
For the #[stable(feature = "rust1", since = "1.0.0")]
impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> HashMap<K, V, S> {
let mut map = HashMap::with_hasher(Default::default());
map.extend(iter);
map
}
} The For the If we avoid using #[cfg(feature = "std")]
/// Blanket [`ToBoundedStatic`] impl for converting `HashMap<K, V>` to `HashMap<K, V>: 'static`.
impl<K, V, S> ToBoundedStatic for std::collections::HashMap<K, V, S>
where
K: ToBoundedStatic,
K::Static: Eq + std::hash::Hash,
V: ToBoundedStatic,
S: ToBoundedStatic,
S::Static: std::hash::BuildHasher,
{
type Static = std::collections::HashMap<K::Static, V::Static, S::Static>;
fn to_static(&self) -> Self::Static {
let mut map = std::collections::HashMap::with_hasher(self.hasher().to_static());
map.extend(self.iter().map(|(k, v)| (k.to_static(), v.to_static())));
map
}
} To make it work I had to add an impl of impl ToBoundedStatic for std::collections::hash_map::RandomState {
type Static = Self;
fn to_static(&self) -> Self::Static {
self.clone()
}
} I also had to fix up the unit tests to add the above impl for your custom
I imagine the same approach could be used for the What do you think? |
87983a2
to
367e658
Compare
@fujiapple852 Thanks for the suggestion, but I think it is unnecessary to add the ToBoundedStatic impl for RandomState First, almost all
After all, due to the orphan rule, it is hard to implement EDIT: It seems that I misunderstood the meaning of the |
367e658
to
5aa0b3a
Compare
@fujiapple852 I have updated the PR, and change the bound of There is another bonus reason not to impl |
I tend to agree that this will be true in practice. My hesitation here is introducing bounds which aren't imposed by the underlying type such as
This is generally true for the library; if you have a One way around this is to have an impl for common 3rd party types in the library behind a feature flag. This is the approach for some common types such as |
5aa0b3a
to
53f1a52
Compare
@fujiapple852 Fine, I have added impls for |
LGTM! |
HashMap
andHashSet
with custom random states such asahash::HashMap
andahash::HashSet
will be supported in this PR.Note that this is a breaking change, as the type of return value is changed from
std::collections::HashMap<_, _, std::collections::hash_map::RandomState>
tostd::collections::HashMap<_, _, S>
.