Skip to main content

rstm_core/rules/impls/
impl_rule.rs

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