Skip to content

Commit f7bcb73

Browse files
nhamalexcrichton
authored andcommitted
Implement Hash for DList
1 parent 3c453b3 commit f7bcb73

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/libcollections/dlist.rs

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use core::fmt;
2929
use core::iter;
3030
use core::mem;
3131
use core::ptr;
32+
use std::hash::{Writer, Hash};
3233

3334
use {Collection, Mutable, Deque, MutableSeq};
3435

@@ -707,10 +708,20 @@ impl<A: fmt::Show> fmt::Show for DList<A> {
707708
}
708709
}
709710

711+
impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
712+
fn hash(&self, state: &mut S) {
713+
self.len().hash(state);
714+
for elt in self.iter() {
715+
elt.hash(state);
716+
}
717+
}
718+
}
719+
710720
#[cfg(test)]
711721
mod tests {
712722
use std::prelude::*;
713723
use std::rand;
724+
use std::hash;
714725
use test::Bencher;
715726
use test;
716727

@@ -1075,6 +1086,24 @@ mod tests {
10751086
assert!(n != m);
10761087
}
10771088

1089+
#[test]
1090+
fn test_hash() {
1091+
let mut x = DList::new();
1092+
let mut y = DList::new();
1093+
1094+
assert!(hash::hash(&x) == hash::hash(&y));
1095+
1096+
x.push_back(1i);
1097+
x.push_back(2);
1098+
x.push_back(3);
1099+
1100+
y.push_front(3i);
1101+
y.push_front(2);
1102+
y.push_front(1);
1103+
1104+
assert!(hash::hash(&x) == hash::hash(&y));
1105+
}
1106+
10781107
#[test]
10791108
fn test_ord() {
10801109
let n: DList<int> = list_from([]);

0 commit comments

Comments
 (0)