facet_reflect/wip/
iset.rs1use facet_core::Field;
2
3#[derive(Clone, Copy, Default, Debug)]
5pub struct ISet(pub(crate) u64);
6
7impl ISet {
8 pub const MAX_INDEX: usize = 63;
10
11 pub fn all(fields: &[Field]) -> Self {
13 let mut iset = ISet::default();
14 for (i, _field) in fields.iter().enumerate() {
15 iset.set(i);
16 }
17 iset
18 }
19
20 pub fn set(&mut self, index: usize) {
22 if index >= 64 {
23 panic!("ISet can only track up to 64 fields. Index {index} is out of bounds.");
24 }
25 self.0 |= 1 << index;
26 }
27
28 pub fn unset(&mut self, index: usize) {
30 if index >= 64 {
31 panic!("ISet can only track up to 64 fields. Index {index} is out of bounds.");
32 }
33 self.0 &= !(1 << index);
34 }
35
36 pub fn has(&self, index: usize) -> bool {
38 if index >= 64 {
39 panic!("ISet can only track up to 64 fields. Index {index} is out of bounds.");
40 }
41 (self.0 & (1 << index)) != 0
42 }
43
44 pub fn are_all_set(&self, count: usize) -> bool {
46 if count > 64 {
47 panic!("ISet can only track up to 64 fields. Count {count} is out of bounds.");
48 }
49 let mask = (1 << count) - 1;
50 self.0 & mask == mask
51 }
52
53 pub fn is_any_set(&self) -> bool {
55 self.0 != 0
56 }
57
58 pub fn clear(&mut self) {
60 self.0 = 0;
61 }
62}