]> git.lizzy.rs Git - rust.git/commitdiff
Make some `Clone` impls `const`
authorwoppopo <woppopo@protonmail.com>
Sat, 11 Dec 2021 19:19:23 +0000 (04:19 +0900)
committerwoppopo <woppopo@protonmail.com>
Sat, 11 Dec 2021 19:19:23 +0000 (04:19 +0900)
library/core/src/clone.rs
library/core/src/convert/mod.rs
library/core/src/lib.rs
library/core/src/option.rs
library/core/src/ptr/non_null.rs
library/core/src/ptr/unique.rs
library/core/src/result.rs

index 6f9579043c37d0fc74973ec66a6ea0609598931e..1912694412b9874178aceb93db1234c9edbe78ae 100644 (file)
@@ -127,7 +127,11 @@ pub trait Clone: Sized {
     /// allocations.
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn clone_from(&mut self, source: &Self) {
+    #[default_method_body_is_const]
+    fn clone_from(&mut self, source: &Self)
+    where
+        Self: ~const Drop,
+    {
         *self = source.clone()
     }
 }
@@ -178,7 +182,8 @@ macro_rules! impl_clone {
         ($($t:ty)*) => {
             $(
                 #[stable(feature = "rust1", since = "1.0.0")]
-                impl Clone for $t {
+                #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+                impl const Clone for $t {
                     #[inline]
                     fn clone(&self) -> Self {
                         *self
@@ -196,7 +201,8 @@ fn clone(&self) -> Self {
     }
 
     #[unstable(feature = "never_type", issue = "35121")]
-    impl Clone for ! {
+    #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+    impl const Clone for ! {
         #[inline]
         fn clone(&self) -> Self {
             *self
@@ -204,7 +210,8 @@ fn clone(&self) -> Self {
     }
 
     #[stable(feature = "rust1", since = "1.0.0")]
-    impl<T: ?Sized> Clone for *const T {
+    #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+    impl<T: ?Sized> const Clone for *const T {
         #[inline]
         fn clone(&self) -> Self {
             *self
@@ -212,7 +219,8 @@ fn clone(&self) -> Self {
     }
 
     #[stable(feature = "rust1", since = "1.0.0")]
-    impl<T: ?Sized> Clone for *mut T {
+    #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+    impl<T: ?Sized> const Clone for *mut T {
         #[inline]
         fn clone(&self) -> Self {
             *self
@@ -221,7 +229,8 @@ fn clone(&self) -> Self {
 
     /// Shared references can be cloned, but mutable references *cannot*!
     #[stable(feature = "rust1", since = "1.0.0")]
-    impl<T: ?Sized> Clone for &T {
+    #[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+    impl<T: ?Sized> const Clone for &T {
         #[inline]
         #[rustc_diagnostic_item = "noop_method_clone"]
         fn clone(&self) -> Self {
index 5aa53deee343d55820c19526dfe76d081184ece0..a50b8904b593eebe324bbb0f77a0749b720fd7b5 100644 (file)
@@ -683,7 +683,8 @@ fn as_mut(&mut self) -> &mut str {
 pub enum Infallible {}
 
 #[stable(feature = "convert_infallible", since = "1.34.0")]
-impl Clone for Infallible {
+#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+impl const Clone for Infallible {
     fn clone(&self) -> Infallible {
         match *self {}
     }
index 78383b54c5d1e410b7f02557a0fb6e5852a8869b..4ef4740799a0baa4c13c05e20cfa2a51dc8f0ee5 100644 (file)
 #![feature(const_caller_location)]
 #![feature(const_cell_into_inner)]
 #![feature(const_char_convert)]
+#![feature(const_clone)]
 #![feature(const_discriminant)]
 #![feature(const_eval_select)]
 #![feature(const_float_bits_conv)]
index 7b9c6e43960f7b61a2e15931dd090ac1a2656ecb..941d7e07926281ebf5e47fc3ebaed21ded4d20c8 100644 (file)
@@ -1668,7 +1668,11 @@ const fn expect_failed(msg: &str) -> ! {
 /////////////////////////////////////////////////////////////////////////////
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: Clone> Clone for Option<T> {
+#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+impl<T> const Clone for Option<T>
+where
+    T: ~const Clone + ~const Drop,
+{
     #[inline]
     fn clone(&self) -> Self {
         match self {
index 58110b068094381e9ac2e74130dab235c9608bf2..cb268abc8eecd6e92fc6b14c2e65b7484d2742f5 100644 (file)
@@ -635,7 +635,8 @@ pub unsafe fn get_unchecked_mut<I>(self, index: I) -> NonNull<I::Output>
 }
 
 #[stable(feature = "nonnull", since = "1.25.0")]
-impl<T: ?Sized> Clone for NonNull<T> {
+#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+impl<T: ?Sized> const Clone for NonNull<T> {
     #[inline]
     fn clone(&self) -> Self {
         *self
index d650a6f974b972c209f8f4b8f89746c39782ed5c..ee984b7b5a4f175235a4b601155235206695c4df 100644 (file)
@@ -146,7 +146,8 @@ pub const fn cast<U>(self) -> Unique<U> {
 }
 
 #[unstable(feature = "ptr_internals", issue = "none")]
-impl<T: ?Sized> Clone for Unique<T> {
+#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+impl<T: ?Sized> const Clone for Unique<T> {
     #[inline]
     fn clone(&self) -> Self {
         *self
index e6b8c8ec3385a09077f2f743e975ec1d63d63558..d639c7188c15adcaedb763fe5e4d31d31bdcaf6f 100644 (file)
@@ -1665,7 +1665,12 @@ fn unwrap_failed(msg: &str, error: &dyn fmt::Debug) -> ! {
 /////////////////////////////////////////////////////////////////////////////
 
 #[stable(feature = "rust1", since = "1.0.0")]
-impl<T: Clone, E: Clone> Clone for Result<T, E> {
+#[rustc_const_unstable(feature = "const_clone", issue = "91805")]
+impl<T, E> const Clone for Result<T, E>
+where
+    T: ~const Clone + ~const Drop,
+    E: ~const Clone + ~const Drop,
+{
     #[inline]
     fn clone(&self) -> Self {
         match self {