rstm_state/traits/
raw_state.rs

1/*
2    appellation: raw_state <module>
3    authors: @FL03
4*/
5#[cfg(feature = "alloc")]
6use alloc::string::String;
7
8pub trait Stated {
9    type Ctx;
10
11    fn get(&self) -> &Self::Ctx;
12    fn get_mut(&mut self) -> &mut Self::Ctx;
13}
14
15/// [`RawState`] is a trait describing objects capable of being used as states in our library.
16/// The trait contains a single associated trait, the context, or inner value of the state.
17pub trait RawState: Send + Sync + core::fmt::Debug {
18    private!();
19}
20/// [`DisplayState`] is a trait that extends the [`RawState`] trait to include symbolic
21/// operations and implementations.
22pub trait DisplayState: RawState
23where
24    Self: core::fmt::Debug + core::fmt::Display,
25{
26    private!();
27}
28/// The [`HashState`] trait extends the [`RawState`] trait to include hashing capabilities,
29/// streamlining the process of using states as keys in hash maps or sets.
30pub trait HashState: RawState
31where
32    Self: Eq + core::hash::Hash,
33{
34    private!();
35}
36/// The [`StdState`] trait extends the base [`RawState`] trait to include additional traits
37/// commonly used alongside the state.
38pub trait StdState: RawState
39where
40    Self: Clone + Default + PartialEq + PartialOrd + core::fmt::Debug + core::fmt::Display,
41{
42    private!();
43}
44/// The [`NumState`] trait extends the [`RawState`] trait to include numeric operations.
45pub trait NumState: StdState
46where
47    Self: Copy
48        + Eq
49        + core::ops::Add<Output = Self>
50        + core::ops::Sub<Output = Self>
51        + core::ops::Mul<Output = Self>
52        + core::ops::Div<Output = Self>
53        + core::ops::Rem<Output = Self>
54        + core::ops::Neg<Output = Self>
55        + core::ops::Not<Output = Self>
56        + core::ops::AddAssign
57        + core::ops::SubAssign
58        + core::ops::MulAssign
59        + core::ops::DivAssign
60        + num_traits::One
61        + num_traits::Zero
62        + num_traits::ToPrimitive
63        + num_traits::FromPrimitive,
64{
65    private!();
66}
67
68/*
69 ************* Implementations *************
70*/
71impl<T> RawState for &T
72where
73    T: RawState,
74{
75    seal!();
76}
77
78impl<T> RawState for &mut T
79where
80    T: RawState,
81{
82    seal!();
83}
84
85impl<T> DisplayState for T
86where
87    T: RawState + core::fmt::Display,
88{
89    seal!();
90}
91
92impl<T> HashState for T
93where
94    T: RawState + Eq + core::hash::Hash,
95{
96    seal!();
97}
98
99impl<T> StdState for T
100where
101    T: RawState + Clone + Default + PartialEq + PartialOrd + core::fmt::Display,
102{
103    seal!();
104}
105
106impl<T> NumState for T
107where
108    T: StdState
109        + Copy
110        + Eq
111        + PartialOrd
112        + core::ops::Add<Output = Self>
113        + core::ops::Sub<Output = Self>
114        + core::ops::Mul<Output = Self>
115        + core::ops::Div<Output = Self>
116        + core::ops::Rem<Output = Self>
117        + core::ops::Neg<Output = Self>
118        + core::ops::Not<Output = Self>
119        + core::ops::AddAssign
120        + core::ops::SubAssign
121        + core::ops::MulAssign
122        + core::ops::DivAssign
123        + num_traits::One
124        + num_traits::Zero
125        + num_traits::ToPrimitive
126        + num_traits::FromPrimitive,
127{
128    seal!();
129}
130
131impl<Q> RawState for Option<Q>
132where
133    Q: RawState,
134{
135    seal!();
136}
137
138impl<Q> RawState for core::ops::ControlFlow<Q, Q>
139where
140    Q: RawState,
141{
142    seal!();
143}
144
145impl<Q> RawState for core::mem::MaybeUninit<Q>
146where
147    Q: RawState,
148{
149    seal!();
150}
151
152#[cfg(feature = "alloc")]
153impl RawState for Box<dyn RawState> {
154    seal!();
155}
156
157macro_rules! impl_raw_state {
158    ($($t:ty),* $(,)?) => {
159        $(
160            impl_raw_state!(@impl $t);
161        )*
162    };
163    (@impl $t:ty) => {
164        impl $crate::traits::RawState for $t {
165            seal!();
166        }
167    };
168}
169
170impl_raw_state! {
171    usize, u8, u16, u32, u64, u128,
172    isize, i8, i16, i32, i64, i128,
173    f32, f64, bool, char, str,
174}
175
176#[cfg(feature = "alloc")]
177impl_raw_state! {
178    String,
179}