Skip to content

Commit f6963e2

Browse files
committed
auto merge of #11369 : kvark/rust/master, r=huonw
-Fixed Gc::clone to not require a Clone implementation for T -Implemented Gc::ptr_eq to match Rc::ptr_eq, added a test for that
2 parents 7613b15 + 5da1663 commit f6963e2

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/libstd/gc.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ collector is task-local so `Gc<T>` is not sendable.
1818

1919
use kinds::Send;
2020
use clone::{Clone, DeepClone};
21+
use managed;
2122

2223
/// Immutable garbage-collected pointer type
2324
#[no_send]
24-
#[deriving(Clone)]
2525
pub struct Gc<T> {
2626
priv ptr: @T
2727
}
@@ -32,14 +32,26 @@ impl<T: 'static> Gc<T> {
3232
pub fn new(value: T) -> Gc<T> {
3333
Gc { ptr: @value }
3434
}
35-
}
3635

37-
impl<T: 'static> Gc<T> {
3836
/// Borrow the value contained in the garbage-collected box
3937
#[inline]
4038
pub fn borrow<'r>(&'r self) -> &'r T {
4139
&*self.ptr
4240
}
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+
}
4355
}
4456

4557
/// The `Send` bound restricts this to acyclic graphs where it is well-defined.
@@ -92,6 +104,16 @@ mod tests {
92104
assert_eq!(*y.borrow(), 5);
93105
}
94106

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+
95117
#[test]
96118
fn test_destructor() {
97119
let x = Gc::new(~5);

0 commit comments

Comments
 (0)