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.
76 #[stable(feature = "rust1", since = "1.0.0")]
78 /// Feeds this value into the state given, updating the hasher as necessary.
79 #[stable(feature = "rust1", since = "1.0.0")]
80 fn hash<H: Hasher>(&self, state: &mut H);
82 /// Feeds a slice of this type into the state provided.
83 #[unstable(feature = "hash", reason = "module was recently redesigned")]
84 fn hash_slice<H: Hasher>(data: &[Self], state: &mut H) where Self: Sized {
91 /// A trait which represents the ability to hash an arbitrary stream of bytes.
92 #[stable(feature = "rust1", since = "1.0.0")]
94 /// Completes a round of hashing, producing the output hash generated.
95 #[stable(feature = "rust1", since = "1.0.0")]
96 fn finish(&self) -> u64;
98 /// Writes some data into this `Hasher`
99 #[stable(feature = "rust1", since = "1.0.0")]
100 fn write(&mut self, bytes: &[u8]);
102 /// Write a single `u8` into this hasher
104 #[unstable(feature = "hash", reason = "module was recently redesigned")]
105 fn write_u8(&mut self, i: u8) { self.write(&[i]) }
106 /// Write a single `u16` into this hasher.
108 #[unstable(feature = "hash", reason = "module was recently redesigned")]
109 fn write_u16(&mut self, i: u16) {
110 self.write(&unsafe { mem::transmute::<_, [u8; 2]>(i) })
112 /// Write a single `u32` into this hasher.
114 #[unstable(feature = "hash", reason = "module was recently redesigned")]
115 fn write_u32(&mut self, i: u32) {
116 self.write(&unsafe { mem::transmute::<_, [u8; 4]>(i) })
118 /// Write a single `u64` into this hasher.
120 #[unstable(feature = "hash", reason = "module was recently redesigned")]
121 fn write_u64(&mut self, i: u64) {
122 self.write(&unsafe { mem::transmute::<_, [u8; 8]>(i) })
124 /// Write a single `usize` into this hasher.
126 #[unstable(feature = "hash", reason = "module was recently redesigned")]
127 fn write_usize(&mut self, i: usize) {
128 if cfg!(target_pointer_width = "32") {
129 self.write_u32(i as u32)
131 self.write_u64(i as u64)
135 /// Write a single `i8` into this hasher.
137 #[unstable(feature = "hash", reason = "module was recently redesigned")]
138 fn write_i8(&mut self, i: i8) { self.write_u8(i as u8) }
139 /// Write a single `i16` into this hasher.
141 #[unstable(feature = "hash", reason = "module was recently redesigned")]
142 fn write_i16(&mut self, i: i16) { self.write_u16(i as u16) }
143 /// Write a single `i32` into this hasher.
145 #[unstable(feature = "hash", reason = "module was recently redesigned")]
146 fn write_i32(&mut self, i: i32) { self.write_u32(i as u32) }
147 /// Write a single `i64` into this hasher.
149 #[unstable(feature = "hash", reason = "module was recently redesigned")]
150 fn write_i64(&mut self, i: i64) { self.write_u64(i as u64) }
151 /// Write a single `isize` into this hasher.
153 #[unstable(feature = "hash", reason = "module was recently redesigned")]
154 fn write_isize(&mut self, i: isize) { self.write_usize(i as usize) }
157 /// Hash a value with the default SipHasher algorithm (two initial keys of 0).
159 /// The specified value will be hashed with this hasher and then the resulting
160 /// hash will be returned.
161 #[unstable(feature = "hash", reason = "module was recently redesigned")]
162 pub fn hash<T: Hash, H: Hasher + Default>(value: &T) -> u64 {
163 let mut h: H = Default::default();
168 //////////////////////////////////////////////////////////////////////////////
176 macro_rules! impl_write {
177 ($(($ty:ident, $meth:ident),)*) => {$(
178 #[stable(feature = "rust1", since = "1.0.0")]
180 fn hash<H: Hasher>(&self, state: &mut H) {
184 fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
185 // FIXME(#23542) Replace with type ascription.
186 #![allow(trivial_casts)]
187 let newlen = data.len() * ::$ty::BYTES as usize;
188 let ptr = data.as_ptr() as *const u8;
189 state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
200 (usize, write_usize),
205 (isize, write_isize),
208 #[stable(feature = "rust1", since = "1.0.0")]
210 fn hash<H: Hasher>(&self, state: &mut H) {
211 state.write_u8(*self as u8)
215 #[stable(feature = "rust1", since = "1.0.0")]
217 fn hash<H: Hasher>(&self, state: &mut H) {
218 state.write_u32(*self as u32)
222 #[stable(feature = "rust1", since = "1.0.0")]
224 fn hash<H: Hasher>(&self, state: &mut H) {
225 state.write(self.as_bytes());
230 macro_rules! impl_hash_tuple {
232 #[stable(feature = "rust1", since = "1.0.0")]
234 fn hash<H: Hasher>(&self, _state: &mut H) {}
238 ( $($name:ident)+) => (
239 #[stable(feature = "rust1", since = "1.0.0")]
240 impl<$($name: Hash),*> Hash for ($($name,)*) {
241 #[allow(non_snake_case)]
242 fn hash<S: Hasher>(&self, state: &mut S) {
243 let ($(ref $name,)*) = *self;
244 $($name.hash(state);)*
251 impl_hash_tuple! { A }
252 impl_hash_tuple! { A B }
253 impl_hash_tuple! { A B C }
254 impl_hash_tuple! { A B C D }
255 impl_hash_tuple! { A B C D E }
256 impl_hash_tuple! { A B C D E F }
257 impl_hash_tuple! { A B C D E F G }
258 impl_hash_tuple! { A B C D E F G H }
259 impl_hash_tuple! { A B C D E F G H I }
260 impl_hash_tuple! { A B C D E F G H I J }
261 impl_hash_tuple! { A B C D E F G H I J K }
262 impl_hash_tuple! { A B C D E F G H I J K L }
264 #[stable(feature = "rust1", since = "1.0.0")]
265 impl<T: Hash> Hash for [T] {
266 fn hash<H: Hasher>(&self, state: &mut H) {
267 self.len().hash(state);
268 Hash::hash_slice(self, state)
273 #[stable(feature = "rust1", since = "1.0.0")]
274 impl<'a, T: ?Sized + Hash> Hash for &'a T {
275 fn hash<H: Hasher>(&self, state: &mut H) {
276 (**self).hash(state);
280 #[stable(feature = "rust1", since = "1.0.0")]
281 impl<'a, T: ?Sized + Hash> Hash for &'a mut T {
282 fn hash<H: Hasher>(&self, state: &mut H) {
283 (**self).hash(state);
287 #[stable(feature = "rust1", since = "1.0.0")]
288 impl<T> Hash for *const T {
289 fn hash<H: Hasher>(&self, state: &mut H) {
290 state.write_usize(*self as usize)
294 #[stable(feature = "rust1", since = "1.0.0")]
295 impl<T> Hash for *mut T {
296 fn hash<H: Hasher>(&self, state: &mut H) {
297 state.write_usize(*self as usize)