rstm_programs/traits/
rulespace.rs

1/*
2    Appellation: rulespace <module>
3    Created At: 2025.08.30:08:03:10
4    Contrib: @FL03
5*/
6use rstm_core::{Head, Tail};
7use rstm_state::RawState;
8
9pub trait RawPoint {
10    type Key;
11    type Value;
12
13    private! {}
14}
15
16/// The [`RawSpace`] trait defines the basic interface for any compatible stores used within
17/// the crate.
18pub trait RawSpace {
19    /// The type used to index into the storage structure.
20    type Key;
21    /// The type of values stored within the structure.
22    type Value;
23
24    private! {}
25
26    /// returns the tail associated with the provided head, if it exists
27    fn get(&self, head: &Self::Key) -> Option<&Self::Value>;
28    /// returns a mutable reference to the tail associated with the provided head, if it exists
29    fn get_mut(&mut self, head: &Self::Key) -> Option<&mut Self::Value>;
30}
31/// The [`RuleSpace`] extends the [`RawSpace`] trait to introduce rule-specific functionality.
32/// It provides a method to retrieve the tail of a rule given its head.
33pub trait RuleSpace<Q, S>
34where
35    Q: RawState,
36{
37    /// returns the tail associated with the provided head, if it exists
38    fn get(&self, head: &Head<Q, S>) -> Option<&Tail<Q, S>>
39    where
40        Q: PartialEq,
41        S: PartialEq;
42    /// returns a mutable reference to the tail associated with the provided head, if it exists
43    fn get_mut(&mut self, head: &Head<Q, S>) -> Option<&mut Tail<Q, S>>
44    where
45        Q: PartialEq,
46        S: PartialEq;
47    /// inserts a new rule into the storage structure
48    fn insert(&mut self, head: Head<Q, S>, tail: Tail<Q, S>);
49}
50
51/*
52 ************* Implementations *************
53*/
54
55impl<T> RawPoint for &T
56where
57    T: RawSpace,
58{
59    type Key = T::Key;
60    type Value = T::Value;
61
62    seal! {}
63}
64
65impl<T> RawPoint for &mut T
66where
67    T: RawSpace,
68{
69    type Key = T::Key;
70    type Value = T::Value;
71
72    seal! {}
73}
74
75impl<T> RawPoint for [T] {
76    type Key = usize;
77    type Value = T;
78
79    seal! {}
80}
81
82#[cfg(feature = "alloc")]
83mod impl_alloc {
84    use super::{RawSpace, RuleSpace};
85    use alloc::vec::Vec;
86    use rstm_core::{Head, Rule, Tail};
87    use rstm_state::RawState;
88
89    impl<T> super::RawPoint for Vec<T> {
90        type Key = usize;
91        type Value = T;
92
93        seal! {}
94    }
95
96    impl<T> RawSpace for Vec<T> {
97        type Key = usize;
98        type Value = T;
99
100        seal! {}
101
102        fn get(&self, key: &Self::Key) -> Option<&Self::Value> {
103            if *key < self.len() {
104                Some(&self[*key])
105            } else {
106                None
107            }
108        }
109
110        fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value> {
111            if *key < self.len() {
112                Some(&mut self[*key])
113            } else {
114                None
115            }
116        }
117    }
118    impl<Q, A> RuleSpace<Q, A> for Vec<Rule<Q, A>>
119    where
120        Q: RawState + PartialEq,
121        A: PartialEq,
122    {
123        fn get(&self, key: &Head<Q, A>) -> Option<&Tail<Q, A>> {
124            self.iter().find_map(|rule| {
125                if rule.head() == key {
126                    Some(rule.tail())
127                } else {
128                    None
129                }
130            })
131        }
132
133        fn get_mut(&mut self, key: &Head<Q, A>) -> Option<&mut Tail<Q, A>> {
134            self.iter_mut().find_map(|rule| {
135                if rule.head() == key {
136                    Some(rule.tail_mut())
137                } else {
138                    None
139                }
140            })
141        }
142
143        fn insert(&mut self, head: Head<Q, A>, tail: Tail<Q, A>) {
144            self.push(Rule { head, tail });
145        }
146    }
147}
148
149#[cfg(feature = "std")]
150mod impl_std {
151    use super::{RawSpace, RuleSpace};
152
153    use core::hash::Hash;
154    use rstm_core::{Head, Tail};
155    use rstm_state::RawState;
156    use std::collections::HashMap;
157
158    impl<K, V> RawSpace for HashMap<K, V>
159    where
160        K: Hash + Eq,
161    {
162        type Key = K;
163        type Value = V;
164
165        seal! {}
166
167        fn get(&self, key: &Self::Key) -> Option<&Self::Value> {
168            HashMap::get(self, key)
169        }
170
171        fn get_mut(&mut self, key: &Self::Key) -> Option<&mut Self::Value> {
172            HashMap::get_mut(self, key)
173        }
174    }
175
176    impl<Q, A> RuleSpace<Q, A> for HashMap<Head<Q, A>, Tail<Q, A>>
177    where
178        Q: RawState + Eq + core::hash::Hash,
179        A: Eq + core::hash::Hash,
180    {
181        fn get(&self, key: &Head<Q, A>) -> Option<&Tail<Q, A>> {
182            self.get(key)
183        }
184
185        fn get_mut(&mut self, key: &Head<Q, A>) -> Option<&mut Tail<Q, A>> {
186            self.get_mut(key)
187        }
188
189        fn insert(&mut self, head: Head<Q, A>, tail: Tail<Q, A>) {
190            self.insert(head, tail);
191        }
192    }
193}