rstm_core/rules/traits/
instruction.rs1use crate::rules::{Head, RawHead, RawTail, Rule, Tail};
7use rstm_state::{RawState, State};
8
9pub trait Instruction<Q, A>
12where
13 Q: RawState,
14{
15 type Head: RawHead<State = Q, Symbol = A>;
17 type Tail: RawTail<State = Q, Symbol = A>;
19 fn head(&self) -> &Self::Head;
21 fn tail(&self) -> &Self::Tail;
23 fn current_state(&self) -> &State<Q>;
25 fn symbol(&self) -> &A;
27 fn direction(&self) -> crate::Direction {
29 self.tail().direction()
30 }
31 fn next_state(&self) -> &State<Q>;
33 fn next_symbol(&self) -> &A;
35}
36
37pub trait InstructionMut<Q, A>: Instruction<Q, A>
38where
39 Q: RawState,
40{
41 fn head_mut(&mut self) -> &mut Self::Head;
43 fn tail_mut(&mut self) -> &mut Self::Tail;
45 fn current_state_mut(&mut self) -> &mut State<Q>;
47 fn symbol_mut(&mut self) -> &mut A;
49 fn next_state_mut(&mut self) -> &mut State<Q>;
51 fn next_symbol_mut(&mut self) -> &mut A;
53}
54
55impl<Q, A> Instruction<Q, A> for (Head<Q, A>, Tail<Q, A>)
60where
61 Q: RawState,
62{
63 type Head = Head<Q, A>;
64 type Tail = Tail<Q, A>;
65
66 fn head(&self) -> &Self::Head {
67 &self.0
68 }
69
70 fn tail(&self) -> &Self::Tail {
71 &self.1
72 }
73
74 fn current_state(&self) -> &State<Q> {
75 self.head().state()
76 }
77
78 fn symbol(&self) -> &A {
79 self.head().symbol()
80 }
81
82 fn next_state(&self) -> &State<Q> {
83 self.tail().next_state()
84 }
85
86 fn next_symbol(&self) -> &A {
87 self.tail().write_symbol()
88 }
89}
90
91impl<Q, A> Instruction<Q, A> for Rule<Q, A>
92where
93 Q: RawState,
94{
95 type Head = Head<Q, A>;
96 type Tail = Tail<Q, A>;
97
98 fn head(&self) -> &Self::Head {
99 &self.head
100 }
101
102 fn tail(&self) -> &Self::Tail {
103 &self.tail
104 }
105
106 fn current_state(&self) -> &State<Q> {
107 self.head().state()
108 }
109
110 fn symbol(&self) -> &A {
111 self.head().symbol()
112 }
113
114 fn next_state(&self) -> &State<Q> {
115 self.tail().next_state()
116 }
117
118 fn next_symbol(&self) -> &A {
119 self.tail().write_symbol()
120 }
121}
122
123impl<Q, A> InstructionMut<Q, A> for Rule<Q, A>
124where
125 Q: RawState,
126{
127 fn head_mut(&mut self) -> &mut Self::Head {
128 &mut self.head
129 }
130
131 fn tail_mut(&mut self) -> &mut Self::Tail {
132 &mut self.tail
133 }
134
135 fn current_state_mut(&mut self) -> &mut State<Q> {
136 self.head_mut().state_mut()
137 }
138
139 fn symbol_mut(&mut self) -> &mut A {
140 self.head_mut().symbol_mut()
141 }
142
143 fn next_state_mut(&mut self) -> &mut State<Q> {
144 self.tail_mut().state_mut()
145 }
146
147 fn next_symbol_mut(&mut self) -> &mut A {
148 self.tail_mut().symbol_mut()
149 }
150}
151
152impl<Q, A> InstructionMut<Q, A> for (Head<Q, A>, Tail<Q, A>)
153where
154 Q: RawState,
155{
156 fn head_mut(&mut self) -> &mut Self::Head {
157 &mut self.0
158 }
159
160 fn tail_mut(&mut self) -> &mut Self::Tail {
161 &mut self.1
162 }
163
164 fn current_state_mut(&mut self) -> &mut State<Q> {
165 self.head_mut().state_mut()
166 }
167
168 fn symbol_mut(&mut self) -> &mut A {
169 self.head_mut().symbol_mut()
170 }
171
172 fn next_state_mut(&mut self) -> &mut State<Q> {
173 self.tail_mut().state_mut()
174 }
175
176 fn next_symbol_mut(&mut self) -> &mut A {
177 self.tail_mut().symbol_mut()
178 }
179}