From 7e59a008f2cd4215c635d0af134a534cbc979c42 Mon Sep 17 00:00:00 2001 From: DCjanus Date: Sun, 9 May 2021 21:04:08 +0800 Subject: [PATCH 1/6] core: impl field::Value for Option --- tracing-core/src/field.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index 14cadb8f64..d9ebcf6858 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -524,7 +524,7 @@ impl fmt::Display for DisplayValue { impl crate::sealed::Sealed for DebugValue {} -impl Value for DebugValue +impl Value for DebugValue where T: fmt::Debug, { @@ -545,6 +545,16 @@ impl Value for Empty { fn record(&self, _: &Field, _: &mut dyn Visit) {} } +impl crate::sealed::Sealed for Option {} + +impl Value for Option { + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + if let Some(v) = &self { + v.record(key, visitor) + } + } +} + // ===== impl Field ===== impl Field { From d8e87d051d03413fdeaf558c9d55e9928620bc7e Mon Sep 17 00:00:00 2001 From: Bryan Burgers Date: Tue, 21 Sep 2021 16:02:47 -0500 Subject: [PATCH 2/6] Add tests for `impl Value for Option` Add tests for making an `Option where T: Value` a value in itself. --- tracing/tests/event.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index b29d622330..0e661b9ade 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -369,3 +369,38 @@ 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)), + ), + ) + .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 = None; + let some_u64 = Some(42_u64); + let none_u64: Option = 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(); +} From 049e3de34585f3d50721103a8e14348c7c22c12f Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 24 Sep 2021 11:41:28 -0700 Subject: [PATCH 3/6] Apply test suggestions from code review This adds tests for references to options, and adds `.only()` to the expected fields in the tests. --- tracing/tests/event.rs | 77 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 0e661b9ade..59e52d1cde 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -379,7 +379,8 @@ fn option_values() { field::mock("some_str") .with_value(&"yes") .and(field::mock("some_bool").with_value(&true)) - .and(field::mock("some_u64").with_value(&42_u64)), + .and(field::mock("some_u64").with_value(&42_u64)) + .only(), ), ) .done() @@ -404,3 +405,77 @@ fn option_values() { 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 = &None; + let some_u64 = &Some(42_u64); + let none_u64: &Option = &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 ption<&'static str> = &mut None; + let some_bool = &mut Some(true); + let none_bool: &mut Option = &mut None; + let some_u64 = &mut Some(42_u64); + let none_u64: &mut Option = &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(); +} From f6d159da3c5dd4c91d3c1f9244d787d677c4147d Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 24 Sep 2021 11:45:30 -0700 Subject: [PATCH 4/6] fix my stupid typo hahaha lol never edit code in the github webapp suggestions UI. i have some regrets. --- tracing/tests/event.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 59e52d1cde..2919f27505 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -462,7 +462,7 @@ fn option_ref_mut_values() { with_default(subscriber, || { let some_str = &mut Some("yes"); - let none_str: &mut ption<&'static str> = &mut None; + let none_str: &mut Option<&'static str> = &mut None; let some_bool = &mut Some(true); let none_bool: &mut Option = &mut None; let some_u64 = &mut Some(42_u64); From 600e8509950dd1a1393035fb91be8a9a35ae049d Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 24 Sep 2021 12:07:48 -0700 Subject: [PATCH 5/6] haha whoops i broke rustfmt lol --- tracing/tests/event.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 2919f27505..296a0a6986 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -441,9 +441,6 @@ fn option_ref_values() { handle.assert_finished(); } - - - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[test] fn option_ref_mut_values() { From 718d4c5e60106a0bad2a9f86edfaac4e1d5a3f06 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 24 Sep 2021 12:08:21 -0700 Subject: [PATCH 6/6] gahh accidentally removed too much whitespace ;__; --- tracing/tests/event.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tracing/tests/event.rs b/tracing/tests/event.rs index 296a0a6986..4b30032dbe 100644 --- a/tracing/tests/event.rs +++ b/tracing/tests/event.rs @@ -441,6 +441,7 @@ fn option_ref_values() { handle.assert_finished(); } + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[test] fn option_ref_mut_values() {