Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1b7f5a3
Remove NullOp::Box
nbdd0121 Sep 26, 2021
037e189
Remove `box_alloc` from `Machine` trait.
nbdd0121 Oct 20, 2021
44fdb98
Use field span in `rustc_macros` when emitting decode call
Aaron1011 Dec 16, 2021
874cd08
Suggest while let x = y when encountering while x = y
Dec 29, 2021
75d8339
Replace TDynBenchFn with Fn(&mut Bencher)
bjorn3 Dec 29, 2021
1ea4810
Remove #![crate_name] attribute from libtest
bjorn3 Dec 29, 2021
f710bc5
Remove unused feature gates from libtest
bjorn3 Dec 29, 2021
e015b9e
Remove unused allow deprecated
bjorn3 Dec 29, 2021
8d7cf1a
Fix spacing in pretty printed PatKind::Struct with no fields
dtolnay Dec 29, 2021
eda61d8
Move Result::as_deref
dtolnay Dec 30, 2021
5aa8f91
Move Result::as_deref_mut
dtolnay Dec 30, 2021
15f57a6
Move Result::expect and Result::unwrap
dtolnay Dec 30, 2021
aa2aca2
Move Result::unwrap_or_default
dtolnay Dec 30, 2021
06ea5eb
Move Result::expect_err and Result::unwrap_err
dtolnay Dec 30, 2021
778ca20
Move Result::into_ok
dtolnay Dec 30, 2021
b2df61f
Move Result::into_err
dtolnay Dec 30, 2021
e63e268
Consolidate impl Result<&T, E>
dtolnay Dec 30, 2021
b7a0ab1
Consolidate impl Result<&mut T, E>
dtolnay Dec 30, 2021
bbcf09f
Move Option::unwrap_or_default
dtolnay Dec 30, 2021
48a91a0
Move Option::as_deref
dtolnay Dec 30, 2021
9d65bc5
Move Option::as_deref_mut
dtolnay Dec 30, 2021
538fe4b
Consolidate impl Option<&T>
dtolnay Dec 30, 2021
dc32916
Consolidate impl Option<&mut T>
dtolnay Dec 30, 2021
5960f7a
UI test updates for Result and Option method moves
dtolnay Dec 30, 2021
57a4f4a
Rollup merge of #90102 - nbdd0121:box3, r=jonas-schievink
matthiaskrgr Jan 3, 2022
fd09f34
Rollup merge of #92011 - Aaron1011:decode-span, r=michaelwoerister
matthiaskrgr Jan 3, 2022
0335b7b
Rollup merge of #92402 - pr2502:while-let-typo, r=oli-obk
matthiaskrgr Jan 3, 2022
92f28bd
Rollup merge of #92409 - bjorn3:libtest_cleanups, r=m-ou-se
matthiaskrgr Jan 3, 2022
df92119
Rollup merge of #92418 - dtolnay:emptystructpat, r=michaelwoerister
matthiaskrgr Jan 3, 2022
13e2840
Rollup merge of #92444 - dtolnay:coremethods, r=joshtriplett
matthiaskrgr Jan 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move Result::as_deref_mut
  • Loading branch information
dtolnay committed Dec 30, 2021
commit 5aa8f91ff0d376eb25ea52a10d7c4cdadf4407ac
51 changes: 26 additions & 25 deletions library/core/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,6 +925,32 @@ impl<T, E> Result<T, E> {
self.as_ref().map(|t| t.deref())
}

/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
///
/// Coerces the [`Ok`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
/// and returns the new [`Result`].
///
/// # Examples
///
/// ```
/// let mut s = "HELLO".to_string();
/// let mut x: Result<String, u32> = Ok("hello".to_string());
/// let y: Result<&mut str, &mut u32> = Ok(&mut s);
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
///
/// let mut i = 42;
/// let mut x: Result<String, u32> = Err(42);
/// let y: Result<&mut str, &mut u32> = Err(&mut i);
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
/// ```
#[stable(feature = "inner_deref", since = "1.47.0")]
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E>
where
T: DerefMut,
{
self.as_mut().map(|t| t.deref_mut())
}

/////////////////////////////////////////////////////////////////////////
// Iterator constructors
/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1532,31 +1558,6 @@ impl<T: Into<!>, E> Result<T, E> {
}
}

impl<T: DerefMut, E> Result<T, E> {
/// Converts from `Result<T, E>` (or `&mut Result<T, E>`) to `Result<&mut <T as DerefMut>::Target, &mut E>`.
///
/// Coerces the [`Ok`] variant of the original [`Result`] via [`DerefMut`](crate::ops::DerefMut)
/// and returns the new [`Result`].
///
/// # Examples
///
/// ```
/// let mut s = "HELLO".to_string();
/// let mut x: Result<String, u32> = Ok("hello".to_string());
/// let y: Result<&mut str, &mut u32> = Ok(&mut s);
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
///
/// let mut i = 42;
/// let mut x: Result<String, u32> = Err(42);
/// let y: Result<&mut str, &mut u32> = Err(&mut i);
/// assert_eq!(x.as_deref_mut().map(|x| { x.make_ascii_uppercase(); x }), y);
/// ```
#[stable(feature = "inner_deref", since = "1.47.0")]
pub fn as_deref_mut(&mut self) -> Result<&mut T::Target, &mut E> {
self.as_mut().map(|t| t.deref_mut())
}
}

impl<T, E> Result<Option<T>, E> {
/// Transposes a `Result` of an `Option` into an `Option` of a `Result`.
///
Expand Down