diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index aa279eea10..8744faf3da 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -225,6 +225,17 @@ pub trait Visit { /// Visit a value implementing `fmt::Debug`. fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug); + + /// Visit an Option + fn record_option(&mut self, field: &Field, value: Option<&(dyn Value + 'static)>) + where + Self: std::marker::Sized, + { + if let Some(inner_value) = value { + inner_value.record(field, self) + } else { + } + } } /// A field value of an erased type. @@ -441,6 +452,15 @@ impl<'a> Value for fmt::Arguments<'a> { } } +impl Value for Option +where + T: Value + 'static, +{ + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + visitor.record_option(key, self.as_ref().map(|v| v as &dyn Value)) + } +} + impl fmt::Debug for dyn Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // We are only going to be recording the field value, so we don't @@ -476,6 +496,8 @@ impl fmt::Display for dyn Value { } } +impl crate::sealed::Sealed for Option {} + // ===== impl DisplayValue ===== impl crate::sealed::Sealed for DisplayValue {}