@@ -138,7 +138,72 @@ pub trait Error: Debug + Display {
138
138
/// }
139
139
/// ```
140
140
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
141
- fn cause ( & self ) -> Option < & dyn Error > { None }
141
+ #[ rustc_deprecated( since = "1.33.0" , reason = "replaced by Error::source, which can support \
142
+ downcasting") ]
143
+ fn cause ( & self ) -> Option < & dyn Error > {
144
+ self . source ( )
145
+ }
146
+
147
+ /// The lower-level source of this error, if any.
148
+ ///
149
+ /// # Examples
150
+ ///
151
+ /// ```
152
+ /// use std::error::Error;
153
+ /// use std::fmt;
154
+ ///
155
+ /// #[derive(Debug)]
156
+ /// struct SuperError {
157
+ /// side: SuperErrorSideKick,
158
+ /// }
159
+ ///
160
+ /// impl fmt::Display for SuperError {
161
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162
+ /// write!(f, "SuperError is here!")
163
+ /// }
164
+ /// }
165
+ ///
166
+ /// impl Error for SuperError {
167
+ /// fn description(&self) -> &str {
168
+ /// "I'm the superhero of errors"
169
+ /// }
170
+ ///
171
+ /// fn source(&self) -> Option<&dyn (Error + 'static)> {
172
+ /// Some(&self.side)
173
+ /// }
174
+ /// }
175
+ ///
176
+ /// #[derive(Debug)]
177
+ /// struct SuperErrorSideKick;
178
+ ///
179
+ /// impl fmt::Display for SuperErrorSideKick {
180
+ /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
181
+ /// write!(f, "SuperErrorSideKick is here!")
182
+ /// }
183
+ /// }
184
+ ///
185
+ /// impl Error for SuperErrorSideKick {
186
+ /// fn description(&self) -> &str {
187
+ /// "I'm SuperError side kick"
188
+ /// }
189
+ /// }
190
+ ///
191
+ /// fn get_super_error() -> Result<(), SuperError> {
192
+ /// Err(SuperError { side: SuperErrorSideKick })
193
+ /// }
194
+ ///
195
+ /// fn main() {
196
+ /// match get_super_error() {
197
+ /// Err(e) => {
198
+ /// println!("Error: {}", e.description());
199
+ /// println!("Caused by: {}", e.source().unwrap());
200
+ /// }
201
+ /// _ => println!("No error"),
202
+ /// }
203
+ /// }
204
+ /// ```
205
+ #[ stable( feature = "rust1" , since = "1.30.0" ) ]
206
+ fn source ( & self ) -> Option < & ( dyn Error + ' static ) > { None }
142
207
143
208
/// Get the `TypeId` of `self`
144
209
#[ doc( hidden) ]
0 commit comments