Skip to content

Commit

Permalink
Better debug impl for Lazy and Once
Browse files Browse the repository at this point in the history
  • Loading branch information
zesterer committed Jun 12, 2023
1 parent 723b2d7 commit 605c9f4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ pub struct Lazy<T, F = fn() -> T, R = Spin> {

impl<T: fmt::Debug, F, R> fmt::Debug for Lazy<T, F, R> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Lazy")
.field("cell", &self.cell)
.field("init", &"..")
.finish()
let mut d = f.debug_tuple("Lazy");
if let Some(x) = self.cell.get() {
d.field(&x)
} else {
d.field(format_args!("<uninit>"))
}
d.finish()
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ impl<T, R> Default for Once<T, R> {

impl<T: fmt::Debug, R> fmt::Debug for Once<T, R> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.get() {
Some(s) => write!(f, "Once {{ data: ")
.and_then(|()| s.fmt(f))
.and_then(|()| write!(f, "}}")),
None => write!(f, "Once {{ <uninitialized> }}"),
let mut d = f.debug_tuple("Once");
if let Some(x) = self.get() {
d.field(&x)
} else {
d.field(format_args!("<uninit>"))
}
d.finish()
}
}

Expand Down Expand Up @@ -461,7 +462,7 @@ impl<T, R> Once<T, R> {
}
}

/// Returns a the inner value if the [`Once`] has been initialized.
/// Returns a the inner value if the [`Once`] has been initialized.
/// # Safety
///
/// This is *extremely* unsafe if the `Once` has not already been initialized because a reference to uninitialized
Expand Down

0 comments on commit 605c9f4

Please sign in to comment.