diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs index 7ab2ae307d465..7948fb88a98a4 100644 --- a/src/libcore/cell.rs +++ b/src/libcore/cell.rs @@ -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] diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index d778f3b47a170..2e21aba86da28 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -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}; @@ -761,5 +761,14 @@ impl<'b, T: Show> Show for RefMut<'b, T> { } } +impl Show for RefCell { + 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.