Skip to content

Commit

Permalink
Implement Show for DList
Browse files Browse the repository at this point in the history
  • Loading branch information
aochagavia committed Jun 7, 2014
1 parent 8e9e484 commit f1bff59
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use core::prelude::*;

use alloc::owned::Box;
use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit f1bff59

Please sign in to comment.