toasty_core/stmt/
path_field_set.rs

1use bit_set::BitSet;
2use std::ops::{BitAnd, BitOr, BitOrAssign};
3
4#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct PathFieldSet {
7    container: BitSet<u32>,
8}
9
10pub struct PathFieldSetIter<'a> {
11    inner: bit_set::Iter<'a, u32>,
12    len: usize,
13}
14
15impl<'a> Iterator for PathFieldSetIter<'a> {
16    type Item = usize;
17
18    fn next(&mut self) -> Option<Self::Item> {
19        let result = self.inner.next();
20        if result.is_some() {
21            self.len -= 1;
22        }
23        result
24    }
25
26    fn size_hint(&self) -> (usize, Option<usize>) {
27        (self.len, Some(self.len))
28    }
29}
30
31impl<'a> ExactSizeIterator for PathFieldSetIter<'a> {}
32
33impl PathFieldSet {
34    pub fn new() -> Self {
35        Self::default()
36    }
37
38    pub fn from_slice<T>(fields: &[T]) -> Self
39    where
40        for<'a> &'a T: Into<usize>,
41    {
42        Self {
43            container: fields.iter().map(Into::into).collect(),
44        }
45    }
46
47    pub fn contains(&self, val: impl Into<usize>) -> bool {
48        self.container.contains(val.into())
49    }
50
51    pub fn iter(&self) -> PathFieldSetIter<'_> {
52        PathFieldSetIter {
53            inner: self.container.iter(),
54            len: self.container.len(),
55        }
56    }
57
58    pub fn is_empty(&self) -> bool {
59        self.container.is_empty()
60    }
61
62    pub fn len(&self) -> usize {
63        self.container.len()
64    }
65
66    pub fn insert(&mut self, val: usize) {
67        self.container.insert(val);
68    }
69}
70
71impl BitOr for PathFieldSet {
72    type Output = Self;
73
74    fn bitor(mut self, rhs: Self) -> Self {
75        self.container.union_with(&rhs.container);
76        self
77    }
78}
79
80impl BitOrAssign for PathFieldSet {
81    fn bitor_assign(&mut self, rhs: Self) {
82        self.container.union_with(&rhs.container);
83    }
84}
85
86impl BitAnd for PathFieldSet {
87    type Output = Self;
88
89    fn bitand(mut self, rhs: Self) -> Self {
90        self.container.intersect_with(&rhs.container);
91        self
92    }
93}
94
95impl FromIterator<usize> for PathFieldSet {
96    fn from_iter<T: IntoIterator<Item = usize>>(iter: T) -> Self {
97        Self {
98            container: BitSet::from_iter(iter),
99        }
100    }
101}