rstm_core/tail/
impl_tail.rs

1/*
2    Appellation: impl_tail <module>
3    Created At: 2025.08.30:23:58:26
4    Contrib: @FL03
5*/
6use 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    /// returns a new instance of the [`Tail`] using the given direction and head
23    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    /// returns the direction, state, and symbol as a 3-tuple
31    pub const fn as_tuple(&self) -> (Direction, &State<Q>, &A) {
32        (self.direction, &self.next_state, &self.write_symbol)
33    }
34    /// consumes the tail and returns the direction, state, and symbol as a 3-tuple
35    pub fn into_tuple(self) -> (Direction, State<Q>, A) {
36        (self.direction, self.next_state, self.write_symbol)
37    }
38    /// returns the direction the [head](StdHead) is instructed to move
39    pub const fn direction(&self) -> Direction {
40        self.direction
41    }
42    /// returns the next state with an immutable reference to the inner value
43    pub const fn state(&self) -> &State<Q> {
44        &self.next_state
45    }
46    /// returns the next state with a mutable reference to the inner value
47    pub const fn state_mut(&mut self) -> &mut State<Q> {
48        &mut self.next_state
49    }
50    /// returns the symbol the [head](Head) is instructed to write
51    pub const fn symbol(&self) -> &A {
52        &self.write_symbol
53    }
54    /// returns a mutable reference to the symbol of the tail
55    pub const fn symbol_mut(&mut self) -> &mut A {
56        &mut self.write_symbol
57    }
58    /// update the direction of the tail
59    pub fn set_direction(&mut self, direction: Direction) {
60        self.direction = direction;
61    }
62    /// update the configured state for the tail
63    pub fn set_state(&mut self, state: Q) {
64        self.next_state = State(state);
65    }
66    /// update the defined symbol for the tail
67    pub fn set_symbol(&mut self, symbol: A) {
68        self.write_symbol = symbol;
69    }
70    /// consumes the current instance to create another with the given [`Direction`]
71    pub fn with_direction(self, direction: Direction) -> Self {
72        Self { direction, ..self }
73    }
74    /// consumes the current instance to create another with the given state
75    pub fn with_state(self, state: Q) -> Self {
76        Self {
77            next_state: State(state),
78            ..self
79        }
80    }
81    /// Configures the tail with a new symbol
82    pub fn with_symbol(self, symbol: A) -> Self {
83        Self {
84            write_symbol: symbol,
85            ..self
86        }
87    }
88    /// converts a [`Tail`] reference into an owned head.
89    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    /// consumes the current tail to convert it into a [head](Head)
96    pub fn into_head(self) -> Head<Q, A> {
97        Head {
98            state: self.next_state,
99            symbol: self.write_symbol,
100        }
101    }
102    /// returns an instance of the [head](Head) where each element within
103    /// the created instance is a mutable reference
104    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    /// returns a new [`Tail`] containing mutabl references to the state and symbol
112    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}