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 //! # #![feature(hash)]
20 //! use std::hash::{hash, Hash, SipHasher};
29 //! let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
30 //! let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
32 //! assert!(hash::<_, SipHasher>(&person1) != hash::<_, SipHasher>(&person2));
35 //! If you need more control over how a value is hashed, you need to implement
39 //! # #![feature(hash)]
40 //! use std::hash::{hash, Hash, Hasher, SipHasher};
48 //! impl Hash for Person {
49 //! fn hash<H: Hasher>(&self, state: &mut H) {
50 //! self.id.hash(state);
51 //! self.phone.hash(state);
55 //! let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
56 //! let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
58 //! assert_eq!(hash::<_, SipHasher>(&person1), hash::<_, SipHasher>(&person2));
61 #![stable(feature = "rust1", since = "1.0.0")]
68 pub use self::sip::SipHasher;
74 /// The `H` type parameter is an abstract hash state that is used by the `Hash`
75 /// to compute the hash.
77 /// If you are also implementing `Eq`, there is an additional property that
81 /// k1 == k2 -> hash(k1) == hash(k2)
84 /// In other words, if two keys are equal, their hashes should also be equal.
85 /// `HashMap` and `HashSet` both rely on this behavior.
86 #[stable(feature = "rust1", since = "1.0.0")]
88 /// Feeds this value into the state given, updating the hasher as necessary.
89 #[stable(feature = "rust1", since = "1.0.0")]
90 fn hash<H: Hasher>(&self, state: &mut H);
92 /// Feeds a slice of this type into the state provided.
93 #[unstable(feature = "hash", reason = "module was recently redesigned")]
94 fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized {
101 /// A trait which represents the ability to hash an arbitrary stream of bytes.
102 #[stable(feature = "rust1", since = "1.0.0")]
104 /// Completes a round of hashing, producing the output hash generated.
105 #[stable(feature = "rust1", since = "1.0.0")]
106 fn finish(&self) -> u64;
108 /// Writes some data into this `Hasher`
109 #[stable(feature = "rust1", since = "1.0.0")]
110 fn write(&mut self, bytes: &[u8]);
112 /// Write a single `u8` into this hasher
114 #[unstable(feature = "hash", reason = "module was recently redesigned")]
115 fn write_u8(&mut self, i: u8) { self.write(&[i]) }
116 /// Write a single `u16` into this hasher.
118 #[unstable(feature = "hash", reason = "module was recently redesigned")]
119 fn write_u16(&mut self, i: u16) {
120 self.write(&unsafe { mem::transmute::<_, [u8; 2]>(i) })
122 /// Write a single `u32` into this hasher.
124 #[unstable(feature = "hash", reason = "module was recently redesigned")]
125 fn write_u32(&mut self, i: u32) {
126 self.write(&unsafe { mem::transmute::<_, [u8; 4]>(i) })
128 /// Write a single `u64` into this hasher.
130 #[unstable(feature = "hash", reason = "module was recently redesigned")]
131 fn write_u64(&mut self, i: u64) {
132 self.write(&unsafe { mem::transmute::<_, [u8; 8]>(i) })
134 /// Write a single `usize` into this hasher.
136 #[unstable(feature = "hash", reason = "module was recently redesigned")]
137 fn write_usize(&mut self, i: usize) {
138 if cfg!(target_pointer_width = "32") {
139 self.write_u32(i as u32)
141 self.write_u64(i as u64)
145 /// Write a single `i8` into this hasher.
147 #[unstable(feature = "hash", reason = "module was recently redesigned")]
148 fn write_i8(&mut self, i: i8) { self.write_u8(i as u8) }
149 /// Write a single `i16` into this hasher.
151 #[unstable(feature = "hash", reason = "module was recently redesigned")]
152 fn write_i16(&mut self, i: i16) { self.write_u16(i as u16) }
153 /// Write a single `i32` into this hasher.
155 #[unstable(feature = "hash", reason = "module was recently redesigned")]
156 fn write_i32(&mut self, i: i32) { self.write_u32(i as u32) }
157 /// Write a single `i64` into this hasher.
159 #[unstable(feature = "hash", reason = "module was recently redesigned")]
160 fn write_i64(&mut self, i: i64) { self.write_u64(i as u64) }
161 /// Write a single `isize` into this hasher.
163 #[unstable(feature = "hash", reason = "module was recently redesigned")]
164 fn write_isize(&mut self, i: isize) { self.write_usize(i as usize) }
167 /// Hash a value with the default SipHasher algorithm (two initial keys of 0).
169 /// The specified value will be hashed with this hasher and then the resulting
170 /// hash will be returned.
171 #[unstable(feature = "hash", reason = "module was recently redesigned")]
172 pub fn hash<T: Hash, H: Hasher + Default>(value: &T) -> u64 {
173 let mut h: H = Default::default();
178 //////////////////////////////////////////////////////////////////////////////
186 macro_rules! impl_write {
187 ($(($ty:ident, $meth:ident),)*) => {$(
188 #[stable(feature = "rust1", since = "1.0.0")]
190 fn hash<H: Hasher>(&self, state: &mut H) {
194 fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
195 // FIXME(#23542) Replace with type ascription.
196 #![allow(trivial_casts)]
197 let newlen = data.len() * ::$ty::BYTES as usize;
198 let ptr = data.as_ptr() as *const u8;
199 state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
210 (usize, write_usize),
215 (isize, write_isize),
218 #[stable(feature = "rust1", since = "1.0.0")]
220 fn hash<H: Hasher>(&self, state: &mut H) {
221 state.write_u8(*self as u8)
225 #[stable(feature = "rust1", since = "1.0.0")]
227 fn hash<H: Hasher>(&self, state: &mut H) {
228 state.write_u32(*self as u32)
232 #[stable(feature = "rust1", since = "1.0.0")]
234 fn hash<H: Hasher>(&self, state: &mut H) {
235 state.write(self.as_bytes());
240 macro_rules! impl_hash_tuple {
242 #[stable(feature = "rust1", since = "1.0.0")]
244 fn hash<H: Hasher>(&self, _state: &mut H) {}
248 ( $($name:ident)+) => (
249 #[stable(feature = "rust1", since = "1.0.0")]
250 impl<$($name: Hash),*> Hash for ($($name,)*) {
251 #[allow(non_snake_case)]
252 fn hash<S: Hasher>(&self, state: &mut S) {
253 let ($(ref $name,)*) = *self;
254 $($name.hash(state);)*
261 impl_hash_tuple! { A }
262 impl_hash_tuple! { A B }
263 impl_hash_tuple! { A B C }
264 impl_hash_tuple! { A B C D }
265 impl_hash_tuple! { A B C D E }
266 impl_hash_tuple! { A B C D E F }
267 impl_hash_tuple! { A B C D E F G }
268 impl_hash_tuple! { A B C D E F G H }
269 impl_hash_tuple! { A B C D E F G H I }
270 impl_hash_tuple! { A B C D E F G H I J }
271 impl_hash_tuple! { A B C D E F G H I J K }
272 impl_hash_tuple! { A B C D E F G H I J K L }
274 #[stable(feature = "rust1", since = "1.0.0")]
275 impl<T: Hash> Hash for [T] {
276 fn hash<H: Hasher>(&self, state: &mut H) {
277 self.len().hash(state);
278 Hash::hash_slice(self, state)
283 #[stable(feature = "rust1", since = "1.0.0")]
284 impl<'a, T: ?Sized + Hash> Hash for &'a T {
285 fn hash<H: Hasher>(&self, state: &mut H) {
286 (**self).hash(state);
290 #[stable(feature = "rust1", since = "1.0.0")]
291 impl<'a, T: ?Sized + Hash> Hash for &'a mut T {
292 fn hash<H: Hasher>(&self, state: &mut H) {
293 (**self).hash(state);
297 #[stable(feature = "rust1", since = "1.0.0")]
298 impl<T> Hash for *const T {
299 fn hash<H: Hasher>(&self, state: &mut H) {
300 state.write_usize(*self as usize)
304 #[stable(feature = "rust1", since = "1.0.0")]
305 impl<T> Hash for *mut T {
306 fn hash<H: Hasher>(&self, state: &mut H) {
307 state.write_usize(*self as usize)