Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upstd: Stabilize custom hasher support in HashMap #31081
Conversation
rust-highfive
assigned
nikomatsakis
Jan 21, 2016
This comment has been minimized.
This comment has been minimized.
|
(rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
|
r? @aturon nominating for a beta-backport |
rust-highfive
assigned
aturon
and unassigned
nikomatsakis
Jan 21, 2016
alexcrichton
added
the
beta-nominated
label
Jan 21, 2016
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Everything here seems reasonable and as was discussed. |
aturon
reviewed
Jan 22, 2016
| pub fn with_hash_state(hash_state: S) -> HashMap<K, V, S> { | ||
| HashMap::with_hasher(hash_state) | ||
| } | ||
|
|
||
| /// Creates an empty HashMap with space for at least `capacity` |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
aturon
reviewed
Jan 22, 2016
| #[allow(deprecated)] | ||
| impl<T: HashState> BuildHasher for T { | ||
| type Hasher = T::Hasher; | ||
| fn build_hasher(&self) -> T::Hasher { self.hasher() } |
This comment has been minimized.
This comment has been minimized.
aturon
Jan 22, 2016
Member
Can you remind me why we take &self here, given that you're describing the trait in terms of being state that's used to generate new hashers? We seem to force you to interior mutability here, presumably for good reason?
This comment has been minimized.
This comment has been minimized.
shepmaster
Jan 22, 2016
Member
Each T::Hasher has to have the same initial state - otherwise hashing the same thing twice wouldn't have the same hash. When this function is called, it shouldn't be doing a whole lot.
For example a factory constructed with a random seed. You don't see the non-random seed in my code because it's piggybacking on the implementation provided by DefaultState
This comment has been minimized.
This comment has been minimized.
aturon
Jan 22, 2016
Member
OK, I think the problem is just that the docs here are very misleading:
- "A trait representing stateful hashes ..."
- "Creates a new hasher based on the given state of this object"
Both of those strongly suggested to me that mutation is expected here.
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jan 23, 2016
Author
Member
Oh actually the HashState trait is basically dead/to be ignored, the relevant documentation on the BuildHasher trait (the real one now) is:
/// A trait representing an object which maintains state to create new instances
/// of the `Hasher` trait.
///
/// A `BuildHasher` is typically used as a factory for instances of `Hasher`
/// which a `HashMap` can then use to hash keys independently. Does that sound ok?
I'll clear out the old documentation in favor of just saying it's deprecated.
This comment has been minimized.
This comment has been minimized.
aturon
Jan 23, 2016
Member
Those are the docs I was referencing :) Look above -- the BuildHasher trait method docs say "Creates a new hasher based on the given state of this object", which is what I was quoting. My point is just that these docs strongly suggest mutable state to me. Not a huge deal, but I found it pretty confusing.
This comment has been minimized.
This comment has been minimized.
alexcrichton
Jan 23, 2016
Author
Member
Ah ok I think I see what you're getting at now. Do you have a rewording in mind? I could try and wordsmith a bit as well and see what I can come up with
This comment has been minimized.
This comment has been minimized.
|
LGTM -- though I did leave one question about the actual API. I suspect I'm just failing to remember one of the design constraints here. |
alexcrichton
force-pushed the
alexcrichton:stabilize-hasher
branch
from
eaff046
to
ad2f9af
Jan 23, 2016
This comment has been minimized.
This comment has been minimized.
|
@aturon I've updated with removing the old and misleading |
alexcrichton
force-pushed the
alexcrichton:stabilize-hasher
branch
from
ad2f9af
to
306bd59
Jan 23, 2016
This comment has been minimized.
This comment has been minimized.
|
This looks good to go, right? |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton corrected the wrong docs problem :) I still find the docs super misleading, but we can fix in post if you prefer. That's my only complaint. |
This comment has been minimized.
This comment has been minimized.
|
I'm fine changing the docs, but I'm not really quite sure what to change them to? Do you have something in mind @aturon ? |
aturon
reviewed
Jan 26, 2016
| @@ -190,6 +191,79 @@ pub trait Hasher { | |||
| } | |||
| } | |||
|
|
|||
| /// A trait representing an object which maintains state to create new instances | |||
This comment has been minimized.
This comment has been minimized.
aturon
reviewed
Jan 26, 2016
| #[stable(since = "1.7.0", feature = "build_hasher")] | ||
| type Hasher: Hasher; | ||
|
|
||
| /// Creates a new hasher based on the given state of this object. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton Left some minimal suggestions. With that r=me. |
alexcrichton
force-pushed the
alexcrichton:stabilize-hasher
branch
from
306bd59
to
1fa0be2
Jan 26, 2016
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jan 26, 2016
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
bors
merged commit 1fa0be2
into
rust-lang:master
Jan 26, 2016
This comment has been minimized.
This comment has been minimized.
|
Marking as accepted for beta as libs talked about this awhile ago |
alexcrichton commentedJan 21, 2016
This commit implements the stabilization of the custom hasher support intended
for 1.7 but left out due to some last-minute questions that needed some
decisions. A summary of the actions done in this PR are:
Stable
std::hash::BuildHasherBuildHasher::HasherBuildHasher::build_hasherstd::hash::BuildHasherDefaultHashMap::with_hasherHashMap::with_capacity_and_hasherHashSet::with_hasherHashSet::with_capacity_and_hasherstd::collections::hash_map::RandomStateRandomState::newDeprecated
std::collections::hash_statestd::collections::hash_state::HashState- this trait was also moved intostd::hashwith a reexport here to ensure that we can have a blanket impl toprevent immediate breakage on nightly. Note that this is unstable in both
location.
HashMap::with_hash_state- renamedHashMap::with_capacity_and_hash_state- renamedHashSet::with_hash_state- renamedHashSet::with_capacity_and_hash_state- renamedCloses #27713