Skip to content

Commit

Permalink
Fixed Gc::clone, implemented Gc::ptr_eq
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 8, 2014
1 parent 7613b15 commit 5da1663
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/libstd/gc.rs
Expand Up @@ -18,10 +18,10 @@ collector is task-local so `Gc<T>` is not sendable.

use kinds::Send;
use clone::{Clone, DeepClone};
use managed;

/// Immutable garbage-collected pointer type
#[no_send]
#[deriving(Clone)]
pub struct Gc<T> {
priv ptr: @T
}
Expand All @@ -32,14 +32,26 @@ impl<T: 'static> Gc<T> {
pub fn new(value: T) -> Gc<T> {
Gc { ptr: @value }
}
}

impl<T: 'static> Gc<T> {
/// Borrow the value contained in the garbage-collected box
#[inline]
pub fn borrow<'r>(&'r self) -> &'r T {
&*self.ptr
}

/// Determine if two garbage-collected boxes point to the same object
#[inline]
pub fn ptr_eq(&self, other: &Gc<T>) -> bool {
managed::ptr_eq(self.ptr, other.ptr)
}
}

impl<T> Clone for Gc<T> {
/// Clone the pointer only
#[inline]
fn clone(&self) -> Gc<T> {
Gc{ ptr: self.ptr }
}
}

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

#[test]
fn test_ptr_eq() {
let x = Gc::new(5);
let y = x.clone();
let z = Gc::new(7);
assert!(x.ptr_eq(&x));
assert!(x.ptr_eq(&y));
assert!(!x.ptr_eq(&z));
}

#[test]
fn test_destructor() {
let x = Gc::new(~5);
Expand Down

5 comments on commit 5da1663

@bors
Copy link
Contributor

@bors bors commented on 5da1663 Jan 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at kvark@5da1663

@bors
Copy link
Contributor

@bors bors commented on 5da1663 Jan 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging kvark/rust/master = 5da1663 into auto

@bors
Copy link
Contributor

@bors bors commented on 5da1663 Jan 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kvark/rust/master = 5da1663 merged ok, testing candidate = f6963e2

@bors
Copy link
Contributor

@bors bors commented on 5da1663 Jan 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 5da1663 Jan 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f6963e2

Please sign in to comment.