about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-06-12 19:36:53 +0000
committerbors <bors@rust-lang.org>2014-06-12 19:36:53 +0000
commit3a9228b7eaa08bfdd4748a7c7178a18d6d435caa (patch)
treedd4b31c9d35ea5c145d5305a7449c38d04f072d1
parent8b87c3a2a82507ac33e067311c591c8f3815a382 (diff)
parenta8f581fad1e08ae566b10279c5ee60cb77b3672d (diff)
downloadrust-3a9228b7eaa08bfdd4748a7c7178a18d6d435caa.tar.gz
rust-3a9228b7eaa08bfdd4748a7c7178a18d6d435caa.zip
auto merge of #14811 : forticulous/rust/refcell-show, r=alexcrichton
Show impl for RefCell and friends
-rw-r--r--src/libcore/cell.rs17
-rw-r--r--src/libcore/fmt/mod.rs15
2 files changed, 31 insertions, 1 deletions
diff --git a/src/libcore/cell.rs b/src/libcore/cell.rs
index eef133181e1..7ab2ae307d4 100644
--- a/src/libcore/cell.rs
+++ b/src/libcore/cell.rs
@@ -385,6 +385,7 @@ impl<'b, T> DerefMut<T> for RefMut<'b, T> {
 #[cfg(test)]
 mod test {
     use super::*;
+    use mem::drop;
 
     #[test]
     fn smoketest_cell() {
@@ -413,6 +414,22 @@ mod test {
     }
 
     #[test]
+    fn ref_and_refmut_have_sensible_show() {
+        use str::StrSlice;
+        use realstd::str::Str;
+
+        let refcell = RefCell::new("foo");
+
+        let refcell_refmut = refcell.borrow_mut();
+        assert!(format!("{}", refcell_refmut).as_slice().contains("foo"));
+        drop(refcell_refmut);
+
+        let refcell_ref = refcell.borrow();
+        assert!(format!("{}", refcell_ref).as_slice().contains("foo"));
+        drop(refcell_ref);
+    }
+
+    #[test]
     fn double_imm_borrow() {
         let x = RefCell::new(0);
         let _b1 = x.borrow();
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 2464dfc9b5e..0770c44dfbc 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -13,13 +13,14 @@
 #![allow(unused_variable)]
 
 use any;
-use cell::Cell;
+use cell::{Cell, Ref, RefMut};
 use char::Char;
 use collections::Collection;
 use iter::{Iterator, range};
 use kinds::Copy;
 use mem;
 use option::{Option, Some, None};
+use ops::Deref;
 use result::{Ok, Err};
 use result;
 use slice::{Vector, ImmutableVector};
@@ -840,5 +841,17 @@ impl<T: Copy + Show> Show for Cell<T> {
     }
 }
 
+impl<'b, T: Show> Show for Ref<'b, T> {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        (**self).fmt(f)
+    }
+}
+
+impl<'b, T: Show> Show for RefMut<'b, T> {
+    fn fmt(&self, f: &mut Formatter) -> Result {
+        (*(self.deref())).fmt(f)
+    }
+}
+
 // 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.