add #[derive(Hash)] test for #21714

This commit is contained in:
Alex Burka 2016-03-05 14:51:24 -05:00
parent 68de28b309
commit 01c0723ef2

View File

@ -12,6 +12,7 @@
#![feature(hash_default)] #![feature(hash_default)]
use std::hash::{Hash, SipHasher, Hasher}; use std::hash::{Hash, SipHasher, Hasher};
use std::mem::size_of;
#[derive(Hash)] #[derive(Hash)]
struct Person { struct Person {
@ -24,12 +25,30 @@ struct Person {
#[derive(Hash)] struct __H__H; #[derive(Hash)] struct __H__H;
#[derive(Hash)] enum Collision<__H> { __H { __H__H: __H } } #[derive(Hash)] enum Collision<__H> { __H { __H__H: __H } }
#[derive(Hash)]
enum E { A=1, B }
fn hash<T: Hash>(t: &T) -> u64 { fn hash<T: Hash>(t: &T) -> u64 {
let mut s = SipHasher::new_with_keys(0, 0); let mut s = SipHasher::new_with_keys(0, 0);
t.hash(&mut s); t.hash(&mut s);
s.finish() 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() { fn main() {
let person1 = Person { let person1 = Person {
id: 5, id: 5,
@ -43,4 +62,11 @@ fn main() {
}; };
assert_eq!(hash(&person1), hash(&person1)); assert_eq!(hash(&person1), hash(&person1));
assert!(hash(&person1) != hash(&person2)); 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);
} }