Skip to content

Commit

Permalink
add #[derive(Hash)] test for rust-lang#21714
Browse files Browse the repository at this point in the history
  • Loading branch information
durka committed Mar 13, 2016
1 parent b8dce9b commit 03e00df
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/test/run-pass/deriving-hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#![feature(hash_default)]

use std::hash::{Hash, SipHasher, Hasher};
use std::mem::size_of;

#[derive(Hash)]
struct Person {
Expand All @@ -20,12 +21,30 @@ struct Person {
phone: usize,
}

#[derive(Hash)]
enum E { A=1, B }

fn hash<T: Hash>(t: &T) -> u64 {
let mut s = SipHasher::new_with_keys(0, 0);
t.hash(&mut s);
s.finish()
}

struct FakeHasher<'a>(&'a mut Vec<u8>);
impl<'a> Hasher for FakeHasher<'a> {
fn finish(&self) -> u64 {
unimplemented!()
}

fn write(&mut self, bytes: &[u8]) {
self.0.extend(bytes);
}
}

fn fake_hash(v: &mut Vec<u8>, e: E) {
e.hash(&mut FakeHasher(v));
}

fn main() {
let person1 = Person {
id: 5,
Expand All @@ -39,4 +58,11 @@ fn main() {
};
assert_eq!(hash(&person1), hash(&person1));
assert!(hash(&person1) != hash(&person2));

// test #21714
let mut va = vec![];
let mut vb = vec![];
fake_hash(&mut va, E::A);
fake_hash(&mut vb, E::B);
assert!(va != vb);
}

0 comments on commit 03e00df

Please sign in to comment.