2013-11-19 22:20:06 -06:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/*! Task-local garbage-collected boxes
|
|
|
|
|
|
|
|
The `Gc` type provides shared ownership of an immutable value. Destruction is not deterministic, and
|
|
|
|
will occur some time between every `Gc` handle being gone and the end of the task. The garbage
|
|
|
|
collector is task-local so `Gc<T>` is not sendable.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-01-14 04:17:19 -06:00
|
|
|
#[allow(experimental)];
|
|
|
|
|
2014-01-22 13:03:02 -06:00
|
|
|
use kinds::marker;
|
2013-11-19 22:20:06 -06:00
|
|
|
use kinds::Send;
|
|
|
|
use clone::{Clone, DeepClone};
|
2014-01-07 19:21:04 -06:00
|
|
|
use managed;
|
2013-11-19 22:20:06 -06:00
|
|
|
|
|
|
|
/// Immutable garbage-collected pointer type
|
2013-12-17 18:46:18 -06:00
|
|
|
#[lang="gc"]
|
|
|
|
#[cfg(not(test))]
|
2014-01-14 04:17:19 -06:00
|
|
|
#[experimental = "Gc is currently based on reference-counting and will not collect cycles until \
|
|
|
|
task annihilation. For now, cycles need to be broken manually by using `Rc<T>` \
|
|
|
|
with a non-owning `Weak<T>` pointer. A tracing garbage collector is planned."]
|
2013-12-17 18:46:18 -06:00
|
|
|
pub struct Gc<T> {
|
2014-01-22 13:03:02 -06:00
|
|
|
priv ptr: @T,
|
|
|
|
priv marker: marker::NoSend,
|
2013-12-17 18:46:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-11-19 22:20:06 -06:00
|
|
|
pub struct Gc<T> {
|
2014-01-22 13:03:02 -06:00
|
|
|
priv ptr: @T,
|
|
|
|
priv marker: marker::NoSend,
|
2013-11-19 22:20:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: 'static> Gc<T> {
|
|
|
|
/// Construct a new garbage-collected box
|
|
|
|
#[inline]
|
|
|
|
pub fn new(value: T) -> Gc<T> {
|
2014-01-22 13:03:02 -06:00
|
|
|
Gc { ptr: @value, marker: marker::NoSend }
|
2013-11-19 22:20:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Borrow the value contained in the garbage-collected box
|
|
|
|
#[inline]
|
|
|
|
pub fn borrow<'r>(&'r self) -> &'r T {
|
|
|
|
&*self.ptr
|
|
|
|
}
|
2014-01-07 19:21:04 -06:00
|
|
|
|
|
|
|
/// 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> {
|
2014-01-22 13:03:02 -06:00
|
|
|
Gc{ ptr: self.ptr, marker: marker::NoSend }
|
2014-01-07 19:21:04 -06:00
|
|
|
}
|
2013-11-19 22:20:06 -06:00
|
|
|
}
|
|
|
|
|
2013-12-17 18:46:18 -06:00
|
|
|
/// An value that represents the task-local managed heap.
|
|
|
|
///
|
|
|
|
/// Use this like `let foo = box(GC) Bar::new(...);`
|
|
|
|
#[lang="managed_heap"]
|
|
|
|
#[cfg(not(test))]
|
|
|
|
pub static GC: () = ();
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub static GC: () = ();
|
|
|
|
|
2013-11-19 22:20:06 -06:00
|
|
|
/// The `Send` bound restricts this to acyclic graphs where it is well-defined.
|
|
|
|
///
|
|
|
|
/// A `Freeze` bound would also work, but `Send` *or* `Freeze` cannot be expressed.
|
|
|
|
impl<T: DeepClone + Send + 'static> DeepClone for Gc<T> {
|
|
|
|
#[inline]
|
|
|
|
fn deep_clone(&self) -> Gc<T> {
|
|
|
|
Gc::new(self.borrow().deep_clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-01-07 00:33:37 -06:00
|
|
|
use prelude::*;
|
2013-11-19 22:20:06 -06:00
|
|
|
use super::*;
|
2013-11-21 23:30:34 -06:00
|
|
|
use cell::RefCell;
|
2013-11-19 22:20:06 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone() {
|
2013-11-21 23:30:34 -06:00
|
|
|
let x = Gc::new(RefCell::new(5));
|
2013-11-19 22:20:06 -06:00
|
|
|
let y = x.clone();
|
2013-11-22 16:15:32 -06:00
|
|
|
x.borrow().with_mut(|inner| {
|
2013-11-19 22:20:06 -06:00
|
|
|
*inner = 20;
|
2013-11-22 16:15:32 -06:00
|
|
|
});
|
2013-11-21 23:30:34 -06:00
|
|
|
assert_eq!(y.borrow().with(|x| *x), 20);
|
2013-11-19 22:20:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_deep_clone() {
|
2013-11-21 23:30:34 -06:00
|
|
|
let x = Gc::new(RefCell::new(5));
|
2013-11-19 22:20:06 -06:00
|
|
|
let y = x.deep_clone();
|
2013-11-22 16:15:32 -06:00
|
|
|
x.borrow().with_mut(|inner| {
|
2013-11-19 22:20:06 -06:00
|
|
|
*inner = 20;
|
2013-11-22 16:15:32 -06:00
|
|
|
});
|
2013-11-21 23:30:34 -06:00
|
|
|
assert_eq!(y.borrow().with(|x| *x), 5);
|
2013-11-19 22:20:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let x = Gc::new(5);
|
|
|
|
assert_eq!(*x.borrow(), 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple_clone() {
|
|
|
|
let x = Gc::new(5);
|
|
|
|
let y = x.clone();
|
|
|
|
assert_eq!(*x.borrow(), 5);
|
|
|
|
assert_eq!(*y.borrow(), 5);
|
|
|
|
}
|
|
|
|
|
2014-01-07 19:21:04 -06:00
|
|
|
#[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));
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:20:06 -06:00
|
|
|
#[test]
|
|
|
|
fn test_destructor() {
|
|
|
|
let x = Gc::new(~5);
|
|
|
|
assert_eq!(**x.borrow(), 5);
|
|
|
|
}
|
|
|
|
}
|