1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /*! The `Clone` trait for types that cannot be 'implicitly copied'
13 In Rust, some simple types are "implicitly copyable" and when you
14 assign them or pass them as arguments, the receiver will get a copy,
15 leaving the original value in place. These types do not require
16 allocation to copy and do not have finalizers (i.e. they do not
17 contain owned boxes or implement `Drop`), so the compiler considers
18 them cheap and safe to copy. For other types copies must be made
19 explicitly, by convention implementing the `Clone` trait and calling
24 /// A common trait for cloning an object.
26 /// Returns a copy of the value. The contents of owned pointers
27 /// are copied to maintain uniqueness, while the contents of
28 /// managed pointers are not copied.
29 fn clone(&self) -> Self;
31 /// Perform copy-assignment from `source`.
33 /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
34 /// but can be overridden to reuse the resources of `a` to avoid unnecessary
37 fn clone_from(&mut self, source: &Self) {
38 *self = source.clone()
42 impl<'a, T> Clone for &'a T {
43 /// Return a shallow copy of the reference.
45 fn clone(&self) -> &'a T { *self }
48 impl<'a, T> Clone for &'a [T] {
49 /// Return a shallow copy of the slice.
51 fn clone(&self) -> &'a [T] { *self }
54 impl<'a> Clone for &'a str {
55 /// Return a shallow copy of the slice.
57 fn clone(&self) -> &'a str { *self }
60 macro_rules! clone_impl(
63 /// Return a deep copy of the value.
65 fn clone(&self) -> $t { *self }
89 macro_rules! extern_fn_clone(
91 impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
92 /// Return a copy of a function pointer
94 fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
101 extern_fn_clone!(A, B)
102 extern_fn_clone!(A, B, C)
103 extern_fn_clone!(A, B, C, D)
104 extern_fn_clone!(A, B, C, D, E)
105 extern_fn_clone!(A, B, C, D, E, F)
106 extern_fn_clone!(A, B, C, D, E, F, G)
107 extern_fn_clone!(A, B, C, D, E, F, G, H)
112 use realstd::owned::Box;
113 use realstd::gc::{Gc, GC};
115 fn realclone<T: ::realstd::clone::Clone>(t: &T) -> T {
116 use realstd::clone::Clone;
120 fn realclone_from<T: ::realstd::clone::Clone>(t1: &mut T, t2: &T) {
121 use realstd::clone::Clone;
126 fn test_owned_clone() {
128 let b: Box<int> = realclone(&a);
133 fn test_managed_clone() {
135 let b: Gc<int> = realclone(&a);
140 fn test_borrowed_clone() {
143 let z: &int = (&y).clone();
148 fn test_clone_from() {
151 realclone_from(&mut b, &a);
156 fn test_extern_fn_clone() {
158 impl Empty for int {}
160 fn test_fn_a() -> f64 { 1.0 }
161 fn test_fn_b<T: Empty>(x: T) -> T { x }
162 fn test_fn_c(_: int, _: f64, _: int, _: int, _: int) {}
164 let _ = test_fn_a.clone();
165 let _ = test_fn_b::<int>.clone();
166 let _ = test_fn_c.clone();