Implement Show for RingBuf
This commit is contained in:
parent
d130acc0d0
commit
8e4e3abb1d
1 changed files with 26 additions and 0 deletions
|
@ -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]");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue