Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: add Value impl for Box<T> where T: Value #2071

Merged
merged 2 commits into from
Apr 14, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,21 @@ impl<'a> Value for fmt::Arguments<'a> {
}
}

#[cfg(feature = "alloc")]
impl<T: ?Sized> crate::sealed::Sealed for alloc::boxed::Box<T> where T: Value {}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<T: ?Sized> Value for alloc::boxed::Box<T>
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
Expand Down Expand Up @@ -1021,4 +1036,24 @@ mod test {
});
assert_eq!(result, String::from("123"));
}

#[test]
#[cfg(feature = "std")]
fn record_error() {
let fields = TEST_META_1.fields();
let err: Box<dyn std::error::Error + Send + Sync + 'static> =
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));
}
}