Skip to main content

rstm_core/rules/impls/
impl_head_ext.rs

1/*
2    Appellation: impl_head_ext <module>
3    Created At: 2025.09.05:17:57:25
4    Contrib: @FL03
5*/
6use crate::rules::Head;
7use crate::{Rule, Tail};
8use rstm_state::{RawState, State};
9use rstm_traits::{Read, ReadBuf};
10
11impl<Q, A, T> ReadBuf<T> for Head<Q, A>
12where
13    Q: RawState,
14    A: Copy + core::slice::SliceIndex<[T], Output = T>,
15    A::Output: Clone,
16{
17    type Buf<_T> = [_T];
18    type Output = A::Output;
19
20    fn read(&mut self, rhs: &mut Self::Buf<T>) -> Self::Output {
21        rhs[self.symbol].clone()
22    }
23}
24
25impl<'a, Q, A> Read<&'a [A]> for Head<Q, usize>
26where
27    Q: RawState,
28{
29    type Output = &'a A;
30    type Error = crate::Error;
31
32    fn read(self, rhs: &'a [A]) -> Result<Self::Output, Self::Error> {
33        let pos = self.symbol;
34        if pos >= rhs.len() {
35            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
36        }
37        Ok(&rhs[pos])
38    }
39}
40
41impl<'a, Q, A> Read<&'a [A]> for &Head<Q, usize>
42where
43    Q: RawState,
44{
45    type Output = &'a A;
46    type Error = crate::Error;
47
48    fn read(self, rhs: &'a [A]) -> Result<Self::Output, Self::Error> {
49        let pos = self.symbol;
50        if pos >= rhs.len() {
51            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
52        }
53        Ok(&rhs[pos])
54    }
55}
56
57impl<'a, Q, A> Read<&'a [A]> for &mut Head<Q, usize>
58where
59    Q: RawState,
60{
61    type Output = &'a A;
62    type Error = crate::Error;
63
64    fn read(self, rhs: &'a [A]) -> Result<Self::Output, Self::Error> {
65        let pos = self.symbol;
66        if pos >= rhs.len() {
67            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
68        }
69        Ok(&rhs[pos])
70    }
71}
72
73impl<'a, Q, A> Read<&'a mut [A]> for Head<Q, usize>
74where
75    Q: RawState,
76{
77    type Output = &'a A;
78    type Error = crate::Error;
79
80    fn read(self, rhs: &'a mut [A]) -> Result<Self::Output, Self::Error> {
81        let pos = self.symbol;
82        if pos >= rhs.len() {
83            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
84        }
85        Ok(&rhs[pos])
86    }
87}
88
89impl<'a, Q, A> Read<&'a mut [A]> for &Head<Q, usize>
90where
91    Q: RawState,
92{
93    type Error = crate::Error;
94    type Output = &'a A;
95
96    fn read(self, rhs: &'a mut [A]) -> Result<Self::Output, Self::Error> {
97        let pos = self.symbol;
98        if pos >= rhs.len() {
99            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
100        }
101        Ok(&rhs[pos])
102    }
103}
104
105impl<'a, Q, A> Read<&'a mut [A]> for &mut Head<Q, usize>
106where
107    Q: RawState,
108{
109    type Error = crate::Error;
110    type Output = &'a A;
111
112    fn read(self, rhs: &'a mut [A]) -> Result<Self::Output, Self::Error> {
113        let pos = self.symbol;
114        if pos >= rhs.len() {
115            return Err(crate::Error::index_out_of_bounds(pos, rhs.len()));
116        }
117        Ok(&rhs[pos])
118    }
119}
120impl<Q, A> core::fmt::Debug for Head<Q, A>
121where
122    Q: core::fmt::Debug,
123    A: core::fmt::Debug,
124{
125    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
126        f.debug_tuple("Head")
127            .field(&self.state)
128            .field(&self.symbol)
129            .finish()
130    }
131}
132
133impl<Q, A> core::fmt::Display for Head<Q, A>
134where
135    Q: core::fmt::Display,
136    A: core::fmt::Display,
137{
138    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
139        write! { f, "{{ state: {}, symbol: {} }}", self.state, self.symbol }
140    }
141}
142
143impl<Q, A, R, B> core::ops::Add<Tail<R, B>> for Head<Q, A>
144where
145    Q: RawState,
146    R: RawState,
147{
148    type Output = Rule<Q, A, R, B>;
149
150    fn add(self, rhs: Tail<R, B>) -> Self::Output {
151        Rule::new(self, rhs)
152    }
153}
154
155impl<Q, A> core::borrow::Borrow<State<Q>> for Head<Q, A>
156where
157    Q: RawState,
158{
159    fn borrow(&self) -> &State<Q> {
160        &self.state
161    }
162}
163
164impl<Q, A> core::borrow::BorrowMut<State<Q>> for Head<Q, A>
165where
166    Q: RawState,
167{
168    fn borrow_mut(&mut self) -> &mut State<Q> {
169        &mut self.state
170    }
171}
172
173impl<Q, A> PartialEq<State<Q>> for Head<Q, A>
174where
175    Q: PartialEq,
176{
177    fn eq(&self, state: &State<Q>) -> bool {
178        &self.state == state
179    }
180}
181
182impl<Q, A> PartialEq<Head<Q, A>> for State<Q>
183where
184    Q: PartialEq,
185{
186    fn eq(&self, head: &Head<Q, A>) -> bool {
187        self == &head.state
188    }
189}
190
191impl<Q, A> PartialEq<Head<Q, A>> for State<&Q>
192where
193    Q: PartialEq,
194{
195    fn eq(&self, head: &Head<Q, A>) -> bool {
196        *self == head.state.view()
197    }
198}
199
200impl<'a, Q, A> PartialEq<State<&'a Q>> for Head<Q, A>
201where
202    Q: PartialEq,
203{
204    fn eq(&self, state: &State<&'a Q>) -> bool {
205        self.state.view() == *state
206    }
207}
208
209impl<Q, A> PartialEq<(State<Q>, A)> for Head<Q, A>
210where
211    Q: PartialEq,
212    A: PartialEq,
213{
214    fn eq(&self, (state, symbol): &(State<Q>, A)) -> bool {
215        &self.state == state && &self.symbol == symbol
216    }
217}
218
219impl<Q, A> PartialEq<Head<Q, A>> for (State<Q>, A)
220where
221    Q: PartialEq,
222    A: PartialEq,
223{
224    fn eq(&self, head: &Head<Q, A>) -> bool {
225        self.0 == head.state && self.1 == head.symbol
226    }
227}
228
229impl<Q, A> PartialEq<(Q, A)> for Head<Q, A>
230where
231    State<Q>: PartialEq,
232    Q: PartialEq,
233    A: PartialEq,
234{
235    fn eq(&self, (state, symbol): &(Q, A)) -> bool {
236        self.state.get() == state && &self.symbol == symbol
237    }
238}
239
240impl<Q, A> PartialEq<Head<Q, A>> for (Q, A)
241where
242    Q: PartialEq,
243    A: PartialEq,
244{
245    fn eq(&self, head: &Head<Q, A>) -> bool {
246        head.state == self.0 && head.symbol == self.1
247    }
248}
249
250impl<Q, S> From<S> for Head<Q, S>
251where
252    Q: Default,
253{
254    fn from(symbol: S) -> Self {
255        Head {
256            state: State::default(),
257            symbol,
258        }
259    }
260}
261
262impl<Q, S> From<(Q, S)> for Head<Q, S> {
263    fn from((state, symbol): (Q, S)) -> Self {
264        Head {
265            state: State(state),
266            symbol,
267        }
268    }
269}
270
271impl<Q, S> From<(State<Q>, S)> for Head<Q, S> {
272    fn from((state, symbol): (State<Q>, S)) -> Self {
273        Head { state, symbol }
274    }
275}
276
277impl<Q, S> From<Head<Q, S>> for (State<Q>, S) {
278    fn from(Head { state, symbol }: Head<Q, S>) -> Self {
279        (state, symbol)
280    }
281}