2016-03-04 17:37:11 -05:00
|
|
|
//! The `Clone` trait for types that cannot be 'implicitly copied'.
|
2014-11-25 21:17:11 -05: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 16:07:31 +02:00
|
|
|
//! contain owned boxes or implement [`Drop`]), so the compiler considers
|
2014-11-25 21:17:11 -05:00
|
|
|
//! them cheap and safe to copy. For other types copies must be made
|
2016-09-09 16:07:31 +02:00
|
|
|
//! explicitly, by convention implementing the [`Clone`] trait and calling
|
2020-09-01 19:56:32 +02:00
|
|
|
//! the [`clone`] method.
|
|
|
|
//!
|
|
|
|
//! [`clone`]: Clone::clone
|
2016-03-22 01:12:59 +01: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-24 18:59:04 -07:00
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-06-23 16:34:29 -07:00
|
|
|
|
2016-05-23 12:52:38 -04:00
|
|
|
/// A common trait for the ability to explicitly duplicate an object.
|
|
|
|
///
|
2016-09-09 16:07:31 +02:00
|
|
|
/// Differs from [`Copy`] in that [`Copy`] is implicit and extremely inexpensive, while
|
2016-05-23 12:58:42 -04:00
|
|
|
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
|
2016-09-09 16:07:31 +02:00
|
|
|
/// these characteristics, Rust does not allow you to reimplement [`Copy`], but you
|
2016-05-23 12:58:42 -04:00
|
|
|
/// may reimplement `Clone` and run arbitrary code.
|
2016-05-22 18:06:13 -04:00
|
|
|
///
|
2016-09-09 16:07:31 +02:00
|
|
|
/// Since `Clone` is more general than [`Copy`], you can automatically make anything
|
|
|
|
/// [`Copy`] be `Clone` as well.
|
2016-05-22 18:06:13 -04:00
|
|
|
///
|
|
|
|
/// ## Derivable
|
2015-11-16 16:57:37 -05:00
|
|
|
///
|
2016-05-20 15:50:34 -04:00
|
|
|
/// This trait can be used with `#[derive]` if all fields are `Clone`. The `derive`d
|
2020-09-01 19:56:32 +02:00
|
|
|
/// implementation of [`Clone`] calls [`clone`] on each field.
|
|
|
|
///
|
|
|
|
/// [`clone`]: Clone::clone
|
2016-05-04 22:09:51 -04:00
|
|
|
///
|
2018-12-26 13:01:30 +08: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 18:06:13 -04:00
|
|
|
/// ## How can I implement `Clone`?
|
|
|
|
///
|
2016-09-09 16:07:31 +02:00
|
|
|
/// Types that are [`Copy`] should have a trivial implementation of `Clone`. More formally:
|
2016-05-04 22:09:51 -04: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 18:06:13 -04:00
|
|
|
///
|
2018-12-26 13:01:30 +08: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 18:06:13 -04:00
|
|
|
///
|
|
|
|
/// ```
|
2018-12-26 13:01:30 +08:00
|
|
|
/// struct Generate<T>(fn() -> T);
|
|
|
|
///
|
|
|
|
/// impl<T> Copy for Generate<T> {}
|
2016-05-22 18:06:13 -04:00
|
|
|
///
|
2018-12-26 13:01:30 +08:00
|
|
|
/// impl<T> Clone for Generate<T> {
|
|
|
|
/// fn clone(&self) -> Self {
|
|
|
|
/// *self
|
|
|
|
/// }
|
2016-05-22 18:06:13 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
2018-02-12 02:31:26 -05: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`)
|
|
|
|
/// * Array types, for all sizes, if the item type also implements `Clone` (e.g., `[i32; 123456]`)
|
|
|
|
/// * Tuple types, if each component also implements `Clone` (e.g., `()`, `(i32, bool)`)
|
2018-02-12 02:31:26 -05: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-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2017-08-25 08:39:02 -07:00
|
|
|
#[lang = "clone"]
|
2019-11-24 01:43:32 -08:00
|
|
|
pub trait Clone: Sized {
|
2014-09-22 13:51:10 -04:00
|
|
|
/// Returns a copy of the value.
|
2015-03-24 16:58:08 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let hello = "Hello"; // &str implements Clone
|
|
|
|
///
|
|
|
|
/// assert_eq!("Hello", hello.clone());
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-03-30 23:06:05 -07:00
|
|
|
#[must_use = "cloning is often expensive and is not expected to have side effects"]
|
2013-01-30 19:42:06 -08:00
|
|
|
fn clone(&self) -> Self;
|
2013-11-08 23:10:09 -05:00
|
|
|
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Performs copy-assignment from `source`.
|
2013-11-08 23:10:09 -05:00
|
|
|
///
|
|
|
|
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
|
2013-12-15 16:26:09 +11:00
|
|
|
/// but can be overridden to reuse the resources of `a` to avoid unnecessary
|
2013-11-08 23:10:09 -05:00
|
|
|
/// allocations.
|
2017-07-20 11:14:13 -07:00
|
|
|
#[inline]
|
2015-04-08 16:38:38 -07:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-11-08 23:10:09 -05:00
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
*self = source.clone()
|
|
|
|
}
|
2012-11-26 16:12:47 -08:00
|
|
|
}
|
|
|
|
|
2019-07-28 01:51:21 +03: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)]
|
2019-11-24 01:43:32 -08:00
|
|
|
pub macro Clone($item:item) {
|
|
|
|
/* compiler built-in */
|
|
|
|
}
|
2019-07-28 01:51:21 +03:00
|
|
|
|
2016-08-26 19:23:42 +03: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-03 19:40:59 -05:00
|
|
|
//
|
2016-08-26 19:23:42 +03:00
|
|
|
// These structs should never appear in user code.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(missing_debug_implementations)]
|
2019-11-24 01:43:32 -08:00
|
|
|
#[unstable(
|
|
|
|
feature = "derive_clone_copy",
|
|
|
|
reason = "deriving hack, should not be public",
|
2019-12-21 13:16:18 +02:00
|
|
|
issue = "none"
|
2019-11-24 01:43:32 -08:00
|
|
|
)]
|
|
|
|
pub struct AssertParamIsClone<T: Clone + ?Sized> {
|
|
|
|
_field: crate::marker::PhantomData<T>,
|
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[allow(missing_debug_implementations)]
|
2019-11-24 01:43:32 -08:00
|
|
|
#[unstable(
|
|
|
|
feature = "derive_clone_copy",
|
|
|
|
reason = "deriving hack, should not be public",
|
2019-12-21 13:16:18 +02:00
|
|
|
issue = "none"
|
2019-11-24 01:43:32 -08: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 01:17:32 -05:00
|
|
|
|
|
|
|
/// Implementations of `Clone` for primitive types.
|
|
|
|
///
|
|
|
|
/// Implementations that cannot be described in Rust
|
2020-04-03 19:03:13 +09: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 01:17:32 -05:00
|
|
|
mod impls {
|
|
|
|
|
|
|
|
use super::Clone;
|
|
|
|
|
|
|
|
macro_rules! impl_clone {
|
|
|
|
($($t:ty)*) => {
|
|
|
|
$(
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl Clone for $t {
|
2018-03-10 17:43:44 -05: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 01:17:32 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_clone! {
|
|
|
|
usize u8 u16 u32 u64 u128
|
|
|
|
isize i8 i16 i32 i64 i128
|
|
|
|
f32 f64
|
|
|
|
bool char
|
|
|
|
}
|
|
|
|
|
2019-12-11 09:55:29 -05:00
|
|
|
#[unstable(feature = "never_type", issue = "35121")]
|
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 01:17:32 -05:00
|
|
|
impl Clone for ! {
|
2018-03-10 17:43:44 -05: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 01:17:32 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T: ?Sized> Clone for *const T {
|
2018-03-10 17:43:44 -05: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 01:17:32 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl<T: ?Sized> Clone for *mut T {
|
2018-03-10 17:43:44 -05: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 01:17:32 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 14:33:37 -04: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 01:17:32 -05:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2018-09-03 04:50:14 -07:00
|
|
|
impl<T: ?Sized> Clone for &T {
|
2018-03-10 17:43:44 -05: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 01:17:32 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
*self
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 06:39:38 -05:00
|
|
|
|
2020-03-13 14:33:37 -04:00
|
|
|
/// Shared references can be cloned, but mutable references *cannot*!
|
2020-01-08 06:39:38 -05: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 01:17:32 -05:00
|
|
|
}
|