Skip to content

Commit 1cfa656

Browse files
committed
Implement Hash for RingBuf
1 parent 32e521f commit 1cfa656

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcollections/ringbuf.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use core::cmp;
1919
use core::default::Default;
2020
use core::fmt;
2121
use core::iter::RandomAccessIterator;
22+
use std::hash::{Writer, Hash};
2223

2324
use {Deque, Collection, Mutable, MutableSeq};
2425
use vec::Vec;
@@ -450,6 +451,14 @@ impl<A: PartialEq> PartialEq for RingBuf<A> {
450451
}
451452
}
452453

454+
impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
455+
fn hash(&self, state: &mut S) {
456+
for elt in self.iter() {
457+
elt.hash(state);
458+
}
459+
}
460+
}
461+
453462
impl<A> FromIterator<A> for RingBuf<A> {
454463
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
455464
let (lower, _) = iterator.size_hint();
@@ -485,6 +494,7 @@ mod tests {
485494
use std::fmt::Show;
486495
use std::prelude::*;
487496
use std::gc::{GC, Gc};
497+
use std::hash;
488498
use test::Bencher;
489499
use test;
490500

@@ -912,6 +922,24 @@ mod tests {
912922
assert!(e == RingBuf::new());
913923
}
914924

925+
#[test]
926+
fn test_hash() {
927+
let mut x = RingBuf::new();
928+
let mut y = RingBuf::new();
929+
930+
x.push(1i);
931+
x.push(2);
932+
x.push(3);
933+
934+
y.push(0i);
935+
y.push(1i);
936+
y.pop_front();
937+
y.push(2);
938+
y.push(3);
939+
940+
assert!(hash::hash(&x) == hash::hash(&y));
941+
}
942+
915943
#[test]
916944
fn test_show() {
917945
let ringbuf: RingBuf<int> = range(0i, 10).collect();

0 commit comments

Comments
 (0)