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