Skip to content

Commit

Permalink
core: impl field::Value for Option<T> (#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 <DCjanus@dcjanus.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
3 people authored Sep 24, 2021
1 parent e455367 commit f250f6f
Show file tree
Hide file tree
Showing 2 changed files with 119 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 @@ -524,7 +524,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 @@ -545,6 +545,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
108 changes: 108 additions & 0 deletions tracing/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,111 @@ fn explicit_child_at_levels() {

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn option_values() {
let (subscriber, handle) = subscriber::mock()
.event(
event::mock().with_fields(
field::mock("some_str")
.with_value(&"yes")
.and(field::mock("some_bool").with_value(&true))
.and(field::mock("some_u64").with_value(&42_u64))
.only(),
),
)
.done()
.run_with_handle();

with_default(subscriber, || {
let some_str = Some("yes");
let none_str: Option<&'static str> = None;
let some_bool = Some(true);
let none_bool: Option<bool> = None;
let some_u64 = Some(42_u64);
let none_u64: Option<u64> = None;
trace!(
some_str = some_str,
none_str = none_str,
some_bool = some_bool,
none_bool = none_bool,
some_u64 = some_u64,
none_u64 = none_u64
);
});

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn option_ref_values() {
let (subscriber, handle) = subscriber::mock()
.event(
event::mock().with_fields(
field::mock("some_str")
.with_value(&"yes")
.and(field::mock("some_bool").with_value(&true))
.and(field::mock("some_u64").with_value(&42_u64))
.only(),
),
)
.done()
.run_with_handle();

with_default(subscriber, || {
let some_str = &Some("yes");
let none_str: &Option<&'static str> = &None;
let some_bool = &Some(true);
let none_bool: &Option<bool> = &None;
let some_u64 = &Some(42_u64);
let none_u64: &Option<u64> = &None;
trace!(
some_str = some_str,
none_str = none_str,
some_bool = some_bool,
none_bool = none_bool,
some_u64 = some_u64,
none_u64 = none_u64
);
});

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn option_ref_mut_values() {
let (subscriber, handle) = subscriber::mock()
.event(
event::mock().with_fields(
field::mock("some_str")
.with_value(&"yes")
.and(field::mock("some_bool").with_value(&true))
.and(field::mock("some_u64").with_value(&42_u64))
.only(),
),
)
.done()
.run_with_handle();

with_default(subscriber, || {
let some_str = &mut Some("yes");
let none_str: &mut Option<&'static str> = &mut None;
let some_bool = &mut Some(true);
let none_bool: &mut Option<bool> = &mut None;
let some_u64 = &mut Some(42_u64);
let none_u64: &mut Option<u64> = &mut None;
trace!(
some_str = some_str,
none_str = none_str,
some_bool = some_bool,
none_bool = none_bool,
some_u64 = some_u64,
none_u64 = none_u64
);
});

handle.assert_finished();
}

0 comments on commit f250f6f

Please sign in to comment.