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
68
69
70
71
72
73
74
75
76
//! Identification systems
//! 
//! TODO
//! 
use std::sync::{Arc, Mutex};

#[derive(Debug, PartialEq, Eq, Hash)]
#[derive(Clone)] // For now you can clone an ID but not create two objects with the same IDs 
// Bug: TOFIX returning a cloned ID may lead to two objects with the same ID being created
pub struct ID(usize, IDSystem);

impl PartialOrd for ID {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        assert_eq!(self.1, other.1);
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for ID {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        assert_eq!(self.1, other.1);
        self.0.cmp(&other.0)
    }
}

impl std::fmt::Display for ID {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl ID{
    pub fn get_value(&self) -> usize{ self.0 }
    pub fn get_system(&self) -> IDSystem { self.1.clone() }
}

#[derive(Debug, Default, Clone)]
pub struct IDSystem {
    inner: Arc<Mutex<IDSystemInner>>
}

impl Eq for IDSystem {}

impl PartialEq for IDSystem {
    fn eq(&self, other: &Self) -> bool {
        Arc::as_ptr(&self.inner) == Arc::as_ptr(&other.inner)
    }
}

impl std::hash::Hash for IDSystem {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.inner).hash(state);
    }
}

#[derive(Debug, Default)]
struct IDSystemInner {
    free: Vec<usize>,
    current: usize,
}

impl IDSystem {

    pub fn take(&self) -> ID {
        let inner = self.inner.clone();
        let inner = &mut inner.lock().unwrap();
        match inner.free.pop() {
            Some(id) => ID(id, self.clone()),
            None => ID({inner.current += 1; inner.current}, self.clone())
        }
    }

    pub fn free(&mut self, id: ID) {
        self.inner.clone().lock().unwrap().free.push(id.0)
    }
}