Skip to content

Commit

Permalink
Fix Display for cell::{Ref,RefMut}
Browse files Browse the repository at this point in the history
These guards changed to pointers in #97027, but their `Display` was
formatting that field directly, which made it show the raw pointer
value. Now we go through `Deref` to display the real value again.
  • Loading branch information
cuviper committed May 20, 2022
1 parent 22ee395 commit 83abb7c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 2 additions & 2 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,7 +1487,7 @@ impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Ref<'b, U>> for Ref<'b,
#[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for Ref<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
(**self).fmt(f)
}
}

Expand Down Expand Up @@ -1735,7 +1735,7 @@ impl<'b, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<RefMut<'b, U>> for RefM
#[stable(feature = "std_guard_impls", since = "1.20.0")]
impl<T: ?Sized + fmt::Display> fmt::Display for RefMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.value.fmt(f)
(**self).fmt(f)
}
}

Expand Down
6 changes: 4 additions & 2 deletions library/core/tests/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ fn ref_and_refmut_have_sensible_show() {
let refcell = RefCell::new("foo");

let refcell_refmut = refcell.borrow_mut();
assert!(format!("{refcell_refmut:?}").contains("foo"));
assert_eq!(format!("{refcell_refmut}"), "foo"); // Display
assert!(format!("{refcell_refmut:?}").contains("foo")); // Debug
drop(refcell_refmut);

let refcell_ref = refcell.borrow();
assert!(format!("{refcell_ref:?}").contains("foo"));
assert_eq!(format!("{refcell_ref}"), "foo"); // Display
assert!(format!("{refcell_ref:?}").contains("foo")); // Debug
drop(refcell_ref);
}

Expand Down

0 comments on commit 83abb7c

Please sign in to comment.