thiserror_ext/
lib.rs

1//! Useful extension utilities for [`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 = "backtrace", 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 = "backtrace")]
34    pub use crate::backtrace::MaybeBacktrace;
35    pub use crate::backtrace::NoExtraBacktrace;
36    pub use crate::ptr::{ErrorArc, ErrorBox};
37    pub use thiserror;
38}
39
40macro_rules! for_dyn_error_types {
41    ($macro:ident) => {
42        $macro! {
43            { dyn std::error::Error },
44            { dyn std::error::Error + Send },
45            { dyn std::error::Error + Sync },
46            { dyn std::error::Error + Send + Sync },
47            { dyn std::error::Error + Send + Sync + std::panic::UnwindSafe },
48        }
49    };
50}
51pub(crate) use for_dyn_error_types;
52
53pub(crate) mod error_sealed {
54    pub trait Sealed {}
55
56    impl<T: std::error::Error> Sealed for T {}
57
58    macro_rules! impl_sealed {
59        ($({$ty:ty },)*) => {
60            $(
61                impl Sealed for $ty {}
62            )*
63        };
64    }
65    for_dyn_error_types! { impl_sealed }
66}