@@ -1072,6 +1072,32 @@ impl<T> Option<T> {
1072
1072
}
1073
1073
}
1074
1074
1075
+ /// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
1076
+ ///
1077
+ /// Leaves the original Option in-place, creating a new one with a reference
1078
+ /// to the original one, additionally coercing the contents via [`Deref`].
1079
+ ///
1080
+ /// # Examples
1081
+ ///
1082
+ /// ```
1083
+ /// let x: Option<String> = Some("hey".to_owned());
1084
+ /// assert_eq!(x.as_deref(), Some("hey"));
1085
+ ///
1086
+ /// let x: Option<String> = None;
1087
+ /// assert_eq!(x.as_deref(), None);
1088
+ /// ```
1089
+ #[ stable( feature = "option_deref" , since = "1.40.0" ) ]
1090
+ #[ rustc_const_unstable( feature = "const_option_ext" , issue = "91930" ) ]
1091
+ pub const fn as_deref ( & self ) -> Option < & T :: Target >
1092
+ where
1093
+ T : ~const Deref ,
1094
+ {
1095
+ match self . as_ref ( ) {
1096
+ Some ( t) => Some ( t. deref ( ) ) ,
1097
+ None => None ,
1098
+ }
1099
+ }
1100
+
1075
1101
/////////////////////////////////////////////////////////////////////////
1076
1102
// Iterator constructors
1077
1103
/////////////////////////////////////////////////////////////////////////
@@ -1724,34 +1750,6 @@ impl<T: Clone> Option<&mut T> {
1724
1750
}
1725
1751
}
1726
1752
1727
- impl < T : Deref > Option < T > {
1728
- /// Converts from `Option<T>` (or `&Option<T>`) to `Option<&T::Target>`.
1729
- ///
1730
- /// Leaves the original Option in-place, creating a new one with a reference
1731
- /// to the original one, additionally coercing the contents via [`Deref`].
1732
- ///
1733
- /// # Examples
1734
- ///
1735
- /// ```
1736
- /// let x: Option<String> = Some("hey".to_owned());
1737
- /// assert_eq!(x.as_deref(), Some("hey"));
1738
- ///
1739
- /// let x: Option<String> = None;
1740
- /// assert_eq!(x.as_deref(), None);
1741
- /// ```
1742
- #[ stable( feature = "option_deref" , since = "1.40.0" ) ]
1743
- #[ rustc_const_unstable( feature = "const_option_ext" , issue = "91930" ) ]
1744
- pub const fn as_deref ( & self ) -> Option < & T :: Target >
1745
- where
1746
- T : ~const Deref ,
1747
- {
1748
- match self . as_ref ( ) {
1749
- Some ( t) => Some ( t. deref ( ) ) ,
1750
- None => None ,
1751
- }
1752
- }
1753
- }
1754
-
1755
1753
impl < T : DerefMut > Option < T > {
1756
1754
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
1757
1755
///
0 commit comments