Skip to content

Commit eda61d8

Browse files
committed
Move Result::as_deref
1 parent f8d4ee7 commit eda61d8

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

library/core/src/result.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,30 @@ impl<T, E> Result<T, E> {
901901
self
902902
}
903903

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+
904928
/////////////////////////////////////////////////////////////////////////
905929
// Iterator constructors
906930
/////////////////////////////////////////////////////////////////////////
@@ -1508,29 +1532,6 @@ impl<T: Into<!>, E> Result<T, E> {
15081532
}
15091533
}
15101534

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-
15341535
impl<T: DerefMut, E> Result<T, E> {
15351536
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
15361537
///

0 commit comments

Comments
 (0)