eryon_core/state/
mod.rs

1/*
2    Appellation: state <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5//! the state module implements the [`State`] type and its associated traits and types
6#[doc(inline)]
7pub use self::{error::*, halt::*, state::State};
8
9mod state;
10
11pub mod error;
12pub mod halt;
13
14mod impls {
15    mod impl_ops;
16    mod impl_repr;
17    mod impl_state;
18}
19pub(crate) mod prelude {
20    #[cfg(feature = "alloc")]
21    pub use super::AnyState;
22    #[doc(inline)]
23    pub use super::MaybeState;
24
25    #[doc(inline)]
26    pub use super::error::*;
27    #[doc(inline)]
28    pub use super::halt::*;
29    #[doc(inline)]
30    pub use super::state::*;
31}
32#[cfg(feature = "alloc")]
33use alloc::string::String;
34
35#[cfg(feature = "alloc")]
36/// A type alias for a [State] whose inner value is the dynamically sized type of a boxed [`Any`](core::any::Any).
37pub type AnyState = State<alloc::boxed::Box<dyn core::any::Any>>;
38/// A type alias for a [State] whose inner value is a [core::mem::MaybeUninit] of generic type `Q`.
39pub type MaybeState<Q = bool> = State<core::mem::MaybeUninit<Q>>;
40
41/// [`RawState`] is a trait describing objects capable of being used as states in our library.
42/// The trait contains a single associated trait, the context, or inner value of the state.
43pub trait RawState: Send + Sync + core::fmt::Debug + core::fmt::Display {
44    private!();
45}
46
47pub trait StdState: RawState
48where
49    Self: Clone + Default + PartialEq + PartialOrd,
50{
51}
52/// The [`NumState`] trait extends the [`RawState`] trait to include numeric operations.
53pub trait NumState: RawState
54where
55    Self: Copy
56        + Eq
57        + PartialOrd
58        + core::ops::Add<Output = Self>
59        + core::ops::Sub<Output = Self>
60        + core::ops::Mul<Output = Self>
61        + core::ops::Div<Output = Self>
62        + core::ops::Rem<Output = Self>
63        + core::ops::Neg<Output = Self>
64        + core::ops::Not<Output = Self>
65        + core::ops::AddAssign
66        + core::ops::SubAssign
67        + core::ops::MulAssign
68        + core::ops::DivAssign
69        + num_traits::One
70        + num_traits::Zero
71        + num_traits::ToPrimitive
72        + num_traits::FromPrimitive,
73{
74}
75
76/// The [`Stated`] trait defines the interface for stateful implementations in the library.
77pub trait Stated {
78    type Item;
79
80    private!();
81
82    fn get(self) -> Self::Item;
83
84    fn get_ref(&self) -> &Self::Item;
85
86    fn get_mut(&mut self) -> &mut Self::Item;
87
88    fn set(&mut self, inner: Self::Item);
89}
90
91/*
92 ************* Implementations *************
93*/
94macro_rules! impl_raw_state {
95    ($($t:ty),* $(,)?) => {
96        $(
97            impl_raw_state!(@impl $t);
98        )*
99    };
100    (@impl $t:ty) => {
101        impl $crate::state::RawState for $t {
102            seal!();
103        }
104    };
105}
106
107macro_rules! impl_state {
108    (@impl $state:ident($($field:tt)*)) => {
109        impl<Q> $crate::state::Stated for $state<Q> {
110            type Item = Q;
111
112            seal!();
113
114            fn get(self) -> Q {
115                self.$($field)*
116            }
117
118            fn get_ref(&self) -> &Q {
119                &self.$($field)*
120            }
121
122            fn get_mut(&mut self) -> &mut Q {
123                &mut self.$($field)*
124            }
125
126            fn set(&mut self, inner: Q) {
127                self.$($field)* = inner;
128            }
129        }
130    };
131    ($($T:ident($($field:tt)*)),* $(,)?) => {
132        $(
133            impl_state!(@impl $T($($field)*));
134        )*
135    };
136}
137
138impl_raw_state! {
139    usize, u8, u16, u32, u64, u128,
140    isize, i8, i16, i32, i64, i128,
141    f32, f64, bool, char,
142}
143
144#[cfg(feature = "alloc")]
145impl_raw_state! {
146    String,
147}
148
149impl_state! {
150    Halt(0),
151    State(0),
152}
153
154impl<'a, Q> RawState for &'a Q
155where
156    Q: 'a + RawState,
157{
158    seal!();
159}
160
161impl<'a, Q> RawState for &'a mut Q
162where
163    Q: 'a + RawState,
164{
165    seal!();
166}