1 use core::convert::TryInto;
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
15 // Operations used by enumset
16 BitAnd<Output = Self> +
17 BitOr<Output = Self> +
18 BitXor<Output = Self> +
23 fn is_empty(&self) -> bool;
26 fn add_bit(&mut self, bit: u32);
27 fn remove_bit(&mut self, bit: u32);
28 fn has_bit(&self, bit: u32) -> bool;
30 fn count_ones(&self) -> u32;
31 fn count_remaining_ones(&self, cursor: u32) -> usize;
32 fn leading_zeros(&self) -> u32;
33 fn trailing_zeros(&self) -> u32;
35 fn and_not(&self, other: Self) -> Self;
37 fn from_u8(v: u8) -> Self;
38 fn from_u16(v: u16) -> Self;
39 fn from_u32(v: u32) -> Self;
40 fn from_u64(v: u64) -> Self;
41 fn from_u128(v: u128) -> Self;
42 fn from_usize(v: usize) -> Self;
44 fn to_u8(&self) -> u8;
45 fn to_u16(&self) -> u16;
46 fn to_u32(&self) -> u32;
47 fn to_u64(&self) -> u64;
48 fn to_u128(&self) -> u128;
49 fn to_usize(&self) -> usize;
51 fn from_u8_opt(v: u8) -> Option<Self>;
52 fn from_u16_opt(v: u16) -> Option<Self>;
53 fn from_u32_opt(v: u32) -> Option<Self>;
54 fn from_u64_opt(v: u64) -> Option<Self>;
55 fn from_u128_opt(v: u128) -> Option<Self>;
56 fn from_usize_opt(v: usize) -> Option<Self>;
58 fn to_u8_opt(&self) -> Option<u8>;
59 fn to_u16_opt(&self) -> Option<u16>;
60 fn to_u32_opt(&self) -> Option<u32>;
61 fn to_u64_opt(&self) -> Option<u64>;
62 fn to_u128_opt(&self) -> Option<u128>;
63 fn to_usize_opt(&self) -> Option<usize>;
66 ($name:ty, $width:expr) => {
67 impl EnumSetTypeRepr for $name {
68 const WIDTH: u32 = $width;
71 fn is_empty(&self) -> bool {
80 fn add_bit(&mut self, bit: u32) {
81 *self |= 1 << bit as $name;
84 fn remove_bit(&mut self, bit: u32) {
85 *self &= !(1 << bit as $name);
88 fn has_bit(&self, bit: u32) -> bool {
89 (self & (1 << bit as $name)) != 0
93 fn count_ones(&self) -> u32 {
97 fn leading_zeros(&self) -> u32 {
98 (*self).leading_zeros()
101 fn trailing_zeros(&self) -> u32 {
102 (*self).trailing_zeros()
106 fn and_not(&self, other: Self) -> Self {
111 fn count_remaining_ones(&self, cursor: u32) -> usize {
112 let left_mask = !((1 as $name)
116 (*self & left_mask).count_ones() as usize
120 fn from_u8(v: u8) -> Self {
124 fn from_u16(v: u16) -> Self {
128 fn from_u32(v: u32) -> Self {
132 fn from_u64(v: u64) -> Self {
136 fn from_u128(v: u128) -> Self {
140 fn from_usize(v: usize) -> Self {
145 fn to_u8(&self) -> u8 {
149 fn to_u16(&self) -> u16 {
153 fn to_u32(&self) -> u32 {
157 fn to_u64(&self) -> u64 {
161 fn to_u128(&self) -> u128 {
165 fn to_usize(&self) -> usize {
170 fn from_u8_opt(v: u8) -> Option<Self> {
174 fn from_u16_opt(v: u16) -> Option<Self> {
178 fn from_u32_opt(v: u32) -> Option<Self> {
182 fn from_u64_opt(v: u64) -> Option<Self> {
186 fn from_u128_opt(v: u128) -> Option<Self> {
190 fn from_usize_opt(v: usize) -> Option<Self> {
195 fn to_u8_opt(&self) -> Option<u8> {
196 (*self).try_into().ok()
199 fn to_u16_opt(&self) -> Option<u16> {
200 (*self).try_into().ok()
203 fn to_u32_opt(&self) -> Option<u32> {
204 (*self).try_into().ok()
207 fn to_u64_opt(&self) -> Option<u64> {
208 (*self).try_into().ok()
211 fn to_u128_opt(&self) -> Option<u128> {
212 (*self).try_into().ok()
215 fn to_usize_opt(&self) -> Option<usize> {
216 (*self).try_into().ok()