Skip to main content

rstm_core/rules/impls/
impl_tail.rs

1/*
2    Appellation: impl_tail <module>
3    Created At: 2025.08.30:23:58:26
4    Contrib: @FL03
5*/
6use 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    /// initializes a new [`Tail`] with the given direction, state, and symbol
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    /// initializes a new [`Tail`] at rest using the given state and symbol
23    pub const fn stay(state: Q, symbol: A) -> Self {
24        Self::new(Direction::Stay, state, symbol)
25    }
26    /// initializes a new [`Tail`] moving to the left using the given state and symbol
27    pub const fn left(state: Q, symbol: A) -> Self {
28        Self::new(Direction::Left, state, symbol)
29    }
30    /// initializes a new [`Tail`] moving to the right using the given state and symbol
31    pub const fn right(state: Q, symbol: A) -> Self {
32        Self::new(Direction::Right, state, symbol)
33    }
34    /// returns a new instance of the [`Tail`] using the given direction and head
35    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    /// returns the direction, state, and symbol as a 3-tuple
50    pub const fn as_tuple(&self) -> (Direction, &State<Q>, &A) {
51        (self.direction, &self.next_state, &self.write_symbol)
52    }
53    /// consumes the tail and returns the direction, state, and symbol as a 3-tuple
54    pub fn into_tuple(self) -> (Direction, State<Q>, A) {
55        (self.direction, self.next_state, self.write_symbol)
56    }
57    /// returns a copy of the direction the head should *shift* after writing the symbol and
58    /// updating the state.
59    pub const fn direction(&self) -> Direction {
60        self.direction
61    }
62    /// returns the next state with an immutable reference to the inner value
63    pub const fn state(&self) -> &State<Q> {
64        &self.next_state
65    }
66    /// returns the next state with a mutable reference to the inner value
67    pub const fn state_mut(&mut self) -> &mut State<Q> {
68        &mut self.next_state
69    }
70    /// returns the symbol the [head](Head) is instructed to write
71    pub const fn symbol(&self) -> &A {
72        &self.write_symbol
73    }
74    /// returns a mutable reference to the symbol of the tail
75    pub const fn symbol_mut(&mut self) -> &mut A {
76        &mut self.write_symbol
77    }
78    /// update the direction of the tail
79    pub const fn set_direction(&mut self, direction: Direction) {
80        self.direction = direction;
81    }
82    /// update the configured state for the tail
83    pub fn set_state(&mut self, state: Q) {
84        self.next_state = State(state);
85    }
86    /// update the defined symbol for the tail
87    pub fn set_symbol(&mut self, symbol: A) {
88        self.write_symbol = symbol;
89    }
90    /// consumes the current instance to create another with the given [`Direction`]
91    pub fn with_direction(self, direction: Direction) -> Self {
92        Self { direction, ..self }
93    }
94    /// consumes the current instance to create another with the given state
95    pub fn with_state(self, state: Q) -> Self {
96        Self {
97            next_state: State(state),
98            ..self
99        }
100    }
101    /// Configures the tail with a new symbol
102    pub fn with_symbol(self, symbol: A) -> Self {
103        Self {
104            write_symbol: symbol,
105            ..self
106        }
107    }
108    /// returns an instance of the [`Head`] containing references to the next state and symbol.
109    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    /// returns a new [`Head`] initialized with mutable references to the state and symbol
116    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    /// consumes the current instance of the tail to convert it into a [`Head`] containing the
123    /// next state and symbol to write.
124    pub fn into_head(self) -> Head<Q, A> {
125        Head {
126            state: self.next_state,
127            symbol: self.write_symbol,
128        }
129    }
130    /// convert the tail into a 2-tuple consisting of a [`Direction`] and a [`Head`]
131    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    /// returns an instance of the [head](Head) where each element within
146    /// the created instance is a mutable reference
147    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    /// returns a new [`Tail`] containing mutabl references to the state and symbol
155    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}