rstm_core/tail/
impl_tail.rs1use super::Tail;
7use crate::head::Head;
8use crate::types::Direction;
9use rstm_state::{RawState, State};
10
11impl<Q, A> Tail<Q, A>
12where
13 Q: RawState,
14{
15 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 fn from_head(direction: Direction, head: Head<Q, A>) -> Self {
24 Self {
25 direction,
26 next_state: head.state,
27 write_symbol: head.symbol,
28 }
29 }
30 pub const fn as_tuple(&self) -> (Direction, &State<Q>, &A) {
32 (self.direction, &self.next_state, &self.write_symbol)
33 }
34 pub fn into_tuple(self) -> (Direction, State<Q>, A) {
36 (self.direction, self.next_state, self.write_symbol)
37 }
38 pub const fn direction(&self) -> Direction {
40 self.direction
41 }
42 pub const fn state(&self) -> &State<Q> {
44 &self.next_state
45 }
46 pub const fn state_mut(&mut self) -> &mut State<Q> {
48 &mut self.next_state
49 }
50 pub const fn symbol(&self) -> &A {
52 &self.write_symbol
53 }
54 pub const fn symbol_mut(&mut self) -> &mut A {
56 &mut self.write_symbol
57 }
58 pub fn set_direction(&mut self, direction: Direction) {
60 self.direction = direction;
61 }
62 pub fn set_state(&mut self, state: Q) {
64 self.next_state = State(state);
65 }
66 pub fn set_symbol(&mut self, symbol: A) {
68 self.write_symbol = symbol;
69 }
70 pub fn with_direction(self, direction: Direction) -> Self {
72 Self { direction, ..self }
73 }
74 pub fn with_state(self, state: Q) -> Self {
76 Self {
77 next_state: State(state),
78 ..self
79 }
80 }
81 pub fn with_symbol(self, symbol: A) -> Self {
83 Self {
84 write_symbol: symbol,
85 ..self
86 }
87 }
88 pub const fn as_head(&self) -> Head<&Q, &A> {
90 Head {
91 state: self.next_state.view(),
92 symbol: &self.write_symbol,
93 }
94 }
95 pub fn into_head(self) -> Head<Q, A> {
97 Head {
98 state: self.next_state,
99 symbol: self.write_symbol,
100 }
101 }
102 pub const fn view(&self) -> Tail<&Q, &A> {
105 Tail {
106 direction: self.direction(),
107 next_state: self.state().view(),
108 write_symbol: self.symbol(),
109 }
110 }
111 pub const fn view_mut(&mut self) -> Tail<&mut Q, &mut A> {
113 Tail {
114 direction: self.direction,
115 next_state: self.next_state.view_mut(),
116 write_symbol: &mut self.write_symbol,
117 }
118 }
119}
120
121impl<Q, A> core::fmt::Debug for Tail<Q, A>
122where
123 Q: core::fmt::Debug,
124 A: core::fmt::Debug,
125{
126 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
127 f.debug_struct("Tail")
128 .field("direction", &self.direction)
129 .field("state", &self.next_state)
130 .field("symbol", &self.write_symbol)
131 .finish()
132 }
133}
134
135impl<Q, S> core::fmt::Display for Tail<Q, S>
136where
137 Q: core::fmt::Display,
138 S: core::fmt::Display,
139{
140 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
141 write!(
142 f,
143 "{{ direction: {}, state: {}, symbol: {} }}",
144 self.direction, self.next_state, self.write_symbol
145 )
146 }
147}