Debian Codegraph @

src/error.rs

raw symbols SCIP
1 /// Implements [`Error`] for structs and forwards the `source` implementation to one of its fields.
2 ///
3 /// Emitted code is not compatible with `#[no_std]`.
4 ///
5 /// Newtype structs can omit the field identifier.
6 ///
7 /// # Examples
8 ///
9 /// For newtype struct:
10 ///
11 /// ```ignore
12 /// use std::error::Error as _;
13 ///
14 /// #[derive(Debug)]
15 /// struct MyError(eyre::Report);
16 ///
17 /// impl_more::forward_display!(MyError);
18 /// impl_more::forward_error!(MyError);
19 ///
20 /// let err = MyError(eyre::eyre!("something went wrong"));
21 /// assert_eq!(err.source().unwrap().to_string(), "something went wrong");
22 /// ```
23 ///
24 /// For struct with named field:
25 ///
26 /// ```ignore
27 /// use std::error::Error as _;
28 ///
29 /// #[derive(Debug)]
30 /// struct MyError {
31 /// cause: eyre::Report,
32 /// }
33 ///
34 /// impl_more::forward_display!(MyError => cause);
35 /// impl_more::forward_error!(MyError => cause);
36 ///
37 /// let err = MyError { cause: eyre::eyre!("something went wrong") };
38 /// assert_eq!(err.source().unwrap().to_string(), "something went wrong");
39 /// ```
40 ///
41 /// This macro does not yet support use with generic error wrappers.
42 ///
43 /// [`Error`]: core::error::Error
44 #[macro_export]
45 macro_rules! forward_errorrust-analyzer cargo impl-more 0.3.1 error/forward_error! {
46 ($ty:ty) => {
47 impl ::core::error::Error for $ty {
48 fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
49 Some(::core::ops::Deref::deref(&self.0))
50 }
51 }
52 };
53
54 ($ty:ty => $field:ident) => {
55 impl ::core::error::Error for $ty {
56 fn source(&self) -> Option<&(dyn ::core::error::Error + 'static)> {
57 Some(::core::ops::Deref::deref(&self.$field))
58 }
59 }
60 };
61 }
62
63 /// Implements [`Error`] for enums.
64 ///
65 /// Emitted code is compatible with `#[no_std]` after Rust v1.81.
66 ///
67 /// # Examples
68 ///
69 /// ```ignore
70 /// # extern crate alloc;
71 /// use core::error::Error as _;
72 ///
73 /// #[derive(Debug)]
74 /// enum Err {
75 /// Io(std::io::Error),
76 /// Generic(String),
77 /// }
78 ///
79 /// impl_more::impl_display_enum!(Err, Io(err) => "{err}", Generic(msg) => "{msg}");
80 /// impl_more::impl_error_enum!(Err, Io(err) => err);
81 ///
82 /// # let io_err = std::io::Error::new(std::io::ErrorKind::Other, "test");
83 /// assert!(Err::Io(io_err).source().is_some());
84 /// assert!(Err::Generic("oops".to_owned()).source().is_none());
85 /// ```
86 ///
87 /// [`Error`]: core::error::Error
88 #[macro_export]
89 macro_rules! impl_error_enumrust-analyzer cargo impl-more 0.3.1 error/impl_error_enum! {
90 ($ty:ty: $($variant:ident ($($inner:ident),+) => $source:expr),+ ,) => {
91 impl ::core::error::Error for $ty {
92 fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
93 match self {
94 $(
95 Self::$variant($($inner),+) => ::core::option::Option::Some($source),
96 )*
97 _ => ::core::option::Option::None,
98 }
99 }
100 }
101 };
102
103 ($ty:ty: $($variant:ident ($($inner:ident),+) => $source:expr),+) => {
104 $crate::impl_error_enum!($ty: $($variant ($($inner),+) => $source),+ ,);
105 };
106
107 ($ty:ty: $($variant:ident { $($inner:ident),+ } => $source:expr),+ ,) => {
108 impl ::core::error::Error for $ty {
109 fn source(&self) -> ::core::option::Option<&(dyn ::core::error::Error + 'static)> {
110 match self {
111 $(
112 Self::$variant($($inner),+) => ::core::option::Option::Some($source),
113 )*
114 _ => ::core::option::Option::None,
115 }
116 }
117 }
118 };
119
120 ($ty:ty: $($variant:ident { $($inner:ident),+ } => $source:expr),+) => {
121 $crate::impl_error_enum!($ty, $($variant { $($inner),+ } => $source),+ ,);
122 };
123
124 ($ty:ty) => {
125 impl ::core::error::Error for $ty {}
126 };
127 }
128
129 /// Implements leaf [`Error`]s.
130 ///
131 /// Emitted code is compatible with `#[no_std]` after Rust v1.81.
132 ///
133 /// # Examples
134 ///
135 /// ```ignore
136 /// #[derive(Debug)]
137 /// struct LeafError;
138 ///
139 /// impl_more::impl_display!(LeafError; "leaf");
140 /// impl_more::impl_error_enum!(LeafError);
141 /// ```
142 ///
143 /// [`Error`]: core::error::Error
144 #[macro_export]
145 macro_rules! impl_leaf_errorrust-analyzer cargo impl-more 0.3.1 error/impl_leaf_error! {
146 ($ty:ty) => {
147 impl ::core::error::Error for $ty {}
148 };
149 }
150
151 #[cfg(test)]
152 #[rustversionrust-analyzer cargo rustversion 1.0.22 crate/::sincerust-analyzer cargo rustversion 1.0.22 since:(1.81)]
153 mod testsrust-analyzer cargo impl-more 0.3.1 error/tests/ {
154 use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/::Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#;
155 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# as _;
156
157 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
158 fn with_trailing_commarust-analyzer cargo impl-more 0.3.1 error/tests/with_trailing_comma().() {
159 #![allow(unused)]
160
161 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
162 enum Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo# {
163 Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#,
164 }
165
166 impl_display_enumrust-analyzer cargo impl-more 0.3.1 display/impl_display_enum!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#: Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar# => "bar");
167 impl_leaf_errorrust-analyzer cargo impl-more 0.3.1 error/impl_leaf_error!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#);
168 }
169
170 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
171 fn no_inner_datarust-analyzer cargo impl-more 0.3.1 error/tests/no_inner_data().() {
172 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
173 enum Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo# {
174 Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#,
175 Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#,
176 }
177
178 impl_display_enumrust-analyzer cargo impl-more 0.3.1 display/impl_display_enum!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#: Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar# => "bar", Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz# => "qux");
179 impl_error_enumrust-analyzer cargo impl-more 0.3.1 error/impl_error_enum!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#);
180
181 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#::Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#.sourcerust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#source().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
182 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#::Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#.sourcerust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#source().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
183 }
184
185 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
186 fn uniform_enumrust-analyzer cargo impl-more 0.3.1 error/tests/uniform_enum().() {
187 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
188 enum Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo# {
189 Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#),
190 Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#),
191 Quxrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Qux#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#),
192 }
193
194 impl_display_enumrust-analyzer cargo impl-more 0.3.1 display/impl_display_enum!! {
195 Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#:
196 Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#(desc) => "{desc}",
197 Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#(err) => "{err}",
198 Quxrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Qux#(desc, err) => "{desc}: {err}"
199 };
200 impl_error_enumrust-analyzer cargo impl-more 0.3.1 error/impl_error_enum!! {
201 Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#:
202 Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#(err) => err,
203 Quxrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Qux#(_desc, err) => err
204 };
205
206 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#::Barrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Bar#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().()).sourcerust-analyzer cargo impl-more 0.3.1 error/tests/impl#[Foo][Error]source().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
207
208 let io_err = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, "test");
209 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#::Bazrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Baz#(io_err).sourcerust-analyzer cargo impl-more 0.3.1 error/tests/impl#[Foo][Error]source().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
210
211 let io_err = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, "test");
212 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(Foorust-analyzer cargo impl-more 0.3.1 error/tests/Foo#::Quxrust-analyzer cargo impl-more 0.3.1 error/tests/Foo#Qux#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(), io_err).sourcerust-analyzer cargo impl-more 0.3.1 error/tests/impl#[Foo][Error]source().().is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().());
213 }
214 }