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
#![feature(box_syntax, integer_atomics)]

#[macro_use]
extern crate lazy_static;
extern crate heapsize;
extern crate serde;

#[cfg(test)]
extern crate serde_json;

mod atom;
mod symbol;

use atom::*;
pub use symbol::Symbol;

use std::collections::HashSet;
use std::sync::RwLock;


lazy_static!{
    static ref SYMBOLS: RwLock<HashSet<Box<AtomString>>> = {
        let mut set = HashSet::new();
        set.insert(box AtomString::empty());
        RwLock::new(set)
    };

    static ref EMPTY_SYMBOL: Symbol = {
        Symbol::wrap(SYMBOLS.read().unwrap().get("").unwrap())
    };
}

fn get_empty_symbol() -> Symbol {
    EMPTY_SYMBOL.clone()
}

fn get_symbol<S: Into<String> + AsRef<str>>(value: S) -> Symbol {
    {
        let set = SYMBOLS.read().unwrap();
        if let Some(s) = set.get(value.as_ref()) {
            return Symbol::new(s);
        }
    }
    {
        let s = box AtomString::new(value);
        let symbol = Symbol::new(&s);
        let mut set = SYMBOLS.write().unwrap();
        set.insert(s);
        symbol
    }
}

fn remove_entry(e: &AtomString) {
    let mut set = SYMBOLS.write().unwrap();
    set.remove(e);
}


pub fn print_symbols() {
    println!("{:#?}", *SYMBOLS.read().unwrap())
}