Skip to main content

rstm_actors/engine/
turing_engine.rs

1/*
2    Appellation: engine <module>
3    Created At: 2025.08.31:14:49:50
4    Contrib: @FL03
5*/
6mod impl_turing_engine;
7
8#[allow(deprecated)]
9mod impl_deprecated;
10
11use crate::tmh::TMH;
12use alloc::vec::Vec;
13use rstm_programs::Program;
14use rstm_state::{RawState, State};
15
16/// The [`TuringEngine`] implementation is essentially a runtime for Turing machine, allowing
17pub struct TuringEngine<'a, Q, A>
18where
19    Q: RawState,
20{
21    /// the actor that will be executing the program
22    pub(crate) driver: &'a mut TMH<Q, A>,
23    /// the program being executed
24    pub(crate) program: Option<Program<Q, A>>,
25    /// the number of cycles executed; independent of the position of the head on the tape
26    pub(crate) cycles: usize,
27    pub(crate) _inputs: Vec<A>,
28}
29
30impl<'a, Q, A> TuringEngine<'a, Q, A>
31where
32    Q: RawState,
33{
34    pub const fn new(driver: &'a mut TMH<Q, A>) -> Self {
35        Self {
36            driver,
37            _inputs: Vec::new(),
38            program: None,
39            cycles: 0,
40        }
41    }
42    /// consumes the instance to return another loaded up with the given program
43    pub fn load_with(self, program: Program<Q, A>) -> Self {
44        TuringEngine {
45            program: Some(program),
46            ..self
47        }
48    }
49    /// returns a reference to the actor
50    pub const fn driver(&self) -> &TMH<Q, A> {
51        self.driver
52    }
53    /// returns a mutable reference to the actor
54    pub const fn driver_mut(&mut self) -> &mut TMH<Q, A> {
55        self.driver
56    }
57    #[doc(hidden)]
58    /// returns a reference to the inputs
59    pub const fn inputs(&self) -> &Vec<A> {
60        &self._inputs
61    }
62    /// returns a reference to the program
63    pub fn program(&self) -> crate::Result<&Program<Q, A>> {
64        self.program.as_ref().ok_or(crate::Error::NoProgram)
65    }
66    /// returns a mutable reference to the program
67    pub fn program_mut(&mut self) -> crate::Result<&mut Program<Q, A>> {
68        self.program.as_mut().ok_or(crate::Error::NoProgram)
69    }
70    /// returns a copy of the total number of cycles, or steps, the engine has preformed
71    pub const fn cycles(&self) -> usize {
72        self.cycles
73    }
74    /// returns a mutable reference to the current steps
75    pub const fn current_state(&self) -> &State<Q> {
76        self.driver().state()
77    }
78    /// returns true if the engine has a program loaded
79    pub const fn has_program(&self) -> bool {
80        self.program.is_some()
81    }
82}