diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2021-12-11 17:35:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-11 17:35:25 +0100 |
| commit | 7fbaf33a3cb2d9f86c1dc037b2087086de25c515 (patch) | |
| tree | 3dcaf8a4dc1095268dcd9885bbf8f978499e01bb | |
| parent | 40482bb539362e1173bbe2b218ed4c06b11fa4b3 (diff) | |
| parent | 9eb7c34f9b4fe388abfb2370ffbdff4e11df3136 (diff) | |
| download | rust-7fbaf33a3cb2d9f86c1dc037b2087086de25c515.tar.gz rust-7fbaf33a3cb2d9f86c1dc037b2087086de25c515.zip | |
Rollup merge of #90741 - mbartlett21:patch-4, r=dtolnay
Const `Option::cloned` This constifies the two `Option::cloned` functions, bounded on `~const Clone`.
| -rw-r--r-- | library/core/src/option.rs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs index 7b9c6e43960..145c7ff5c32 100644 --- a/library/core/src/option.rs +++ b/library/core/src/option.rs @@ -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, + } } } @@ -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, + } } } |
