Skip to content

Commit

Permalink
core: impl field::Value for Option<T> (tokio-rs#1585)
Browse files Browse the repository at this point in the history
## Motivation

Building a span with some field might be `None` is bother now.

Before:
```rust
let span = info!("example", data = Empty );
if let Some(data) = foo() {
    span.record("data", &data);
}
```

After:
```rust
let span = info!("example", data = foo() );
```

Co-authored-by: DCjanus <[email protected]>
Co-authored-by: Eliza Weisman <[email protected]>
  • Loading branch information
3 people authored and kaffarell committed May 22, 2024
1 parent d1c1b5f commit fd7ba5a
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl<T: fmt::Display> fmt::Display for DisplayValue<T> {

impl<T: fmt::Debug> crate::sealed::Sealed for DebugValue<T> {}

impl<T: fmt::Debug> Value for DebugValue<T>
impl<T> Value for DebugValue<T>
where
T: fmt::Debug,
{
Expand All @@ -664,6 +664,16 @@ impl Value for Empty {
fn record(&self, _: &Field, _: &mut dyn Visit) {}
}

impl<T: Value> crate::sealed::Sealed for Option<T> {}

impl<T: Value> Value for Option<T> {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
if let Some(v) = &self {
v.record(key, visitor)
}
}
}

// ===== impl Field =====

impl Field {
Expand Down

0 comments on commit fd7ba5a

Please sign in to comment.