]> git.lizzy.rs Git - rust.git/blobdiff - src/libcollections/dlist.rs
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[rust.git] / src / libcollections / dlist.rs
index 68b6416b69bf5c16142775cd1cbfd81b8f2d2634..5e3ce75eb9586bc56a085d28536b4bf3df4780ec 100644 (file)
@@ -29,6 +29,7 @@
 use core::iter;
 use core::mem;
 use core::ptr;
+use std::hash::{Writer, Hash};
 
 use {Collection, Mutable, Deque, MutableSeq};
 
@@ -707,10 +708,20 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
     }
 }
 
+impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
+    fn hash(&self, state: &mut S) {
+        self.len().hash(state);
+        for elt in self.iter() {
+            elt.hash(state);
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use std::prelude::*;
     use std::rand;
+    use std::hash;
     use test::Bencher;
     use test;
 
@@ -1075,6 +1086,24 @@ fn test_eq() {
         assert!(n != m);
     }
 
+    #[test]
+    fn test_hash() {
+      let mut x = DList::new();
+      let mut y = DList::new();
+
+      assert!(hash::hash(&x) == hash::hash(&y));
+
+      x.push_back(1i);
+      x.push_back(2);
+      x.push_back(3);
+
+      y.push_front(3i);
+      y.push_front(2);
+      y.push_front(1);
+
+      assert!(hash::hash(&x) == hash::hash(&y));
+    }
+
     #[test]
     fn test_ord() {
         let n: DList<int> = list_from([]);