1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /*! Task-local garbage-collected boxes
13 The `Gc` type provides shared ownership of an immutable value. Destruction is not deterministic, and
14 will occur some time between every `Gc` handle being gone and the end of the task. The garbage
15 collector is task-local so `Gc<T>` is not sendable.
19 #![allow(experimental)]
22 use cmp::{Ord, PartialOrd, Ordering, Eq, PartialEq};
30 /// Immutable garbage-collected pointer type
32 #[experimental = "Gc is currently based on reference-counting and will not collect cycles until \
33 task annihilation. For now, cycles need to be broken manually by using `Rc<T>` \
34 with a non-owning `Weak<T>` pointer. A tracing garbage collector is planned."]
37 marker: marker::NoSend,
40 impl<T> Clone for Gc<T> {
41 /// Clone the pointer only
43 fn clone(&self) -> Gc<T> { *self }
46 /// An value that represents the task-local managed heap.
48 /// Use this like `let foo = box(GC) Bar::new(...);`
49 #[lang="managed_heap"]
51 pub static GC: () = ();
53 impl<T: PartialEq + 'static> PartialEq for Gc<T> {
55 fn eq(&self, other: &Gc<T>) -> bool { *(*self) == *(*other) }
57 fn ne(&self, other: &Gc<T>) -> bool { *(*self) != *(*other) }
59 impl<T: PartialOrd + 'static> PartialOrd for Gc<T> {
61 fn lt(&self, other: &Gc<T>) -> bool { *(*self) < *(*other) }
63 fn le(&self, other: &Gc<T>) -> bool { *(*self) <= *(*other) }
65 fn ge(&self, other: &Gc<T>) -> bool { *(*self) >= *(*other) }
67 fn gt(&self, other: &Gc<T>) -> bool { *(*self) > *(*other) }
69 impl<T: Ord + 'static> Ord for Gc<T> {
71 fn cmp(&self, other: &Gc<T>) -> Ordering { (**self).cmp(&**other) }
73 impl<T: Eq + 'static> Eq for Gc<T> {}
75 impl<T: 'static> Deref<T> for Gc<T> {
76 fn deref<'a>(&'a self) -> &'a T { &**self }
79 impl<T: Default + 'static> Default for Gc<T> {
80 fn default() -> Gc<T> {
81 box(GC) Default::default()
85 impl<T: 'static> raw::Repr<*raw::Box<T>> for Gc<T> {}
87 impl<S: hash::Writer, T: hash::Hash<S> + 'static> hash::Hash<S> for Gc<T> {
88 fn hash(&self, s: &mut S) {
93 impl<T: 'static + fmt::Show> fmt::Show for Gc<T> {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107 let x = Gc::new(RefCell::new(5));
109 *x.borrow().borrow_mut() = 20;
110 assert_eq!(*y.borrow().borrow(), 20);
116 assert_eq!(*x.borrow(), 5);
120 fn test_simple_clone() {
123 assert_eq!(*x.borrow(), 5);
124 assert_eq!(*y.borrow(), 5);
132 assert!(x.ptr_eq(&x));
133 assert!(x.ptr_eq(&y));
134 assert!(!x.ptr_eq(&z));
138 fn test_destructor() {
139 let x = Gc::new(box 5);
140 assert_eq!(**x.borrow(), 5);