diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-07-31 22:51:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-31 22:51:12 +0200 |
| commit | bcfa49f162de64209cc9ed64cfcd36942e67b02a (patch) | |
| tree | e6b553b0766ebfc8ea3bd8f24ed36a2591761544 /library/core | |
| parent | b3df56a65f9424a7f4af101091582f49cfc29286 (diff) | |
| parent | b7e68dfc945c4cf8d1753cc3a8be48a71bb39d5a (diff) | |
| download | rust-bcfa49f162de64209cc9ed64cfcd36942e67b02a.tar.gz rust-bcfa49f162de64209cc9ed64cfcd36942e67b02a.zip | |
Rollup merge of #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`.
Diffstat (limited to 'library/core')
| -rw-r--r-- | library/core/src/cell/once.rs | 8 | ||||
| -rw-r--r-- | library/core/src/fmt/mod.rs | 20 |
2 files changed, 10 insertions, 18 deletions
diff --git a/library/core/src/cell/once.rs b/library/core/src/cell/once.rs index 5f06a7b0795..2e8534f651a 100644 --- a/library/core/src/cell/once.rs +++ b/library/core/src/cell/once.rs @@ -250,10 +250,12 @@ impl<T> Default for OnceCell<T> { #[stable(feature = "once_cell", since = "1.70.0")] impl<T: fmt::Debug> fmt::Debug for OnceCell<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut d = f.debug_tuple("OnceCell"); match self.get() { - Some(v) => f.debug_tuple("OnceCell").field(v).finish(), - None => f.write_str("OnceCell(Uninit)"), - } + Some(v) => d.field(v), + None => d.field(&format_args!("<uninit>")), + }; + d.finish() } } diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 1786b309c5b..9ce6093f1d1 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -2521,22 +2521,12 @@ impl<T: Copy + Debug> Debug for Cell<T> { #[stable(feature = "rust1", since = "1.0.0")] impl<T: ?Sized + Debug> Debug for RefCell<T> { fn fmt(&self, f: &mut Formatter<'_>) -> Result { + let mut d = f.debug_struct("RefCell"); match self.try_borrow() { - Ok(borrow) => f.debug_struct("RefCell").field("value", &borrow).finish(), - Err(_) => { - // The RefCell is mutably borrowed so we can't look at its value - // here. Show a placeholder instead. - 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() - } - } + Ok(borrow) => d.field("value", &borrow), + Err(_) => d.field("value", &format_args!("<borrowed>")), + }; + d.finish() } } |
