From 50fca0fbbb8f40714f79dbbec83bf61f34ed3945 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Tue, 1 Apr 2014 23:58:31 +1100 Subject: [PATCH] std: fix Cell's Show instance. Previously it was printing the address of the Unsafe contained in the Cell (i.e. the address of the Cell itself). This is clearly useless, and was presumably a mistake due to writing `*&` instead of `&*`. However, this later expression is likely also incorrect, since it takes a reference into a Cell while other user code is executing (i.e. the Show instance for the contained type), hence the contents should just be copied out. --- src/libstd/cell.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 102b87a3733..eb114e89510 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -61,9 +61,9 @@ impl Eq for Cell { } } -impl fmt::Show for Cell { +impl fmt::Show for Cell { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f.buf, r"Cell \{ value: {} \}", unsafe{*&self.value.get()}) + write!(f.buf, r"Cell \{ value: {} \}", self.get()) } } @@ -265,6 +265,17 @@ mod test { assert_eq!(y.get(), (30, 40)); } + #[test] + fn cell_has_sensible_show() { + use str::StrSlice; + + let x = Cell::new("foo bar"); + assert!(format!("{}", x).contains(x.get())); + + x.set("baz qux"); + assert!(format!("{}", x).contains(x.get())); + } + #[test] fn double_imm_borrow() { let x = RefCell::new(0);