Skip to content

Commit c4bcf5c

Browse files
authored
core: impl Value for dyn Error + Send/Sync (#2066)
## Motivation `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. This is related to #1308. ## Solution Add impls for `dyn Error + …` variants for `Send`, `Sync`, and `Send + Sync`. 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 5888369 commit c4bcf5c

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
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
@@ -423,6 +423,39 @@ impl Value for dyn std::error::Error + 'static {
423423
}
424424
}
425425

426+
#[cfg(feature = "std")]
427+
impl crate::sealed::Sealed for dyn std::error::Error + Send + 'static {}
428+
429+
#[cfg(feature = "std")]
430+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
431+
impl Value for dyn std::error::Error + Send + 'static {
432+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
433+
(self as &dyn std::error::Error).record(key, visitor)
434+
}
435+
}
436+
437+
#[cfg(feature = "std")]
438+
impl crate::sealed::Sealed for dyn std::error::Error + Sync + 'static {}
439+
440+
#[cfg(feature = "std")]
441+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
442+
impl Value for dyn std::error::Error + Sync + 'static {
443+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
444+
(self as &dyn std::error::Error).record(key, visitor)
445+
}
446+
}
447+
448+
#[cfg(feature = "std")]
449+
impl crate::sealed::Sealed for dyn std::error::Error + Send + Sync + 'static {}
450+
451+
#[cfg(feature = "std")]
452+
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
453+
impl Value for dyn std::error::Error + Send + Sync + 'static {
454+
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
455+
(self as &dyn std::error::Error).record(key, visitor)
456+
}
457+
}
458+
426459
impl<'a, T: ?Sized> crate::sealed::Sealed for &'a T where T: Value + crate::sealed::Sealed + 'a {}
427460

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

0 commit comments

Comments
 (0)