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
/*
    Appellation: identity <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use super::ids::AtomicId;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::ops::Deref;

pub enum TypeClass {
    Alphanumeric,
    Numeric,
}

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize,))]
pub struct Id<T = AtomicId> {
    id: T,
    timestamp: u128,
}

impl<T> Id<T> {
    pub fn new(id: T) -> Self {
        Self {
            id,
            timestamp: crate::time::systime(),
        }
    }

    pub fn id(&self) -> &T {
        &self.id
    }

    pub fn set(&mut self, id: T) {
        self.id = id;
        self.on_update();
    }

    pub fn timestamp(&self) -> u128 {
        self.timestamp
    }

    fn on_update(&mut self) {
        self.timestamp = crate::time::systime();
    }
}

impl<T> AsRef<T> for Id<T> {
    fn as_ref(&self) -> &T {
        &self.id
    }
}

impl<T> Deref for Id<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.id
    }
}