Implement Show for DList

This commit is contained in:
Adolfo Ochagavía 2014-06-07 15:01:44 +02:00
parent 8e9e484d70
commit f1bff592b1

View File

@ -24,6 +24,7 @@
use core::prelude::*;
use alloc::owned::Box;
use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
@ -608,6 +609,19 @@ impl<A: Clone> Clone for DList<A> {
}
}
impl<A: fmt::Show> fmt::Show for DList<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "["));
for (i, e) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *e));
}
write!(f, "]")
}
}
#[cfg(test)]
mod tests {
use std::prelude::*;
@ -1027,6 +1041,17 @@ mod tests {
}
}
#[test]
fn test_show() {
let list: DList<int> = range(0, 10).collect();
assert!(list.to_str().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
.collect();
assert!(list.to_str().as_slice() == "[just, one, test, more]");
}
#[cfg(test)]
fn fuzz_test(sz: int) {
let mut m: DList<int> = DList::new();