Skip to content

Commit 48a91a0

Browse files
committed
Move Option::as_deref
1 parent bbcf09f commit 48a91a0

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
@@ -1072,6 +1072,32 @@ impl<T> Option<T> {
10721072
}
10731073
}
10741074

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+
10751101
/////////////////////////////////////////////////////////////////////////
10761102
// Iterator constructors
10771103
/////////////////////////////////////////////////////////////////////////
@@ -1724,34 +1750,6 @@ impl<T: Clone> Option<&mut T> {
17241750
}
17251751
}
17261752

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-
17551753
impl<T: DerefMut> Option<T> {
17561754
/// Converts from `Option<T>` (or `&mut Option<T>`) to `Option<&mut T::Target>`.
17571755
///

0 commit comments

Comments
 (0)