]>
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 | ||
54a0048b | 11 | //! The `Clone` trait for types that cannot be 'implicitly copied'. |
1a4d82fc JJ |
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. | |
54a0048b SL |
21 | //! |
22 | //! Basic usage example: | |
23 | //! | |
24 | //! ``` | |
25 | //! let s = String::new(); // String type implements Clone | |
26 | //! let copy = s.clone(); // so we can clone it | |
27 | //! ``` | |
28 | //! | |
29 | //! To easily implement the Clone trait, you can also use | |
30 | //! `#[derive(Clone)]`. Example: | |
31 | //! | |
32 | //! ``` | |
33 | //! #[derive(Clone)] // we add the Clone trait to Morpheus struct | |
34 | //! struct Morpheus { | |
35 | //! blue_pill: f32, | |
36 | //! red_pill: i64, | |
37 | //! } | |
38 | //! | |
39 | //! fn main() { | |
40 | //! let f = Morpheus { blue_pill: 0.0, red_pill: 0 }; | |
41 | //! let copy = f.clone(); // and now we can clone it! | |
42 | //! } | |
43 | //! ``` | |
1a4d82fc | 44 | |
85aaf69f | 45 | #![stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
46 | |
47 | use marker::Sized; | |
48 | ||
49 | /// A common trait for cloning an object. | |
92a42be0 SL |
50 | /// |
51 | /// This trait can be used with `#[derive]`. | |
85aaf69f | 52 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
53 | pub trait Clone : Sized { |
54 | /// Returns a copy of the value. | |
c34b1796 AL |
55 | /// |
56 | /// # Examples | |
57 | /// | |
58 | /// ``` | |
59 | /// let hello = "Hello"; // &str implements Clone | |
60 | /// | |
61 | /// assert_eq!("Hello", hello.clone()); | |
62 | /// ``` | |
85aaf69f | 63 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
64 | fn clone(&self) -> Self; |
65 | ||
9346a6ac | 66 | /// Performs copy-assignment from `source`. |
1a4d82fc JJ |
67 | /// |
68 | /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality, | |
69 | /// but can be overridden to reuse the resources of `a` to avoid unnecessary | |
70 | /// allocations. | |
71 | #[inline(always)] | |
9346a6ac | 72 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
73 | fn clone_from(&mut self, source: &Self) { |
74 | *self = source.clone() | |
75 | } | |
76 | } | |
77 | ||
85aaf69f | 78 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 79 | impl<'a, T: ?Sized> Clone for &'a T { |
9346a6ac | 80 | /// Returns a shallow copy of the reference. |
1a4d82fc JJ |
81 | #[inline] |
82 | fn clone(&self) -> &'a T { *self } | |
83 | } | |
84 | ||
85 | macro_rules! clone_impl { | |
86 | ($t:ty) => { | |
85aaf69f | 87 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 88 | impl Clone for $t { |
9346a6ac | 89 | /// Returns a deep copy of the value. |
1a4d82fc JJ |
90 | #[inline] |
91 | fn clone(&self) -> $t { *self } | |
92 | } | |
93 | } | |
94 | } | |
95 | ||
85aaf69f | 96 | clone_impl! { isize } |
1a4d82fc JJ |
97 | clone_impl! { i8 } |
98 | clone_impl! { i16 } | |
99 | clone_impl! { i32 } | |
100 | clone_impl! { i64 } | |
101 | ||
85aaf69f | 102 | clone_impl! { usize } |
1a4d82fc JJ |
103 | clone_impl! { u8 } |
104 | clone_impl! { u16 } | |
105 | clone_impl! { u32 } | |
106 | clone_impl! { u64 } | |
107 | ||
108 | clone_impl! { f32 } | |
109 | clone_impl! { f64 } | |
110 | ||
111 | clone_impl! { () } | |
112 | clone_impl! { bool } | |
113 | clone_impl! { char } |