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
/*
    Appellation: state <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
use scsys::id::AtomicId;

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[repr(C)]
pub struct StateBase<S, T> {
    id: AtomicId,
    data: T,
    state: S,
}

impl<Q, T> StateBase<Q, T> {
    pub fn new(data: T, state: Q) -> Self {
        Self {
            id: AtomicId::new(),
            data,
            state,
        }
    }

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

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

    pub fn data_mut(&mut self) -> &mut T {
        &mut self.data
    }

    pub fn state(&self) -> &Q {
        &self.state
    }

    pub fn state_mut(&mut self) -> &mut Q {
        &mut self.state
    }
}

impl<Q, T> core::borrow::Borrow<Q> for StateBase<Q, T> {
    fn borrow(&self) -> &Q {
        &self.state
    }
}