Skip to main content

rstm_core/actors/traits/
raw_driver.rs

1/*
2    Appellation: raw_driver <module>
3    Created At: 2025.08.31:00:17:44
4    Contrib: @FL03
5*/
6use crate::rules::{Head, Tail};
7use rspace_traits::RawSpace;
8use rstm_state::{RawState, State};
9use rstm_traits::{Read, TryExecute};
10
11/// The [`Driver`] is the basis for all compatible actors within the system. Each
12/// implementation is required to define the _type_ of internal store it will use to
13/// manage its data. This abstraction allows for flexibility in the choice of data structures,
14/// enabling the actor to adapt to various use cases and performance requirements.
15pub trait Driver<Q, A>
16where
17    for<'a> Self: Read<&'a mut [A], Output = &'a A>,
18{
19    private! {}
20    /// returns the current position of the driver.
21    fn current_position(&self) -> usize;
22    /// returns a view of the current state of the driver.
23    fn current_state(&self) -> State<&Q>;
24}
25
26/// An [`Actor`] is a particular kind of driver capable of maintaining its own internal store.
27pub trait Actor<Q, A>: Driver<Q, A> + TryExecute<Tail<Q, A>>
28where
29    for<'a> Self: Read<&'a mut [A], Output = &'a A>,
30{
31    type Tape<_T>: RawSpace<Elem = _T>;
32    /// returns a reference to the driver's internal store
33    fn store(&self) -> &Self::Tape<A>;
34    /// returns a mutable reference to the internal store
35    fn store_mut(&mut self) -> &mut Self::Tape<A>;
36    /// [`replace`](core::mem::replace) the current store with another before returning the previous value
37    fn replace_store(&mut self, store: Self::Tape<A>) -> Self::Tape<A> {
38        core::mem::replace(self.store_mut(), store)
39    }
40    /// set the store to the given value
41    fn set_store(&mut self, store: Self::Tape<A>) {
42        *self.store_mut() = store;
43    }
44    /// [`swap`](core::mem::swap) the current store with another
45    fn swap_store(&mut self, store: &mut Self::Tape<A>) {
46        core::mem::swap(self.store_mut(), store);
47    }
48    /// [`take`](core::mem::take) the current store, leaving the default value in its place
49    fn take_store(&mut self) -> Self::Tape<A>
50    where
51        Self::Tape<A>: Default,
52    {
53        core::mem::take(self.store_mut())
54    }
55}
56
57impl<Q, A> Driver<Q, A> for Head<Q, usize>
58where
59    Q: RawState,
60    for<'a> Self: Read<&'a mut [A], Output = &'a A>,
61{
62    seal! {}
63
64    fn current_position(&self) -> usize {
65        self.symbol
66    }
67
68    fn current_state(&self) -> State<&Q> {
69        self.state.view()
70    }
71}