Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
110 changes: 110 additions & 0 deletions tracing/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,113 @@ 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();
}