]> git.proxmox.com Git - rustc.git/blob - src/libcore/clone.rs
Imported Upstream version 1.10.0+dfsg1
[rustc.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 //! 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 //! ```
44
45 #![stable(feature = "rust1", since = "1.0.0")]
46
47 use marker::Sized;
48
49 /// A common trait for cloning an object.
50 ///
51 /// This trait can be used with `#[derive]`.
52 ///
53 /// Types that are `Copy` should have a trivial implementation of `Clone`. More formally:
54 /// if `T: Copy`, `x: T`, and `y: &T`, then `let x = y.clone();` is equivalent to `let x = *y;`.
55 /// Manual implementations should be careful to uphold this invariant; however, unsafe code
56 /// must not rely on it to ensure memory safety.
57 #[stable(feature = "rust1", since = "1.0.0")]
58 pub trait Clone : Sized {
59 /// Returns a copy of the value.
60 ///
61 /// # Examples
62 ///
63 /// ```
64 /// let hello = "Hello"; // &str implements Clone
65 ///
66 /// assert_eq!("Hello", hello.clone());
67 /// ```
68 #[stable(feature = "rust1", since = "1.0.0")]
69 fn clone(&self) -> Self;
70
71 /// Performs copy-assignment from `source`.
72 ///
73 /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
74 /// but can be overridden to reuse the resources of `a` to avoid unnecessary
75 /// allocations.
76 #[inline(always)]
77 #[stable(feature = "rust1", since = "1.0.0")]
78 fn clone_from(&mut self, source: &Self) {
79 *self = source.clone()
80 }
81 }
82
83 // FIXME(aburka): this method is used solely by #[derive] to
84 // assert that every component of a type implements Clone.
85 //
86 // This should never be called by user code.
87 #[doc(hidden)]
88 #[inline(always)]
89 #[unstable(feature = "derive_clone_copy",
90 reason = "deriving hack, should not be public",
91 issue = "0")]
92 pub fn assert_receiver_is_clone<T: Clone + ?Sized>(_: &T) {}
93
94 #[stable(feature = "rust1", since = "1.0.0")]
95 impl<'a, T: ?Sized> Clone for &'a T {
96 /// Returns a shallow copy of the reference.
97 #[inline]
98 fn clone(&self) -> &'a T { *self }
99 }
100
101 macro_rules! clone_impl {
102 ($t:ty) => {
103 #[stable(feature = "rust1", since = "1.0.0")]
104 impl Clone for $t {
105 /// Returns a deep copy of the value.
106 #[inline]
107 fn clone(&self) -> $t { *self }
108 }
109 }
110 }
111
112 clone_impl! { isize }
113 clone_impl! { i8 }
114 clone_impl! { i16 }
115 clone_impl! { i32 }
116 clone_impl! { i64 }
117
118 clone_impl! { usize }
119 clone_impl! { u8 }
120 clone_impl! { u16 }
121 clone_impl! { u32 }
122 clone_impl! { u64 }
123
124 clone_impl! { f32 }
125 clone_impl! { f64 }
126
127 clone_impl! { () }
128 clone_impl! { bool }
129 clone_impl! { char }