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.
|
|
|
|
|
2013-12-24 17:08:28 +01:00
|
|
|
/*! The `Clone` trait for types that cannot be 'implicitly copied'
|
2013-03-24 18:59:04 -07: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
|
2013-04-06 20:35:11 -04:00
|
|
|
contain owned boxes or implement `Drop`), so the compiler considers
|
2013-07-10 14:43:25 -07:00
|
|
|
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-24 18:59:04 -07:00
|
|
|
|
2012-11-26 16:12:47 -08:00
|
|
|
*/
|
2013-03-24 18:59:04 -07:00
|
|
|
|
2014-05-05 18:56:44 -07:00
|
|
|
use owned::Box;
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A common trait for cloning an object.
|
2012-11-26 16:12:47 -08:00
|
|
|
pub trait Clone {
|
2013-05-15 19:08:50 -07:00
|
|
|
/// Returns a copy of the value. The contents of owned pointers
|
|
|
|
/// are copied to maintain uniqueness, while the contents of
|
|
|
|
/// managed pointers are not copied.
|
2013-01-30 19:42:06 -08:00
|
|
|
fn clone(&self) -> Self;
|
2013-11-08 23:10:09 -05:00
|
|
|
|
|
|
|
/// Perform copy-assignment from `source`.
|
|
|
|
///
|
|
|
|
/// `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)]
|
|
|
|
fn clone_from(&mut self, source: &Self) {
|
|
|
|
*self = source.clone()
|
|
|
|
}
|
2012-11-26 16:12:47 -08:00
|
|
|
}
|
|
|
|
|
2014-05-05 18:56:44 -07:00
|
|
|
impl<T: Clone> Clone for Box<T> {
|
2013-11-08 23:10:09 -05:00
|
|
|
/// Return a copy of the owned box.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-05-05 18:56:44 -07:00
|
|
|
fn clone(&self) -> Box<T> { box {(**self).clone()} }
|
2013-11-08 23:10:09 -05:00
|
|
|
|
|
|
|
/// Perform copy-assignment from `source` by reusing the existing allocation.
|
2014-03-18 23:16:21 +01:00
|
|
|
#[inline]
|
2014-05-05 18:56:44 -07:00
|
|
|
fn clone_from(&mut self, source: &Box<T>) {
|
2014-03-18 23:16:21 +01:00
|
|
|
(**self).clone_from(&(**source));
|
2013-11-08 23:10:09 -05:00
|
|
|
}
|
2013-03-15 18:26:59 -04:00
|
|
|
}
|
|
|
|
|
2013-04-06 20:35:11 -04:00
|
|
|
impl<T> Clone for @T {
|
|
|
|
/// Return a shallow copy of the managed box.
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-04-06 20:35:11 -04:00
|
|
|
fn clone(&self) -> @T { *self }
|
2013-04-05 16:41:47 -06:00
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a, T> Clone for &'a T {
|
2014-01-07 18:49:13 -08:00
|
|
|
/// Return 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
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a, T> Clone for &'a [T] {
|
2013-06-23 22:16:19 -04:00
|
|
|
/// Return a shallow copy of the slice.
|
|
|
|
#[inline]
|
2013-12-09 23:16:18 -08:00
|
|
|
fn clone(&self) -> &'a [T] { *self }
|
2013-06-23 22:16:19 -04:00
|
|
|
}
|
|
|
|
|
2013-12-09 23:16:18 -08:00
|
|
|
impl<'a> Clone for &'a str {
|
2013-06-23 22:16:19 -04:00
|
|
|
/// Return a shallow copy of the slice.
|
|
|
|
#[inline]
|
2013-12-09 23:16:18 -08:00
|
|
|
fn clone(&self) -> &'a str { *self }
|
2013-06-23 22:16:19 -04:00
|
|
|
}
|
|
|
|
|
2013-03-09 12:14:12 -05:00
|
|
|
macro_rules! clone_impl(
|
|
|
|
($t:ty) => {
|
|
|
|
impl Clone for $t {
|
2013-05-15 00:45:40 -04:00
|
|
|
/// Return 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 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
clone_impl!(int)
|
|
|
|
clone_impl!(i8)
|
|
|
|
clone_impl!(i16)
|
|
|
|
clone_impl!(i32)
|
|
|
|
clone_impl!(i64)
|
|
|
|
|
|
|
|
clone_impl!(uint)
|
|
|
|
clone_impl!(u8)
|
|
|
|
clone_impl!(u16)
|
|
|
|
clone_impl!(u32)
|
|
|
|
clone_impl!(u64)
|
|
|
|
|
|
|
|
clone_impl!(f32)
|
|
|
|
clone_impl!(f64)
|
|
|
|
|
2013-05-15 00:45:40 -04:00
|
|
|
clone_impl!(())
|
2013-03-09 12:14:12 -05:00
|
|
|
clone_impl!(bool)
|
|
|
|
clone_impl!(char)
|
2013-04-05 17:51:43 -06:00
|
|
|
|
2013-07-28 23:31:53 +02:00
|
|
|
macro_rules! extern_fn_clone(
|
|
|
|
($($A:ident),*) => (
|
|
|
|
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 }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-05-01 18:06:59 -07:00
|
|
|
use prelude::*;
|
2014-05-07 08:20:22 -07:00
|
|
|
use owned::Box;
|
2014-05-01 18:06:59 -07:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_owned_clone() {
|
|
|
|
let a = box 5i;
|
|
|
|
let b: Box<int> = a.clone();
|
|
|
|
assert_eq!(a, b);
|
|
|
|
}
|
2013-04-05 17:51:43 -06:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_managed_clone() {
|
|
|
|
let a = @5i;
|
|
|
|
let b: @int = a.clone();
|
|
|
|
assert_eq!(a, b);
|
|
|
|
}
|
2013-04-05 17:51:43 -06:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_borrowed_clone() {
|
|
|
|
let x = 5i;
|
|
|
|
let y: &int = &x;
|
|
|
|
let z: &int = (&y).clone();
|
|
|
|
assert_eq!(*z, 5);
|
|
|
|
}
|
2013-07-28 23:31:53 +02:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_clone_from() {
|
2014-05-07 08:20:22 -07:00
|
|
|
let a = box 5;
|
|
|
|
let mut b = box 10;
|
2014-04-30 20:56:40 -07:00
|
|
|
b.clone_from(&a);
|
|
|
|
assert_eq!(*b, 5);
|
|
|
|
}
|
2013-11-08 23:10:09 -05:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
#[test]
|
|
|
|
fn test_extern_fn_clone() {
|
|
|
|
trait Empty {}
|
|
|
|
impl Empty for int {}
|
2013-07-28 23:31:53 +02:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
fn test_fn_a() -> f64 { 1.0 }
|
|
|
|
fn test_fn_b<T: Empty>(x: T) -> T { x }
|
|
|
|
fn test_fn_c(_: int, _: f64, _: ~[int], _: int, _: int, _: int) {}
|
2013-07-28 23:31:53 +02:00
|
|
|
|
2014-04-30 20:56:40 -07:00
|
|
|
let _ = test_fn_a.clone();
|
|
|
|
let _ = test_fn_b::<int>.clone();
|
|
|
|
let _ = test_fn_c.clone();
|
|
|
|
}
|
2013-07-28 23:31:53 +02:00
|
|
|
}
|