1use super::RuleMap;
6use crate::Rule;
7use core::hash::Hash;
8use rstm_core::{Head, Tail};
9use rstm_state::RawState;
10use std::collections::hash_map;
11
12impl<Q, S> RuleMap<Q, S>
13where
14 Q: RawState + Eq + Hash,
15 S: Eq + Hash,
16{
17}
18
19impl<Q, S> core::ops::Index<Head<Q, S>> for RuleMap<Q, S>
20where
21 Q: RawState + Eq + Hash,
22 S: Eq + Hash,
23{
24 type Output = Tail<Q, S>;
25
26 fn index(&self, head: Head<Q, S>) -> &Self::Output {
27 self.get(&head).expect("Rule not found")
28 }
29}
30impl<Q, S> Extend<(Head<Q, S>, Tail<Q, S>)> for RuleMap<Q, S>
31where
32 Q: RawState + Eq + Hash,
33 S: Eq + Hash,
34{
35 fn extend<I>(&mut self, iter: I)
36 where
37 I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
38 {
39 for (head, tail) in iter {
40 self.insert(head, tail);
41 }
42 }
43}
44
45impl<Q, S> Extend<Rule<Q, S>> for RuleMap<Q, S>
46where
47 Q: RawState + Eq + Hash,
48 S: Eq + Hash,
49{
50 fn extend<I>(&mut self, iter: I)
51 where
52 I: IntoIterator<Item = Rule<Q, S>>,
53 {
54 for rule in iter {
55 self.insert(rule.head, rule.tail);
56 }
57 }
58}
59
60impl<Q, S> FromIterator<(Head<Q, S>, Tail<Q, S>)> for RuleMap<Q, S>
61where
62 Q: RawState + Default + Eq + Hash,
63 S: Eq + Hash,
64{
65 fn from_iter<I>(iter: I) -> Self
66 where
67 I: IntoIterator<Item = (Head<Q, S>, Tail<Q, S>)>,
68 {
69 Self::from_iter(iter)
70 }
71}
72
73impl<Q, S> FromIterator<Rule<Q, S>> for RuleMap<Q, S>
74where
75 Q: RawState + Default + Eq + Hash,
76 S: Eq + Hash,
77{
78 fn from_iter<I>(iter: I) -> Self
79 where
80 I: IntoIterator<Item = Rule<Q, S>>,
81 {
82 Self::from_rules(iter)
83 }
84}
85
86impl<Q, S> IntoIterator for RuleMap<Q, S>
87where
88 Q: RawState + Eq + Hash,
89 S: Eq + Hash,
90{
91 type Item = (Head<Q, S>, Tail<Q, S>);
92 type IntoIter = hash_map::IntoIter<Head<Q, S>, Tail<Q, S>>;
93
94 fn into_iter(self) -> Self::IntoIter {
95 self.rules.into_iter()
96 }
97}
98
99impl<'a, Q, S> IntoIterator for &'a RuleMap<Q, S>
100where
101 Q: RawState + Eq + Hash,
102 S: Eq + Hash,
103{
104 type Item = (&'a Head<Q, S>, &'a Tail<Q, S>);
105 type IntoIter = hash_map::Iter<'a, Head<Q, S>, Tail<Q, S>>;
106
107 fn into_iter(self) -> Self::IntoIter {
108 self.rules.iter()
109 }
110}
111
112impl<'a, Q, S> IntoIterator for &'a mut RuleMap<Q, S>
113where
114 Q: RawState + Eq + Hash,
115 S: Eq + Hash,
116{
117 type Item = (&'a Head<Q, S>, &'a mut Tail<Q, S>);
118 type IntoIter = hash_map::IterMut<'a, Head<Q, S>, Tail<Q, S>>;
119
120 fn into_iter(self) -> Self::IntoIter {
121 self.rules.iter_mut()
122 }
123}