2016-03-04 22:37:11 +00:00
|
|
|
//! The `Clone` trait for types that cannot be 'implicitly copied'.
|
2014-11-26 02:17:11 +00:00
|
|
|
//!
|
|
|
|
//! In Rust, some simple types are "implicitly copyable" and when you
|
|
|
|
//! assign them or pass them as arguments, the receiver will get a copy,
|
|
|
|
//! leaving the original value in place. These types do not require
|
2018-11-27 02:59:49 +00:00
|
|
|
//! allocation to copy and do not have finalizers (i.e., they do not
|
2016-09-09 14:07:31 +00:00
|
|
|
//! contain owned boxes or implement [`Drop`]), so the compiler considers
|
2014-11-26 02:17:11 +00:00
|
|
|
//! them cheap and safe to copy. For other types copies must be made
|
2016-09-09 14:07:31 +00:00
|
|
|
//! explicitly, by convention implementing the [`Clone`] trait and calling
|
2020-09-01 17:56:32 +00:00
|
|
|
//! the [`clone`] method.
|
|
|
|
//!
|
|
|
|
//! [`clone`]: Clone::clone
|
2016-03-22 00:12:59 +00:00
|
|
|
//!
|
|
|
|
//! Basic usage example:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! let s = String::new(); // String type implements Clone
|
|
|
|
//! let copy = s.clone(); // so we can clone it
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! To easily implement the Clone trait, you can also use
|
|
|
|
//! `#[derive(Clone)]`. Example:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! #[derive(Clone)] // we add the Clone trait to Morpheus struct
|
|
|
|
//! struct Morpheus {
|
|
|
|
//! blue_pill: f32,
|
|
|
|
//! red_pill: i64,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let f = Morpheus { blue_pill: 0.0, red_pill: 0 };
|
|
|
|
//! let copy = f.clone(); // and now we can clone it!
|
|
|
|
//! }
|
|
|
|
//! ```
|
2013-03-25 01:59:04 +00:00
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-06-23 23:34:29 +00:00
|
|
|
|
2024-06-25 23:22:07 +00:00
|
|
|
mod uninit;
|
2023-09-24 15:55:19 +00:00
|
|
|
|
2016-05-23 16:52:38 +00:00
|
|
|
/// A common trait for the ability to explicitly duplicate an object.
|
|
|
|
///
|
2021-06-10 15:28:26 +00:00
|
|
|
/// Differs from [`Copy`] in that [`Copy`] is implicit and an inexpensive bit-wise copy, while
|
2016-05-23 16:58:42 +00:00
|
|
|
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
|
2016-09-09 14:07:31 +00:00
|
|
|
/// these characteristics, Rust does not allow you to reimplement [`Copy`], but you
|
2016-05-23 16:58:42 +00:00
|
|
|
/// may reimplement `Clone` and run arbitrary code.
|
2016-05-22 22:06:13 +00:00
|
|
|
///
|
2016-09-09 14:07:31 +00:00
|
|
|
/// Since `Clone` is more general than [`Copy`], you can automatically make anything
|
|
|
|
/// [`Copy`] be `Clone` as well.
|
2016-05-22 22:06:13 +00:00
|
|
|
///
|
|
|
|
/// ## Derivable
|
2015-11-16 21:57:37 +00:00
|
|
|
///
|
2016-05-20 19:50:34 +00:00
|
|
|
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
|
2020-09-01 17:56:32 +00:00
|
|
|
/// implementation of [`Clone`] calls [`clone`] on each field.
|
|
|
|
///
|
|
|
|
/// [`clone`]: Clone::clone
|
2016-05-05 02:09:51 +00:00
|
|
|
///
|
2018-12-26 05:01:30 +00:00
|
|
|
/// For a generic struct, `#[derive]` implements `Clone` conditionally by adding bound `Clone` on
|
|
|
|
/// generic parameters.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // `derive` implements Clone for Reading<T> when T is Clone.
|
|
|
|
/// #[derive(Clone)]
|
|
|
|
/// struct Reading<T> {
|
|
|
|
/// frequency: T,
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2016-05-22 22:06:13 +00:00
|
|
|
/// ## How can I implement `Clone`?
|
|
|
|
///
|
2016-09-09 14:07:31 +00:00
|
|
|
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
|
2016-05-05 02:09:51 +00:00
|
|
|
/// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
|
|
|
|
/// Manual implementations should be careful to uphold this invariant; however, unsafe code
|
|
|
|
/// must not rely on it to ensure memory safety.
|
2016-05-22 22:06:13 +00:00
|
|
|
///
|
2018-12-26 05:01:30 +00:00
|
|
|
/// An example is a generic struct holding a function pointer. In this case, the
|
|
|
|
/// implementation of `Clone` cannot be `derive`d, but can be implemented as:
|
2016-05-22 22:06:13 +00:00
|
|
|
///
|
|
|
|
/// ```
|
2018-12-26 05:01:30 +00:00
|
|
|
/// struct Generate<T>(fn() -> T);
|
|
|
|
///
|
|
|
|
/// impl<T> Copy for Generate<T> {}
|
2016-05-22 22:06:13 +00:00
|
|
|
///
|
2018-12-26 05:01:30 +00:00
|
|
|
/// impl<T> Clone for Generate<T> {
|
|
|
|
/// fn clone(&self) -> Self {
|
|
|
|
/// *self
|
|
|
|
/// }
|
2016-05-22 22:06:13 +00:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-02-12 07:31:26 +00:00
|
|
|
///
|
2023-07-24 17:58:13 +00:00
|
|
|
/// If we `derive`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #[derive(Copy, Clone)]
|
|
|
|
/// struct Generate<T>(fn() -> T);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// the auto-derived implementations will have unnecessary `T: Copy` and `T: Clone` bounds:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # struct Generate<T>(fn() -> T);
|
|
|
|
///
|
|
|
|
/// // Automatically derived
|
|
|
|
/// impl<T: Copy> Copy for Generate<T> { }
|
|
|
|
///
|
|
|
|
/// // Automatically derived
|
|
|
|
/// impl<T: Clone> Clone for Generate<T> {
|
|
|
|
/// fn clone(&self) -> Generate<T> {
|
|
|
|
/// Generate(Clone::clone(&self.0))
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The bounds are unnecessary because clearly the function itself should be
|
|
|
|
/// copy- and cloneable even if its return type is not:
|
|
|
|
///
|
|
|
|
/// ```compile_fail,E0599
|
|
|
|
/// #[derive(Copy, Clone)]
|
|
|
|
/// struct Generate<T>(fn() -> T);
|
|
|
|
///
|
|
|
|
/// struct NotCloneable;
|
|
|
|
///
|
|
|
|
/// fn generate_not_cloneable() -> NotCloneable {
|
|
|
|
/// NotCloneable
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// Generate(generate_not_cloneable).clone(); // error: trait bounds were not satisfied
|
|
|
|
/// // Note: With the manual implementations the above line will compile.
|
|
|
|
/// ```
|
|
|
|
///
|
2018-02-12 07:31:26 +00:00
|
|
|
/// ## Additional implementors
|
|
|
|
///
|
|
|
|
/// In addition to the [implementors listed below][impls],
|
|
|
|
/// the following types also implement `Clone`:
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// * Function item types (i.e., the distinct types defined for each function)
|
|
|
|
/// * Function pointer types (e.g., `fn() -> i32`)
|
2018-02-12 07:31:26 +00:00
|
|
|
/// * Closure types, if they capture no value from the environment
|
|
|
|
/// or if all such captured values implement `Clone` themselves.
|
|
|
|
/// Note that variables captured by shared reference always implement `Clone`
|
|
|
|
/// (even if the referent doesn't),
|
|
|
|
/// while variables captured by mutable reference never implement `Clone`.
|
|
|
|
///
|
|
|
|
/// [impls]: #implementors
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-08-25 15:39:02 +00:00
|
|
|
#[lang = "clone"]
|
2021-01-05 15:46:50 +00:00
|
|
|
#[rustc_diagnostic_item = "Clone"]
|
2021-10-19 07:27:59 +00:00
|
|
|
#[rustc_trivial_field_reads]
|
2014-12-18 20:27:41 +00:00
|
|
|
pub trait Clone: Sized {
|
2014-09-22 17:51:10 +00:00
|
|
|
/// Returns a copy of the value.
|
2015-03-24 20:58:08 +00:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2021-01-06 20:11:08 +00:00
|
|
|
/// # #![allow(noop_method_call)]
|
2015-03-24 20:58:08 +00:00
|
|
|
/// let hello = "Hello"; // &str implements Clone
|
|
|
|
///
|
|
|
|
/// assert_eq!("Hello", hello.clone());
|
|
|
|
/// ```
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-31 06:06:05 +00:00
|
|
|
#[must_use = "cloning is often expensive and is not expected to have side effects"]
|
2024-07-25 02:43:12 +00:00
|
|
|
// Clone::clone is special because the compiler generates MIR to implement it for some types.
|
|
|
|
// See InstanceKind::CloneShim.
|
2024-09-05 16:23:40 +00:00
|
|
|
#[lang = "clone_fn"]
|
2013-01-31 03:42:06 +00:00
|
|
|
fn clone(&self) -> Self;
|
2013-11-09 04:10:09 +00:00
|
|
|
|
2015-04-13 14:21:32 +00:00
|
|
|
/// Performs copy-assignment from `source`.
|
2013-11-09 04:10:09 +00:00
|
|
|
///
|
|
|
|
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
|
2013-12-15 05:26:09 +00:00
|
|
|
/// but can be overridden to reuse the resources of `a` to avoid unnecessary
|
2013-11-09 04:10:09 +00:00
|
|
|
/// allocations.
|
2017-07-20 18:14:13 +00:00
|
|
|
#[inline]
|
2015-04-08 23:38:38 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 07:20:26 +00:00
|
|
|
fn clone_from(&mut self, source: &Self) {
|
2013-11-09 04:10:09 +00:00
|
|
|
*self = source.clone()
|
|
|
|
}
|
2012-11-27 00:12:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-27 22:51:21 +00:00
|
|
|
/// Derive macro generating an impl of the trait `Clone`.
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
#[stable(feature = "builtin_macro_prelude", since = "1.38.0")]
|
|
|
|
#[allow_internal_unstable(core_intrinsics, derive_clone_copy)]
|
|
|
|
pub macro Clone($item:item) {
|
|
|
|
/* compiler built-in */
|
|
|
|
}
|
|
|
|
|
2016-08-26 16:23:42 +00:00
|
|
|
// FIXME(aburka): these structs are used solely by #[derive] to
|
|
|
|
// assert that every component of a type implements Clone or Copy.
|
shallow Clone for #[derive(Copy,Clone)]
Changes #[derive(Copy, Clone)] to use a faster impl of Clone when
both derives are present, and there are no generics in the type.
The faster impl is simply returning *self (which works because the
type is also Copy). See the comments in libsyntax_ext/deriving/clone.rs
for more details.
There are a few types which are Copy but not Clone, in violation
of the definition of Copy. These include large arrays and tuples. The
very existence of these types is arguably a bug, but in order for this
optimization not to change the applicability of #[derive(Copy, Clone)],
the faster Clone impl also injects calls to a new function,
core::clone::assert_receiver_is_clone, to verify that all members are
actually Clone.
This is not a breaking change, because pursuant to RFC 1521, any type
that implements Copy should not do any observable work in its Clone
impl.
2016-02-04 00:40:59 +00:00
|
|
|
//
|
2016-08-26 16:23:42 +00:00
|
|
|
// These structs should never appear in user code.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
|
|
#[unstable(
|
|
|
|
feature = "derive_clone_copy",
|
|
|
|
reason = "deriving hack, should not be public",
|
2019-12-21 11:16:18 +00:00
|
|
|
issue = "none"
|
2016-08-26 16:23:42 +00:00
|
|
|
)]
|
2019-04-15 02:23:21 +00:00
|
|
|
pub struct AssertParamIsClone<T: Clone + ?Sized> {
|
|
|
|
_field: crate::marker::PhantomData<T>,
|
|
|
|
}
|
2016-08-26 16:23:42 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(missing_debug_implementations)]
|
|
|
|
#[unstable(
|
|
|
|
feature = "derive_clone_copy",
|
|
|
|
reason = "deriving hack, should not be public",
|
2019-12-21 11:16:18 +00:00
|
|
|
issue = "none"
|
2016-08-26 16:23:42 +00:00
|
|
|
)]
|
2019-04-15 02:23:21 +00:00
|
|
|
pub struct AssertParamIsCopy<T: Copy + ?Sized> {
|
|
|
|
_field: crate::marker::PhantomData<T>,
|
|
|
|
}
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
|
2023-09-24 15:55:19 +00:00
|
|
|
/// A generalization of [`Clone`] to dynamically-sized types stored in arbitrary containers.
|
|
|
|
///
|
|
|
|
/// This trait is implemented for all types implementing [`Clone`], and also [slices](slice) of all
|
|
|
|
/// such types. You may also implement this trait to enable cloning trait objects and custom DSTs
|
|
|
|
/// (structures containing dynamically-sized fields).
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Implementations must ensure that when `.clone_to_uninit(dst)` returns normally rather than
|
|
|
|
/// panicking, it always leaves `*dst` initialized as a valid value of type `Self`.
|
|
|
|
///
|
|
|
|
/// # See also
|
|
|
|
///
|
|
|
|
/// * [`Clone::clone_from`] is a safe function which may be used instead when `Self` is a [`Sized`]
|
|
|
|
/// and the destination is already initialized; it may be able to reuse allocations owned by
|
|
|
|
/// the destination.
|
|
|
|
/// * [`ToOwned`], which allocates a new destination container.
|
|
|
|
///
|
|
|
|
/// [`ToOwned`]: ../../std/borrow/trait.ToOwned.html
|
|
|
|
#[unstable(feature = "clone_to_uninit", issue = "126799")]
|
|
|
|
pub unsafe trait CloneToUninit {
|
|
|
|
/// Performs copy-assignment from `self` to `dst`.
|
|
|
|
///
|
2024-11-01 05:07:59 +00:00
|
|
|
/// This is analogous to `std::ptr::write(dst.cast(), self.clone())`,
|
2023-09-24 15:55:19 +00:00
|
|
|
/// except that `self` may be a dynamically-sized type ([`!Sized`](Sized)).
|
|
|
|
///
|
|
|
|
/// Before this function is called, `dst` may point to uninitialized memory.
|
|
|
|
/// After this function is called, `dst` will point to initialized memory; it will be
|
2024-11-01 05:07:59 +00:00
|
|
|
/// sound to create a `&Self` reference from the pointer with the [pointer metadata]
|
|
|
|
/// from `self`.
|
2023-09-24 15:55:19 +00:00
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Behavior is undefined if any of the following conditions are violated:
|
|
|
|
///
|
2024-11-01 05:07:59 +00:00
|
|
|
/// * `dst` must be [valid] for writes for `std::mem::size_of_val(self)` bytes.
|
|
|
|
/// * `dst` must be properly aligned to `std::mem::align_of_val(self)`.
|
2023-09-24 15:55:19 +00:00
|
|
|
///
|
2024-06-25 23:22:07 +00:00
|
|
|
/// [valid]: crate::ptr#safety
|
2023-09-24 15:55:19 +00:00
|
|
|
/// [pointer metadata]: crate::ptr::metadata()
|
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This function may panic. (For example, it might panic if memory allocation for a clone
|
|
|
|
/// of a value owned by `self` fails.)
|
|
|
|
/// If the call panics, then `*dst` should be treated as uninitialized memory; it must not be
|
|
|
|
/// read or dropped, because even if it was previously valid, it may have been partially
|
|
|
|
/// overwritten.
|
|
|
|
///
|
|
|
|
/// The caller may also need to take care to deallocate the allocation pointed to by `dst`,
|
|
|
|
/// if applicable, to avoid a memory leak, and may need to take other precautions to ensure
|
|
|
|
/// soundness in the presence of unwinding.
|
|
|
|
///
|
|
|
|
/// Implementors should avoid leaking values by, upon unwinding, dropping all component values
|
|
|
|
/// that might have already been created. (For example, if a `[Foo]` of length 3 is being
|
|
|
|
/// cloned, and the second of the three calls to `Foo::clone()` unwinds, then the first `Foo`
|
|
|
|
/// cloned should be dropped.)
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe fn clone_to_uninit(&self, dst: *mut u8);
|
2023-09-24 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "clone_to_uninit", issue = "126799")]
|
|
|
|
unsafe impl<T: Clone> CloneToUninit for T {
|
2024-06-24 15:09:27 +00:00
|
|
|
#[inline]
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
|
2024-06-25 23:22:07 +00:00
|
|
|
// SAFETY: we're calling a specialization with the same contract
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe { <T as self::uninit::CopySpec>::clone_one(self, dst.cast::<T>()) }
|
2023-09-24 15:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "clone_to_uninit", issue = "126799")]
|
|
|
|
unsafe impl<T: Clone> CloneToUninit for [T] {
|
2024-06-24 15:09:27 +00:00
|
|
|
#[inline]
|
2023-09-24 15:55:19 +00:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
|
|
|
|
let dst: *mut [T] = dst.with_metadata_of(self);
|
2024-06-25 23:22:07 +00:00
|
|
|
// SAFETY: we're calling a specialization with the same contract
|
|
|
|
unsafe { <T as self::uninit::CopySpec>::clone_slice(self, dst) }
|
2023-09-24 15:55:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:11:35 +00:00
|
|
|
#[unstable(feature = "clone_to_uninit", issue = "126799")]
|
|
|
|
unsafe impl CloneToUninit for str {
|
2024-06-24 15:09:27 +00:00
|
|
|
#[inline]
|
2024-06-23 19:11:35 +00:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
|
2024-06-23 19:11:35 +00:00
|
|
|
// SAFETY: str is just a [u8] with UTF-8 invariant
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe { self.as_bytes().clone_to_uninit(dst) }
|
2024-06-23 19:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unstable(feature = "clone_to_uninit", issue = "126799")]
|
|
|
|
unsafe impl CloneToUninit for crate::ffi::CStr {
|
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe fn clone_to_uninit(&self, dst: *mut u8) {
|
2024-06-23 19:11:35 +00:00
|
|
|
// SAFETY: For now, CStr is just a #[repr(trasnsparent)] [c_char] with some invariants.
|
|
|
|
// And we can cast [c_char] to [u8] on all supported platforms (see: to_bytes_with_nul).
|
2024-11-01 05:07:59 +00:00
|
|
|
// The pointer metadata properly preserves the length (so NUL is also copied).
|
2024-06-23 19:11:35 +00:00
|
|
|
// See: `cstr_metadata_is_length_with_nul` in tests.
|
2024-11-01 05:07:59 +00:00
|
|
|
unsafe { self.to_bytes_with_nul().clone_to_uninit(dst) }
|
2024-06-23 19:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
/// Implementations of `Clone` for primitive types.
|
|
|
|
///
|
|
|
|
/// Implementations that cannot be described in Rust
|
2020-04-03 10:03:13 +00:00
|
|
|
/// are implemented in `traits::SelectionContext::copy_clone_conditions()`
|
|
|
|
/// in `rustc_trait_selection`.
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
mod impls {
|
|
|
|
macro_rules! impl_clone {
|
|
|
|
($($t:ty)*) => {
|
|
|
|
$(
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl Clone for $t {
|
2022-12-07 16:11:17 +00:00
|
|
|
#[inline(always)]
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_clone! {
|
|
|
|
usize u8 u16 u32 u64 u128
|
|
|
|
isize i8 i16 i32 i64 i128
|
2024-04-05 02:18:24 +00:00
|
|
|
f16 f32 f64 f128
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
bool char
|
|
|
|
}
|
|
|
|
|
2019-12-11 14:55:29 +00:00
|
|
|
#[unstable(feature = "never_type", issue = "35121")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl Clone for ! {
|
2018-03-10 22:43:44 +00:00
|
|
|
#[inline]
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl<T: ?Sized> Clone for *const T {
|
2022-12-07 16:11:17 +00:00
|
|
|
#[inline(always)]
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl<T: ?Sized> Clone for *mut T {
|
2022-12-07 16:11:17 +00:00
|
|
|
#[inline(always)]
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 18:33:37 +00:00
|
|
|
/// Shared references can be cloned, but mutable references *cannot*!
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2023-04-16 06:49:27 +00:00
|
|
|
impl<T: ?Sized> Clone for &T {
|
2022-12-07 16:11:17 +00:00
|
|
|
#[inline(always)]
|
2021-01-05 15:14:39 +00:00
|
|
|
#[rustc_diagnostic_item = "noop_method_clone"]
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 11:39:38 +00:00
|
|
|
|
2020-03-13 18:33:37 +00:00
|
|
|
/// Shared references can be cloned, but mutable references *cannot*!
|
2020-01-08 11:39:38 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T: ?Sized> !Clone for &mut T {}
|
Move some implementations of Clone and Copy to libcore
Add implementations of `Clone` and `Copy` for some primitive types to
libcore so that they show up in the documentation. The concerned types
are the following:
* All primitive signed and unsigned integer types (`usize`, `u8`, `u16`,
`u32`, `u64`, `u128`, `isize`, `i8`, `i16`, `i32`, `i64`, `i128`);
* All primitive floating point types (`f32`, `f64`)
* `bool`
* `char`
* `!`
* Raw pointers (`*const T` and `*mut T`)
* Shared references (`&'a T`)
These types already implemented `Clone` and `Copy`, but the
implementation was provided by the compiler. The compiler no longer
provides these implementations and instead tries to look them up as
normal trait implementations. The goal of this change is to make the
implementations appear in the generated documentation.
For `Copy` specifically, the compiler would reject an attempt to write
an `impl` for the primitive types listed above with error `E0206`; this
error no longer occurs for these types, but it will still occur for the
other types that used to raise that error.
The trait implementations are guarded with `#[cfg(not(stage0))]` because
they are invalid according to the stage0 compiler. When the stage0
compiler is updated to a revision that includes this change, the
attribute will have to be removed, otherwise the stage0 build will fail
because the types mentioned above no longer implement `Clone` or `Copy`.
For type variants that are variadic, such as tuples and function
pointers, and for array types, the `Clone` and `Copy` implementations
are still provided by the compiler, because the language is not
expressive enough yet to be able to write the appropriate
implementations in Rust.
The initial plan was to add `impl` blocks guarded by `#[cfg(dox)]` to
make them apply only when generating documentation, without having to
touch the compiler. However, rustdoc's usage of the compiler still
rejected those `impl` blocks.
This is a [breaking-change] for users of `#![no_core]`, because they
will now have to supply their own implementations of `Clone` and `Copy`
for the primitive types listed above. The easiest way to do that is to
simply copy the implementations from `src/libcore/clone.rs` and
`src/libcore/marker.rs`.
Fixes #25893
2018-02-12 06:17:32 +00:00
|
|
|
}
|