@@ -234,11 +234,18 @@ impl Display for Error {
234
234
///
235
235
/// This trait cannot be implemented on custom types and is meant for usage with `Result`
236
236
pub trait ResultExt < T > : private:: Sealed {
237
+ /// Creates a new error with the specified kind
238
+ fn map_kind ( self , kind : ErrorKind ) -> Result < T >
239
+ where
240
+ Self : Sized ;
241
+
242
+ /// Creates a new error with the specified kind and message
237
243
fn context < C > ( self , kind : ErrorKind , message : C ) -> Result < T >
238
244
where
239
245
Self : Sized ,
240
246
C : Into < Cow < ' static , str > > ;
241
247
248
+ /// Creates a new error with the specified kind and formatted message
242
249
fn with_context < F , C > ( self , kind : ErrorKind , f : F ) -> Result < T >
243
250
where
244
251
Self : Sized ,
@@ -256,6 +263,13 @@ impl<T, E> ResultExt<T> for std::result::Result<T, E>
256
263
where
257
264
E : std:: error:: Error + Send + Sync + ' static ,
258
265
{
266
+ fn map_kind ( self , kind : ErrorKind ) -> Result < T >
267
+ where
268
+ Self : Sized ,
269
+ {
270
+ self . map_err ( |e| Error :: new ( kind, e) )
271
+ }
272
+
259
273
fn context < C > ( self , kind : ErrorKind , message : C ) -> Result < T >
260
274
where
261
275
Self : Sized ,
@@ -400,4 +414,11 @@ mod tests {
400
414
if error_code. as_deref( ) == Some ( "teepot" )
401
415
) ) ;
402
416
}
417
+
418
+ #[ test]
419
+ fn set_result_kind ( ) {
420
+ let result = std:: result:: Result :: < ( ) , _ > :: Err ( create_error ( ) ) ;
421
+ let result = result. map_kind ( ErrorKind :: Io ) ;
422
+ assert_eq ! ( & ErrorKind :: Io , result. unwrap_err( ) . kind( ) ) ;
423
+ }
403
424
}
0 commit comments