rstm_core/rules/traits/
rulespace.rs1use crate::{Head, Rule};
7use rspace_traits::RawSpace;
8use rstm_state::{RawState, State};
9
10pub trait HeadSpace<Q, S>: RawSpace<Elem = Head<Q, S>> {}
12pub trait RuleSpace<Q, S>
17where
18 Q: RawState,
19{
20 type Space<_Q, _S>: ?Sized + HeadSpace<_Q, _S>;
21
22 fn get_rule(&self, state: &Q, symbol: &S) -> Option<&Rule<Q, S>>;
24}
25
26impl<Q, S> HeadSpace<Q, S> for [Head<Q, S>] {}
31
32impl<Q, S> RuleSpace<Q, S> for [Rule<Q, S>]
33where
34 Q: RawState + PartialEq,
35 S: PartialEq,
36{
37 type Space<_Q, _S> = dyn HeadSpace<_Q, _S>;
38
39 fn get_rule(&self, state: &Q, symbol: &S) -> Option<&Rule<Q, S>> {
40 self.iter().find(|&Rule { head, .. }| {
41 head.state().view() == State(state) && head.symbol() == symbol
42 })
43 }
44}
45
46#[cfg(feature = "alloc")]
47mod impl_alloc {
48 use super::{HeadSpace, RuleSpace};
49 use crate::{Head, Rule};
50 use alloc::vec::Vec;
51 use rstm_state::{RawState, State};
52
53 impl<Q, A> HeadSpace<Q, A> for Vec<Head<Q, A>> {}
54
55 impl<Q, A> RuleSpace<Q, A> for Vec<Rule<Q, A>>
56 where
57 Q: RawState + PartialEq,
58 A: PartialEq,
59 {
60 type Space<_Q, _S> = dyn HeadSpace<_Q, _S>;
61
62 fn get_rule(&self, state: &Q, symbol: &A) -> Option<&Rule<Q, A>> {
63 self.iter().find(|&Rule { head, .. }| {
64 head.state().view() == State(state) && head.symbol() == symbol
65 })
66 }
67 }
68}