rstm_core/rules/impls/
impl_tail.rs1use crate::rules::Tail;
7use crate::{Direction, Head};
8use rstm_state::{RawState, State};
9
10impl<Q, A> Tail<Q, A>
11where
12 Q: RawState,
13{
14 pub const fn new(direction: Direction, next_state: Q, write_symbol: A) -> Self {
16 Self {
17 direction,
18 next_state: State(next_state),
19 write_symbol,
20 }
21 }
22 pub const fn stay(state: Q, symbol: A) -> Self {
24 Self::new(Direction::Stay, state, symbol)
25 }
26 pub const fn left(state: Q, symbol: A) -> Self {
28 Self::new(Direction::Left, state, symbol)
29 }
30 pub const fn right(state: Q, symbol: A) -> Self {
32 Self::new(Direction::Right, state, symbol)
33 }
34 pub fn from_head(head: Head<Q, A>) -> Self {
36 Self {
37 direction: Direction::Stay,
38 next_state: head.state,
39 write_symbol: head.symbol,
40 }
41 }
42 pub fn from_head_with_direction(head: Head<Q, A>, direction: Direction) -> Self {
43 Self {
44 direction,
45 next_state: head.state,
46 write_symbol: head.symbol,
47 }
48 }
49 pub const fn as_tuple(&self) -> (Direction, &State<Q>, &A) {
51 (self.direction, &self.next_state, &self.write_symbol)
52 }
53 pub fn into_tuple(self) -> (Direction, State<Q>, A) {
55 (self.direction, self.next_state, self.write_symbol)
56 }
57 pub const fn direction(&self) -> Direction {
60 self.direction
61 }
62 pub const fn state(&self) -> &State<Q> {
64 &self.next_state
65 }
66 pub const fn state_mut(&mut self) -> &mut State<Q> {
68 &mut self.next_state
69 }
70 pub const fn symbol(&self) -> &A {
72 &self.write_symbol
73 }
74 pub const fn symbol_mut(&mut self) -> &mut A {
76 &mut self.write_symbol
77 }
78 pub const fn set_direction(&mut self, direction: Direction) {
80 self.direction = direction;
81 }
82 pub fn set_state(&mut self, state: Q) {
84 self.next_state = State(state);
85 }
86 pub fn set_symbol(&mut self, symbol: A) {
88 self.write_symbol = symbol;
89 }
90 pub fn with_direction(self, direction: Direction) -> Self {
92 Self { direction, ..self }
93 }
94 pub fn with_state(self, state: Q) -> Self {
96 Self {
97 next_state: State(state),
98 ..self
99 }
100 }
101 pub fn with_symbol(self, symbol: A) -> Self {
103 Self {
104 write_symbol: symbol,
105 ..self
106 }
107 }
108 pub const fn get_head(&self) -> Head<&Q, &A> {
110 Head {
111 state: self.next_state.view(),
112 symbol: &self.write_symbol,
113 }
114 }
115 pub fn get_head_mut(&mut self) -> Head<&mut Q, &mut A> {
117 Head {
118 state: self.next_state.view_mut(),
119 symbol: &mut self.write_symbol,
120 }
121 }
122 pub fn into_head(self) -> Head<Q, A> {
125 Head {
126 state: self.next_state,
127 symbol: self.write_symbol,
128 }
129 }
130 pub fn into_head_tuple(self) -> (Direction, Head<Q, A>) {
132 let Tail {
133 direction,
134 next_state,
135 write_symbol,
136 } = self;
137 (
138 direction,
139 Head {
140 state: next_state,
141 symbol: write_symbol,
142 },
143 )
144 }
145 pub const fn view(&self) -> Tail<&Q, &A> {
148 Tail {
149 direction: self.direction(),
150 next_state: self.state().view(),
151 write_symbol: self.symbol(),
152 }
153 }
154 pub const fn view_mut(&mut self) -> Tail<&mut Q, &mut A> {
156 Tail {
157 direction: self.direction,
158 next_state: self.next_state.view_mut(),
159 write_symbol: &mut self.write_symbol,
160 }
161 }
162}