-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.rs
58 lines (50 loc) · 1.15 KB
/
example.rs
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
#![feature(arbitrary_self_types)]
use elise::{Finalize, Gc, GcStore, HeapRoot, GC};
#[derive(GC)]
#[gc(finalize)]
struct Foo<'root> {
int: u64,
#[gc]
bar: GcStore<'root, Bar<'root>>,
}
impl<'root> Foo<'root> {
pub fn new(int: u64, data: String) -> Foo<'root> {
Foo {
int: int,
bar: GcStore::new(Bar::new(data)),
}
}
pub fn gc_method(self: Gc<'root, Foo<'root>>, x: u64) -> u64 {
let bar: Gc<Bar> = self.bar();
let data: Gc<String> = bar.data();
println!("{}", &*data);
self.int + x
}
}
impl<'root> Finalize for Foo<'root> {
fn finalize(&mut self) {
println!("Dropping ({})", self.int);
}
}
#[derive(GC)]
struct Bar<'root> {
#[gc]
data: GcStore<'root, String>,
}
impl<'root> Bar<'root> {
pub fn new(data: String) -> Bar<'root> {
Bar {
data: GcStore::new(data),
}
}
}
fn main() {
{
let foo = Foo::new(2, String::from("Hello, world!"));
let root = HeapRoot::new(foo);
let foo = root.gc();
elise::collect();
println!("{}", foo.gc_method(2));
}
elise::collect();
}