]> git.lizzy.rs Git - enumset.git/blob - enumset/src/repr.rs
eb0f9153b3b09fedf1afb63c8c4c12cd9f630d6f
[enumset.git] / enumset / src / repr.rs
1 use core::convert::TryInto;
2 use core::fmt::{Debug};
3 use core::hash::{Hash};
4 use core::ops::*;
5
6 /// A trait marking valid underlying bitset storage types and providing the
7 /// operations `EnumSet` and related types use.
8 pub trait EnumSetTypeRepr :
9     // Basic traits used to derive traits
10     Copy +
11     Ord +
12     Eq +
13     Debug +
14     Hash +
15     // Operations used by enumset
16     BitAnd<Output = Self> +
17     BitOr<Output = Self> +
18     BitXor<Output = Self> +
19     Not<Output = Self> +
20 {
21     const WIDTH: u32;
22
23     fn is_empty(&self) -> bool;
24     fn empty() -> Self;
25
26     fn add_bit(&mut self, bit: u32);
27     fn remove_bit(&mut self, bit: u32);
28     fn has_bit(&self, bit: u32) -> bool;
29
30     fn count_ones(&self) -> u32;
31     fn count_remaining_ones(&self, cursor: u32) -> usize;
32     fn leading_zeros(&self) -> u32;
33
34     fn and_not(&self, other: Self) -> Self;
35
36     fn from_u8(v: u8) -> Self;
37     fn from_u16(v: u16) -> Self;
38     fn from_u32(v: u32) -> Self;
39     fn from_u64(v: u64) -> Self;
40     fn from_u128(v: u128) -> Self;
41     fn from_usize(v: usize) -> Self;
42
43     fn to_u8(&self) -> u8;
44     fn to_u16(&self) -> u16;
45     fn to_u32(&self) -> u32;
46     fn to_u64(&self) -> u64;
47     fn to_u128(&self) -> u128;
48     fn to_usize(&self) -> usize;
49
50     fn from_u8_opt(v: u8) -> Option<Self>;
51     fn from_u16_opt(v: u16) -> Option<Self>;
52     fn from_u32_opt(v: u32) -> Option<Self>;
53     fn from_u64_opt(v: u64) -> Option<Self>;
54     fn from_u128_opt(v: u128) -> Option<Self>;
55     fn from_usize_opt(v: usize) -> Option<Self>;
56
57     fn to_u8_opt(&self) -> Option<u8>;
58     fn to_u16_opt(&self) -> Option<u16>;
59     fn to_u32_opt(&self) -> Option<u32>;
60     fn to_u64_opt(&self) -> Option<u64>;
61     fn to_u128_opt(&self) -> Option<u128>;
62     fn to_usize_opt(&self) -> Option<usize>;
63 }
64 macro_rules! prim {
65     ($name:ty, $width:expr) => {
66         impl EnumSetTypeRepr for $name {
67             const WIDTH: u32 = $width;
68
69             fn is_empty(&self) -> bool { *self == 0 }
70             fn empty() -> Self { 0 }
71
72             fn add_bit(&mut self, bit: u32) {
73                 *self |= 1 << bit as $name;
74             }
75             fn remove_bit(&mut self, bit: u32) {
76                 *self &= !(1 << bit as $name);
77             }
78             fn has_bit(&self, bit: u32) -> bool {
79                 (self & (1 << bit as $name)) != 0
80             }
81
82             fn count_ones(&self) -> u32 { (*self).count_ones() }
83             fn leading_zeros(&self) -> u32 { (*self).leading_zeros() }
84
85             fn and_not(&self, other: Self) -> Self { (*self) & !other }
86
87             fn count_remaining_ones(&self, cursor: u32) -> usize {
88                 let left_mask =
89                     !((1 as $name).checked_shl(cursor).unwrap_or(0).wrapping_sub(1));
90                 (*self & left_mask).count_ones() as usize
91             }
92
93             fn from_u8(v: u8) -> Self { v as $name }
94             fn from_u16(v: u16) -> Self { v as $name }
95             fn from_u32(v: u32) -> Self { v as $name }
96             fn from_u64(v: u64) -> Self { v as $name }
97             fn from_u128(v: u128) -> Self { v as $name }
98             fn from_usize(v: usize) -> Self { v as $name }
99
100             fn to_u8(&self) -> u8 { (*self) as u8 }
101             fn to_u16(&self) -> u16 { (*self) as u16 }
102             fn to_u32(&self) -> u32 { (*self) as u32 }
103             fn to_u64(&self) -> u64 { (*self) as u64 }
104             fn to_u128(&self) -> u128 { (*self) as u128 }
105             fn to_usize(&self) -> usize { (*self) as usize }
106
107             fn from_u8_opt(v: u8) -> Option<Self> { v.try_into().ok() }
108             fn from_u16_opt(v: u16) -> Option<Self> { v.try_into().ok() }
109             fn from_u32_opt(v: u32) -> Option<Self> { v.try_into().ok() }
110             fn from_u64_opt(v: u64) -> Option<Self> { v.try_into().ok() }
111             fn from_u128_opt(v: u128) -> Option<Self> { v.try_into().ok() }
112             fn from_usize_opt(v: usize) -> Option<Self> { v.try_into().ok() }
113
114             fn to_u8_opt(&self) -> Option<u8> { (*self).try_into().ok() }
115             fn to_u16_opt(&self) -> Option<u16> { (*self).try_into().ok() }
116             fn to_u32_opt(&self) -> Option<u32> { (*self).try_into().ok() }
117             fn to_u64_opt(&self) -> Option<u64> { (*self).try_into().ok() }
118             fn to_u128_opt(&self) -> Option<u128> { (*self).try_into().ok() }
119             fn to_usize_opt(&self) -> Option<usize> { (*self).try_into().ok() }
120         }
121     }
122 }
123 prim!(u8  , 8  );
124 prim!(u16 , 16 );
125 prim!(u32 , 32 );
126 prim!(u64 , 64 );
127 prim!(u128, 128);