]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2014 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
62682a34 SL |
11 | #![unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear")] |
12 | ||
1a4d82fc JJ |
13 | use clone::Clone; |
14 | use default::Default; | |
15 | use hash; | |
85aaf69f | 16 | use marker; |
1a4d82fc JJ |
17 | |
18 | /// A trait representing stateful hashes which can be used to hash keys in a | |
19 | /// `HashMap`. | |
20 | /// | |
21 | /// A HashState is used as a factory for instances of `Hasher` which a `HashMap` | |
22 | /// can then use to hash keys independently. A `HashMap` by default uses a state | |
23 | /// which will create instances of a `SipHasher`, but a custom state factory can | |
24 | /// be provided to the `with_hash_state` function. | |
25 | /// | |
26 | /// If a hashing algorithm has no initial state, then the `Hasher` type for that | |
27 | /// algorithm can implement the `Default` trait and create hash maps with the | |
28 | /// `DefaultState` structure. This state is 0-sized and will simply delegate | |
29 | /// to `Default` when asked to create a hasher. | |
30 | pub trait HashState { | |
c34b1796 | 31 | /// Type of the hasher that will be created. |
1a4d82fc JJ |
32 | type Hasher: hash::Hasher; |
33 | ||
34 | /// Creates a new hasher based on the given state of this object. | |
35 | fn hasher(&self) -> Self::Hasher; | |
36 | } | |
37 | ||
38 | /// A structure which is a factory for instances of `Hasher` which implement the | |
39 | /// default trait. | |
40 | /// | |
c1a9b12d | 41 | /// This struct is 0-sized and does not need construction. |
85aaf69f | 42 | pub struct DefaultState<H>(marker::PhantomData<H>); |
1a4d82fc JJ |
43 | |
44 | impl<H: Default + hash::Hasher> HashState for DefaultState<H> { | |
45 | type Hasher = H; | |
46 | fn hasher(&self) -> H { Default::default() } | |
47 | } | |
48 | ||
49 | impl<H> Clone for DefaultState<H> { | |
85aaf69f | 50 | fn clone(&self) -> DefaultState<H> { DefaultState(marker::PhantomData) } |
1a4d82fc JJ |
51 | } |
52 | ||
53 | impl<H> Default for DefaultState<H> { | |
85aaf69f | 54 | fn default() -> DefaultState<H> { DefaultState(marker::PhantomData) } |
1a4d82fc | 55 | } |