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!(blanket_impls): impl Diagnostic for &T and Box<T> #367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,48 @@ pub trait Diagnostic: std::error::Error {
}
}

macro_rules! blanket_ref_impls {
($($ref_type:ty),+ $(,)?) => {
$(
impl<T: Diagnostic> Diagnostic for $ref_type {
fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
(**self).code()
}

fn severity(&self) -> Option<Severity> {
(**self).severity()
}

fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
(**self).help()
}

fn url<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
(**self).url()
}

fn source_code(&self) -> Option<&dyn SourceCode> {
(**self).source_code()
}

fn labels(&self) -> Option<Box<dyn Iterator<Item = LabeledSpan> + '_>> {
(**self).labels()
}

fn related<'a>(&'a self) -> Option<Box<dyn Iterator<Item = &'a dyn Diagnostic> + 'a>> {
(**self).related()
}

fn diagnostic_source(&self) -> Option<&dyn Diagnostic> {
(**self).diagnostic_source()
}
}
)+
};
}

blanket_ref_impls!(&T, Box<T>);

macro_rules! box_error_impls {
($($box_type:ty),*) => {
$(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_blanket.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use miette::{Diagnostic, MietteDiagnostic};

fn assert_diagnostic<T: Diagnostic>() {}

#[test]
fn test_ref() {
assert_diagnostic::<&MietteDiagnostic>()
}

#[test]
fn test_box() {
assert_diagnostic::<Box<MietteDiagnostic>>()
}