Skip to content

Commit 5aa8f91

Browse files
committed
Move Result::as_deref_mut
1 parent eda61d8 commit 5aa8f91

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

library/core/src/result.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,32 @@ impl<T, E> Result<T, E> {
925925
self.as_ref().map(|t| t.deref())
926926
}
927927

928+
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
929+
///
930+
/// Coerces the [`Ok`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
931+
/// and returns the new [`Result`].
932+
///
933+
/// # Examples
934+
///
935+
/// ```
936+
/// let mut s = "HELLO".to_string();
937+
/// let mut x: Result<String, u32> = Ok("hello".to_string());
938+
/// let y: Result<&mut str, &mut u32> = Ok(&mut s);
939+
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
940+
///
941+
/// let mut i = 42;
942+
/// let mut x: Result<String, u32> = Err(42);
943+
/// let y: Result<&mut str, &mut u32> = Err(&mut i);
944+
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
945+
/// ```
946+
#[stable(feature = "inner_deref", since = "1.47.0")]
947+
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E>
948+
where
949+
T: DerefMut,
950+
{
951+
self.as_mut().map(|t| t.deref_mut())
952+
}
953+
928954
/////////////////////////////////////////////////////////////////////////
929955
// Iterator constructors
930956
/////////////////////////////////////////////////////////////////////////
@@ -1532,31 +1558,6 @@ impl<T: Into<!>, E> Result<T, E> {
15321558
}
15331559
}
15341560

1535-
impl<T: DerefMut, E> Result<T, E> {
1536-
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
1537-
///
1538-
/// Coerces the [`Ok`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
1539-
/// and returns the new [`Result`].
1540-
///
1541-
/// # Examples
1542-
///
1543-
/// ```
1544-
/// let mut s = "HELLO".to_string();
1545-
/// let mut x: Result<String, u32> = Ok("hello".to_string());
1546-
/// let y: Result<&mut str, &mut u32> = Ok(&mut s);
1547-
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
1548-
///
1549-
/// let mut i = 42;
1550-
/// let mut x: Result<String, u32> = Err(42);
1551-
/// let y: Result<&mut str, &mut u32> = Err(&mut i);
1552-
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
1553-
/// ```
1554-
#[stable(feature = "inner_deref", since = "1.47.0")]
1555-
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
1556-
self.as_mut().map(|t| t.deref_mut())
1557-
}
1558-
}
1559-
15601561
impl<T, E> Result<Option<T>, E> {
15611562
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
15621563
///

0 commit comments

Comments
 (0)