rstm_traits/
symbols.rs

1/*
2    Appellation: symbols <module>
3    Created At: 2026.01.11:13:55:56
4    Contrib: @FL03
5*/
6#[cfg(feature = "alloc")]
7use alloc::vec::Vec;
8
9/// The [`Symbolic`] trait is a marker trait used to define types that can be used as symbols in
10/// formal languages.
11pub trait Symbolic
12where
13    Self: 'static
14        + Clone
15        + Default
16        + Eq
17        + PartialOrd
18        + Send
19        + Sync
20        + core::fmt::Debug
21        + core::fmt::Display
22        + core::hash::Hash,
23{
24    private! {}
25}
26/// [`Alphabet`] describes a finite set of symbols used to construct a formal language.
27///
28/// Ideally, the alphabet should be implemented on unit enums since each symbol can be
29/// represented as a unique variant and assigned a particular value. The values of the variants
30/// may then be used as pointers, specifiying the location of the symbol w.r.t. the alphabet.
31pub trait Alphabet {
32    type Elem: Symbolic;
33
34    fn as_slice(&self) -> &[Self::Elem];
35
36    fn is_empty(&self) -> bool {
37        self.len() == 0
38    }
39
40    fn len(&self) -> usize {
41        self.as_slice().len()
42    }
43    #[cfg(feature = "alloc")]
44    /// returns the alphabet as a vector
45    fn to_vec(&self) -> Vec<Self::Elem>;
46}
47
48pub trait AlphabetMut: Alphabet {
49    fn as_mut_slice(&mut self) -> &mut [Self::Elem];
50}
51
52/*
53 ************* Implementations *************
54*/
55
56impl<S> Symbolic for S
57where
58    S: 'static
59        + Clone
60        + Default
61        + Eq
62        + Ord
63        + Send
64        + Sync
65        + core::fmt::Debug
66        + core::fmt::Display
67        + core::hash::Hash,
68{
69    seal! {}
70}
71
72impl<S> Alphabet for [S]
73where
74    S: Symbolic,
75{
76    type Elem = S;
77
78    fn as_slice(&self) -> &[S] {
79        self
80    }
81
82    fn is_empty(&self) -> bool {
83        self.is_empty()
84    }
85
86    fn len(&self) -> usize {
87        self.len()
88    }
89
90    #[cfg(feature = "alloc")]
91    fn to_vec(&self) -> Vec<S> {
92        self.to_vec()
93    }
94}
95
96impl<A> AlphabetMut for [A]
97where
98    A: Symbolic,
99{
100    fn as_mut_slice(&mut self) -> &mut [A] {
101        &mut self[..]
102    }
103}
104
105impl<S> Alphabet for &[S]
106where
107    S: Symbolic,
108{
109    type Elem = S;
110
111    fn as_slice(&self) -> &[S] {
112        self
113    }
114
115    fn is_empty(&self) -> bool {
116        <[S]>::is_empty(self)
117    }
118
119    fn len(&self) -> usize {
120        <[S]>::len(self)
121    }
122
123    #[cfg(feature = "alloc")]
124    fn to_vec(&self) -> Vec<S> {
125        <[S]>::to_vec(self)
126    }
127}
128
129impl<S> Alphabet for &mut [S]
130where
131    S: Symbolic,
132{
133    type Elem = S;
134
135    fn as_slice(&self) -> &[S] {
136        self
137    }
138
139    fn is_empty(&self) -> bool {
140        <[S]>::is_empty(self)
141    }
142
143    fn len(&self) -> usize {
144        <[S]>::len(self)
145    }
146
147    #[cfg(feature = "alloc")]
148    fn to_vec(&self) -> Vec<S> {
149        <[S]>::to_vec(self)
150    }
151}
152
153impl<S> AlphabetMut for &mut [S]
154where
155    S: Symbolic,
156{
157    fn as_mut_slice(&mut self) -> &mut [S] {
158        self
159    }
160}
161
162impl<const N: usize, S> Alphabet for [S; N]
163where
164    S: Symbolic,
165{
166    type Elem = S;
167
168    fn as_slice(&self) -> &[S] {
169        self
170    }
171
172    fn is_empty(&self) -> bool {
173        <[S]>::is_empty(self)
174    }
175
176    fn len(&self) -> usize {
177        <[S]>::len(self)
178    }
179
180    #[cfg(feature = "alloc")]
181    fn to_vec(&self) -> Vec<S> {
182        <[S]>::to_vec(self)
183    }
184}
185
186impl<const N: usize, S> AlphabetMut for [S; N]
187where
188    S: Symbolic,
189{
190    fn as_mut_slice(&mut self) -> &mut [S] {
191        &mut self[..]
192    }
193}
194
195#[cfg(feature = "alloc")]
196mod impl_alloc {
197    use super::{Alphabet, AlphabetMut, Symbolic};
198    use alloc::vec::Vec;
199
200    impl<S> Alphabet for Vec<S>
201    where
202        S: Symbolic,
203    {
204        type Elem = S;
205
206        fn as_slice(&self) -> &[S] {
207            self.as_slice()
208        }
209
210        fn is_empty(&self) -> bool {
211            self.is_empty()
212        }
213
214        fn len(&self) -> usize {
215            self.len()
216        }
217
218        fn to_vec(&self) -> Vec<S> {
219            self.clone()
220        }
221    }
222
223    impl<S> AlphabetMut for Vec<S>
224    where
225        S: Symbolic,
226    {
227        fn as_mut_slice(&mut self) -> &mut [S] {
228            self.as_mut_slice()
229        }
230    }
231}