diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-04-01 23:58:31 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-04-01 23:58:31 +1100 |
| commit | 50fca0fbbb8f40714f79dbbec83bf61f34ed3945 (patch) | |
| tree | 557e59830af936367fb71b3077738927ca22071b | |
| parent | b8ef9fd9c9f642ce7b8aed82782a1ed745d08d64 (diff) | |
| download | rust-50fca0fbbb8f40714f79dbbec83bf61f34ed3945.tar.gz rust-50fca0fbbb8f40714f79dbbec83bf61f34ed3945.zip | |
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.
| -rw-r--r-- | src/libstd/cell.rs | 15 |
1 files 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<T:Eq + Copy> Eq for Cell<T> { } } -impl<T: fmt::Show> fmt::Show for Cell<T> { +impl<T: Copy + fmt::Show> fmt::Show for Cell<T> { 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()) } } @@ -266,6 +266,17 @@ mod test { } #[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); let _b1 = x.borrow(); |
