File tree 1 file changed +25
-3
lines changed
1 file changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -18,10 +18,10 @@ collector is task-local so `Gc<T>` is not sendable.
18
18
19
19
use kinds:: Send ;
20
20
use clone:: { Clone , DeepClone } ;
21
+ use managed;
21
22
22
23
/// Immutable garbage-collected pointer type
23
24
#[ no_send]
24
- #[ deriving( Clone ) ]
25
25
pub struct Gc < T > {
26
26
priv ptr: @T
27
27
}
@@ -32,14 +32,26 @@ impl<T: 'static> Gc<T> {
32
32
pub fn new ( value : T ) -> Gc < T > {
33
33
Gc { ptr : @value }
34
34
}
35
- }
36
35
37
- impl < T : ' static > Gc < T > {
38
36
/// Borrow the value contained in the garbage-collected box
39
37
#[ inline]
40
38
pub fn borrow < ' r > ( & ' r self ) -> & ' r T {
41
39
& * self . ptr
42
40
}
41
+
42
+ /// Determine if two garbage-collected boxes point to the same object
43
+ #[ inline]
44
+ pub fn ptr_eq ( & self , other : & Gc < T > ) -> bool {
45
+ managed:: ptr_eq ( self . ptr , other. ptr )
46
+ }
47
+ }
48
+
49
+ impl < T > Clone for Gc < T > {
50
+ /// Clone the pointer only
51
+ #[ inline]
52
+ fn clone ( & self ) -> Gc < T > {
53
+ Gc { ptr : self . ptr }
54
+ }
43
55
}
44
56
45
57
/// The `Send` bound restricts this to acyclic graphs where it is well-defined.
@@ -92,6 +104,16 @@ mod tests {
92
104
assert_eq ! ( * y. borrow( ) , 5 ) ;
93
105
}
94
106
107
+ #[ test]
108
+ fn test_ptr_eq ( ) {
109
+ let x = Gc :: new ( 5 ) ;
110
+ let y = x. clone ( ) ;
111
+ let z = Gc :: new ( 7 ) ;
112
+ assert ! ( x. ptr_eq( & x) ) ;
113
+ assert ! ( x. ptr_eq( & y) ) ;
114
+ assert ! ( !x. ptr_eq( & z) ) ;
115
+ }
116
+
95
117
#[ test]
96
118
fn test_destructor ( ) {
97
119
let x = Gc :: new ( ~5 ) ;
You can’t perform that action at this time.
0 commit comments