Last active
May 24, 2020 12:39
-
-
Save redradist/9e2391ca4b56a1eb251cca7d2785be02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use ferris_gc::{Trace, Finalize, ferris_gc_main, ApplicationCleanup}; | |
use ferris_gc::sync::{Gc, GcRefCell}; | |
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 gc0 = Gc::new(MyStruct { id: 17 }); | |
let gc1 = Gc::new(MyStruct { id: 3 }); | |
thread::spawn(move || { | |
println!("gc1.id is {}", gc1.id); | |
}); | |
} | |
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