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

feat(derive): enable more boxed types to be #[diagnostic_source] #338

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 19 additions & 2 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -85,12 +85,29 @@ macro_rules! box_impls {
}
}

box_impls! {
box_error_impls! {
Box<dyn Diagnostic>,
Box<dyn Diagnostic + Send>,
Box<dyn Diagnostic + Send + Sync>
}

macro_rules! box_borrow_impls {
($($box_type:ty),*) => {
$(
impl std::borrow::Borrow<dyn Diagnostic> for $box_type {
fn borrow(&self) -> &(dyn Diagnostic + 'static) {
self.as_ref()
}
}
)*
}
}

box_borrow_impls! {
Box<dyn Diagnostic + Send>,
Box<dyn Diagnostic + Send + Sync>
}

impl<T: Diagnostic + Send + Sync + 'static> From<T>
for Box<dyn Diagnostic + Send + Sync + 'static>
{
Expand Down
14 changes: 14 additions & 0 deletions tests/test_diagnostic_source_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ struct TestTupleError(#[diagnostic_source] AnErr);
#[error("TestError")]
struct TestBoxedError(#[diagnostic_source] Box<dyn Diagnostic>);

#[derive(Debug, miette::Diagnostic, thiserror::Error)]
#[error("TestError")]
struct TestBoxedSendError(#[diagnostic_source] Box<dyn Diagnostic + Send>);

#[derive(Debug, miette::Diagnostic, thiserror::Error)]
#[error("TestError")]
struct TestBoxedSendSyncError(#[diagnostic_source] Box<dyn Diagnostic + Send + Sync>);

#[derive(Debug, miette::Diagnostic, thiserror::Error)]
#[error("TestError")]
struct TestArcedError(#[diagnostic_source] std::sync::Arc<dyn Diagnostic>);
Expand Down Expand Up @@ -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());
}
Expand Down
Loading