Skip to content

Commit 9d65bc5

Browse files
committed
Move Option::as_deref_mut
1 parent 48a91a0 commit 9d65bc5

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

library/core/src/option.rs

+26-28
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,32 @@ impl<T> Option<T> {
10981098
}
10991099
}
11001100

1101+
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
1102+
///
1103+
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
1104+
/// the inner type's [`Deref::Target`] type.
1105+
///
1106+
/// # Examples
1107+
///
1108+
/// ```
1109+
/// let mut x: Option<String> = Some("hey".to_owned());
1110+
/// assert_eq!(x.as_deref_mut().map(|x| {
1111+
/// x.make_ascii_uppercase();
1112+
/// x
1113+
/// }), Some("HEY".to_owned().as_mut_str()));
1114+
/// ```
1115+
#[stable(feature = "option_deref", since = "1.40.0")]
1116+
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1117+
pub const fn as_deref_mut(&mut self) -> Option<&mut T::Target>
1118+
where
1119+
T: ~const DerefMut,
1120+
{
1121+
match self.as_mut() {
1122+
Some(t) => Some(t.deref_mut()),
1123+
None => None,
1124+
}
1125+
}
1126+
11011127
/////////////////////////////////////////////////////////////////////////
11021128
// Iterator constructors
11031129
/////////////////////////////////////////////////////////////////////////
@@ -1750,34 +1776,6 @@ impl<T: Clone> Option<&mut T> {
17501776
}
17511777
}
17521778

1753-
impl<T: DerefMut> Option<T> {
1754-
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
1755-
///
1756-
/// Leaves the original `Option` in-place, creating a new one containing a mutable reference to
1757-
/// the inner type's [`Deref::Target`] type.
1758-
///
1759-
/// # Examples
1760-
///
1761-
/// ```
1762-
/// let mut x: Option<String> = Some("hey".to_owned());
1763-
/// assert_eq!(x.as_deref_mut().map(|x| {
1764-
/// x.make_ascii_uppercase();
1765-
/// x
1766-
/// }), Some("HEY".to_owned().as_mut_str()));
1767-
/// ```
1768-
#[stable(feature = "option_deref", since = "1.40.0")]
1769-
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1770-
pub const fn as_deref_mut(&mut self) -> Option<&mut T::Target>
1771-
where
1772-
T: ~const DerefMut,
1773-
{
1774-
match self.as_mut() {
1775-
Some(t) => Some(t.deref_mut()),
1776-
None => None,
1777-
}
1778-
}
1779-
}
1780-
17811779
impl<T, E> Option<Result<T, E>> {
17821780
/// Transposes an `Option` of a [`Result`] into a [`Result`] of an `Option`.
17831781
///

0 commit comments

Comments
 (0)