Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
use std::hash::{Writer, Hash};

use {Collection, Mutable, Deque, MutableSeq};

Expand Down Expand Up @@ -707,10 +708,20 @@ impl<A: fmt::Show> fmt::Show for DList<A> {
}
}

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;

Expand Down Expand Up @@ -1075,6 +1086,24 @@ mod tests {
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([]);
Expand Down