turing_machine_rs/instruction/
state.rs

1use std::fmt::{Display, Error, Formatter};
2use std::ops::{Add, AddAssign};
3
4/// Wrapper around the [`usize`] type. It is necessary for the decrease in value
5/// mismatching (when several values have the same type but two different meanings)
6/// but without the type limit.
7///
8/// Earlier implementations used the [`u32`] type when [`usize`] was only used for
9/// indexes.Now there is no limit for type usage (it was a manual limit against
10/// possible program bugs).
11#[derive(Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub struct State(pub usize);
13
14impl Display for State {
15    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
16        write!(f, "{}", self.0)
17    }
18}
19
20impl Add for State {
21    type Output = State;
22
23    fn add(self, rhs: Self) -> Self {
24        State(self.0 + rhs.0)
25    }
26}
27
28impl AddAssign for State {
29    fn add_assign(&mut self, other: Self) {
30        self.0 += other.0;
31    }
32}