Debian Codegraph @

third_party/rust/derive_more-impl/doc/error.md

raw
1 # Using `#[derive(Error)]`
2
3 Deriving `Error` will generate an `Error` implementation, that contains
4 (depending on the type) a `source()` and a `provide()` method. Please note,
5 at the time of writing `provide()` is only supported on nightly rust. So you
6 have to use that to make use of it.
7
8 For a struct, these methods always do the same. For an `enum` they have separate
9 behaviour for each of the variants. The variant is first matched and then the
10 implementation will do the same as it would have done if the variant was a
11 struct.
12
13 Usually when you derive `Error` you will also want to [derive `Display`](crate::Display) and
14 often [`From` as well](crate::From).
15
16
17 ### When and how does it derive `source()`?
18
19 1. It's a struct/variant with named fields and one is the fields is
20 called `source`. Then it would return that field as the `source`.
21 2. It's a tuple struct/variant and there's exactly one field that is not used as
22 the `backtrace`. So either a tuple struct with one field, or one with two where one
23 is the `backtrace`. Then it returns this field as the `source`.
24 3. One of the fields is annotated with `#[error(source)]`. Then it would
25 return that field as the `source`.
26
27 ### When and how does it derive `provide()`?
28
29 1. It's a struct/variant with named fields and one of the fields is
30 called `backtrace`. Then it would return that field as the `backtrace`.
31 2. It's a tuple struct/variant and the type of exactly one of the fields is
32 called `Backtrace`. Then it would return that field as the `backtrace`.
33 3. One of the fields is annotated with `#[error(backtrace)]`. Then it would
34 return that field as the `backtrace`.
35
36 ### Ignoring fields for derives
37
38 It's possible to ignore a field or a whole enum variant completely for this
39 derive using the `#[error(ignore)]` attribute. This will ignore it both for
40 detecting `backtrace` and `source`. It's also possible to mark a field only
41 ignored for one of these methods by using `#[error(not(backtrace))]` or
42 `#[error(not(source))]`.
43
44
45 ### What works in `no_std`?
46
47 If you want to use the `Error` derive on `no_std` environments, then you need to
48 compile with nightly and enable this feature:
49 ```ignore
50 #![feature(error_in_core)]
51 ```
52
53 Backtraces don't work though, because the `Backtrace` type is only available in
54 `std`.
55
56
57
58
59 ## Example usage
60
61 ```rust
62 # #![cfg_attr(nightly, feature(error_generic_member_access, provide_any))]
63 // Nightly requires enabling these features:
64 // #![feature(error_generic_member_access, provide_any)]
65 # #[cfg(not(nightly))] fn main() {}
66 # #[cfg(nightly)] fn main() {
67 # use std::{any, error::Error as _, backtrace::Backtrace};
68 #
69 # use derive_more::{Display, Error, From};
70
71 // std::error::Error requires std::fmt::Debug and std::fmt::Display,
72 // so we can also use derive_more::Display for fully declarative
73 // error-type definitions.
74
75 #[derive(Default, Debug, Display, Error)]
76 struct Simple;
77
78 #[derive(Default, Debug, Display, Error)]
79 struct WithSource {
80 source: Simple,
81 }
82 #[derive(Default, Debug, Display, Error)]
83 struct WithExplicitSource {
84 #[error(source)]
85 explicit_source: Simple,
86 }
87
88 #[derive(Default, Debug, Display, Error)]
89 struct Tuple(Simple);
90
91 #[derive(Default, Debug, Display, Error)]
92 struct WithoutSource(#[error(not(source))] i32);
93
94 #[derive(Debug, Display, Error)]
95 #[display("An error with a backtrace")]
96 struct WithSourceAndBacktrace {
97 source: Simple,
98 backtrace: Backtrace,
99 }
100
101 // derive_more::From fits nicely into this pattern as well
102 #[derive(Debug, Display, Error, From)]
103 enum CompoundError {
104 Simple,
105 WithSource {
106 source: Simple,
107 },
108 #[from(ignore)]
109 WithBacktraceFromSource {
110 #[error(backtrace)]
111 source: Simple,
112 },
113 #[display("{source}")]
114 WithDifferentBacktrace {
115 source: Simple,
116 backtrace: Backtrace,
117 },
118 WithExplicitSource {
119 #[error(source)]
120 explicit_source: WithSource,
121 },
122 #[from(ignore)]
123 WithBacktraceFromExplicitSource {
124 #[error(backtrace, source)]
125 explicit_source: WithSource,
126 },
127 Tuple(WithExplicitSource),
128 WithoutSource(#[error(not(source))] Tuple),
129 }
130
131 assert!(Simple.source().is_none());
132 assert!(any::request_ref::<Backtrace>(&Simple).is_none());
133 assert!(WithSource::default().source().is_some());
134 assert!(WithExplicitSource::default().source().is_some());
135 assert!(Tuple::default().source().is_some());
136 assert!(WithoutSource::default().source().is_none());
137 let with_source_and_backtrace = WithSourceAndBacktrace {
138 source: Simple,
139 backtrace: Backtrace::capture(),
140 };
141 assert!(with_source_and_backtrace.source().is_some());
142 assert!(any::request_ref::<Backtrace>(&with_source_and_backtrace).is_some());
143
144 assert!(CompoundError::Simple.source().is_none());
145 assert!(CompoundError::from(Simple).source().is_some());
146 assert!(CompoundError::from(WithSource::default()).source().is_some());
147 assert!(CompoundError::from(WithExplicitSource::default()).source().is_some());
148 assert!(CompoundError::from(Tuple::default()).source().is_none());
149 # }
150 ```