From 2677d191c574c7921838d0d98e98a6220662c65c Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 1 Dec 2022 10:05:13 -0800 Subject: [PATCH] feat(util): less noisy `Lazy`/`InitOnce` `Debug` This branch simplifies the `Lazy` and `InitOnce` types' `fmt::Debug` impls to behave transparently if the value is initialized. It's not actually that useful to indicate that the value is in a lazy cell once it has been initialized, since that can be determined from reading the code. --- util/src/sync/once.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/util/src/sync/once.rs b/util/src/sync/once.rs index c5eca2793..a9062cdd7 100644 --- a/util/src/sync/once.rs +++ b/util/src/sync/once.rs @@ -205,12 +205,10 @@ impl InitOnce { impl fmt::Debug for InitOnce { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut d = f.debug_struct("InitOnce"); - d.field("type", &any::type_name::()); match self.state.load(Ordering::Acquire) { - INITIALIZED => d.field("value", self.get()).finish(), - INITIALIZING => d.field("value", &format_args!("")).finish(), - UNINITIALIZED => d.field("value", &format_args!("")).finish(), + INITIALIZED => self.get().fmt(f), + INITIALIZING => f.pad(""), + UNINITIALIZED => f.pad(""), _state => unsafe { unreachable_unchecked!("unexpected state value {}, this is a bug!", _state) }, @@ -358,13 +356,13 @@ where T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut d = f.debug_struct("Lazy"); - d.field("type", &any::type_name::()) - .field("initializer", &format_args!("...")); match self.state.load(Ordering::Acquire) { - INITIALIZED => d.field("value", self.get_if_present().unwrap()).finish(), - INITIALIZING => d.field("value", &format_args!("")).finish(), - UNINITIALIZED => d.field("value", &format_args!("")).finish(), + INITIALIZED => self + .get_if_present() + .expect("if state is `INITIALIZED`, value should be present") + .fmt(f), + INITIALIZING => f.pad(""), + UNINITIALIZED => f.pad(""), _state => unsafe { unreachable_unchecked!("unexpected state value {}, this is a bug!", _state) },