1
Fork 0

Make some Clone impls const

This commit is contained in:
woppopo 2021-12-12 04:19:23 +09:00
parent b9a37ad0d9
commit 662024478d
7 changed files with 33 additions and 11 deletions

View 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 @@ mod impls {
($($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 @@ mod impls {
}
#[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 @@ mod impls {
}
#[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 @@ mod impls {
}
#[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 @@ mod impls {
/// 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 {

View file

@ -683,7 +683,8 @@ impl AsMut<str> for 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 {}
}

View file

@ -106,6 +106,7 @@
#![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)]

View 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 {

View file

@ -635,7 +635,8 @@ impl<T> NonNull<[T]> {
}
#[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

View file

@ -146,7 +146,8 @@ impl<T: ?Sized> Unique<T> {
}
#[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

View 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 {