rstm_core/rules/impls/
impl_head.rs1use crate::motion::HeadStep;
7use crate::{Head, HeadMut, HeadRef};
8use crate::{Rule, Tail};
9use rstm_state::{RawState, State};
10
11impl<Q, A> Head<Q, A>
15where
16 Q: RawState,
17{
18 pub const fn new(state: Q, symbol: A) -> Self {
20 Self {
21 state: State(state),
22 symbol,
23 }
24 }
25 pub fn from_state(state: Q) -> Self
27 where
28 A: Default,
29 {
30 Self::new(state, <A>::default())
31 }
32 pub fn from_symbol(symbol: A) -> Self
34 where
35 Q: Default,
36 {
37 Self::new(<Q>::default(), symbol)
38 }
39 pub fn from_tail(
41 Tail {
42 next_state,
43 write_symbol,
44 ..
45 }: Tail<Q, A>,
46 ) -> Self {
47 Head {
48 state: next_state,
49 symbol: write_symbol,
50 }
51 }
52 pub fn from_tuple((state, symbol): (State<Q>, A)) -> Self {
54 Self { state, symbol }
55 }
56 pub const fn state(&self) -> &State<Q> {
58 &self.state
59 }
60 pub const fn state_mut(&mut self) -> &mut State<Q> {
62 &mut self.state
63 }
64 pub const fn symbol(&self) -> &A {
66 &self.symbol
67 }
68 pub const fn symbol_mut(&mut self) -> &mut A {
70 &mut self.symbol
71 }
72 #[inline]
74 pub fn set_state(&mut self, state: Q) {
75 self.state_mut().set(state)
76 }
77 #[inline]
79 pub fn set_symbol(&mut self, symbol: A) {
80 self.symbol = symbol;
81 }
82 #[inline]
84 pub fn with_state<Q2>(self, state: Q2) -> Head<Q2, A> {
85 Head {
86 state: State(state),
87 symbol: self.symbol,
88 }
89 }
90 #[inline]
92 pub fn with_symbol<A2>(self, symbol: A2) -> Head<Q, A2> {
93 Head {
94 state: self.state,
95 symbol,
96 }
97 }
98 pub const fn replace(&mut self, state: State<Q>, symbol: A) -> Self {
101 Head {
102 state: self.replace_state(state),
103 symbol: self.replace_symbol(symbol),
104 }
105 }
106 pub const fn replace_state(&mut self, state: State<Q>) -> State<Q> {
109 core::mem::replace(self.state_mut(), state)
110 }
111 pub const fn replace_symbol(&mut self, symbol: A) -> A {
114 core::mem::replace(self.symbol_mut(), symbol)
115 }
116 pub const fn swap(&mut self, other: &mut Self) {
118 self.swap_state(other.state_mut());
119 self.swap_symbol(other.symbol_mut());
120 }
121 pub const fn swap_state(&mut self, other: &mut State<Q>) {
123 core::mem::swap(self.state_mut(), other)
124 }
125 pub const fn swap_symbol(&mut self, other: &mut A) {
127 core::mem::swap(self.symbol_mut(), other)
128 }
129 pub fn update(&mut self, state: Option<State<Q>>, symbol: Option<A>) {
131 if let Some(s) = state {
132 self.set_state(s.value())
133 }
134 if let Some(s) = symbol {
135 self.set_symbol(s)
136 }
137 }
138 pub const fn as_tuple(&self) -> (&State<Q>, &A) {
140 (self.state(), self.symbol())
141 }
142 pub const fn as_mut_tuple(&mut self) -> (&mut State<Q>, &mut A) {
144 (&mut self.state, &mut self.symbol)
145 }
146 pub fn into_tuple(self) -> (State<Q>, A) {
148 (self.state, self.symbol)
149 }
150 pub fn into_rule_with_tail(self, tail: Tail<Q, A>) -> Rule<Q, A>
152 where
153 Q: RawState,
154 {
155 Rule { head: self, tail }
156 }
157 pub fn read_tape<T>(self, tape: &'_ [T]) -> Option<&A::Output>
159 where
160 A: core::slice::SliceIndex<[T]>,
161 {
162 tape.get(self.symbol)
163 }
164 pub const fn view(&self) -> HeadRef<'_, Q, A> {
166 Head {
167 state: self.state().view(),
168 symbol: self.symbol(),
169 }
170 }
171 pub const fn view_mut(&mut self) -> HeadMut<'_, Q, A> {
173 Head {
174 state: self.state.view_mut(),
175 symbol: &mut self.symbol,
176 }
177 }
178 pub fn step<'a, Q2, B>(&'a mut self, tail: Tail<Q2, B>) -> HeadStep<'a, Q, A, Q2, B>
184 where
185 Q2: RawState,
186 {
187 HeadStep { head: self, tail }
188 }
189}