std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
This commit is contained in:
parent
9e4e524e0e
commit
511f0b8a3d
50 changed files with 1084 additions and 1035 deletions
|
|
@ -30,7 +30,8 @@ pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
|
|||
let generics = LifetimeBounds {
|
||||
lifetimes: Vec::new(),
|
||||
bounds: vec!(("__S",
|
||||
vec!(Path::new(vec!("std", "hash", "Writer"))))),
|
||||
vec!(Path::new(vec!("std", "hash", "Writer")),
|
||||
Path::new(vec!("std", "hash", "Hasher"))))),
|
||||
};
|
||||
let args = Path::new_local("__S");
|
||||
let inline = cx.meta_word(span, InternedString::new("inline"));
|
||||
|
|
|
|||
|
|
@ -37,9 +37,11 @@
|
|||
//! Moreover, a switch to, e.g. `P<'a, T>` would be easy and mostly automated.
|
||||
|
||||
use std::fmt::{self, Show};
|
||||
use std::hash::Hash;
|
||||
use std::hash::{Hash, Hasher};
|
||||
#[cfg(stage0)] use std::hash::Writer;
|
||||
use std::ops::Deref;
|
||||
use std::ptr;
|
||||
|
||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||
|
||||
/// An owned smart pointer.
|
||||
|
|
@ -105,7 +107,14 @@ impl<T: Show> Show for P<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S, T: Hash<S>> Hash<S> for P<T> {
|
||||
#[cfg(stage0)]
|
||||
impl<S: Writer, T: Hash<S>> Hash<S> for P<T> {
|
||||
fn hash(&self, state: &mut S) {
|
||||
(**self).hash(state);
|
||||
}
|
||||
}
|
||||
#[cfg(not(stage0))]
|
||||
impl<S: Hasher, T: Hash<S>> Hash<S> for P<T> {
|
||||
fn hash(&self, state: &mut S) {
|
||||
(**self).hash(state);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ use std::cmp::Ordering;
|
|||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::hash::Hash;
|
||||
use std::collections::hash_map::Hasher;
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
|
||||
|
|
@ -28,8 +29,8 @@ pub struct Interner<T> {
|
|||
vect: RefCell<Vec<T> >,
|
||||
}
|
||||
|
||||
// when traits can extend traits, we should extend index<Name,T> to get .index(&FullRange)
|
||||
impl<T: Eq + Hash + Clone + 'static> Interner<T> {
|
||||
// when traits can extend traits, we should extend index<Name,T> to get []
|
||||
impl<T: Eq + Hash<Hasher> + Clone + 'static> Interner<T> {
|
||||
pub fn new() -> Interner<T> {
|
||||
Interner {
|
||||
map: RefCell::new(HashMap::new()),
|
||||
|
|
@ -78,7 +79,7 @@ impl<T: Eq + Hash + Clone + 'static> Interner<T> {
|
|||
}
|
||||
|
||||
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
|
||||
where Q: BorrowFrom<T> + Eq + Hash {
|
||||
where Q: BorrowFrom<T> + Eq + Hash<Hasher> {
|
||||
let map = self.map.borrow();
|
||||
match (*map).get(val) {
|
||||
Some(v) => Some(*v),
|
||||
|
|
@ -203,7 +204,7 @@ impl StrInterner {
|
|||
}
|
||||
|
||||
pub fn find<Q: ?Sized>(&self, val: &Q) -> Option<Name>
|
||||
where Q: BorrowFrom<RcStr> + Eq + Hash {
|
||||
where Q: BorrowFrom<RcStr> + Eq + Hash<Hasher> {
|
||||
match (*self.map.borrow()).get(val) {
|
||||
Some(v) => Some(*v),
|
||||
None => None,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue