1use rstm_state::{RawState, State};
6
7pub type HeadRef<'a, Q, S> = Head<&'a Q, &'a S>;
9pub type HeadMut<'a, Q, S> = Head<&'a mut Q, &'a mut S>;
11pub type HeadEntry<'a, Q, S> = Head<&'a Q, &'a mut S>;
14#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, Ord, PartialOrd)]
18#[cfg_attr(
19 feature = "serde",
20 derive(serde::Deserialize, serde::Serialize),
21 serde(rename_all = "snake_case")
22)]
23#[repr(C)]
24pub struct Head<Q, A> {
25 #[cfg_attr(feature = "serde", serde(alias = "current_state"))]
26 pub state: State<Q>,
27 #[cfg_attr(feature = "serde", serde(alias = "current_symbol"))]
28 pub symbol: A,
29}
30pub trait RawHead {
33 type State: RawState;
34 type Symbol;
35 fn state(&self) -> &State<Self::State>;
37 fn symbol(&self) -> &Self::Symbol;
39
40 private! {}
41}
42pub trait RawHeadMut: RawHead {
44 fn state_mut(&mut self) -> &mut State<Self::State>;
46 fn symbol_mut(&mut self) -> &mut Self::Symbol;
48}
49pub trait HeadRepr: RawHead + Sized {
51 fn new(state: Self::State, symbol: Self::Symbol) -> Self;
53 fn from_state(state: Self::State) -> Self
55 where
56 Self::Symbol: Default,
57 {
58 Self::new(state, Default::default())
59 }
60 fn from_symbol(symbol: Self::Symbol) -> Self
62 where
63 Self::State: Default,
64 {
65 Self::new(Default::default(), symbol)
66 }
67}
68
69impl<Q, A> RawHead for (State<Q>, A)
73where
74 Q: RawState,
75{
76 type State = Q;
77 type Symbol = A;
78 seal! {}
79 fn state(&self) -> &State<Q> {
81 &self.0
82 }
83 fn symbol(&self) -> &A {
85 &self.1
86 }
87}
88
89impl<Q, A> RawHeadMut for (State<Q>, A)
90where
91 Q: RawState,
92{
93 fn state_mut(&mut self) -> &mut State<Q> {
95 &mut self.0
96 }
97 fn symbol_mut(&mut self) -> &mut A {
99 &mut self.1
100 }
101}
102
103impl<Q, A> HeadRepr for (State<Q>, A)
104where
105 Q: RawState,
106{
107 fn new(state: Q, symbol: A) -> Self {
109 (State(state), symbol)
110 }
111}
112
113impl<Q, A> RawHead for Head<Q, A>
114where
115 Q: RawState,
116{
117 type State = Q;
118 type Symbol = A;
119 seal! {}
120 fn state(&self) -> &State<Q> {
122 &self.state
123 }
124 fn symbol(&self) -> &A {
126 &self.symbol
127 }
128}
129
130impl<Q, A> RawHeadMut for Head<Q, A>
131where
132 Q: RawState,
133{
134 fn state_mut(&mut self) -> &mut State<Q> {
136 &mut self.state
137 }
138 fn symbol_mut(&mut self) -> &mut A {
140 &mut self.symbol
141 }
142}
143
144impl<Q, A> HeadRepr for Head<Q, A>
145where
146 Q: RawState,
147{
148 fn new(state: Q, symbol: A) -> Self {
150 Self {
151 state: State(state),
152 symbol,
153 }
154 }
155}
156#[cfg(test)]
157mod tests {
158 use super::Head;
159
160 #[test]
161 fn test_head_creation() {
162 let head = Head::new("s", 0usize);
163 assert_eq! { head, ("s", 0usize) }
164 }
165}