rstm_core/rule/
impl_rule.rs

1/*
2    appellation: impl_rule <module>
3    authors: @FL03
4*/
5use super::{Rule, RuleBuilder};
6
7use crate::{Direction, Head, Tail};
8use rstm_state::{RawState, State};
9
10impl<Q, A> Rule<Q, A>
11where
12    Q: RawState,
13{
14    /// returns a new instance of the [`Rule`] from the given head and tail
15    pub const fn new(head: Head<Q, A>, tail: Tail<Q, A>) -> Self {
16        Self { head, tail }
17    }
18    /// returns a new instance of a [`RuleBuilder`] for constructing a new [`Rule`]
19    pub const fn create() -> RuleBuilder<Q, A> {
20        RuleBuilder::new()
21    }
22    /// initialize a new instance of the [`Rule`] from its consituent parts
23    pub const fn from_parts(
24        state: Q,
25        symbol: A,
26        direction: Direction,
27        next_state: Q,
28        write_symbol: A,
29    ) -> Self {
30        let head = Head::new(state, symbol);
31        let tail = Tail::new(direction, next_state, write_symbol);
32        Self::new(head, tail)
33    }
34    /// returns an immutable reference to the [Head]
35    pub const fn head(&self) -> &Head<Q, A> {
36        &self.head
37    }
38    /// returns a mutable reference to the [Head]
39    pub const fn head_mut(&mut self) -> &mut Head<Q, A> {
40        &mut self.head
41    }
42    /// returns an immutable reference to the [Tail] of the [Instruction]
43    pub const fn tail(&self) -> &Tail<Q, A> {
44        &self.tail
45    }
46    /// returns a mutable reference to the [Tail] of the [Instruction]
47    pub const fn tail_mut(&mut self) -> &mut Tail<Q, A> {
48        &mut self.tail
49    }
50    /// returns an instance of the [Head] whose elements are immutable references
51    pub const fn head_view(&self) -> Head<&'_ Q, &'_ A> {
52        self.head().view()
53    }
54    /// returns an instance of the [Tail] whose elements are immutable references
55    pub const fn tail_view(&self) -> Tail<&'_ Q, &'_ A> {
56        self.tail().view()
57    }
58    /// returns the direction of the shift
59    pub const fn direction(&self) -> Direction {
60        self.tail().direction()
61    }
62    /// returns the current [State] of the system
63    pub const fn state(&self) -> &State<Q> {
64        self.head().state()
65    }
66    /// returns a mutable reference to the current [State] of the system
67    pub const fn state_mut(&mut self) -> &mut State<Q> {
68        self.head_mut().state_mut()
69    }
70    /// returns the symbol of the [Head]
71    pub const fn symbol(&self) -> &A {
72        self.head().symbol()
73    }
74    /// returns a mutable reference to the symbol of the [`Head`]
75    pub const fn symbol_mut(&mut self) -> &mut A {
76        self.head_mut().symbol_mut()
77    }
78    /// returns the next [State] of the system
79    pub const fn next_state(&self) -> &State<Q> {
80        self.tail().state()
81    }
82    /// returns a mutable reference to the next [State] of the system
83    pub const fn next_state_mut(&mut self) -> &mut State<Q> {
84        self.tail_mut().state_mut()
85    }
86    /// returns the symbol which will be written by the [Head]
87    pub const fn next_symbol(&self) -> &A {
88        self.tail().symbol()
89    }
90    /// returns a mutable reference to the next symbol
91    pub const fn next_symbol_mut(&mut self) -> &mut A {
92        self.tail_mut().symbol_mut()
93    }
94    /// updates the current [Direction] and returns a mutable reference to the [Rule]
95    pub fn set_direction(&mut self, direction: Direction) -> &mut Self {
96        self.tail_mut().set_direction(direction);
97        self
98    }
99    /// update the current symbol and return a mutable reference to the [Rule]
100    pub fn set_symbol(&mut self, symbol: A) -> &mut Self {
101        self.head_mut().set_symbol(symbol);
102        self
103    }
104    /// updates the current [State] and returns a mutable reference to the [Rule]
105    pub fn set_state(&mut self, state: Q) -> &mut Self {
106        self.head_mut().set_state(state);
107        self
108    }
109    /// updates the current [State] and returns a mutable reference to the [Rule]
110    pub fn set_next_state(&mut self, state: Q) -> &mut Self {
111        self.tail_mut().set_state(state);
112        self
113    }
114    /// updates the next symbol and returns a mutable reference to the [Rule]
115    pub fn set_next_symbol(&mut self, symbol: A) -> &mut Self {
116        self.tail_mut().set_symbol(symbol);
117        self
118    }
119    /// updates the current [State] and symbol and returns a mutable reference to the [Rule]
120    pub fn set_head(&mut self, state: Q, symbol: A) -> &mut Self {
121        self.head_mut().set_state(state);
122        self.head_mut().set_symbol(symbol);
123        self
124    }
125    /// updates the current [State] and symbol and returns a mutable reference to the [Rule]
126    pub fn set_tail(&mut self, state: Q, symbol: A) -> &mut Self {
127        self.tail_mut().set_state(state);
128        self.tail_mut().set_symbol(symbol);
129        self
130    }
131    /// returns the next [Head] of the system
132    pub const fn next_head(&self) -> Head<&'_ Q, &'_ A> {
133        self.tail().as_head()
134    }
135    /// consumes the current object and returns the next [Head] of the system
136    pub fn into_next_head(self) -> Head<Q, A> {
137        self.tail.into_head()
138    }
139    /// returns the value which for which the current object will be replaced with
140    pub const fn write_symbol(&self) -> &A {
141        self.tail().symbol()
142    }
143    /// consumes the current object and returns a 2-tuple consisting of the [Head] and [Tail]
144    pub fn into_tuple(self) -> (Head<Q, A>, Tail<Q, A>) {
145        (self.head, self.tail)
146    }
147    /// returns a new instance of the [`Rule`] with cloned elements
148    pub fn cloned(&self) -> Rule<Q, A>
149    where
150        Q: Clone,
151        A: Clone,
152    {
153        Rule {
154            head: self.head.clone(),
155            tail: self.tail.clone(),
156        }
157    }
158    /// returns a new instance of the [`Rule`] with copied elements
159    pub fn copied(&self) -> Rule<Q, A>
160    where
161        Q: Clone,
162        A: Clone,
163    {
164        Rule {
165            head: self.head.clone(),
166            tail: self.tail.clone(),
167        }
168    }
169}