rstm_core/
tail.rs

1/*
2    Appellation: tail <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5//! The [`Tail`] of a rule defines the _reaction_ of the actor under specific conditions.
6//! Specifically, it defines the next state, the symbol to write, and the direction to move
7mod impl_tail;
8mod impl_tail_repr;
9
10#[allow(deprecated)]
11mod impl_deprecated;
12
13use crate::Direction;
14use rstm_state::{RawState, State};
15
16/// A type alias for a [`Tail`] containing immutable references to the next state and symbol.
17pub type TailRef<'a, Q, A> = Tail<&'a Q, &'a A>;
18/// A type alias for a [`Tail`] containing mutable references to the next state and symbol.
19pub type TailMut<'a, Q, A> = Tail<&'a mut Q, &'a mut A>;
20
21/// Converts a type into a [`Tail`] by reference.
22pub trait AsTail<Q, A>
23where
24    Q: RawState,
25{
26    fn as_tail(&self) -> Tail<Q, A>;
27
28    private! {}
29}
30/// A consuming trait for converting a type into a [`Tail`].
31pub trait IntoTail<Q, A>
32where
33    Q: RawState,
34{
35    fn into_tail(self) -> Tail<Q, A>;
36
37    private! {}
38}
39
40/// The [`Tail`] of a rule in a Turing machine
41#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
42#[cfg_attr(
43    feature = "serde",
44    derive(serde::Deserialize, serde::Serialize),
45    serde(rename_all = "camelCase")
46)]
47#[repr(C)]
48pub struct Tail<Q, A> {
49    pub direction: Direction,
50    #[cfg_attr(feature = "serde", serde(alias = "writeState"))]
51    pub next_state: State<Q>,
52    #[cfg_attr(feature = "serde", serde(alias = "nextSymbol"))]
53    pub write_symbol: A,
54}
55
56/*
57 ************* Implementations *************
58*/
59
60impl<Q, A, T> IntoTail<Q, A> for T
61where
62    Q: RawState,
63    T: Into<Tail<Q, A>>,
64{
65    fn into_tail(self) -> Tail<Q, A> {
66        self.into()
67    }
68
69    seal! {}
70}
71
72impl<Q, A> From<(Direction, State<Q>, A)> for Tail<Q, A>
73where
74    Q: RawState,
75{
76    fn from((direction, next_state, write_symbol): (Direction, State<Q>, A)) -> Self {
77        Tail {
78            direction,
79            next_state,
80            write_symbol,
81        }
82    }
83}
84
85impl<Q, A> From<(Direction, Q, A)> for Tail<Q, A>
86where
87    Q: RawState,
88{
89    fn from((direction, next_state, write_symbol): (Direction, Q, A)) -> Self {
90        Tail {
91            direction,
92            next_state: State(next_state),
93            write_symbol,
94        }
95    }
96}
97
98impl<Q, A> From<Tail<Q, A>> for (Direction, State<Q>, A)
99where
100    Q: RawState,
101{
102    fn from(tail: Tail<Q, A>) -> Self {
103        (tail.direction, tail.next_state, tail.write_symbol)
104    }
105}