Skip to main content

rstm_core/rules/impls/
impl_head_repr.rs

1/*
2    Appellation: impl_head_repr <module>
3    Created At: 2025.08.31:00:01:56
4    Contrib: @FL03
5*/
6use crate::Direction;
7use crate::actors::EngineBase;
8use crate::rules::{Head, HeadMut, HeadRef};
9use rstm_state::RawState;
10
11impl<'a, Q, S> HeadRef<'a, Q, S>
12where
13    Q: RawState,
14{
15    /// returns a new [`Head`] with cloned elements
16    pub fn cloned(&self) -> Head<Q, S>
17    where
18        Q: Clone,
19        S: Clone,
20    {
21        Head {
22            state: self.state.cloned(),
23            symbol: self.symbol.clone(),
24        }
25    }
26    /// returns a new [`Head`] with copied elements
27    pub const fn copied(&self) -> Head<Q, S>
28    where
29        Q: Copy,
30        S: Copy,
31    {
32        Head {
33            state: self.state().copied(),
34            symbol: *self.symbol,
35        }
36    }
37}
38
39impl<'a, Q, S> HeadMut<'a, Q, S>
40where
41    Q: RawState,
42{
43    /// returns a new [`Head`] with cloned elements
44    pub fn cloned(&self) -> Head<Q, S>
45    where
46        Q: Clone,
47        S: Clone,
48    {
49        Head {
50            state: self.state.cloned(),
51            symbol: self.symbol.clone(),
52        }
53    }
54    /// returns a new [`Head`] with copied elements
55    pub fn copied(&self) -> Head<Q, S>
56    where
57        Q: Copy,
58        S: Copy,
59    {
60        Head {
61            state: self.state.copied(),
62            symbol: *self.symbol,
63        }
64    }
65}
66
67impl<Q> Head<Q, usize>
68where
69    Q: RawState,
70{
71    /// load the head into an engine loaded with the given program
72    pub fn load<A>(self, program: crate::Program<Q, A>) -> EngineBase<Self, Q, A>
73    where
74        Q: PartialEq,
75        A: PartialEq,
76    {
77        EngineBase::from_driver(self).with_program(program)
78    }
79
80    pub fn shift(self, direction: Direction) -> Self {
81        Self {
82            symbol: direction.apply_unsigned(self.symbol),
83            ..self
84        }
85    }
86
87    pub fn shift_inplace(&mut self, direction: Direction) {
88        self.symbol = direction.apply_unsigned(self.symbol);
89    }
90}