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)]`:
20 //! use std::hash::Hash;
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::hash(&person1) != hash::hash(&person2));
35 //! If you need more control over how a value is hashed, you need to implement
40 //! use std::hash::Hash;
41 //! use std::hash::sip::SipState;
49 //! impl Hash for Person {
50 //! fn hash(&self, state: &mut SipState) {
51 //! self.id.hash(state);
52 //! self.phone.hash(state);
56 //! let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
57 //! let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
59 //! assert!(hash::hash(&person1) == hash::hash(&person2));
62 #![allow(unused_must_use)]
66 use borrow::{Cow, ToOwned};
67 use intrinsics::TypeId;
71 /// Reexport the `sip::hash` function as our default hasher.
72 pub use self::sip::hash as hash;
76 /// A hashable type. The `S` type parameter is an abstract hash state that is
77 /// used by the `Hash` to compute the hash. It defaults to
78 /// `std::hash::sip::SipState`.
79 pub trait Hash<S = sip::SipState> for Sized? {
80 /// Computes the hash of a value.
81 fn hash(&self, state: &mut S);
84 /// A trait that computes a hash for a value. The main users of this trait are
85 /// containers like `HashMap`, which need a generic way hash multiple types.
87 /// Compute the hash of a value.
88 fn hash<Sized? T: Hash<S>>(&self, value: &T) -> u64;
91 #[allow(missing_docs)]
93 fn write(&mut self, bytes: &[u8]);
96 //////////////////////////////////////////////////////////////////////////////
98 macro_rules! impl_hash {
99 ($ty:ident, $uty:ident) => {
100 impl<S: Writer> Hash<S> for $ty {
102 fn hash(&self, state: &mut S) {
103 let a: [u8; ::$ty::BYTES] = unsafe {
104 mem::transmute((*self as $uty).to_le() as $ty)
106 state.write(a.as_slice())
112 impl_hash! { u8, u8 }
113 impl_hash! { u16, u16 }
114 impl_hash! { u32, u32 }
115 impl_hash! { u64, u64 }
116 impl_hash! { uint, uint }
117 impl_hash! { i8, u8 }
118 impl_hash! { i16, u16 }
119 impl_hash! { i32, u32 }
120 impl_hash! { i64, u64 }
121 impl_hash! { int, uint }
123 impl<S: Writer> Hash<S> for bool {
125 fn hash(&self, state: &mut S) {
126 (*self as u8).hash(state);
130 impl<S: Writer> Hash<S> for char {
132 fn hash(&self, state: &mut S) {
133 (*self as u32).hash(state);
137 impl<S: Writer> Hash<S> for str {
139 fn hash(&self, state: &mut S) {
140 state.write(self.as_bytes());
145 macro_rules! impl_hash_tuple {
147 impl<S: Writer> Hash<S> for () {
149 fn hash(&self, state: &mut S) {
155 ( $($name:ident)+) => (
156 impl<S: Writer, $($name: Hash<S>),*> Hash<S> for ($($name,)*) {
158 #[allow(non_snake_case)]
159 fn hash(&self, state: &mut S) {
161 ($(ref $name,)*) => {
173 impl_hash_tuple! { A }
174 impl_hash_tuple! { A B }
175 impl_hash_tuple! { A B C }
176 impl_hash_tuple! { A B C D }
177 impl_hash_tuple! { A B C D E }
178 impl_hash_tuple! { A B C D E F }
179 impl_hash_tuple! { A B C D E F G }
180 impl_hash_tuple! { A B C D E F G H }
181 impl_hash_tuple! { A B C D E F G H I }
182 impl_hash_tuple! { A B C D E F G H I J }
183 impl_hash_tuple! { A B C D E F G H I J K }
184 impl_hash_tuple! { A B C D E F G H I J K L }
186 impl<S: Writer, T: Hash<S>> Hash<S> for [T] {
188 fn hash(&self, state: &mut S) {
189 self.len().hash(state);
190 for elt in self.iter() {
197 impl<'a, S: Writer, Sized? T: Hash<S>> Hash<S> for &'a T {
199 fn hash(&self, state: &mut S) {
200 (**self).hash(state);
204 impl<'a, S: Writer, Sized? T: Hash<S>> Hash<S> for &'a mut T {
206 fn hash(&self, state: &mut S) {
207 (**self).hash(state);
211 impl<S: Writer, T> Hash<S> for *const T {
213 fn hash(&self, state: &mut S) {
214 // NB: raw-pointer Hash does _not_ dereference
215 // to the target; it just gives you the pointer-bytes.
216 (*self as uint).hash(state);
220 impl<S: Writer, T> Hash<S> for *mut T {
222 fn hash(&self, state: &mut S) {
223 // NB: raw-pointer Hash does _not_ dereference
224 // to the target; it just gives you the pointer-bytes.
225 (*self as uint).hash(state);
229 impl<S: Writer> Hash<S> for TypeId {
231 fn hash(&self, state: &mut S) {
232 self.hash().hash(state)
236 impl<'a, T, Sized? B, S> Hash<S> for Cow<'a, T, B> where B: Hash<S> + ToOwned<T> {
238 fn hash(&self, state: &mut S) {
239 Hash::hash(&**self, state)