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
59
60
61
62
63
64
65
66
67
use std::{
    collections::HashSet,
    hash::{Hash, Hasher},
    sync::Mutex,
};
#[macro_use]
extern crate lazy_static;

pub trait MarkSweep: Send {
    fn is_root(&self) -> bool;
    fn trace(&self, tracer: Box<dyn Fn(&Box<dyn MarkSweep>)>);
    fn unsafe_destory(&self); // should be written in safe rust
    fn downgrade(&self) -> Box<dyn WeakMarkSweep>;
    fn address(&self) -> usize;
}

pub fn is_root_changes<T: MarkSweep>(x: &T) {
    panic!("TODO")
}

pub fn init<T: MarkSweep>(x: &T) {
    panic!("TODO")
}

pub trait WeakMarkSweep: Send {
    fn upgrade(&self) -> Option<Box<dyn MarkSweep>>;
}

struct Element {
    val: Box<dyn WeakMarkSweep>,
    marked: bool,
}
impl Hash for Element {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.marked.hash(state);
        let address: usize;
        if let Some(x) = self.val.upgrade() {
            address = x.address();
        } else {
            address = 0;
        }
        address.hash(state);
    }
}
impl PartialEq for Element {
    fn eq(&self, other: &Self) -> bool {
        let slf: usize;
        if let Some(x) = self.val.upgrade() {
            slf = x.address();
        } else {
            slf = 0;
        }
        let othr: usize;
        if let Some(x) = other.val.upgrade() {
            othr = x.address();
        } else {
            othr = 0;
        }
        slf == othr && self.marked == other.marked
    }
}
impl Eq for Element {}

lazy_static! {
    static ref root_set: Mutex<HashSet<Element>> = Mutex::new(HashSet::new());
    static ref not_root_set: Mutex<HashSet<Element>> = Mutex::new(HashSet::new());
}