Skip to content

Commit c9cfe7e

Browse files
committed
core: impl Value for dyn Error + Send/Sync
`Value` was already implemented for `dyn Error + 'static`, but rust doesn't silently coerce trait objects. This means that passing an error of type `dyn Error + Send + Sync + 'static` would not work. These extra impls just delegate to the existing `dyn Error + 'static` impl. Also update one of the examples to use `dyn Error + Send + Sync` to demonstrate that this works. Refs: #1308
1 parent 4f714f0 commit c9cfe7e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

examples/examples/fmt/yak_shave.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use tracing::{debug, error, info, span, trace, warn, Level};
77
// every time the instrumented function is called. The span is named after the
88
// the function or method. Paramaters passed to the function are recorded as fields.
99
#[tracing::instrument]
10-
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> {
10+
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
1111
// this creates an event at the TRACE log level with two fields:
1212
// - `excitement`, with the key "excitement" and the value "yay!"
1313
// - `message`, with the key "message" and the value "hello! I'm gonna shave a yak."

tracing-core/src/field.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,39 @@ impl Value for dyn std::error::Error + 'static {
523523
}
524524
}
525525

526+
#[cfg(feature = "std")]
527+
impl crate::sealed::Sealed for dyn std::error::Error + Send + 'static {}
528+
529+
#[cfg(feature = "std")]
530+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
531+
impl Value for dyn std::error::Error + Send + 'static {
532+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
533+
(self as &dyn std::error::Error).record(key, visitor)
534+
}
535+
}
536+
537+
#[cfg(feature = "std")]
538+
impl crate::sealed::Sealed for dyn std::error::Error + Sync + 'static {}
539+
540+
#[cfg(feature = "std")]
541+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
542+
impl Value for dyn std::error::Error + Sync + 'static {
543+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
544+
(self as &dyn std::error::Error).record(key, visitor)
545+
}
546+
}
547+
548+
#[cfg(feature = "std")]
549+
impl crate::sealed::Sealed for dyn std::error::Error + Send + Sync + 'static {}
550+
551+
#[cfg(feature = "std")]
552+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
553+
impl Value for dyn std::error::Error + Send + Sync + 'static {
554+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
555+
(self as &dyn std::error::Error).record(key, visitor)
556+
}
557+
}
558+
526559
impl<'a, T: ?Sized> crate::sealed::Sealed for &'a T where T: Value + crate::sealed::Sealed + 'a {}
527560

528561
impl<'a, T: ?Sized> Value for &'a T

0 commit comments

Comments
 (0)