1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
use ::std::cell::{Cell, Ref, RefCell, RefMut};
use ::std::ops::{Deref, DerefMut};
use super::trace::Trace;

/// A `RefCell` wrapper compatible with garbage-collection.
#[derive(Debug)]
pub struct GcRefCell<T: Trace> {
  rooted: Cell<bool>,
  ref_cell: RefCell<T>,
}

impl<T: Trace> GcRefCell<T> {
  pub fn new(value: T) -> GcRefCell<T> {
    GcRefCell {
      rooted: Cell::new(true),
      ref_cell: RefCell::new(value),
    }
  }

  pub fn borrow(&self) -> GcRef<T> {
    GcRef { _ref: self.ref_cell.borrow() }
  }

  pub fn borrow_mut(&self) -> GcRefMut<T> {
    // Root the content of the cell for the duration of the mutable borrow, this will be restored
    // once `GcRefMut` is dropped.
    if !self.rooted.get() {
      unsafe { self.ref_cell.borrow().root(); }
    }
    GcRefMut { rooted: &self.rooted, _ref: self.ref_cell.borrow_mut() }
  }
}

unsafe impl<T: Trace> Trace for GcRefCell<T> {
  unsafe fn mark(&self) {
    // If we can't borrow, it means that there is an active RefMut and the value is rooted
    // (no need to trace)
    match self.ref_cell.try_borrow() {
      Ok(ref value) => value.mark(),
      Err(_) => (),
    }
  }

  unsafe fn root(&self) {
    assert!(!self.rooted.get());
    self.rooted.set(true);
    match self.ref_cell.try_borrow() {
      Ok(ref value) => value.root(),
      Err(_) => (),
    }
  }

  unsafe fn unroot(&self) {
    assert!(self.rooted.get());
    self.rooted.set(false);
    match self.ref_cell.try_borrow() {
      Ok(ref value) => value.unroot(),
      Err(_) => (),
    }
  }
}

pub struct GcRef<'a, T: Trace + 'a> {
  _ref: Ref<'a, T>,
}

impl<'a, T: Trace + 'a> Deref for GcRef<'a, T> {
  type Target = T;

  fn deref(&self) -> &T {
    self._ref.deref()
  }
}

pub struct GcRefMut<'a, T: Trace + 'a> {
  rooted: &'a Cell<bool>,
  _ref: RefMut<'a, T>,
}

impl<'a, T: Trace + 'a> Deref for GcRefMut<'a, T> {
  type Target = T;

  fn deref(&self) -> &T {
    self._ref.deref()
  }
}

impl<'a, T: Trace + 'a> DerefMut for GcRefMut<'a, T> {
  fn deref_mut(&mut self) -> &mut T {
    self._ref.deref_mut()
  }
}

impl<'a, T: Trace + 'a> Drop for GcRefMut<'a, T> {
  fn drop(&mut self) {
    // Restore the `rooted state` of the inner value before the call to `borrow_mut`
    if !self.rooted.get() {
      unsafe { self._ref.unroot(); }
    }
  }
}