rstm_core/traits/
symbols.rs

1/*
2    Appellation: symbolic <module>
3    Contrib: FL03 <jo3mccain@icloud.com>
4*/
5
6/// [Alphabet] describes a finite set of symbols used to construct a formal language.
7///
8/// Ideally, the alphabet should be implemented on unit enums since
9/// each symbol can be represented as a unique variant and assigned
10/// a particular value. The values of the variants may then be used
11/// as pointers, specifiying the location of the symbol w.r.t. the
12/// alphabet.
13pub trait Alphabet {
14    type Elem;
15
16    fn as_slice(&self) -> &[Self::Elem];
17
18    fn as_mut_slice(&mut self) -> &mut [Self::Elem];
19
20    fn is_empty(&self) -> bool {
21        self.len() == 0
22    }
23
24    fn len(&self) -> usize {
25        self.as_slice().len()
26    }
27
28    fn to_vec(&self) -> Vec<Self::Elem>;
29}
30
31/// [Symbolic] is a trait denoting types that can be used as symbols;
32/// this is useful for allowing symbols to represented with [char] or
33/// be a position on the tape, value mapping for an alphabet,.
34pub trait Symbolic
35where
36    Self: Clone
37        + Copy
38        + Default
39        + Eq
40        + Ord
41        + PartialEq
42        + PartialOrd
43        + core::fmt::Debug
44        + core::fmt::Display
45        + core::hash::Hash
46        + Send
47        + Sync
48        + 'static,
49{
50}
51
52/*
53 ************* Implementations *************
54*/
55#[cfg(feature = "alloc")]
56use alloc::vec::Vec;
57
58impl<S: Symbolic> Alphabet for [S] {
59    type Elem = S;
60
61    fn as_slice(&self) -> &[S] {
62        self
63    }
64
65    fn as_mut_slice(&mut self) -> &mut [S] {
66        self
67    }
68
69    fn is_empty(&self) -> bool {
70        self.is_empty()
71    }
72
73    fn len(&self) -> usize {
74        self.len()
75    }
76
77    fn to_vec(&self) -> Vec<S> {
78        self.to_vec()
79    }
80}
81
82#[cfg(feature = "alloc")]
83impl<S: Symbolic> Alphabet for Vec<S> {
84    type Elem = S;
85
86    fn as_slice(&self) -> &[S] {
87        self.as_slice()
88    }
89
90    fn as_mut_slice(&mut self) -> &mut [S] {
91        self.as_mut_slice()
92    }
93
94    fn is_empty(&self) -> bool {
95        self.is_empty()
96    }
97
98    fn len(&self) -> usize {
99        self.len()
100    }
101
102    fn to_vec(&self) -> Vec<S> {
103        self.clone()
104    }
105}
106
107impl<S> Symbolic for S where
108    S: Copy
109        + Default
110        + Eq
111        + Ord
112        + core::fmt::Debug
113        + core::fmt::Display
114        + core::hash::Hash
115        + Send
116        + Sync
117        + 'static
118{
119}