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.
This commit is contained in:
Huon Wilson 2014-04-01 23:58:31 +11:00
parent b8ef9fd9c9
commit 50fca0fbbb
1 changed files with 13 additions and 2 deletions

View File

@ -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())
}
}
@ -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);