]> git.lizzy.rs Git - rust.git/blob - src/libcore/clone.rs
/*! -> //!
[rust.git] / src / libcore / clone.rs
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 #![unstable]
23
24 use kinds::Sized;
25
26 /// A common trait for cloning an object.
27 pub trait Clone {
28     /// Returns a copy of the value.
29     fn clone(&self) -> Self;
30
31     /// Perform copy-assignment from `source`.
32     ///
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
35     /// allocations.
36     #[inline(always)]
37     #[experimental = "this function is mostly unused"]
38     fn clone_from(&mut self, source: &Self) {
39         *self = source.clone()
40     }
41 }
42
43 impl<'a, Sized? T> Clone for &'a T {
44     /// Return a shallow copy of the reference.
45     #[inline]
46     fn clone(&self) -> &'a T { *self }
47 }
48
49 macro_rules! clone_impl(
50     ($t:ty) => {
51         impl Clone for $t {
52             /// Return a deep copy of the value.
53             #[inline]
54             fn clone(&self) -> $t { *self }
55         }
56     }
57 )
58
59 clone_impl!(int)
60 clone_impl!(i8)
61 clone_impl!(i16)
62 clone_impl!(i32)
63 clone_impl!(i64)
64
65 clone_impl!(uint)
66 clone_impl!(u8)
67 clone_impl!(u16)
68 clone_impl!(u32)
69 clone_impl!(u64)
70
71 clone_impl!(f32)
72 clone_impl!(f64)
73
74 clone_impl!(())
75 clone_impl!(bool)
76 clone_impl!(char)
77
78 macro_rules! extern_fn_clone(
79     ($($A:ident),*) => (
80         #[experimental = "this may not be sufficient for fns with region parameters"]
81         impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
82             /// Return a copy of a function pointer
83             #[inline]
84             fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
85         }
86     )
87 )
88
89 extern_fn_clone!()
90 extern_fn_clone!(A)
91 extern_fn_clone!(A, B)
92 extern_fn_clone!(A, B, C)
93 extern_fn_clone!(A, B, C, D)
94 extern_fn_clone!(A, B, C, D, E)
95 extern_fn_clone!(A, B, C, D, E, F)
96 extern_fn_clone!(A, B, C, D, E, F, G)
97 extern_fn_clone!(A, B, C, D, E, F, G, H)
98