Skip to content

Commit afdec88

Browse files
authored
Make Option::cloned const fn.
1 parent d608229 commit afdec88

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

library/core/src/option.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,12 @@ impl<T: Clone> Option<&T> {
14971497
/// ```
14981498
#[must_use = "`self` will be dropped if the result is not used"]
14991499
#[stable(feature = "rust1", since = "1.0.0")]
1500-
pub fn cloned(self) -> Option<T> {
1501-
self.map(|t| t.clone())
1500+
#[rustc_const_unstable(feature = "option_const_cloned", issue = "none")]
1501+
pub const fn cloned(self) -> Option<T> where T: ~const Clone {
1502+
match self {
1503+
Some(t) => Some(t.clone()),
1504+
None => None,
1505+
}
15021506
}
15031507
}
15041508

@@ -1515,9 +1519,14 @@ impl<T: Clone> Option<&mut T> {
15151519
/// let cloned = opt_x.cloned();
15161520
/// assert_eq!(cloned, Some(12));
15171521
/// ```
1522+
#[must_use = "`self` will be dropped if the result is not used"]
15181523
#[stable(since = "1.26.0", feature = "option_ref_mut_cloned")]
1519-
pub fn cloned(self) -> Option<T> {
1520-
self.map(|t| t.clone())
1524+
#[rustc_const_unstable(feature = "option_const_cloned", issue = "none")]
1525+
pub const fn cloned(self) -> Option<T> where T: ~const Clone {
1526+
match self {
1527+
Some(t) => Some(t.clone()),
1528+
None => None,
1529+
}
15211530
}
15221531
}
15231532

0 commit comments

Comments
 (0)