scsys_core/state/
nstate.rs

1/*
2    Appellation: state <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5#[doc(inline)]
6pub use self::kind::*;
7
8mod kind;
9
10use core::marker::PhantomData;
11
12/// A type alias for a [Nary] state with a default value of 4.
13pub type NaryState<T, const N: usize = 4> = NState<Nary<N>, T>;
14
15/// [State] is an abstract object that allows a particular _kind_ of state to be associated
16/// with some data.
17#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
18#[cfg_attr(
19    feature = "serde",
20    derive(serde::Deserialize, serde::Serialize),
21    serde(default)
22)]
23pub struct NState<K, V> {
24    pub(crate) data: V,
25    pub(crate) _state: PhantomData<K>,
26}
27
28impl<K, V> NState<K, V> {
29    pub fn new(data: V) -> Self {
30        Self {
31            data,
32            _state: PhantomData::<K>,
33        }
34    }
35
36    pub fn data(&self) -> &V {
37        &self.data
38    }
39
40    pub fn is_state<R: 'static>(&self) -> bool
41    where
42        K: 'static,
43    {
44        use core::any::TypeId;
45        TypeId::of::<PhantomData<K>>() == TypeId::of::<PhantomData<R>>()
46    }
47}