@@ -14,7 +14,7 @@ In Rust, some simple types are "implicitly copyable" and when you
14
14
assign them or pass them as arguments, the receiver will get a copy,
15
15
leaving the original value in place. These types do not require
16
16
allocation to copy and do not have finalizers (i.e. they do not
17
- contain owned pointers or implement `Drop`), so the compiler considers
17
+ contain owned boxes or implement `Drop`), so the compiler considers
18
18
them cheap and safe to copy and automatically implements the `Copy`
19
19
trait for them. For other types copies must be made explicitly,
20
20
by convention implementing the `Clone` trait and calling the
@@ -23,32 +23,38 @@ by convention implementing the `Clone` trait and calling the
23
23
*/
24
24
25
25
pub trait Clone {
26
+ /// Return a deep copy of the owned object tree. Managed boxes are cloned with a shallow copy.
26
27
fn clone ( & self ) -> Self ;
27
28
}
28
29
29
30
impl Clone for ( ) {
31
+ /// Return a copy of the value.
30
32
#[ inline( always) ]
31
33
fn clone ( & self ) -> ( ) { ( ) }
32
34
}
33
35
34
36
impl < T : Clone > Clone for ~T {
37
+ /// Return a deep copy of the owned box.
35
38
#[ inline( always) ]
36
39
fn clone ( & self ) -> ~T { ~( * * self ) . clone ( ) }
37
40
}
38
41
39
- impl < T : Clone > Clone for @T {
42
+ impl < T > Clone for @T {
43
+ /// Return a shallow copy of the managed box.
40
44
#[ inline( always) ]
41
- fn clone ( & self ) -> @T { @ ( * * self ) . clone ( ) }
45
+ fn clone ( & self ) -> @T { * self }
42
46
}
43
47
44
- impl < T : Clone > Clone for @mut T {
48
+ impl < T > Clone for @mut T {
49
+ /// Return a shallow copy of the managed box.
45
50
#[ inline( always) ]
46
- fn clone ( & self ) -> @mut T { @ mut ( * * self ) . clone ( ) }
51
+ fn clone ( & self ) -> @mut T { * self }
47
52
}
48
53
49
54
macro_rules! clone_impl(
50
55
( $t: ty) => {
51
56
impl Clone for $t {
57
+ /// Return a copy of the value.
52
58
#[ inline( always) ]
53
59
fn clone( & self ) -> $t { * self }
54
60
}
@@ -76,21 +82,23 @@ clone_impl!(char)
76
82
77
83
#[ test]
78
84
fn test_owned_clone ( ) {
79
- let a : ~int = ~5 i;
80
- let b : ~int = a. clone ( ) ;
85
+ let a: ~int = ~5 i;
86
+ let b: ~int = a. clone ( ) ;
81
87
assert ! ( a == b) ;
82
88
}
83
89
84
90
#[ test]
85
91
fn test_managed_clone ( ) {
86
- let a : @int = @5 i;
87
- let b : @int = a. clone ( ) ;
92
+ let a: @int = @5 i;
93
+ let b: @int = a. clone ( ) ;
88
94
assert ! ( a == b) ;
89
95
}
90
96
91
97
#[ test]
92
98
fn test_managed_mut_clone ( ) {
93
- let a : @int = @5 i;
94
- let b : @int = a. clone ( ) ;
99
+ let a: @mut int = @mut 5 i;
100
+ let b: @mut int = a. clone ( ) ;
101
+ assert ! ( a == b) ;
102
+ * b = 10 ;
95
103
assert ! ( a == b) ;
96
104
}
0 commit comments