Skip to content

Commit

Permalink
Implement Show for RingBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
aochagavia committed Jun 4, 2014
1 parent d130acc commit 8e4e3ab
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/libcollections/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
//! collections::deque::Deque`.
use std::cmp;
use std::fmt;
use std::fmt::Show;
use std::iter::RandomAccessIterator;

use deque::Deque;
Expand Down Expand Up @@ -391,6 +393,19 @@ impl<A> Extendable<A> for RingBuf<A> {
}
}

impl<T: Show> Show for RingBuf<T> {
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 {
extern crate test;
Expand Down Expand Up @@ -819,4 +834,15 @@ mod tests {
e.clear();
assert!(e == RingBuf::new());
}

#[test]
fn test_show() {
let ringbuf: RingBuf<int> = range(0, 10).collect();
assert!(format!("{}", ringbuf).as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");

let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
.collect();
assert!(format!("{}", ringbuf).as_slice() == "[just, one, test, more]");
}
}

0 comments on commit 8e4e3ab

Please sign in to comment.