Skip to content

Commit

Permalink
feat(util): less noisy Lazy/InitOnce Debug (#375)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hawkw committed Dec 1, 2022
1 parent 68a2145 commit bf86b6e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions util/src/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,10 @@ impl<T> InitOnce<T> {

impl<T: fmt::Debug> fmt::Debug for InitOnce<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("InitOnce");
d.field("type", &any::type_name::<T>());
match self.state.load(Ordering::Acquire) {
INITIALIZED => d.field("value", self.get()).finish(),
INITIALIZING => d.field("value", &format_args!("<initializing>")).finish(),
UNINITIALIZED => d.field("value", &format_args!("<uninitialized>")).finish(),
INITIALIZED => self.get().fmt(f),
INITIALIZING => f.pad("<initializing>"),
UNINITIALIZED => f.pad("<uninitialized>"),
_state => unsafe {
unreachable_unchecked!("unexpected state value {}, this is a bug!", _state)
},
Expand Down Expand Up @@ -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::<T>())
.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!("<initializing>")).finish(),
UNINITIALIZED => d.field("value", &format_args!("<uninitialized>")).finish(),
INITIALIZED => self
.get_if_present()
.expect("if state is `INITIALIZED`, value should be present")
.fmt(f),
INITIALIZING => f.pad("<initializing>"),
UNINITIALIZED => f.pad("<uninitialized>"),
_state => unsafe {
unreachable_unchecked!("unexpected state value {}, this is a bug!", _state)
},
Expand Down

0 comments on commit bf86b6e

Please sign in to comment.