]> git.lizzy.rs Git - rust.git/blob - src/libstd/gc.rs
Register new snapshots
[rust.git] / src / libstd / gc.rs
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.
4 //
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.
10
11 /*! Task-local garbage-collected boxes
12
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.
16
17 */
18
19 #![allow(experimental)]
20
21 use clone::Clone;
22 use cmp::{Ord, PartialOrd, Ordering, Eq, PartialEq};
23 use default::Default;
24 use fmt;
25 use hash;
26 use kinds::marker;
27 use ops::Deref;
28 use raw;
29
30 /// Immutable garbage-collected pointer type
31 #[lang="gc"]
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."]
35 pub struct Gc<T> {
36     _ptr: *T,
37     marker: marker::NoSend,
38 }
39
40 impl<T> Clone for Gc<T> {
41     /// Clone the pointer only
42     #[inline]
43     fn clone(&self) -> Gc<T> { *self }
44 }
45
46 /// An value that represents the task-local managed heap.
47 ///
48 /// Use this like `let foo = box(GC) Bar::new(...);`
49 #[lang="managed_heap"]
50 #[cfg(not(test))]
51 pub static GC: () = ();
52
53 impl<T: PartialEq + 'static> PartialEq for Gc<T> {
54     #[inline]
55     fn eq(&self, other: &Gc<T>) -> bool { *(*self) == *(*other) }
56     #[inline]
57     fn ne(&self, other: &Gc<T>) -> bool { *(*self) != *(*other) }
58 }
59 impl<T: PartialOrd + 'static> PartialOrd for Gc<T> {
60     #[inline]
61     fn lt(&self, other: &Gc<T>) -> bool { *(*self) < *(*other) }
62     #[inline]
63     fn le(&self, other: &Gc<T>) -> bool { *(*self) <= *(*other) }
64     #[inline]
65     fn ge(&self, other: &Gc<T>) -> bool { *(*self) >= *(*other) }
66     #[inline]
67     fn gt(&self, other: &Gc<T>) -> bool { *(*self) > *(*other) }
68 }
69 impl<T: Ord + 'static> Ord for Gc<T> {
70     #[inline]
71     fn cmp(&self, other: &Gc<T>) -> Ordering { (**self).cmp(&**other) }
72 }
73 impl<T: Eq + 'static> Eq for Gc<T> {}
74
75 impl<T: 'static> Deref<T> for Gc<T> {
76     fn deref<'a>(&'a self) -> &'a T { &**self }
77 }
78
79 impl<T: Default + 'static> Default for Gc<T> {
80     fn default() -> Gc<T> {
81         box(GC) Default::default()
82     }
83 }
84
85 impl<T: 'static> raw::Repr<*raw::Box<T>> for Gc<T> {}
86
87 impl<S: hash::Writer, T: hash::Hash<S> + 'static> hash::Hash<S> for Gc<T> {
88     fn hash(&self, s: &mut S) {
89         (**self).hash(s)
90     }
91 }
92
93 impl<T: 'static + fmt::Show> fmt::Show for Gc<T> {
94     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95         (**self).fmt(f)
96     }
97 }
98
99 #[cfg(test)]
100 mod tests {
101     use prelude::*;
102     use super::*;
103     use cell::RefCell;
104
105     #[test]
106     fn test_clone() {
107         let x = Gc::new(RefCell::new(5));
108         let y = x.clone();
109         *x.borrow().borrow_mut() = 20;
110         assert_eq!(*y.borrow().borrow(), 20);
111     }
112
113     #[test]
114     fn test_simple() {
115         let x = Gc::new(5);
116         assert_eq!(*x.borrow(), 5);
117     }
118
119     #[test]
120     fn test_simple_clone() {
121         let x = Gc::new(5);
122         let y = x.clone();
123         assert_eq!(*x.borrow(), 5);
124         assert_eq!(*y.borrow(), 5);
125     }
126
127     #[test]
128     fn test_ptr_eq() {
129         let x = Gc::new(5);
130         let y = x.clone();
131         let z = Gc::new(7);
132         assert!(x.ptr_eq(&x));
133         assert!(x.ptr_eq(&y));
134         assert!(!x.ptr_eq(&z));
135     }
136
137     #[test]
138     fn test_destructor() {
139         let x = Gc::new(box 5);
140         assert_eq!(**x.borrow(), 5);
141     }
142 }