2013-04-05 23:51:43 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00: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.
|
|
|
|
|
2014-11-26 02:17:11 +00:00
|
|
|
//! The `Clone` trait for types that cannot be 'implicitly copied'
|
|
|
|
//!
|
|
|
|
//! 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.
|
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
|
|
|
|
2015-01-06 22:33:42 +00:00
|
|
|
use marker::Sized;
|
2014-11-17 19:35:27 +00:00
|
|
|
|
2013-05-28 21:35:52 +00:00
|
|
|
/// A common trait for cloning an object.
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
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-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-01-31 03:42:06 +00:00
|
|
|
fn clone(&self) -> Self;
|
2013-11-09 04:10:09 +00:00
|
|
|
|
|
|
|
/// Perform copy-assignment from `source`.
|
|
|
|
///
|
|
|
|
/// `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.
|
|
|
|
#[inline(always)]
|
2015-01-23 02:22:03 +00:00
|
|
|
#[unstable(feature = "core",
|
2015-01-13 02:40:19 +00:00
|
|
|
reason = "this function is rarely used")]
|
2013-11-09 04:10:09 +00:00
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
*self = source.clone()
|
|
|
|
}
|
2012-11-27 00:12:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 21:16:49 +00:00
|
|
|
impl<'a, T: ?Sized> Clone for &'a T {
|
2014-01-08 02:49:13 +00:00
|
|
|
/// Return a shallow copy of the reference.
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2013-12-10 07:16:18 +00:00
|
|
|
fn clone(&self) -> &'a T { *self }
|
2013-05-15 16:23:19 +00:00
|
|
|
}
|
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
macro_rules! clone_impl {
|
2013-03-09 17:14:12 +00:00
|
|
|
($t:ty) => {
|
2015-01-24 05:48:20 +00:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-03-09 17:14:12 +00:00
|
|
|
impl Clone for $t {
|
2013-05-15 04:45:40 +00:00
|
|
|
/// Return a deep copy of the value.
|
2013-06-18 21:45:18 +00:00
|
|
|
#[inline]
|
2013-03-09 17:14:12 +00:00
|
|
|
fn clone(&self) -> $t { *self }
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 17:18:10 +00:00
|
|
|
}
|
2013-03-09 17:14:12 +00:00
|
|
|
|
2015-02-14 23:50:26 +00:00
|
|
|
clone_impl! { isize }
|
2014-11-14 17:18:10 +00:00
|
|
|
clone_impl! { i8 }
|
|
|
|
clone_impl! { i16 }
|
|
|
|
clone_impl! { i32 }
|
|
|
|
clone_impl! { i64 }
|
2013-03-09 17:14:12 +00:00
|
|
|
|
2015-02-14 23:50:26 +00:00
|
|
|
clone_impl! { usize }
|
2014-11-14 17:18:10 +00:00
|
|
|
clone_impl! { u8 }
|
|
|
|
clone_impl! { u16 }
|
|
|
|
clone_impl! { u32 }
|
|
|
|
clone_impl! { u64 }
|
2013-03-09 17:14:12 +00:00
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
clone_impl! { f32 }
|
|
|
|
clone_impl! { f64 }
|
2013-03-09 17:14:12 +00:00
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
clone_impl! { () }
|
|
|
|
clone_impl! { bool }
|
|
|
|
clone_impl! { char }
|
2013-04-05 23:51:43 +00:00
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
macro_rules! extern_fn_clone {
|
2013-07-28 21:31:53 +00:00
|
|
|
($($A:ident),*) => (
|
2015-01-23 02:22:03 +00:00
|
|
|
#[unstable(feature = "core",
|
2015-01-13 02:40:19 +00:00
|
|
|
reason = "this may not be sufficient for fns with region parameters")]
|
2013-07-28 21:31:53 +00:00
|
|
|
impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
|
|
|
|
/// Return a copy of a function pointer
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
|
|
|
|
}
|
|
|
|
)
|
2014-11-14 17:18:10 +00:00
|
|
|
}
|
2013-07-28 21:31:53 +00:00
|
|
|
|
2014-11-14 17:18:10 +00:00
|
|
|
extern_fn_clone! {}
|
|
|
|
extern_fn_clone! { A }
|
|
|
|
extern_fn_clone! { A, B }
|
|
|
|
extern_fn_clone! { A, B, C }
|
|
|
|
extern_fn_clone! { A, B, C, D }
|
|
|
|
extern_fn_clone! { A, B, C, D, E }
|
|
|
|
extern_fn_clone! { A, B, C, D, E, F }
|
|
|
|
extern_fn_clone! { A, B, C, D, E, F, G }
|
|
|
|
extern_fn_clone! { A, B, C, D, E, F, G, H }
|