Skip to content

Commit 96347c7

Browse files
committed
core: add Value impl for Box<T> where T: Value (#2071)
This commit adds a `Value` implementation for `Box<T> where T: Value`. This is *primarily* intended to make `Box<dyn Error + ...>` 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
1 parent 8c67359 commit 96347c7

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

tracing-core/src/field.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,18 @@ impl<'a> Value for fmt::Arguments<'a> {
588588
}
589589
}
590590

591+
impl<T: ?Sized> crate::sealed::Sealed for crate::stdlib::boxed::Box<T> where T: Value {}
592+
593+
impl<T: ?Sized> Value for crate::stdlib::boxed::Box<T>
594+
where
595+
T: Value,
596+
{
597+
#[inline]
598+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
599+
self.as_ref().record(key, visitor)
600+
}
601+
}
602+
591603
impl fmt::Debug for dyn Value {
592604
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
593605
// We are only going to be recording the field value, so we don't
@@ -1155,4 +1167,24 @@ mod test {
11551167
});
11561168
assert_eq!(result, "123".to_owned());
11571169
}
1170+
1171+
#[test]
1172+
#[cfg(feature = "std")]
1173+
fn record_error() {
1174+
let fields = TEST_META_1.fields();
1175+
let err: Box<dyn std::error::Error + Send + Sync + 'static> =
1176+
std::io::Error::new(std::io::ErrorKind::Other, "lol").into();
1177+
let values = &[
1178+
(&fields.field("foo").unwrap(), Some(&err as &dyn Value)),
1179+
(&fields.field("bar").unwrap(), Some(&Empty as &dyn Value)),
1180+
(&fields.field("baz").unwrap(), Some(&Empty as &dyn Value)),
1181+
];
1182+
let valueset = fields.value_set(values);
1183+
let mut result = String::new();
1184+
valueset.record(&mut |_: &Field, value: &dyn fmt::Debug| {
1185+
use core::fmt::Write;
1186+
write!(&mut result, "{:?}", value).unwrap();
1187+
});
1188+
assert_eq!(result, format!("{}", err));
1189+
}
11581190
}

0 commit comments

Comments
 (0)