Skip to content

Commit 6d68aab

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 f333048 commit 6d68aab

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

tracing-core/src/field.rs

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

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

0 commit comments

Comments
 (0)