Skip to content

Commit c987134

Browse files
committed
auto merge of #13243 : huonw/rust/cellshow, r=thestinger
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.
2 parents 80a9ff2 + 50fca0f commit c987134

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/libstd/cell.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ impl<T:Eq + Copy> Eq for Cell<T> {
6161
}
6262
}
6363

64-
impl<T: fmt::Show> fmt::Show for Cell<T> {
64+
impl<T: Copy + fmt::Show> fmt::Show for Cell<T> {
6565
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
66-
write!(f.buf, r"Cell \{ value: {} \}", unsafe{*&self.value.get()})
66+
write!(f.buf, r"Cell \{ value: {} \}", self.get())
6767
}
6868
}
6969

@@ -265,6 +265,17 @@ mod test {
265265
assert_eq!(y.get(), (30, 40));
266266
}
267267

268+
#[test]
269+
fn cell_has_sensible_show() {
270+
use str::StrSlice;
271+
272+
let x = Cell::new("foo bar");
273+
assert!(format!("{}", x).contains(x.get()));
274+
275+
x.set("baz qux");
276+
assert!(format!("{}", x).contains(x.get()));
277+
}
278+
268279
#[test]
269280
fn double_imm_borrow() {
270281
let x = RefCell::new(0);

0 commit comments

Comments
 (0)