提交 a7ac6a4e 编写于 作者: B bors

auto merge of #14642 : aochagavia/rust/pr, r=alexcrichton

The contents of a `RingBuf` are shown in the same way as the contents of a `Vector`. See the tests for examples.
......@@ -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 @@ fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
}
}
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 @@ fn test_eq() {
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]");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册