Skip to content

Commit

Permalink
Temporarially mark some unsafe code safe for the compiler plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mystor committed May 17, 2015
1 parent 56fee4f commit 8b7dca2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
2 changes: 2 additions & 0 deletions gc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
40 changes: 18 additions & 22 deletions gc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,27 @@ impl<T: Trace> Trace for Gc<T> {
self.inner().trace_inner();
}

unsafe fn root(&self) {
fn root(&self) {
assert!(!self.root.get(), "Can't double-root a Gc<T>");
self.root.set(true);
self.inner().root_inner();
unsafe {
self.inner().root_inner();
}
}

unsafe fn unroot(&self) {
fn unroot(&self) {
assert!(self.root.get(), "Can't double-unroot a Gc<T>");
self.root.set(false);
self.inner().unroot_inner();
unsafe {
self.inner().unroot_inner();
}
}
}

impl<T: Trace> Clone for Gc<T> {
fn clone(&self) -> Gc<T> {
unsafe {
self.root();
Gc { _ptr: self._ptr, root: Cell::new(true) }
}
self.root();
Gc { _ptr: self._ptr, root: Cell::new(true) }
}
}

Expand All @@ -91,9 +93,7 @@ impl<T: Trace> Drop for Gc<T> {
// If we are being collected by the garbage collector (we are
// not a root), then our reference may not be valid anymore due to cycles
if self.root.get() {
unsafe {
self.unroot();
}
self.unroot();
}
}
}
Expand Down Expand Up @@ -125,10 +125,8 @@ impl <T: Trace> GcCell<T> {
pub fn borrow_mut(&self) -> GcCellRefMut<T> {
let val_ref = self.cell.borrow_mut();

unsafe {
// Root everything inside the box for the lifetime of the GcCellRefMut
val_ref.root();
}
// Root everything inside the box for the lifetime of the GcCellRefMut
val_ref.root();

GcCellRefMut {
_ref: val_ref,
Expand All @@ -146,12 +144,12 @@ impl<T: Trace> Trace for GcCell<T> {
}
}

unsafe fn root(&self) {
fn root(&self) {
// XXX: Maybe handle this better than panicking? (can we just dodge the check?)
self.cell.borrow().root();
}

unsafe fn unroot(&self) {
fn unroot(&self) {
// XXX: Maybe handle this better than panicking? (can we just dodge the check?)
self.cell.borrow().unroot();
}
Expand Down Expand Up @@ -187,10 +185,8 @@ impl<'a, T: Trace> DerefMut for GcCellRefMut<'a, T> {

impl<'a, T: Trace> Drop for GcCellRefMut<'a, T> {
fn drop(&mut self) {
unsafe {
// The data is now within a Gc tree again
// we don't have to keep it alive explicitly any longer
self._ref.unroot();
}
// the data is now within a gc tree again
// we don't have to keep it alive explicitly any longer
self._ref.unroot();
}
}
20 changes: 9 additions & 11 deletions gc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@ pub trait Trace {
/// Mark all contained Gcs
fn trace(&self);
/// Increment the root-count of all contained Gcs
unsafe fn root(&self);
fn root(&self);
/// Decrement the root-count of all contained Gcs
unsafe fn unroot(&self);
fn unroot(&self);
}

/*
impl<'a, T> Trace for &'a T {
impl<T> Trace for &'static T {
fn trace(&self) {}
unsafe fn root(&self) {}
unsafe fn unroot(&self) {}
fn root(&self) {}
fn unroot(&self) {}
}
*/

impl<'a, T: Trace> Trace for Box<T> {
fn trace(&self) {
(**self).trace();
}

unsafe fn root(&self) {
fn root(&self) {
(**self).root();
}

unsafe fn unroot(&self) {
fn unroot(&self) {
(**self).unroot();
}
}
Expand All @@ -37,13 +35,13 @@ impl<'a, T: Trace> Trace for Vec<T> {
}
}

unsafe fn root(&self) {
fn root(&self) {
for e in self {
e.root();
}
}

unsafe fn unroot(&self) {
fn unroot(&self) {
for e in self {
e.unroot();
}
Expand Down

0 comments on commit 8b7dca2

Please sign in to comment.