3 ms·
https://crates.io/crates/error-chain https://crates.io/crates/error-chain or https://crates.io/crates/quick-error https://crates.io/crates/quick-error (which er
by Arnavion 10y ago
https://crates.io/crates/error-chain https://crates.io/crates/error-chain or https://crates.io/crates/quick-error https://crates.io/crates/quick-error (which error-chain uses and adds to) provide macros to help with defining error types.
Edit: Specifically with error-chain, you get:
- Concise declaration of the enum for each wrapped error type (your first code block).
- Automatic impl of From for each wrapped error type (your first code block).
- A custom Result type (your first code block).
- Concise way to define description() functions for each enum variant, which get used for the Display impl (your second block). For "foreign_links" it would forward to the wrapped error's fmt automatically, so you only need to do this for the others.
- Bonuses like backtraces conditional on RUST_BACKTRACE=1
- Animats 10y agoHow well does that play with packages that don't use it? There seem to be several such packages.
- dbaupp 10y agoIt is a macro that just helps you define a type that implements the appropriate traits so of course it works with the rest of the world. No-one outside the crate that uses it even needs to notice, as one could just write out the definition by hand.
- Arnavion 10y agoThis is correct. Technically the backtrace functionality and ErrorChainIter implementation come from the error-chain crate (the former as a re-export of the backtrace crate) so your linker will notice.