use crate::traits::RawState;
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(default, transparent)
)]
#[repr(transparent)]
pub struct State<Q: ?Sized = usize>(pub Q);
impl<Q> State<Q>
where
Q: RawState,
{
pub const fn new(state: Q) -> Self {
State(state)
}
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,
{
State(Q::default())
}
pub fn one() -> Self
where
Q: num_traits::One,
{
State(Q::one())
}
pub fn zero() -> Self
where
Q: num_traits::Zero,
{
State(Q::zero())
}
#[cfg(feature = "rand")]
pub fn random() -> Self
where
rand_distr::StandardUniform: rand_distr::Distribution<Q>,
{
use rand::Rng;
let mut rng = rand::rng();
Self::new(rng.random())
}
#[cfg(feature = "rand")]
pub fn random_with<R, Dist>(rng: &mut R, distr: Dist) -> Self
where
Dist: rand_distr::Distribution<Q>,
R: rand::RngCore,
{
use rand::Rng;
let value = rng.sample(distr);
State(value)
}
pub const fn get(&self) -> &Q {
&self.0
}
pub const fn get_mut(&mut self) -> &mut Q {
&mut self.0
}
#[inline]
pub fn value(self) -> Q {
self.0
}
pub const fn replace(&mut self, state: Q) -> Q {
core::mem::replace(self.get_mut(), state)
}
pub fn set(&mut self, state: Q) {
*self.get_mut() = state
}
pub const fn swap(&mut self, state: &mut State<Q>) {
core::mem::swap(self.get_mut(), state.get_mut())
}
pub fn take(&mut self) -> Q
where
Q: Default,
{
core::mem::take(self.get_mut())
}
pub fn with<U>(self, value: U) -> State<U> {
State(value)
}
pub fn map<U, F>(self, f: F) -> State<U>
where
F: FnOnce(Q) -> U,
{
State(f(self.value()))
}
pub fn map_inplace<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut Q),
{
f(self.get_mut());
self
}
pub const fn view(&self) -> State<&Q> {
State(self.get())
}
pub const fn view_mut(&mut self) -> State<&mut Q> {
State(self.get_mut())
}
}