File tree 3 files changed +24
-15
lines changed
3 files changed +24
-15
lines changed Original file line number Diff line number Diff line change @@ -21,10 +21,17 @@ Similar to a mutable option type, but friendlier.
21
21
*/
22
22
23
23
#[ mutable]
24
+ #[ deriving( Clone ) ]
24
25
pub struct Cell < T > {
25
26
priv value : Option < T >
26
27
}
27
28
29
+ impl < T : DeepClone > DeepClone for Cell < T > {
30
+ fn deep_clone ( & self ) -> Cell < T > {
31
+ Cell { value : self . value . deep_clone ( ) }
32
+ }
33
+ }
34
+
28
35
impl < T : cmp:: Eq > cmp:: Eq for Cell < T > {
29
36
fn eq ( & self , other : & Cell < T > ) -> bool {
30
37
( self . value ) == ( other. value )
Original file line number Diff line number Diff line change @@ -49,6 +49,7 @@ use num::Zero;
49
49
use old_iter:: { BaseIter , MutableIter , ExtendedIter } ;
50
50
use old_iter;
51
51
use str:: StrSlice ;
52
+ use clone:: DeepClone ;
52
53
53
54
#[ cfg( test) ] use str;
54
55
@@ -59,6 +60,15 @@ pub enum Option<T> {
59
60
Some ( T ) ,
60
61
}
61
62
63
+ impl < T : DeepClone > DeepClone for Option < T > {
64
+ fn deep_clone ( & self ) -> Option < T > {
65
+ match * self {
66
+ Some ( ref x) => Some ( x. deep_clone ( ) ) ,
67
+ None => None
68
+ }
69
+ }
70
+ }
71
+
62
72
impl < T : Ord > Ord for Option < T > {
63
73
fn lt ( & self , other : & Option < T > ) -> bool {
64
74
match ( self , other) {
Original file line number Diff line number Diff line change @@ -103,28 +103,20 @@ mod test_rc {
103
103
fn test_clone( ) {
104
104
let x = Rc :: new ( Cell ( 5 ) ) ;
105
105
let y = x. clone ( ) ;
106
- do x. with_borrow |cell| {
107
- do value. with_mut_ref |inner| {
108
- * inner = 20 ;
109
- }
110
- }
111
- do y. with_borrow |value| {
112
- assert_eq ! ( value. take( ) , 20 ) ;
106
+ do x. borrow ( ) . with_mut_ref |inner| {
107
+ * inner = 20 ;
113
108
}
109
+ assert_eq ! ( y. borrow( ) . take( ) , 20 ) ;
114
110
}
115
111
116
112
#[ test]
117
113
fn test_deep_clone( ) {
118
114
let x = Rc :: new ( Cell ( 5 ) ) ;
119
115
let y = x. deep_clone ( ) ;
120
- do x. with_borrow |cell| {
121
- do value. with_mut_ref |inner| {
122
- * inner = 20 ;
123
- }
124
- }
125
- do y. with_borrow |value| {
126
- assert_eq ! ( value. take( ) , 5 ) ;
116
+ do x. borrow ( ) . with_mut_ref |inner| {
117
+ * inner = 20 ;
127
118
}
119
+ assert_eq ! ( y. borrow( ) . take( ) , 5 ) ;
128
120
}
129
121
130
122
#[ test]
@@ -134,7 +126,7 @@ mod test_rc {
134
126
}
135
127
136
128
#[ test]
137
- fn test_clone ( ) {
129
+ fn test_simple_clone ( ) {
138
130
let x = Rc :: new ( 5 ) ;
139
131
let y = x. clone ( ) ;
140
132
assert_eq ! ( * x. borrow( ) , 5 ) ;
You can’t perform that action at this time.
0 commit comments