Skip to content

Commit 7fbaf33

Browse files
authored
Rollup merge of #90741 - mbartlett21:patch-4, r=dtolnay
Const `Option::cloned` This constifies the two `Option::cloned` functions, bounded on `~const Clone`.
2 parents 40482bb + 9eb7c34 commit 7fbaf33

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

library/core/src/option.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1523,8 +1523,15 @@ impl<T: Clone> Option<&T> {
15231523
/// ```
15241524
#[must_use = "`self` will be dropped if the result is not used"]
15251525
#[stable(feature = "rust1", since = "1.0.0")]
1526-
pub fn cloned(self) -> Option<T> {
1527-
self.map(|t| t.clone())
1526+
#[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")]
1527+
pub const fn cloned(self) -> Option<T>
1528+
where
1529+
T: ~const Clone,
1530+
{
1531+
match self {
1532+
Some(t) => Some(t.clone()),
1533+
None => None,
1534+
}
15281535
}
15291536
}
15301537

@@ -1541,9 +1548,17 @@ impl<T: Clone> Option<&mut T> {
15411548
/// let cloned = opt_x.cloned();
15421549
/// assert_eq!(cloned, Some(12));
15431550
/// ```
1551+
#[must_use = "`self` will be dropped if the result is not used"]
15441552
#[stable(since = "1.26.0", feature = "option_ref_mut_cloned")]
1545-
pub fn cloned(self) -> Option<T> {
1546-
self.map(|t| t.clone())
1553+
#[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")]
1554+
pub const fn cloned(self) -> Option<T>
1555+
where
1556+
T: ~const Clone,
1557+
{
1558+
match self {
1559+
Some(t) => Some(t.clone()),
1560+
None => None,
1561+
}
15471562
}
15481563
}
15491564

0 commit comments

Comments
 (0)