From c2f06f6cca15cbdd083dbff3d46b7729056ac6a4 Mon Sep 17 00:00:00 2001 From: Brooks Rady Date: Wed, 7 Feb 2024 18:20:18 +0000 Subject: [PATCH] feat(derive): enable more boxed types to be #[diagnostic_source] (#338) --- src/protocol.rs | 21 +++++++++++++++++++-- tests/test_diagnostic_source_macro.rs | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index 6b98a97..310fc86 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -69,7 +69,7 @@ pub trait Diagnostic: std::error::Error { } } -macro_rules! box_impls { +macro_rules! box_error_impls { ($($box_type:ty),*) => { $( impl std::error::Error for $box_type { @@ -85,12 +85,29 @@ macro_rules! box_impls { } } -box_impls! { +box_error_impls! { Box, Box, Box } +macro_rules! box_borrow_impls { + ($($box_type:ty),*) => { + $( + impl std::borrow::Borrow for $box_type { + fn borrow(&self) -> &(dyn Diagnostic + 'static) { + self.as_ref() + } + } + )* + } +} + +box_borrow_impls! { + Box, + Box +} + impl From for Box { diff --git a/tests/test_diagnostic_source_macro.rs b/tests/test_diagnostic_source_macro.rs index e5305ac..939812c 100644 --- a/tests/test_diagnostic_source_macro.rs +++ b/tests/test_diagnostic_source_macro.rs @@ -41,6 +41,14 @@ struct TestTupleError(#[diagnostic_source] AnErr); #[error("TestError")] struct TestBoxedError(#[diagnostic_source] Box); +#[derive(Debug, miette::Diagnostic, thiserror::Error)] +#[error("TestError")] +struct TestBoxedSendError(#[diagnostic_source] Box); + +#[derive(Debug, miette::Diagnostic, thiserror::Error)] +#[error("TestError")] +struct TestBoxedSendSyncError(#[diagnostic_source] Box); + #[derive(Debug, miette::Diagnostic, thiserror::Error)] #[error("TestError")] struct TestArcedError(#[diagnostic_source] std::sync::Arc); @@ -71,6 +79,12 @@ fn test_diagnostic_source() { let error = TestBoxedError(Box::new(AnErr)); assert!(error.diagnostic_source().is_some()); + let error = TestBoxedSendError(Box::new(AnErr)); + assert!(error.diagnostic_source().is_some()); + + let error = TestBoxedSendSyncError(Box::new(AnErr)); + assert!(error.diagnostic_source().is_some()); + let error = TestArcedError(std::sync::Arc::new(AnErr)); assert!(error.diagnostic_source().is_some()); }