Skip to content

Commit

Permalink
add as_dyn trait
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao committed Nov 8, 2023
1 parent 20118b0 commit 5853053
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/as_dyn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Extension trait for [`std::error::Error`] that casts the error to
/// a trait object.
pub trait AsDyn: crate::error_sealed::Sealed {
/// Casts the error to a trait object.
fn as_dyn(&self) -> &(dyn std::error::Error + '_);
}

impl<T: std::error::Error> AsDyn for T {
fn as_dyn(&self) -> &(dyn std::error::Error + '_) {
self
}
}

macro_rules! impl_as_dyn {
($({$ty:ty},)*) => {
$(
impl AsDyn for $ty {
fn as_dyn(&self) -> &(dyn std::error::Error + '_) {
self
}
}
)*
};
}

crate::for_dyn_error_types! { impl_as_dyn }
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#![feature(error_generic_member_access)] // TODO: it's nightly-only

mod as_dyn;
mod backtrace;
mod error_box;
mod report;

pub use as_dyn::AsDyn;
pub use report::{AsReport, Report};
pub use thiserror_ext_derive::{Box, Construct, ContextInto};

Expand Down

0 comments on commit 5853053

Please sign in to comment.