2013-04-05 18:51:43 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06: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-03-24 20:59:04 -05: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
|
2013-04-06 19:35:11 -05:00
|
|
|
contain owned boxes or implement `Drop`), so the compiler considers
|
2013-03-24 20:59:04 -05:00
|
|
|
them cheap and safe to copy and automatically implements the `Copy`
|
|
|
|
trait for them. For other types copies must be made explicitly,
|
|
|
|
by convention implementing the `Clone` trait and calling the
|
|
|
|
`clone` method.
|
|
|
|
|
2012-11-26 18:12:47 -06:00
|
|
|
*/
|
2013-03-24 20:59:04 -05:00
|
|
|
|
2013-05-15 20:01:14 -05:00
|
|
|
use core::kinds::Const;
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A common trait for cloning an object.
|
2012-11-26 18:12:47 -06:00
|
|
|
pub trait Clone {
|
2013-05-15 21:08:50 -05: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 21:42:06 -06:00
|
|
|
fn clone(&self) -> Self;
|
2012-11-26 18:12:47 -06:00
|
|
|
}
|
|
|
|
|
2013-05-14 23:40:14 -05:00
|
|
|
impl<T: Clone> Clone for ~T {
|
2013-04-06 19:35:11 -05:00
|
|
|
/// Return a deep copy of the owned box.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-15 17:26:59 -05:00
|
|
|
fn clone(&self) -> ~T { ~(**self).clone() }
|
|
|
|
}
|
|
|
|
|
2013-04-06 19:35:11 -05:00
|
|
|
impl<T> Clone for @T {
|
|
|
|
/// Return a shallow copy of the managed box.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-06 19:35:11 -05:00
|
|
|
fn clone(&self) -> @T { *self }
|
2013-04-05 17:41:47 -05:00
|
|
|
}
|
|
|
|
|
2013-04-06 19:35:11 -05:00
|
|
|
impl<T> Clone for @mut T {
|
|
|
|
/// Return a shallow copy of the managed box.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-06 19:35:11 -05:00
|
|
|
fn clone(&self) -> @mut T { *self }
|
2013-04-05 17:41:47 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 11:23:19 -05:00
|
|
|
impl<'self, T> Clone for &'self T {
|
|
|
|
/// Return a shallow copy of the borrowed pointer.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-15 11:23:19 -05:00
|
|
|
fn clone(&self) -> &'self T { *self }
|
|
|
|
}
|
|
|
|
|
2013-06-23 21:16:19 -05:00
|
|
|
impl<'self, T> Clone for &'self [T] {
|
|
|
|
/// Return a shallow copy of the slice.
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> &'self [T] { *self }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Clone for &'self str {
|
|
|
|
/// Return a shallow copy of the slice.
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> &'self str { *self }
|
|
|
|
}
|
|
|
|
|
2013-03-09 11:14:12 -06:00
|
|
|
macro_rules! clone_impl(
|
|
|
|
($t:ty) => {
|
|
|
|
impl Clone for $t {
|
2013-05-14 23:45:40 -05:00
|
|
|
/// Return a deep copy of the value.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-03-09 11:14:12 -06: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!(float)
|
|
|
|
clone_impl!(f32)
|
|
|
|
clone_impl!(f64)
|
|
|
|
|
2013-05-14 23:45:40 -05:00
|
|
|
clone_impl!(())
|
2013-03-09 11:14:12 -06:00
|
|
|
clone_impl!(bool)
|
|
|
|
clone_impl!(char)
|
2013-04-05 18:51:43 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait distinct from `Clone` which represents "deep copies" of things like
|
|
|
|
/// managed boxes which would otherwise not be copied.
|
2013-05-14 23:45:40 -05:00
|
|
|
pub trait DeepClone {
|
2013-05-15 21:08:50 -05:00
|
|
|
/// Return a deep copy of the value. Unlike `Clone`, the contents of shared pointer types
|
2013-06-04 17:40:50 -05:00
|
|
|
/// *are* copied.
|
2013-05-14 23:45:40 -05:00
|
|
|
fn deep_clone(&self) -> Self;
|
|
|
|
}
|
|
|
|
|
2013-05-15 20:01:14 -05:00
|
|
|
impl<T: DeepClone> DeepClone for ~T {
|
|
|
|
/// Return a deep copy of the owned box.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-15 20:01:14 -05:00
|
|
|
fn deep_clone(&self) -> ~T { ~(**self).deep_clone() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: #6525: should also be implemented for `T: Owned + DeepClone`
|
|
|
|
impl<T: Const + DeepClone> DeepClone for @T {
|
|
|
|
/// Return a deep copy of the managed box. The `Const` trait is required to prevent performing
|
|
|
|
/// a deep clone of a potentially cyclical type.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-15 20:01:14 -05:00
|
|
|
fn deep_clone(&self) -> @T { @(**self).deep_clone() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: #6525: should also be implemented for `T: Owned + DeepClone`
|
|
|
|
impl<T: Const + DeepClone> DeepClone for @mut T {
|
|
|
|
/// Return a deep copy of the managed box. The `Const` trait is required to prevent performing
|
|
|
|
/// a deep clone of a potentially cyclical type.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-15 20:01:14 -05:00
|
|
|
fn deep_clone(&self) -> @mut T { @mut (**self).deep_clone() }
|
|
|
|
}
|
|
|
|
|
2013-05-14 23:45:40 -05:00
|
|
|
macro_rules! deep_clone_impl(
|
|
|
|
($t:ty) => {
|
|
|
|
impl DeepClone for $t {
|
|
|
|
/// Return a deep copy of the value.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-14 23:45:40 -05:00
|
|
|
fn deep_clone(&self) -> $t { *self }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
deep_clone_impl!(int)
|
|
|
|
deep_clone_impl!(i8)
|
|
|
|
deep_clone_impl!(i16)
|
|
|
|
deep_clone_impl!(i32)
|
|
|
|
deep_clone_impl!(i64)
|
|
|
|
|
|
|
|
deep_clone_impl!(uint)
|
|
|
|
deep_clone_impl!(u8)
|
|
|
|
deep_clone_impl!(u16)
|
|
|
|
deep_clone_impl!(u32)
|
|
|
|
deep_clone_impl!(u64)
|
|
|
|
|
|
|
|
deep_clone_impl!(float)
|
|
|
|
deep_clone_impl!(f32)
|
|
|
|
deep_clone_impl!(f64)
|
|
|
|
|
|
|
|
deep_clone_impl!(())
|
|
|
|
deep_clone_impl!(bool)
|
|
|
|
deep_clone_impl!(char)
|
|
|
|
|
2013-04-05 18:51:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_owned_clone() {
|
2013-05-15 20:01:14 -05:00
|
|
|
let a = ~5i;
|
2013-04-06 19:35:11 -05:00
|
|
|
let b: ~int = a.clone();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, b);
|
2013-04-05 18:51:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_managed_clone() {
|
2013-05-15 20:01:14 -05:00
|
|
|
let a = @5i;
|
2013-04-06 19:35:11 -05:00
|
|
|
let b: @int = a.clone();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, b);
|
2013-04-05 18:51:43 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 20:01:14 -05:00
|
|
|
#[test]
|
|
|
|
fn test_managed_mut_deep_clone() {
|
|
|
|
let x = @mut 5i;
|
|
|
|
let y: @mut int = x.deep_clone();
|
|
|
|
*x = 20;
|
|
|
|
assert_eq!(*y, 5);
|
|
|
|
}
|
|
|
|
|
2013-04-05 18:51:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_managed_mut_clone() {
|
2013-05-15 20:01:14 -05:00
|
|
|
let a = @mut 5i;
|
2013-04-06 19:35:11 -05:00
|
|
|
let b: @mut int = a.clone();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, b);
|
2013-04-06 19:35:11 -05:00
|
|
|
*b = 10;
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(a, b);
|
2013-04-05 18:51:43 -05:00
|
|
|
}
|
2013-05-15 11:23:19 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_clone() {
|
|
|
|
let x = 5i;
|
|
|
|
let y: &int = &x;
|
|
|
|
let z: &int = (&y).clone();
|
|
|
|
assert_eq!(*z, 5);
|
|
|
|
}
|