Skip to content

Implement Show for RingBuf #14642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]");
}
}