]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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. | |
4 | // | |
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. | |
10 | ||
11 | //! The `Clone` trait for types that cannot be 'implicitly copied' | |
12 | //! | |
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 | |
20 | //! the `clone` method. | |
21 | ||
22 | #![stable] | |
23 | ||
24 | use marker::Sized; | |
25 | ||
26 | /// A common trait for cloning an object. | |
27 | #[stable] | |
28 | pub trait Clone : Sized { | |
29 | /// Returns a copy of the value. | |
30 | #[stable] | |
31 | fn clone(&self) -> Self; | |
32 | ||
33 | /// Perform copy-assignment from `source`. | |
34 | /// | |
35 | /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality, | |
36 | /// but can be overridden to reuse the resources of `a` to avoid unnecessary | |
37 | /// allocations. | |
38 | #[inline(always)] | |
39 | #[unstable = "this function is rarely used"] | |
40 | fn clone_from(&mut self, source: &Self) { | |
41 | *self = source.clone() | |
42 | } | |
43 | } | |
44 | ||
45 | #[stable] | |
46 | impl<'a, T: ?Sized> Clone for &'a T { | |
47 | /// Return a shallow copy of the reference. | |
48 | #[inline] | |
49 | fn clone(&self) -> &'a T { *self } | |
50 | } | |
51 | ||
52 | macro_rules! clone_impl { | |
53 | ($t:ty) => { | |
54 | #[stable] | |
55 | impl Clone for $t { | |
56 | /// Return a deep copy of the value. | |
57 | #[inline] | |
58 | fn clone(&self) -> $t { *self } | |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | clone_impl! { int } | |
64 | clone_impl! { i8 } | |
65 | clone_impl! { i16 } | |
66 | clone_impl! { i32 } | |
67 | clone_impl! { i64 } | |
68 | ||
69 | clone_impl! { uint } | |
70 | clone_impl! { u8 } | |
71 | clone_impl! { u16 } | |
72 | clone_impl! { u32 } | |
73 | clone_impl! { u64 } | |
74 | ||
75 | clone_impl! { f32 } | |
76 | clone_impl! { f64 } | |
77 | ||
78 | clone_impl! { () } | |
79 | clone_impl! { bool } | |
80 | clone_impl! { char } | |
81 | ||
82 | macro_rules! extern_fn_clone { | |
83 | ($($A:ident),*) => ( | |
84 | #[unstable = "this may not be sufficient for fns with region parameters"] | |
85 | impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType { | |
86 | /// Return a copy of a function pointer | |
87 | #[inline] | |
88 | fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self } | |
89 | } | |
90 | ) | |
91 | } | |
92 | ||
93 | extern_fn_clone! {} | |
94 | extern_fn_clone! { A } | |
95 | extern_fn_clone! { A, B } | |
96 | extern_fn_clone! { A, B, C } | |
97 | extern_fn_clone! { A, B, C, D } | |
98 | extern_fn_clone! { A, B, C, D, E } | |
99 | extern_fn_clone! { A, B, C, D, E, F } | |
100 | extern_fn_clone! { A, B, C, D, E, F, G } | |
101 | extern_fn_clone! { A, B, C, D, E, F, G, H } |