From 96347c7b78d58f3d51b0979cfbe8499dac406583 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 14 Apr 2022 12:23:52 -0700 Subject: [PATCH] core: add `Value` impl for `Box where T: Value` (#2071) This commit adds a `Value` implementation for `Box where T: Value`. This is *primarily* intended to make `Box` implement `Value`, building on the `Value` impls for `dyn Error + ...` added in #2066, but it may be useful for other boxed values as well. Refs: #1308 --- tracing-core/src/field.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tracing-core/src/field.rs b/tracing-core/src/field.rs index eeff65ddf6..e40d382773 100644 --- a/tracing-core/src/field.rs +++ b/tracing-core/src/field.rs @@ -588,6 +588,18 @@ impl<'a> Value for fmt::Arguments<'a> { } } +impl crate::sealed::Sealed for crate::stdlib::boxed::Box where T: Value {} + +impl Value for crate::stdlib::boxed::Box +where + T: Value, +{ + #[inline] + fn record(&self, key: &Field, visitor: &mut dyn Visit) { + self.as_ref().record(key, visitor) + } +} + 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 @@ -1155,4 +1167,24 @@ mod test { }); assert_eq!(result, "123".to_owned()); } + + #[test] + #[cfg(feature = "std")] + fn record_error() { + let fields = TEST_META_1.fields(); + let err: Box = + std::io::Error::new(std::io::ErrorKind::Other, "lol").into(); + let values = &[ + (&fields.field("foo").unwrap(), Some(&err as &dyn Value)), + (&fields.field("bar").unwrap(), Some(&Empty as &dyn Value)), + (&fields.field("baz").unwrap(), Some(&Empty as &dyn Value)), + ]; + let valueset = fields.value_set(values); + let mut result = String::new(); + valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| { + use core::fmt::Write; + write!(&mut result, "{:?}", value).unwrap(); + }); + assert_eq!(result, format!("{}", err)); + } }