Skip to content

Instantly share code, notes, and snippets.

@redradist
Last active May 24, 2020 12:39
Show Gist options
  • Save redradist/c7247368274598e9e89311dc0e046bc2 to your computer and use it in GitHub Desktop.
Save redradist/c7247368274598e9e89311dc0e046bc2 to your computer and use it in GitHub Desktop.
Ferris Gc example
use ferris_gc::{Gc, GcRefCell, Trace, Finalize, ferris_gc_main, ApplicationCleanup};
use std::{time, thread};
use std::fs::File;
use std::io::Write;
#[derive(Trace)]
struct MyStruct {
id: u32,
}
impl Finalize for MyStruct {
fn finalize(&self) {
println!("MyStruct in finalize !!");
let mut file = File::create("foo.txt");
match file {
Ok(mut f) => { f.write_all(b"Hello, world!") },
Err(e) => { Err(e) },
};
}
}
#[derive(Trace, Finalize)]
struct Counter {
count: Gc<u32>,
}
#[ferris_gc_main]
fn main() {
{
let counter = Gc::new(2);
let gc0 = Gc::new(MyStruct { id: 3 });
let gc1 = GcRefCell::new(MyStruct { id: 4 });
gc1.borrow_mut().id = 5;
let gc3 = Gc::new(Counter { count: counter.clone() });
}
let ten_secs = time::Duration::from_secs(5);
thread::sleep(ten_secs);
println!("Hello, GC World !!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment