thiserror_ext/
lib.rs

1//! Useful extension utilities for [`thiserror`](https://docs.rs/thiserror).
2//!
3//! ## Painless construction
4//!
5//! With derive macros of [`Construct`], [`ContextInto`] and [`Macro`],
6//! one can construct an error in a much more convenient way, no matter it's
7//! from scratch or converted from other errors.
8//!
9//! ## Better formatting
10//!
11//! With extension [`AsReport`], one can format an error in a pretty and
12//! concise way, without losing any information from the error sources.
13//!
14//! ## Easier to interact with
15//!
16//! With derive macros of [`derive@Box`] and [`derive@Arc`], one can easily
17//! wrap an `enum` error type into a new type, reducing the size to improve
18//! performance, and automatically capturing backtraces if needed.
19
20#![cfg_attr(feature = "provide", feature(error_generic_member_access))]
21
22mod as_dyn;
23mod backtrace;
24mod ptr;
25mod report;
26
27pub use as_dyn::AsDyn;
28pub use report::{AsReport, Report};
29pub use thiserror_ext_derive::*;
30
31#[doc(hidden)]
32pub mod __private {
33    #[cfg(feature = "provide")]
34    pub use crate::backtrace::MaybeBacktrace;
35    pub use crate::backtrace::NoExtraBacktrace;
36    pub use crate::ptr::{ErrorArc, ErrorBox};
37}
38
39macro_rules! for_dyn_error_types {
40    ($macro:ident) => {
41        $macro! {
42            { dyn std::error::Error },
43            { dyn std::error::Error + Send },
44            { dyn std::error::Error + Sync },
45            { dyn std::error::Error + Send + Sync },
46            { dyn std::error::Error + Send + Sync + std::panic::UnwindSafe },
47        }
48    };
49}
50pub(crate) use for_dyn_error_types;
51
52pub(crate) mod error_sealed {
53    pub trait Sealed {}
54
55    impl<T: std::error::Error> Sealed for T {}
56
57    macro_rules! impl_sealed {
58        ($({$ty:ty },)*) => {
59            $(
60                impl Sealed for $ty {}
61            )*
62        };
63    }
64    for_dyn_error_types! { impl_sealed }
65}