/rust/registry/src/index.crates.io-6f17d22bba15001f/yansi-1.0.1/src/set.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::fmt; |
2 | | use core::marker::PhantomData; |
3 | | |
4 | | pub struct Set<T>(PhantomData<T>, pub(crate) u16); |
5 | | |
6 | | pub trait SetMember: Copy + fmt::Debug { |
7 | | const MAX_VALUE: u8; |
8 | | |
9 | | fn bit_mask(self) -> u16; |
10 | | fn from_bit_mask(value: u16) -> Option<Self>; |
11 | | } |
12 | | |
13 | | impl<T: SetMember> Set<T> { |
14 | | pub const EMPTY: Self = Set(PhantomData, 0); |
15 | | |
16 | 0 | pub fn contains(self, value: T) -> bool { |
17 | 0 | (value.bit_mask() & self.1) == value.bit_mask() |
18 | 0 | } Unexecuted instantiation: <yansi::set::Set<yansi::attr_quirk::Quirk>>::contains Unexecuted instantiation: <yansi::set::Set<yansi::attr_quirk::Attribute>>::contains |
19 | | |
20 | 0 | pub const fn iter(self) -> Iter<T> { |
21 | 0 | Iter { index: 0, set: self } |
22 | 0 | } |
23 | | } |
24 | | |
25 | | pub struct Iter<T> { |
26 | | index: u8, |
27 | | set: Set<T>, |
28 | | } |
29 | | |
30 | | impl<T: SetMember> Iterator for Iter<T> { |
31 | | type Item = T; |
32 | | |
33 | 0 | fn next(&mut self) -> Option<Self::Item> { |
34 | 0 | while self.index <= T::MAX_VALUE { |
35 | 0 | let mask: u16 = 1 << self.index; |
36 | 0 |
|
37 | 0 | self.index += 1; |
38 | 0 | if let Some(v) = T::from_bit_mask(mask) { |
39 | 0 | if self.set.contains(v) { |
40 | 0 | return Some(v); |
41 | 0 | } |
42 | 0 | } |
43 | | } |
44 | | |
45 | 0 | None |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl<T: SetMember> Default for Set<T> { |
50 | 0 | fn default() -> Self { |
51 | 0 | Set::EMPTY |
52 | 0 | } |
53 | | } |
54 | | |
55 | | impl<T: SetMember> fmt::Debug for Set<T> { |
56 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
57 | 0 | f.debug_set().entries(self.iter()).finish() |
58 | 0 | } |
59 | | } |
60 | | |
61 | | impl<T> Copy for Set<T> { } |
62 | | |
63 | | impl<T> Clone for Set<T> { |
64 | 0 | fn clone(&self) -> Self { |
65 | 0 | Self(self.0, self.1) |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl<T> PartialEq for Set<T> { |
70 | 0 | fn eq(&self, other: &Self) -> bool { |
71 | 0 | self.1 == other.1 |
72 | 0 | } |
73 | | } |
74 | | |
75 | | impl<T> Eq for Set<T> { } |
76 | | |
77 | | impl<T> PartialOrd for Set<T> { |
78 | 0 | fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
79 | 0 | self.1.partial_cmp(&other.1) |
80 | 0 | } |
81 | | } |
82 | | |
83 | | impl<T> Ord for Set<T> { |
84 | 0 | fn cmp(&self, other: &Self) -> core::cmp::Ordering { |
85 | 0 | self.1.cmp(&other.1) |
86 | 0 | } |
87 | | } |
88 | | |
89 | | impl<T> core::hash::Hash for Set<T> { |
90 | 0 | fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
91 | 0 | self.1.hash(state); |
92 | 0 | } |
93 | | } |