Skip to content

Show impl for RefCell #14883

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
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion src/libcore/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,18 @@ mod test {

let refcell_ref = refcell.borrow();
assert!(format!("{}", refcell_ref).as_slice().contains("foo"));
drop(refcell_ref);
}

#[test]
fn refcell_has_sensible_show() {
use str::StrSlice;
use realstd::str::Str;

let rc = RefCell::new("foo");
assert!(format!("{}", rc).as_slice().contains("foo"));

let _rc_mut = rc.borrow_mut();
assert!(format!("{}", rc).as_slice().contains("RefCell (borrowed)"));
}

#[test]
Expand Down
11 changes: 10 additions & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#![allow(unused_variable)]

use any;
use cell::{Cell, Ref, RefMut};
use cell::{Cell, RefCell, Ref, RefMut};
use char::Char;
use collections::Collection;
use iter::{Iterator, range};
Expand Down Expand Up @@ -761,5 +761,14 @@ impl<'b, T: Show> Show for RefMut<'b, T> {
}
}

impl<T: Show> Show for RefCell<T> {
fn fmt(&self, f: &mut Formatter) -> Result {
match self.try_borrow() {
Some(_) => (*self.borrow()).fmt(f),
None => f.pad("RefCell (borrowed)")
}
}
}

// If you expected tests to be here, look instead at the run-pass/ifmt.rs test,
// it's a lot easier than creating all of the rt::Piece structures here.