diff options
| author | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:36:55 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2021-12-30 10:36:55 -0800 |
| commit | 9d65bc51c1ea5fc482cc42d79732043201bcf54a (patch) | |
| tree | 5bc4551d420bd9962a87337b47f7d04d93edbeaf | |
| parent | 48a91a08d166d293fcc03fdc9f86fa30ecbcc51c (diff) | |
| download | rust-9d65bc51c1ea5fc482cc42d79732043201bcf54a.tar.gz rust-9d65bc51c1ea5fc482cc42d79732043201bcf54a.zip | |
Move Option::as_deref_mut
| -rw-r--r-- | library/core/src/option.rs | 54 |
1 files changed, 26 insertions, 28 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 8feebe11e97..c3bea55772f 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -1098,6 +1098,32 @@ impl<T> Option<T> { } } + /// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`. + /// + /// Leaves the original `Option` in-place, creating a new one containing a mutable reference to + /// the inner type's [`Deref::Target`] type. + /// + /// # Examples + /// + /// ``` + /// let mut x: Option<String> = Some("hey".to_owned()); + /// assert_eq!(x.as_deref_mut().map(|x| { + /// x.make_ascii_uppercase(); + /// x + /// }), Some("HEY".to_owned().as_mut_str())); + /// ``` + #[stable(feature = "option_deref", since = "1.40.0")] + #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")] + pub const fn as_deref_mut(&mut self) -> Option<&mut T::Target> + where + T: ~const DerefMut, + { + match self.as_mut() { + Some(t) => Some(t.deref_mut()), + None => None, + } + } + ///////////////////////////////////////////////////////////////////////// // Iterator constructors ///////////////////////////////////////////////////////////////////////// @@ -1750,34 +1776,6 @@ impl<T: Clone> Option<&mut T> { } } -impl<T: DerefMut> Option<T> { - /// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`. - /// - /// Leaves the original `Option` in-place, creating a new one containing a mutable reference to - /// the inner type's [`Deref::Target`] type. - /// - /// # Examples - /// - /// ``` - /// let mut x: Option<String> = Some("hey".to_owned()); - /// assert_eq!(x.as_deref_mut().map(|x| { - /// x.make_ascii_uppercase(); - /// x - /// }), Some("HEY".to_owned().as_mut_str())); - /// ``` - #[stable(feature = "option_deref", since = "1.40.0")] - #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")] - pub const fn as_deref_mut(&mut self) -> Option<&mut T::Target> - where - T: ~const DerefMut, - { - match self.as_mut() { - Some(t) => Some(t.deref_mut()), - None => None, - } - } -} - impl<T, E> Option<Result<T, E>> { /// Transposes an `Option` of a [`Result`] into a [`Result`] of an `Option`. /// |
