Skip to content

Commit 538fe4b

Browse files
committed
Consolidate impl Option<&T>
1 parent 9d65bc5 commit 538fe4b

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

library/core/src/option.rs

+29-28
Original file line numberDiff line numberDiff line change
@@ -1672,7 +1672,7 @@ impl<T, U> Option<(T, U)> {
16721672
}
16731673
}
16741674

1675-
impl<T: Copy> Option<&T> {
1675+
impl<T> Option<&T> {
16761676
/// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
16771677
/// option.
16781678
///
@@ -1688,41 +1688,18 @@ impl<T: Copy> Option<&T> {
16881688
#[must_use = "`self` will be dropped if the result is not used"]
16891689
#[stable(feature = "copied", since = "1.35.0")]
16901690
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
1691-
pub const fn copied(self) -> Option<T> {
1691+
pub const fn copied(self) -> Option<T>
1692+
where
1693+
T: Copy,
1694+
{
16921695
// FIXME: this implementation, which sidesteps using `Option::map` since it's not const
16931696
// ready yet, should be reverted when possible to avoid code repetition
16941697
match self {
16951698
Some(&v) => Some(v),
16961699
None => None,
16971700
}
16981701
}
1699-
}
1700-
1701-
impl<T: Copy> Option<&mut T> {
1702-
/// Maps an `Option<&mut T>` to an `Option<T>` by copying the contents of the
1703-
/// option.
1704-
///
1705-
/// # Examples
1706-
///
1707-
/// ```
1708-
/// let mut x = 12;
1709-
/// let opt_x = Some(&mut x);
1710-
/// assert_eq!(opt_x, Some(&mut 12));
1711-
/// let copied = opt_x.copied();
1712-
/// assert_eq!(copied, Some(12));
1713-
/// ```
1714-
#[must_use = "`self` will be dropped if the result is not used"]
1715-
#[stable(feature = "copied", since = "1.35.0")]
1716-
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1717-
pub const fn copied(self) -> Option<T> {
1718-
match self {
1719-
Some(&mut t) => Some(t),
1720-
None => None,
1721-
}
1722-
}
1723-
}
17241702

1725-
impl<T: Clone> Option<&T> {
17261703
/// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
17271704
/// option.
17281705
///
@@ -1749,6 +1726,30 @@ impl<T: Clone> Option<&T> {
17491726
}
17501727
}
17511728

1729+
impl<T: Copy> Option<&mut T> {
1730+
/// Maps an `Option<&mut T>` to an `Option<T>` by copying the contents of the
1731+
/// option.
1732+
///
1733+
/// # Examples
1734+
///
1735+
/// ```
1736+
/// let mut x = 12;
1737+
/// let opt_x = Some(&mut x);
1738+
/// assert_eq!(opt_x, Some(&mut 12));
1739+
/// let copied = opt_x.copied();
1740+
/// assert_eq!(copied, Some(12));
1741+
/// ```
1742+
#[must_use = "`self` will be dropped if the result is not used"]
1743+
#[stable(feature = "copied", since = "1.35.0")]
1744+
#[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
1745+
pub const fn copied(self) -> Option<T> {
1746+
match self {
1747+
Some(&mut t) => Some(t),
1748+
None => None,
1749+
}
1750+
}
1751+
}
1752+
17521753
impl<T: Clone> Option<&mut T> {
17531754
/// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
17541755
/// option.

0 commit comments

Comments
 (0)