Skip to main content

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 = "nightly", feature(error_generic_member_access))]
21#![cfg_attr(not(feature = "std"), no_std)]
22
23// Re-export the alloc crate for use within derived code.
24#[doc(hidden)]
25pub extern crate alloc;
26
27mod as_dyn;
28mod backtrace;
29mod ptr;
30mod report;
31
32pub use as_dyn::AsDyn;
33pub use report::{AsReport, Report};
34pub use thiserror_ext_derive::*;
35
36#[doc(hidden)]
37pub mod __private {
38    #[cfg(feature = "std")]
39    pub use crate::backtrace::AlwaysBacktrace;
40    #[cfg(feature = "nightly")]
41    pub use crate::backtrace::MaybeBacktrace;
42    pub use crate::backtrace::NoExtraBacktrace;
43    pub use crate::ptr::ErrorArc;
44    pub use crate::ptr::ErrorBox;
45}
46
47macro_rules! for_dyn_error_types {
48    ($macro:ident) => {
49        $macro! {
50            { dyn core::error::Error },
51            { dyn core::error::Error + core::marker::Send },
52            { dyn core::error::Error + core::marker::Sync },
53            { dyn core::error::Error + core::marker::Send + core::marker::Sync },
54            { dyn core::error::Error + core::marker::Send + core::marker::Sync + core::panic::UnwindSafe },
55        }
56    };
57}
58pub(crate) use for_dyn_error_types;
59
60pub(crate) mod error_sealed {
61    pub trait Sealed {}
62
63    impl<T: core::error::Error> Sealed for T {}
64
65    macro_rules! impl_sealed {
66        ($({$ty:ty },)*) => {
67            $(
68                impl Sealed for $ty {}
69            )*
70        };
71    }
72    for_dyn_error_types! { impl_sealed }
73}