Skip to content

Commit bcfa49f

Browse files
authored
Rollup merge of rust-lang#109318 - joboet:better_fmt_placeholder, r=dtolnay
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`.
2 parents b3df56a + b7e68df commit bcfa49f

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 = "1.70.0")]
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
@@ -2521,22 +2521,12 @@ impl<T: Copy + Debug> Debug for Cell<T> {
25212521
#[stable(feature = "rust1", since = "1.0.0")]
25222522
impl<T: ?Sized + Debug> Debug for RefCell<T> {
25232523
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2524+
let mut d = f.debug_struct("RefCell");
25242525
match self.try_borrow() {
2525-
Ok(borrow) => f.debug_struct("RefCell").field("value", &borrow).finish(),
2526-
Err(_) => {
2527-
// The RefCell is mutably borrowed so we can't look at its value
2528-
// here. Show a placeholder instead.
2529-
struct BorrowedPlaceholder;
2530-
2531-
impl Debug for BorrowedPlaceholder {
2532-
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
2533-
f.write_str("<borrowed>")
2534-
}
2535-
}
2536-
2537-
f.debug_struct("RefCell").field("value", &BorrowedPlaceholder).finish()
2538-
}
2539-
}
2526+
Ok(borrow) => d.field("value", &borrow),
2527+
Err(_) => d.field("value", &format_args!("<borrowed>")),
2528+
};
2529+
d.finish()
25402530
}
25412531
}
25422532

library/std/src/sync/lazy_lock.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,12 @@ impl<T: Default> Default for LazyLock<T> {
222222
#[unstable(feature = "lazy_cell", issue = "109736")]
223223
impl<T: fmt::Debug, F> fmt::Debug for LazyLock<T, F> {
224224
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225+
let mut d = f.debug_tuple("LazyLock");
225226
match self.get() {
226-
Some(v) => f.debug_tuple("LazyLock").field(v).finish(),
227-
None => f.write_str("LazyLock(Uninit)"),
228-
}
227+
Some(v) => d.field(v),
228+
None => d.field(&format_args!("<uninit>")),
229+
};
230+
d.finish()
229231
}
230232
}
231233

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
@@ -365,10 +365,12 @@ impl<T> Default for OnceLock<T> {
365365
#[stable(feature = "once_cell", since = "1.70.0")]
366366
impl<T: fmt::Debug> fmt::Debug for OnceLock<T> {
367367
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
368+
let mut d = f.debug_tuple("OnceLock");
368369
match self.get() {
369-
Some(v) => f.debug_tuple("Once").field(v).finish(),
370-
None => f.write_str("Once(Uninit)"),
371-
}
370+
Some(v) => d.field(v),
371+
None => d.field(&format_args!("<uninit>")),
372+
};
373+
d.finish()
372374
}
373375
}
374376

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)