1 // Copyright 2012-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.
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.
11 //! Generic hashing support.
13 //! This module provides a generic way to compute the hash of a value. The
14 //! simplest way to make a type hashable is to use `#[derive(Hash)]`:
19 //! use std::hash::{hash, Hash, SipHasher};
28 //! let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
29 //! let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
31 //! assert!(hash::<_, SipHasher>(&person1) != hash::<_, SipHasher>(&person2));
34 //! If you need more control over how a value is hashed, you need to implement
38 //! use std::hash::{hash, Hash, Hasher, Writer, SipHasher};
46 //! impl<H: Hasher + Writer> Hash<H> for Person {
47 //! fn hash(&self, state: &mut H) {
48 //! self.id.hash(state);
49 //! self.phone.hash(state);
53 //! let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
54 //! let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
56 //! assert_eq!(hash::<_, SipHasher>(&person1), hash::<_, SipHasher>(&person2));
59 #![unstable(feature = "hash",
60 reason = "module was recently redesigned")]
64 use borrow::{Cow, ToOwned};
69 pub use self::sip::SipHasher;
75 /// The `H` type parameter is an abstract hash state that is used by the `Hash`
76 /// to compute the hash. Specific implementations of this trait may specialize
77 /// for particular instances of `H` in order to be able to optimize the hashing
79 pub trait Hash<H: Hasher> {
80 /// Feeds this value into the state given, updating the hasher as necessary.
81 fn hash(&self, state: &mut H);
84 /// A trait which represents the ability to hash an arbitrary stream of bytes.
86 /// Result type of one run of hashing generated by this hasher.
89 /// Resets this hasher back to its initial state (as if it were just
93 /// Completes a round of hashing, producing the output hash generated.
94 fn finish(&self) -> Self::Output;
97 /// A common bound on the `Hasher` parameter to `Hash` implementations in order
98 /// to generically hash an aggregate.
99 #[unstable(feature = "hash",
100 reason = "this trait will likely be replaced by io::Writer")]
101 #[allow(missing_docs)]
103 fn write(&mut self, bytes: &[u8]);
106 /// Hash a value with the default SipHasher algorithm (two initial keys of 0).
108 /// The specified value will be hashed with this hasher and then the resulting
109 /// hash will be returned.
110 pub fn hash<T: Hash<H>, H: Hasher + Default>(value: &T) -> H::Output {
111 let mut h: H = Default::default();
116 //////////////////////////////////////////////////////////////////////////////
118 macro_rules! impl_hash {
119 ($ty:ident, $uty:ident) => {
120 impl<S: Writer + Hasher> Hash<S> for $ty {
122 fn hash(&self, state: &mut S) {
123 let a: [u8; ::$ty::BYTES] = unsafe {
124 mem::transmute((*self as $uty).to_le() as $ty)
132 impl_hash! { u8, u8 }
133 impl_hash! { u16, u16 }
134 impl_hash! { u32, u32 }
135 impl_hash! { u64, u64 }
136 impl_hash! { uint, uint }
137 impl_hash! { i8, u8 }
138 impl_hash! { i16, u16 }
139 impl_hash! { i32, u32 }
140 impl_hash! { i64, u64 }
141 impl_hash! { int, uint }
143 impl<S: Writer + Hasher> Hash<S> for bool {
145 fn hash(&self, state: &mut S) {
146 (*self as u8).hash(state);
150 impl<S: Writer + Hasher> Hash<S> for char {
152 fn hash(&self, state: &mut S) {
153 (*self as u32).hash(state);
157 impl<S: Writer + Hasher> Hash<S> for str {
159 fn hash(&self, state: &mut S) {
160 state.write(self.as_bytes());
165 macro_rules! impl_hash_tuple {
167 impl<S: Hasher> Hash<S> for () {
169 fn hash(&self, _state: &mut S) {}
173 ( $($name:ident)+) => (
174 impl<S: Hasher, $($name: Hash<S>),*> Hash<S> for ($($name,)*) {
176 #[allow(non_snake_case)]
177 fn hash(&self, state: &mut S) {
179 ($(ref $name,)*) => {
191 impl_hash_tuple! { A }
192 impl_hash_tuple! { A B }
193 impl_hash_tuple! { A B C }
194 impl_hash_tuple! { A B C D }
195 impl_hash_tuple! { A B C D E }
196 impl_hash_tuple! { A B C D E F }
197 impl_hash_tuple! { A B C D E F G }
198 impl_hash_tuple! { A B C D E F G H }
199 impl_hash_tuple! { A B C D E F G H I }
200 impl_hash_tuple! { A B C D E F G H I J }
201 impl_hash_tuple! { A B C D E F G H I J K }
202 impl_hash_tuple! { A B C D E F G H I J K L }
204 impl<S: Writer + Hasher, T: Hash<S>> Hash<S> for [T] {
206 fn hash(&self, state: &mut S) {
207 self.len().hash(state);
215 impl<'a, S: Hasher, T: ?Sized + Hash<S>> Hash<S> for &'a T {
217 fn hash(&self, state: &mut S) {
218 (**self).hash(state);
222 impl<'a, S: Hasher, T: ?Sized + Hash<S>> Hash<S> for &'a mut T {
224 fn hash(&self, state: &mut S) {
225 (**self).hash(state);
229 impl<S: Writer + Hasher, T> Hash<S> for *const T {
231 fn hash(&self, state: &mut S) {
232 // NB: raw-pointer Hash does _not_ dereference
233 // to the target; it just gives you the pointer-bytes.
234 (*self as uint).hash(state);
238 impl<S: Writer + Hasher, T> Hash<S> for *mut T {
240 fn hash(&self, state: &mut S) {
241 // NB: raw-pointer Hash does _not_ dereference
242 // to the target; it just gives you the pointer-bytes.
243 (*self as uint).hash(state);
247 impl<'a, T, B: ?Sized, S: Hasher> Hash<S> for Cow<'a, T, B>
248 where B: Hash<S> + ToOwned<T>
251 fn hash(&self, state: &mut S) {
252 Hash::hash(&**self, state)