std: make Debug representations of [Lazy, Once]*[Cell, Lock] consistent with Mutex and RwLock

`Mutex` prints `<locked>` as a field value when its inner value cannot be accessed, but the lazy types print a fixed string like "`OnceCell(Uninit)`". This could cause confusion if the inner type is a unit type named `Uninit` and does not respect the pretty-printing flag. With this change, the format message is now "`OnceCell(<uninit>)`", consistent with `Mutex`.
This commit is contained in:
joboet 2023-03-18 20:06:12 +01:00
parent 9e7f72c57d
commit b7e68dfc94
No known key found for this signature in database
GPG Key ID: 704E0149B0194B3C
6 changed files with 22 additions and 38 deletions

View File

@ -250,10 +250,12 @@ fn default() -> Self {
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> { impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_tuple("OnceCell");
match self.get() { match self.get() {
Some(v) => f.debug_tuple("OnceCell").field(v).finish(), Some(v) => d.field(v),
None => f.write_str("OnceCell(Uninit)"), None => d.field(&format_args!("<uninit>")),
} };
d.finish()
} }
} }

View File

@ -2669,22 +2669,12 @@ fn fmt(&self, f: &mut Formatter<'_>) -> Result {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl<T: ?Sized + Debug> Debug for RefCell<T> { impl<T: ?Sized + Debug> Debug for RefCell<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let mut d = f.debug_struct("RefCell");
match self.try_borrow() { match self.try_borrow() {
Ok(borrow) => f.debug_struct("RefCell").field("value", &borrow).finish(), Ok(borrow) => d.field("value", &borrow),
Err(_) => { Err(_) => d.field("value", &format_args!("<borrowed>")),
// The RefCell is mutably borrowed so we can't look at its value };
// here. Show a placeholder instead. d.finish()
struct BorrowedPlaceholder;
impl Debug for BorrowedPlaceholder {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_str("<borrowed>")
}
}
f.debug_struct("RefCell").field("value", &BorrowedPlaceholder).finish()
}
}
} }
} }

View File

@ -157,10 +157,12 @@ fn default() -> LazyLock<T> {
#[unstable(feature = "lazy_cell", issue = "109736")] #[unstable(feature = "lazy_cell", issue = "109736")]
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> { impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_tuple("LazyLock");
match self.get() { match self.get() {
Some(v) => f.debug_tuple("LazyLock").field(v).finish(), Some(v) => d.field(v),
None => f.write_str("LazyLock(Uninit)"), None => d.field(&format_args!("<uninit>")),
} };
d.finish()
} }
} }

View File

@ -490,13 +490,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
d.field("data", &&**err.get_ref()); d.field("data", &&**err.get_ref());
} }
Err(TryLockError::WouldBlock) => { Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder; d.field("data", &format_args!("<locked>"));
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
d.field("data", &LockedPlaceholder);
} }
} }
d.field("poisoned", &self.poison.get()); d.field("poisoned", &self.poison.get());

View File

@ -366,10 +366,12 @@ fn default() -> OnceLock<T> {
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")] #[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
impl<T: fmt::Debug> fmt::Debug for OnceLock<T> { impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_tuple("OnceLock");
match self.get() { match self.get() {
Some(v) => f.debug_tuple("Once").field(v).finish(), Some(v) => d.field(v),
None => f.write_str("Once(Uninit)"), None => d.field(&format_args!("<uninit>")),
} };
d.finish()
} }
} }

View File

@ -485,13 +485,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
d.field("data", &&**err.get_ref()); d.field("data", &&**err.get_ref());
} }
Err(TryLockError::WouldBlock) => { Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder; d.field("data", &format_args!("<locked>"));
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
d.field("data", &LockedPlaceholder);
} }
} }
d.field("poisoned", &self.poison.get()); d.field("poisoned", &self.poison.get());