2013-04-05 17:51:43 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
|
|
|
//! allocation to copy and do not have finalizers (i.e. they do not
|
|
|
|
//! contain owned boxes or implement `Drop`), so the compiler considers
|
|
|
|
//! them cheap and safe to copy. For other types copies must be made
|
|
|
|
//! explicitly, by convention implementing the `Clone` trait and calling
|
|
|
|
//! the `clone` method.
|
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
|
|
|
|
2015-01-07 11:33:42 +13:00
|
|
|
use marker::Sized;
|
2014-11-17 14:35:27 -05:00
|
|
|
|
2016-05-23 12:52:38 -04:00
|
|
|
/// A common trait for the ability to explicitly duplicate an object.
|
|
|
|
///
|
2016-05-23 12:58:42 -04:00
|
|
|
/// Differs from `Copy` in that `Copy` is implicit and extremely inexpensive, while
|
|
|
|
/// `Clone` is always explicit and may or may not be expensive. In order to enforce
|
|
|
|
/// these characteristics, Rust does not allow you to reimplement `Copy`, but you
|
|
|
|
/// may reimplement `Clone` and run arbitrary code.
|
2016-05-22 18:06:13 -04:00
|
|
|
///
|
|
|
|
/// Since `Clone` is more general than `Copy`, you can automatically make anything
|
|
|
|
/// `Copy` be `Clone` as well.
|
|
|
|
///
|
|
|
|
/// ## 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
|
|
|
|
/// implementation of `clone()` calls `clone()` on each field.
|
2016-05-04 22:09:51 -04:00
|
|
|
///
|
2016-05-22 18:06:13 -04:00
|
|
|
/// ## How can I implement `Clone`?
|
|
|
|
///
|
2016-05-04 22:09:51 -04:00
|
|
|
/// Types that are `Copy` should have a trivial implementation of `Clone`. More formally:
|
|
|
|
/// 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
|
|
|
///
|
2016-05-23 13:00:01 -04:00
|
|
|
/// An example is an array holding more than 32 elements of a type that is `Clone`; the standard
|
|
|
|
/// library only implements `Clone` up until arrays of size 32. In this case, the implementation of
|
2016-05-22 18:06:13 -04:00
|
|
|
/// `Clone` cannot be `derive`d, but can be implemented as:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #[derive(Copy)]
|
|
|
|
/// struct Stats {
|
|
|
|
/// frequencies: [i32; 100],
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Clone for Stats {
|
2016-05-23 13:13:09 -04:00
|
|
|
/// fn clone(&self) -> Stats { *self }
|
2016-05-22 18:06:13 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-18 15:27:41 -05: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")]
|
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.
|
|
|
|
#[inline(always)]
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
// FIXME(aburka): this method is used solely by #[derive] to
|
|
|
|
// assert that every component of a type implements Clone.
|
|
|
|
//
|
|
|
|
// This should never be called by user code.
|
|
|
|
#[doc(hidden)]
|
|
|
|
#[inline(always)]
|
|
|
|
#[unstable(feature = "derive_clone_copy",
|
|
|
|
reason = "deriving hack, should not be public",
|
|
|
|
issue = "0")]
|
|
|
|
pub fn assert_receiver_is_clone<T: Clone + ?Sized>(_: &T) {}
|
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-06 10:16:49 +13:00
|
|
|
impl<'a, T: ?Sized> Clone for &'a T {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Returns a shallow copy of the reference.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-12-09 23:16:18 -08:00
|
|
|
fn clone(&self) -> &'a T { *self }
|
2013-05-15 12:23:19 -04:00
|
|
|
}
|
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! clone_impl {
|
2013-03-09 12:14:12 -05:00
|
|
|
($t:ty) => {
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-03-09 12:14:12 -05:00
|
|
|
impl Clone for $t {
|
2015-04-13 10:21:32 -04:00
|
|
|
/// Returns a deep copy of the value.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-09 12:14:12 -05:00
|
|
|
fn clone(&self) -> $t { *self }
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
2013-03-09 12:14:12 -05:00
|
|
|
|
2015-02-14 15:50:26 -08:00
|
|
|
clone_impl! { isize }
|
2014-11-14 09:18:10 -08:00
|
|
|
clone_impl! { i8 }
|
|
|
|
clone_impl! { i16 }
|
|
|
|
clone_impl! { i32 }
|
|
|
|
clone_impl! { i64 }
|
2013-03-09 12:14:12 -05:00
|
|
|
|
2015-02-14 15:50:26 -08:00
|
|
|
clone_impl! { usize }
|
2014-11-14 09:18:10 -08:00
|
|
|
clone_impl! { u8 }
|
|
|
|
clone_impl! { u16 }
|
|
|
|
clone_impl! { u32 }
|
|
|
|
clone_impl! { u64 }
|
2013-03-09 12:14:12 -05:00
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
clone_impl! { f32 }
|
|
|
|
clone_impl! { f64 }
|
2013-03-09 12:14:12 -05:00
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
clone_impl! { () }
|
|
|
|
clone_impl! { bool }
|
|
|
|
clone_impl! { char }
|