rstm_core/
head.rs

1/*
2    Appellation: head <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5mod impl_head;
6mod impl_head_ext;
7mod impl_head_repr;
8
9#[allow(deprecated)]
10mod impl_deprecated;
11
12use rstm_state::{RawState, State};
13
14/// a type alias for a [`Head`] containing immutable references to its state and symbol
15pub type HeadRef<'a, Q, S> = Head<&'a Q, &'a S>;
16/// a type alias for a [`Head`] containing mutable references to its state and symbol
17pub type HeadMut<'a, Q, S> = Head<&'a mut Q, &'a mut S>;
18
19/// Converts to a [`Head`] by reference.
20pub trait AsHead<Q, A>
21where
22    Q: RawState,
23{
24    fn as_head(&self) -> Head<Q, A>;
25}
26/// Consumes the caller to convert it into a [`Head`].
27pub trait IntoHead<Q, A>
28where
29    Q: RawState,
30{
31    fn into_head(self) -> Head<Q, A>;
32}
33
34/// The [`Head`] of a Turing machine is defined to be a two-tuple consisting of a state and a
35/// symbol. Our implementation is generic over both the state and symbol types, allowing for
36/// flexibility in their representation(s).
37#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
38#[cfg_attr(
39    feature = "serde",
40    derive(serde::Deserialize, serde::Serialize),
41    serde(rename_all = "camelCase")
42)]
43#[repr(C)]
44pub struct Head<Q, S> {
45    #[cfg_attr(feature = "serde", serde(alias = "currentState"))]
46    pub state: State<Q>,
47    #[cfg_attr(feature = "serde", serde(alias = "currentSymbol"))]
48    pub symbol: S,
49}
50
51/*
52 ************* Implementations *************
53*/
54
55impl<Q, A, T> IntoHead<Q, A> for T
56where
57    Q: RawState,
58    T: Into<Head<Q, A>>,
59{
60    fn into_head(self) -> Head<Q, A> {
61        self.into()
62    }
63}
64
65impl<Q, S> From<(Q, S)> for Head<Q, S>
66where
67    Q: RawState,
68{
69    fn from((state, symbol): (Q, S)) -> Self {
70        Self::new(state, symbol)
71    }
72}
73
74impl<Q, S> From<(State<Q>, S)> for Head<Q, S>
75where
76    Q: RawState,
77{
78    fn from((state, symbol): (State<Q>, S)) -> Self {
79        Head { state, symbol }
80    }
81}
82
83impl<Q, S> From<Head<Q, S>> for (State<Q>, S)
84where
85    Q: RawState,
86{
87    fn from(Head { state, symbol }: Head<Q, S>) -> Self {
88        (state, symbol)
89    }
90}