@@ -901,6 +901,30 @@ impl<T, E> Result<T, E> {
901
901
self
902
902
}
903
903
904
+ /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&<T as Deref>::Target, &E>`.
905
+ ///
906
+ /// Coerces the [`Ok`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
907
+ /// and returns the new [`Result`].
908
+ ///
909
+ /// # Examples
910
+ ///
911
+ /// ```
912
+ /// let x: Result<String, u32> = Ok("hello".to_string());
913
+ /// let y: Result<&str, &u32> = Ok("hello");
914
+ /// assert_eq!(x.as_deref(), y);
915
+ ///
916
+ /// let x: Result<String, u32> = Err(42);
917
+ /// let y: Result<&str, &u32> = Err(&42);
918
+ /// assert_eq!(x.as_deref(), y);
919
+ /// ```
920
+ #[ stable( feature = "inner_deref" , since = "1.47.0" ) ]
921
+ pub fn as_deref ( & self ) -> Result < & T :: Target , & E >
922
+ where
923
+ T : Deref ,
924
+ {
925
+ self . as_ref ( ) . map ( |t| t. deref ( ) )
926
+ }
927
+
904
928
/////////////////////////////////////////////////////////////////////////
905
929
// Iterator constructors
906
930
/////////////////////////////////////////////////////////////////////////
@@ -1508,29 +1532,6 @@ impl<T: Into<!>, E> Result<T, E> {
1508
1532
}
1509
1533
}
1510
1534
1511
- impl < T : Deref , E > Result < T , E > {
1512
- /// Converts from `Result<T, E>` (or `&Result<T, E>`) to `Result<&<T as Deref>::Target, &E>`.
1513
- ///
1514
- /// Coerces the [`Ok`] variant of the original [`Result`] via [`Deref`](crate::ops::Deref)
1515
- /// and returns the new [`Result`].
1516
- ///
1517
- /// # Examples
1518
- ///
1519
- /// ```
1520
- /// let x: Result<String, u32> = Ok("hello".to_string());
1521
- /// let y: Result<&str, &u32> = Ok("hello");
1522
- /// assert_eq!(x.as_deref(), y);
1523
- ///
1524
- /// let x: Result<String, u32> = Err(42);
1525
- /// let y: Result<&str, &u32> = Err(&42);
1526
- /// assert_eq!(x.as_deref(), y);
1527
- /// ```
1528
- #[ stable( feature = "inner_deref" , since = "1.47.0" ) ]
1529
- pub fn as_deref ( & self ) -> Result < & T :: Target , & E > {
1530
- self . as_ref ( ) . map ( |t| t. deref ( ) )
1531
- }
1532
- }
1533
-
1534
1535
impl < T : DerefMut , E > Result < T , E > {
1535
1536
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
1536
1537
///
0 commit comments