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.
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.
12 * Generic hashing support.
14 * This module provides a generic way to compute the hash of a value. The
15 * simplest way to make a type hashable is to use `#[deriving(Hash)]`:
21 * use std::hash::Hash;
30 * let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
31 * let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
33 * assert!(hash::hash(&person1) != hash::hash(&person2));
36 * If you need more control over how a value is hashed, you need to implement
41 * use std::hash::Hash;
42 * use std::hash::sip::SipState;
50 * impl Hash for Person {
51 * fn hash(&self, state: &mut SipState) {
52 * self.id.hash(state);
53 * self.phone.hash(state);
57 * let person1 = Person { id: 5, name: "Janet".to_string(), phone: 555_666_7777 };
58 * let person2 = Person { id: 5, name: "Bob".to_string(), phone: 555_666_7777 };
60 * assert!(hash::hash(&person1) == hash::hash(&person2));
66 pub use core_collections::hash::{Hash, Hasher, Writer, hash, sip};
68 use core::kinds::Sized;
73 /// `RandomSipHasher` computes the SipHash algorithm from a stream of bytes
74 /// initialized with random keys.
76 pub struct RandomSipHasher {
77 hasher: sip::SipHasher,
80 impl RandomSipHasher {
81 /// Construct a new `RandomSipHasher` that is initialized with random keys.
83 pub fn new() -> RandomSipHasher {
84 let mut r = rand::task_rng();
88 hasher: sip::SipHasher::new_with_keys(r0, r1),
93 impl Hasher<sip::SipState> for RandomSipHasher {
95 fn hash<Sized? T: Hash<sip::SipState>>(&self, value: &T) -> u64 {
96 self.hasher.hash(value)
100 impl Default for RandomSipHasher {
102 fn default() -> RandomSipHasher {
103 RandomSipHasher::new()