kutil_std/error/captured.rs
1use std::error::Error;
2
3//
4// CapturedError
5//
6
7/// A thread-safe `dyn` error.
8///
9/// Returning it as an error allows a function to gloss over actual error types. While not usually
10/// desirable, it's a simple or even necessary solution for generic code in which internal error
11/// types are not and cannot be known at compile time.
12///
13/// For generic returned errors it can be useful to specify [Into]\<[CapturedError]\> as a bound.
14/// (Rust doesn't support trait aliases, so you have to spell it out.) This allows for implicit
15/// error conversion, e.g. using the `?` operator.
16///
17/// Boxing errors is mentioned in
18/// [Rust by Example](https://doc.rust-lang.org/rust-by-example/error/multiple_error_types/boxing_errors.html),
19/// though the example there is not thread-safe.
20///
21/// Other implementations add thread-safety, e.g.
22/// [BoxError](https://docs.rs/tower-http/latest/tower_http/type.BoxError.html)
23/// in Tower HTTP, which is the inspiration for this.
24///
25/// A far more comprehensive solution is provided by [anyhow](https://docs.rs/anyhow/latest/anyhow/).
26pub type CapturedError = Box<dyn Error + Send + Sync>;
27
28//
29// BoxedError
30//
31
32/// A non-thread-safe (less constrained) version of [CapturedError].
33pub type BoxedError = Box<dyn Error>;