Skip to content

Commit b7e68df

Browse files
committed
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`.
1 parent 9e7f72c commit b7e68df

File tree

6 files changed

+22
-38
lines changed

6 files changed

+22
-38
lines changed

library/core/src/cell/once.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,12 @@ impl<T> Default for OnceCell<T> {
250250
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
251251
impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
252252
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
253+
let mut d = f.debug_tuple("OnceCell");
253254
match self.get() {
254-
Some(v) => f.debug_tuple("OnceCell").field(v).finish(),
255-
None => f.write_str("OnceCell(Uninit)"),
256-
}
255+
Some(v) => d.field(v),
256+
None => d.field(&format_args!("<uninit>")),
257+
};
258+
d.finish()
257259
}
258260
}
259261

library/core/src/fmt/mod.rs

+5-15
Original file line numberDiff line numberDiff line change
@@ -2669,22 +2669,12 @@ impl<T: Copy + Debug> Debug for Cell<T> {
26692669
#[stable(feature = "rust1", since = "1.0.0")]
26702670
impl<T: ?Sized + Debug> Debug for RefCell<T> {
26712671
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2672+
let mut d = f.debug_struct("RefCell");
26722673
match self.try_borrow() {
2673-
Ok(borrow) => f.debug_struct("RefCell").field("value", &borrow).finish(),
2674-
Err(_) => {
2675-
// The RefCell is mutably borrowed so we can't look at its value
2676-
// here. Show a placeholder instead.
2677-
struct BorrowedPlaceholder;
2678-
2679-
impl Debug for BorrowedPlaceholder {
2680-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2681-
f.write_str("<borrowed>")
2682-
}
2683-
}
2684-
2685-
f.debug_struct("RefCell").field("value", &BorrowedPlaceholder).finish()
2686-
}
2687-
}
2674+
Ok(borrow) => d.field("value", &borrow),
2675+
Err(_) => d.field("value", &format_args!("<borrowed>")),
2676+
};
2677+
d.finish()
26882678
}
26892679
}
26902680

library/std/src/sync/lazy_lock.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,12 @@ impl<T: Default> Default for LazyLock<T> {
157157
#[unstable(feature = "lazy_cell", issue = "109736")]
158158
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
159159
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160+
let mut d = f.debug_tuple("LazyLock");
160161
match self.get() {
161-
Some(v) => f.debug_tuple("LazyLock").field(v).finish(),
162-
None => f.write_str("LazyLock(Uninit)"),
163-
}
162+
Some(v) => d.field(v),
163+
None => d.field(&format_args!("<uninit>")),
164+
};
165+
d.finish()
164166
}
165167
}
166168

library/std/src/sync/mutex.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -490,13 +490,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
490490
d.field("data", &&**err.get_ref());
491491
}
492492
Err(TryLockError::WouldBlock) => {
493-
struct LockedPlaceholder;
494-
impl fmt::Debug for LockedPlaceholder {
495-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
496-
f.write_str("<locked>")
497-
}
498-
}
499-
d.field("data", &LockedPlaceholder);
493+
d.field("data", &format_args!("<locked>"));
500494
}
501495
}
502496
d.field("poisoned", &self.poison.get());

library/std/src/sync/once_lock.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,12 @@ impl<T> const Default for OnceLock<T> {
366366
#[stable(feature = "once_cell", since = "CURRENT_RUSTC_VERSION")]
367367
impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
368368
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
369+
let mut d = f.debug_tuple("OnceLock");
369370
match self.get() {
370-
Some(v) => f.debug_tuple("Once").field(v).finish(),
371-
None => f.write_str("Once(Uninit)"),
372-
}
371+
Some(v) => d.field(v),
372+
None => d.field(&format_args!("<uninit>")),
373+
};
374+
d.finish()
373375
}
374376
}
375377

library/std/src/sync/rwlock.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -485,13 +485,7 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
485485
d.field("data", &&**err.get_ref());
486486
}
487487
Err(TryLockError::WouldBlock) => {
488-
struct LockedPlaceholder;
489-
impl fmt::Debug for LockedPlaceholder {
490-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
491-
f.write_str("<locked>")
492-
}
493-
}
494-
d.field("data", &LockedPlaceholder);
488+
d.field("data", &format_args!("<locked>"));
495489
}
496490
}
497491
d.field("poisoned", &self.poison.get());

0 commit comments

Comments
 (0)