Skip to content

Commit

Permalink
Rollup merge of #90741 - mbartlett21:patch-4, r=dtolnay
Browse files Browse the repository at this point in the history
Const `Option::cloned`

This constifies the two `Option::cloned` functions, bounded on `~const Clone`.
  • Loading branch information
matthiaskrgr committed Dec 11, 2021
2 parents 40482bb + 9eb7c34 commit 7fbaf33
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,15 @@ impl<T: Clone> Option<&T> {
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn cloned(self) -> Option<T> {
self.map(|t| t.clone())
#[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")]
pub const fn cloned(self) -> Option<T>
where
T: ~const Clone,
{
match self {
Some(t) => Some(t.clone()),
None => None,
}
}
}

Expand All @@ -1541,9 +1548,17 @@ impl<T: Clone> Option<&mut T> {
/// let cloned = opt_x.cloned();
/// assert_eq!(cloned, Some(12));
/// ```
#[must_use = "`self` will be dropped if the result is not used"]
#[stable(since = "1.26.0", feature = "option_ref_mut_cloned")]
pub fn cloned(self) -> Option<T> {
self.map(|t| t.clone())
#[rustc_const_unstable(feature = "const_option_cloned", issue = "91582")]
pub const fn cloned(self) -> Option<T>
where
T: ~const Clone,
{
match self {
Some(t) => Some(t.clone()),
None => None,
}
}
}

Expand Down

0 comments on commit 7fbaf33

Please sign in to comment.