Implement Show for RingBuf

This commit is contained in:
Adolfo Ochagavía 2014-06-04 16:15:04 +02:00
parent d130acc0d0
commit 8e4e3abb1d

View file

@ -14,6 +14,8 @@
//! collections::deque::Deque`.
use std::cmp;
use std::fmt;
use std::fmt::Show;
use std::iter::RandomAccessIterator;
use deque::Deque;
@ -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;
@ -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]");
}
}