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.
20 #![allow(experimental)]
23 use cmp::{Ord, PartialOrd, Ordering, Eq, PartialEq};
32 /// Immutable garbage-collected pointer type
34 #[experimental = "Gc is currently based on reference-counting and will not collect cycles until \
35 task annihilation. For now, cycles need to be broken manually by using `Rc<T>` \
36 with a non-owning `Weak<T>` pointer. A tracing garbage collector is planned."]
39 marker: marker::NoSend,
43 impl<T> Clone for Gc<T> {
44 /// Clone the pointer only
46 fn clone(&self) -> Gc<T> { *self }
49 /// An value that represents the task-local managed heap.
51 /// Use this like `let foo = box(GC) Bar::new(...);`
52 #[lang="managed_heap"]
54 pub static GC: () = ();
56 impl<T: PartialEq + 'static> PartialEq for Gc<T> {
58 fn eq(&self, other: &Gc<T>) -> bool { *(*self) == *(*other) }
60 fn ne(&self, other: &Gc<T>) -> bool { *(*self) != *(*other) }
62 impl<T: PartialOrd + 'static> PartialOrd for Gc<T> {
64 fn partial_cmp(&self, other: &Gc<T>) -> Option<Ordering> {
65 (**self).partial_cmp(&**other)
68 fn lt(&self, other: &Gc<T>) -> bool { *(*self) < *(*other) }
70 fn le(&self, other: &Gc<T>) -> bool { *(*self) <= *(*other) }
72 fn ge(&self, other: &Gc<T>) -> bool { *(*self) >= *(*other) }
74 fn gt(&self, other: &Gc<T>) -> bool { *(*self) > *(*other) }
76 impl<T: Ord + 'static> Ord for Gc<T> {
78 fn cmp(&self, other: &Gc<T>) -> Ordering { (**self).cmp(&**other) }
80 impl<T: Eq + 'static> Eq for Gc<T> {}
82 impl<T: 'static> Deref<T> for Gc<T> {
83 fn deref<'a>(&'a self) -> &'a T { &**self }
86 impl<T: Default + 'static> Default for Gc<T> {
87 fn default() -> Gc<T> {
88 box(GC) Default::default()
92 impl<T: 'static> raw::Repr<*const raw::Box<T>> for Gc<T> {}
94 impl<S: hash::Writer, T: hash::Hash<S> + 'static> hash::Hash<S> for Gc<T> {
95 fn hash(&self, s: &mut S) {
100 impl<T: 'static + fmt::Show> fmt::Show for Gc<T> {
101 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113 fn test_managed_clone() {
115 let b: Gc<int> = a.clone();
121 let x = Gc::new(RefCell::new(5));
123 *x.borrow().borrow_mut() = 20;
124 assert_eq!(*y.borrow().borrow(), 20);
130 assert_eq!(*x.borrow(), 5);
134 fn test_simple_clone() {
137 assert_eq!(*x.borrow(), 5);
138 assert_eq!(*y.borrow(), 5);
146 assert!(x.ptr_eq(&x));
147 assert!(x.ptr_eq(&y));
148 assert!(!x.ptr_eq(&z));
152 fn test_destructor() {
153 let x = Gc::new(box 5);
154 assert_eq!(**x.borrow(), 5);