]> git.lizzy.rs Git - rust.git/commitdiff
Implement Hash for RingBuf
authornham <hamann.nick@gmail.com>
Sun, 27 Jul 2014 02:33:47 +0000 (22:33 -0400)
committernham <hamann.nick@gmail.com>
Sun, 27 Jul 2014 02:33:47 +0000 (22:33 -0400)
src/libcollections/ringbuf.rs

index dcb8628c5a64840cee91ee2d6a4385b09473f566..03532051d7a6d406c848f5ba3c71fe39ccf317bd 100644 (file)
@@ -19,6 +19,7 @@
 use core::default::Default;
 use core::fmt;
 use core::iter::RandomAccessIterator;
+use std::hash::{Writer, Hash};
 
 use {Deque, Collection, Mutable, MutableSeq};
 use vec::Vec;
@@ -450,6 +451,14 @@ fn ne(&self, other: &RingBuf<A>) -> bool {
     }
 }
 
+impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
+    fn hash(&self, state: &mut S) {
+        for elt in self.iter() {
+            elt.hash(state);
+        }
+    }
+}
+
 impl<A> FromIterator<A> for RingBuf<A> {
     fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
         let (lower, _) = iterator.size_hint();
@@ -485,6 +494,7 @@ mod tests {
     use std::fmt::Show;
     use std::prelude::*;
     use std::gc::{GC, Gc};
+    use std::hash;
     use test::Bencher;
     use test;
 
@@ -912,6 +922,24 @@ fn test_eq() {
         assert!(e == RingBuf::new());
     }
 
+    #[test]
+    fn test_hash() {
+      let mut x = RingBuf::new();
+      let mut y = RingBuf::new();
+
+      x.push(1i);
+      x.push(2);
+      x.push(3);
+
+      y.push(0i);
+      y.push(1i);
+      y.pop_front();
+      y.push(2);
+      y.push(3);
+
+      assert!(hash::hash(&x) == hash::hash(&y));
+    }
+
     #[test]
     fn test_show() {
         let ringbuf: RingBuf<int> = range(0i, 10).collect();