Skip to main content

rstm_state/traits/
raw_state.rs

1/*
2    appellation: raw_state <module>
3    authors: @FL03
4*/
5use crate::Halting;
6/// [`RawState`] is a sealed marker trait used to define objects used to represent states.
7/// The primary benefit of such a trait is preventing cases where the [`State`](crate::State)
8/// implementation is used as a state itself: `State<State<Q>>`.
9pub trait RawState {
10    private! {}
11}
12/// The [`HaltingState`] trait is used to define states that are capable of representing halting
13/// conditions within a Turing machine simulation. For instance, if our state is of type
14/// `isize`, then we might define `<isize>::MAX` to represent a halting state.
15pub trait HaltingState
16where
17    Self: RawState + Halting,
18{
19    private! {}
20}
21/// The [`StateExt`] trait is used to extend the base [`RawState`] trait, introducing
22/// additional traits and constraints that are commonly required for state representations.
23pub trait StateExt: HaltingState
24where
25    Self: Clone + Default + PartialEq + PartialOrd + core::fmt::Debug + core::fmt::Display,
26{
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 [`NumState`] trait extends the [`RawState`] trait to include numeric operations.
37pub trait NumState: StateExt
38where
39    Self: Copy
40        + Default
41        + Eq
42        + core::ops::Add<Output = Self>
43        + core::ops::Div<Output = Self>
44        + core::ops::Mul<Output = Self>
45        + core::ops::Not<Output = Self>
46        + core::ops::Rem<Output = Self>
47        + core::ops::Sub<Output = Self>
48        + core::ops::AddAssign
49        + core::ops::DivAssign
50        + core::ops::MulAssign
51        + core::ops::RemAssign
52        + core::ops::SubAssign
53        + num_traits::One
54        + num_traits::Zero
55        + num_traits::ToPrimitive
56        + num_traits::FromPrimitive,
57{
58    private! {}
59}
60
61/*
62 ************* Implementations *************
63*/
64
65impl RawState for () {
66    seal! {}
67}
68
69impl<T> RawState for &T
70where
71    T: RawState,
72{
73    seal! {}
74}
75
76impl<T> RawState for &mut T
77where
78    T: RawState,
79{
80    seal! {}
81}
82
83impl<T> HaltingState for T
84where
85    T: RawState + Halting,
86{
87    seal! {}
88}
89
90impl<T> HashState for T
91where
92    T: RawState + Eq + core::hash::Hash,
93{
94    seal! {}
95}
96
97impl<T> StateExt for T where
98    T: HaltingState
99        + Clone
100        + Default
101        + PartialEq
102        + PartialOrd
103        + core::fmt::Debug
104        + core::fmt::Display
105{
106}
107
108impl<T> NumState for T
109where
110    T: StateExt
111        + Copy
112        + Eq
113        + PartialOrd
114        + core::ops::Add<Output = Self>
115        + core::ops::Sub<Output = Self>
116        + core::ops::Mul<Output = Self>
117        + core::ops::Div<Output = Self>
118        + core::ops::Rem<Output = Self>
119        + core::ops::Not<Output = Self>
120        + core::ops::AddAssign
121        + core::ops::DivAssign
122        + core::ops::MulAssign
123        + core::ops::RemAssign
124        + core::ops::SubAssign
125        + num_traits::One
126        + num_traits::Zero
127        + num_traits::ToPrimitive
128        + num_traits::FromPrimitive,
129{
130    seal! {}
131}
132
133impl RawState for &str {
134    seal! {}
135}
136
137impl<Q> RawState for [Q]
138where
139    Q: RawState,
140{
141    seal! {}
142}
143
144impl<Q> RawState for &[Q]
145where
146    Q: RawState,
147{
148    seal! {}
149}
150
151impl<Q> RawState for &mut [Q]
152where
153    Q: RawState,
154{
155    seal! {}
156}
157
158impl<const N: usize, Q> RawState for [Q; N]
159where
160    Q: RawState,
161{
162    seal! {}
163}
164
165macro_rules! impl_raw_state {
166    (impl $trait:ident for {$($($cont:ident)::*<$($T:ident),*> $({where $($rest:tt)*})?),* $(,)?}) => {
167        $(impl_raw_state! { @impl $trait for $($cont)::*<$($T),*> $(where $($rest)*)?})*
168    };
169    (impl $trait:ident for {$($T:ty),* $(,)?}) => {
170        $(impl_raw_state!{ @impl $trait for $T })*
171    };
172    (@impl $trait:ident for $($cont:ident)::*<$($T:ident),*> $(where $($rest:tt)*)?) => {
173        impl<$($T),*> $trait for $($cont)::*<$($T),*> $(where $($rest)*)? {
174            seal! {}
175        }
176    };
177    (@impl $trait:ident for $t:ty) => {
178        impl $trait for $t {
179            seal! {}
180        }
181    };
182}
183
184impl_raw_state! {
185    impl RawState for {
186        usize, u8, u16, u32, u64, u128,
187        isize, i8, i16, i32, i64, i128,
188        f32, f64,
189        bool, char, str
190    }
191}
192
193impl_raw_state! {
194    impl RawState for {
195        Option<Q> { where Q: RawState },
196        Result<Q, E> { where Q: RawState },
197        core::cell::Cell<Q> { where Q: RawState },
198        core::cell::RefCell<Q> { where Q: RawState },
199        core::mem::MaybeUninit<Q> { where Q: RawState },
200        core::ops::ControlFlow<Q, H> { where Q: RawState, H: RawState },
201        core::ops::Range<Q> { where Q: RawState },
202    }
203}
204
205#[cfg(feature = "alloc")]
206impl_raw_state! {
207    impl RawState for {
208        alloc::string::String,
209    }
210}
211
212#[cfg(feature = "alloc")]
213impl_raw_state! {
214    impl RawState for {
215        alloc::boxed::Box<Q> { where Q: RawState },
216        alloc::collections::VecDeque<Q> { where Q: RawState },
217        alloc::vec::Vec<Q> { where Q: RawState },
218    }
219}