about summary refs log tree commit diff
diff options
context:
space:
mode:
authormbartlett21 <29034492+mbartlett21@users.noreply.github.com>2021-11-10 08:41:01 +1000
committerGitHub <noreply@github.com>2021-11-10 08:41:01 +1000
commitafdec886f38dfbfaa9084580e109c4b5188fd0fe (patch)
treeecc92a55b89e7a783412df9d917de87477bd282e
parentd6082292a6f3207cbdacd6633a5b9d1476bb6772 (diff)
downloadrust-afdec886f38dfbfaa9084580e109c4b5188fd0fe.tar.gz
rust-afdec886f38dfbfaa9084580e109c4b5188fd0fe.zip
Make `Option::cloned` const fn.
-rw-r--r--library/core/src/option.rs17
1 files changed, 13 insertions, 4 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index baf9948857b..da3c5f9f5aa 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -1497,8 +1497,12 @@ 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 = "option_const_cloned", issue = "none")]
+    pub const fn cloned(self) -> Option<T> where T: ~const Clone {
+        match self {
+            Some(t) => Some(t.clone()),
+            None => None,
+        }
     }
 }
 
@@ -1515,9 +1519,14 @@ 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 = "option_const_cloned", issue = "none")]
+    pub const fn cloned(self) -> Option<T> where T: ~const Clone {
+        match self {
+            Some(t) => Some(t.clone()),
+            None => None,
+        }
     }
 }