Skip to content

Commit

Permalink
core: add Value impl for Box<T> where T: Value (tokio-rs#2071)
Browse files Browse the repository at this point in the history
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
tokio-rs#2066, but it may be useful for other boxed values as well.

Refs: tokio-rs#1308
  • Loading branch information
hawkw authored and kaffarell committed May 22, 2024
1 parent 903258e commit bfda89e
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,4 +1299,24 @@ mod test {
});
assert_eq!(result, format!("{}", r#"[61 62 63]" "[c0 ff ee]"#));
}

#[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));
}
}

0 comments on commit bfda89e

Please sign in to comment.