rstm_rules/rule/
impl_rule.rs

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