From 28c4459605f3b2d59fc3805f627b001d51cea152 Mon Sep 17 00:00:00 2001 From: fort Date: Fri, 13 Jun 2014 17:58:06 -0700 Subject: [PATCH] Show impl for RefCell --- src/libcore/cell.rs | 13 ++++++++++++- src/libcore/fmt/mod.rs | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) 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.