Skip to content

Commit

Permalink
impl<T: Value> Value for Option<T> from tokio-rs#1585
Browse files Browse the repository at this point in the history
  • Loading branch information
AmoleR committed Oct 10, 2023
1 parent 4be2eab commit 17eb586
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,16 @@ impl Value for str {
}
}

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)
}
}
}

#[cfg(feature = "std")]
impl crate::sealed::Sealed for dyn std::error::Error + 'static {}

Expand Down
108 changes: 108 additions & 0 deletions tracing/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,111 @@ fn constant_field_name() {

handle.assert_finished();
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn option_values() {
let (subscriber, handle) = collector::mock()
.event(
expect::event().with_fields(
expect::field("some_str")
.with_value(&"yes")
.and(expect::field("some_bool").with_value(&true))
.and(expect::field("some_u64").with_value(&42_u64))
.only(),
),
)
.only()
.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) = collector::mock()
.event(
expect::event().with_fields(
expect::field("some_str")
.with_value(&"yes")
.and(expect::field("some_bool").with_value(&true))
.and(expect::field("some_u64").with_value(&42_u64))
.only(),
),
)
.only()
.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) = collector::mock()
.event(
expect::event().with_fields(
expect::field("some_str")
.with_value(&"yes")
.and(expect::field("some_bool").with_value(&true))
.and(expect::field("some_u64").with_value(&42_u64))
.only(),
),
)
.only()
.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 17eb586

Please sign in to comment.