about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2021-12-30 10:37:27 -0800
committerDavid Tolnay <dtolnay@gmail.com>2021-12-30 10:37:27 -0800
commit538fe4b28d3aca5657a95aadf65800d2237276d1 (patch)
treea8d66f3f6f5d9a29c5cd94f259309580ac412cac
parent9d65bc51c1ea5fc482cc42d79732043201bcf54a (diff)
downloadrust-538fe4b28d3aca5657a95aadf65800d2237276d1.tar.gz
rust-538fe4b28d3aca5657a95aadf65800d2237276d1.zip
Consolidate impl Option<&T>
-rw-r--r--library/core/src/option.rs57
1 files changed, 29 insertions, 28 deletions
diff --git a/library/core/src/option.rs b/library/core/src/option.rs
index c3bea55772f..9b3b27b68a3 100644
--- a/library/core/src/option.rs
+++ b/library/core/src/option.rs
@@ -1672,7 +1672,7 @@ impl<T, U> Option<(T, U)> {
     }
 }
 
-impl<T: Copy> Option<&T> {
+impl<T> Option<&T> {
     /// Maps an `Option<&T>` to an `Option<T>` by copying the contents of the
     /// option.
     ///
@@ -1688,7 +1688,10 @@ impl<T: Copy> Option<&T> {
     #[must_use = "`self` will be dropped if the result is not used"]
     #[stable(feature = "copied", since = "1.35.0")]
     #[rustc_const_unstable(feature = "const_option", issue = "67441")]
-    pub const fn copied(self) -> Option<T> {
+    pub const fn copied(self) -> Option<T>
+    where
+        T: Copy,
+    {
         // FIXME: this implementation, which sidesteps using `Option::map` since it's not const
         // ready yet, should be reverted when possible to avoid code repetition
         match self {
@@ -1696,33 +1699,7 @@ impl<T: Copy> Option<&T> {
             None => None,
         }
     }
-}
-
-impl<T: Copy> Option<&mut T> {
-    /// Maps an `Option<&mut T>` to an `Option<T>` by copying the contents of the
-    /// option.
-    ///
-    /// # Examples
-    ///
-    /// ```
-    /// let mut x = 12;
-    /// let opt_x = Some(&mut x);
-    /// assert_eq!(opt_x, Some(&mut 12));
-    /// let copied = opt_x.copied();
-    /// assert_eq!(copied, Some(12));
-    /// ```
-    #[must_use = "`self` will be dropped if the result is not used"]
-    #[stable(feature = "copied", since = "1.35.0")]
-    #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
-    pub const fn copied(self) -> Option<T> {
-        match self {
-            Some(&mut t) => Some(t),
-            None => None,
-        }
-    }
-}
 
-impl<T: Clone> Option<&T> {
     /// Maps an `Option<&T>` to an `Option<T>` by cloning the contents of the
     /// option.
     ///
@@ -1749,6 +1726,30 @@ impl<T: Clone> Option<&T> {
     }
 }
 
+impl<T: Copy> Option<&mut T> {
+    /// Maps an `Option<&mut T>` to an `Option<T>` by copying the contents of the
+    /// option.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// let mut x = 12;
+    /// let opt_x = Some(&mut x);
+    /// assert_eq!(opt_x, Some(&mut 12));
+    /// let copied = opt_x.copied();
+    /// assert_eq!(copied, Some(12));
+    /// ```
+    #[must_use = "`self` will be dropped if the result is not used"]
+    #[stable(feature = "copied", since = "1.35.0")]
+    #[rustc_const_unstable(feature = "const_option_ext", issue = "91930")]
+    pub const fn copied(self) -> Option<T> {
+        match self {
+            Some(&mut t) => Some(t),
+            None => None,
+        }
+    }
+}
+
 impl<T: Clone> Option<&mut T> {
     /// Maps an `Option<&mut T>` to an `Option<T>` by cloning the contents of the
     /// option.