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
20 //! the `clone` method.
22 #![stable(feature = "rust1", since = "1.0.0")]
26 /// A common trait for cloning an object.
27 #[stable(feature = "rust1", since = "1.0.0")]
28 pub trait Clone : Sized {
29 /// Returns a copy of the value.
34 /// let hello = "Hello"; // &str implements Clone
36 /// assert_eq!("Hello", hello.clone());
38 #[stable(feature = "rust1", since = "1.0.0")]
39 fn clone(&self) -> Self;
41 /// Performs copy-assignment from `source`.
43 /// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
44 /// but can be overridden to reuse the resources of `a` to avoid unnecessary
47 #[stable(feature = "rust1", since = "1.0.0")]
48 fn clone_from(&mut self, source: &Self) {
49 *self = source.clone()
53 #[stable(feature = "rust1", since = "1.0.0")]
54 impl<'a, T: ?Sized> Clone for &'a T {
55 /// Returns a shallow copy of the reference.
57 fn clone(&self) -> &'a T { *self }
60 macro_rules! clone_impl {
62 #[stable(feature = "rust1", since = "1.0.0")]
64 /// Returns a deep copy of the value.
66 fn clone(&self) -> $t { *self }
90 macro_rules! extern_fn_clone {
92 #[unstable(feature = "core",
93 reason = "this may not be sufficient for fns with region parameters")]
94 impl<$($A,)* ReturnType> Clone for extern "Rust" fn($($A),*) -> ReturnType {
95 /// Returns a copy of a function pointer.
97 fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
100 #[unstable(feature = "core", reason = "brand new")]
101 impl<$($A,)* ReturnType> Clone for extern "C" fn($($A),*) -> ReturnType {
102 /// Returns a copy of a function pointer.
104 fn clone(&self) -> extern "C" fn($($A),*) -> ReturnType { *self }
107 #[unstable(feature = "core", reason = "brand new")]
108 impl<$($A,)* ReturnType> Clone for unsafe extern "Rust" fn($($A),*) -> ReturnType {
109 /// Returns a copy of a function pointer.
111 fn clone(&self) -> unsafe extern "Rust" fn($($A),*) -> ReturnType { *self }
114 #[unstable(feature = "core", reason = "brand new")]
115 impl<$($A,)* ReturnType> Clone for unsafe extern "C" fn($($A),*) -> ReturnType {
116 /// Returns a copy of a function pointer.
118 fn clone(&self) -> unsafe extern "C" fn($($A),*) -> ReturnType { *self }
124 extern_fn_clone! { A }
125 extern_fn_clone! { A, B }
126 extern_fn_clone! { A, B, C }
127 extern_fn_clone! { A, B, C, D }
128 extern_fn_clone! { A, B, C, D, E }
129 extern_fn_clone! { A, B, C, D, E, F }
130 extern_fn_clone! { A, B, C, D, E, F, G }
131 extern_fn_clone! { A, B, C, D, E, F, G, H }