use super::RawStateKind;
use core::marker::PhantomData;
#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(default)
)]
pub struct StateBase<Q, K> {
pub(crate) state: Q,
pub(crate) _kind: PhantomData<K>,
}
impl<K, Q> StateBase<Q, K>
where
K: RawStateKind,
{
pub const fn new(state: Q) -> Self {
Self {
state,
_kind: PhantomData::<K>,
}
}
pub fn new_with<F>(f: F) -> Self
where
F: FnOnce() -> Q,
{
Self::new(f())
}
#[allow(clippy::should_implement_trait)]
pub fn default() -> Self
where
Q: Default,
{
Self::new(Default::default())
}
pub const fn get(&self) -> &Q {
&self.state
}
pub const fn get_mut(&mut self) -> &mut Q {
&mut self.state
}
pub fn is_kind<R>(&self) -> bool
where
R: 'static,
{
use core::any::TypeId;
TypeId::of::<K>() == TypeId::of::<R>()
}
}