From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- third_party/rust/enumset/src/repr.rs | 225 +++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 third_party/rust/enumset/src/repr.rs (limited to 'third_party/rust/enumset/src/repr.rs') diff --git a/third_party/rust/enumset/src/repr.rs b/third_party/rust/enumset/src/repr.rs new file mode 100644 index 0000000000..38546aa8c9 --- /dev/null +++ b/third_party/rust/enumset/src/repr.rs @@ -0,0 +1,225 @@ +use core::convert::TryInto; +use core::fmt::Debug; +use core::hash::Hash; +use core::ops::*; + +/// A trait marking valid underlying bitset storage types and providing the +/// operations `EnumSet` and related types use. +pub trait EnumSetTypeRepr : + // Basic traits used to derive traits + Copy + + Ord + + Eq + + Debug + + Hash + + // Operations used by enumset + BitAnd + + BitOr + + BitXor + + Not + +{ + const WIDTH: u32; + + fn is_empty(&self) -> bool; + fn empty() -> Self; + + fn add_bit(&mut self, bit: u32); + fn remove_bit(&mut self, bit: u32); + fn has_bit(&self, bit: u32) -> bool; + + fn count_ones(&self) -> u32; + fn count_remaining_ones(&self, cursor: u32) -> usize; + fn leading_zeros(&self) -> u32; + fn trailing_zeros(&self) -> u32; + + fn and_not(&self, other: Self) -> Self; + + fn from_u8(v: u8) -> Self; + fn from_u16(v: u16) -> Self; + fn from_u32(v: u32) -> Self; + fn from_u64(v: u64) -> Self; + fn from_u128(v: u128) -> Self; + fn from_usize(v: usize) -> Self; + + fn to_u8(&self) -> u8; + fn to_u16(&self) -> u16; + fn to_u32(&self) -> u32; + fn to_u64(&self) -> u64; + fn to_u128(&self) -> u128; + fn to_usize(&self) -> usize; + + fn from_u8_opt(v: u8) -> Option; + fn from_u16_opt(v: u16) -> Option; + fn from_u32_opt(v: u32) -> Option; + fn from_u64_opt(v: u64) -> Option; + fn from_u128_opt(v: u128) -> Option; + fn from_usize_opt(v: usize) -> Option; + + fn to_u8_opt(&self) -> Option; + fn to_u16_opt(&self) -> Option; + fn to_u32_opt(&self) -> Option; + fn to_u64_opt(&self) -> Option; + fn to_u128_opt(&self) -> Option; + fn to_usize_opt(&self) -> Option; +} +macro_rules! prim { + ($name:ty, $width:expr) => { + impl EnumSetTypeRepr for $name { + const WIDTH: u32 = $width; + + #[inline(always)] + fn is_empty(&self) -> bool { + *self == 0 + } + #[inline(always)] + fn empty() -> Self { + 0 + } + + #[inline(always)] + fn add_bit(&mut self, bit: u32) { + *self |= 1 << bit as $name; + } + #[inline(always)] + fn remove_bit(&mut self, bit: u32) { + *self &= !(1 << bit as $name); + } + #[inline(always)] + fn has_bit(&self, bit: u32) -> bool { + (self & (1 << bit as $name)) != 0 + } + + #[inline(always)] + fn count_ones(&self) -> u32 { + (*self).count_ones() + } + #[inline(always)] + fn leading_zeros(&self) -> u32 { + (*self).leading_zeros() + } + #[inline(always)] + fn trailing_zeros(&self) -> u32 { + (*self).trailing_zeros() + } + + #[inline(always)] + fn and_not(&self, other: Self) -> Self { + (*self) & !other + } + + #[inline(always)] + fn count_remaining_ones(&self, cursor: u32) -> usize { + let left_mask = !((1 as $name) + .checked_shl(cursor) + .unwrap_or(0) + .wrapping_sub(1)); + (*self & left_mask).count_ones() as usize + } + + #[inline(always)] + fn from_u8(v: u8) -> Self { + v as $name + } + #[inline(always)] + fn from_u16(v: u16) -> Self { + v as $name + } + #[inline(always)] + fn from_u32(v: u32) -> Self { + v as $name + } + #[inline(always)] + fn from_u64(v: u64) -> Self { + v as $name + } + #[inline(always)] + fn from_u128(v: u128) -> Self { + v as $name + } + #[inline(always)] + fn from_usize(v: usize) -> Self { + v as $name + } + + #[inline(always)] + fn to_u8(&self) -> u8 { + (*self) as u8 + } + #[inline(always)] + fn to_u16(&self) -> u16 { + (*self) as u16 + } + #[inline(always)] + fn to_u32(&self) -> u32 { + (*self) as u32 + } + #[inline(always)] + fn to_u64(&self) -> u64 { + (*self) as u64 + } + #[inline(always)] + fn to_u128(&self) -> u128 { + (*self) as u128 + } + #[inline(always)] + fn to_usize(&self) -> usize { + (*self) as usize + } + + #[inline(always)] + fn from_u8_opt(v: u8) -> Option { + v.try_into().ok() + } + #[inline(always)] + fn from_u16_opt(v: u16) -> Option { + v.try_into().ok() + } + #[inline(always)] + fn from_u32_opt(v: u32) -> Option { + v.try_into().ok() + } + #[inline(always)] + fn from_u64_opt(v: u64) -> Option { + v.try_into().ok() + } + #[inline(always)] + fn from_u128_opt(v: u128) -> Option { + v.try_into().ok() + } + #[inline(always)] + fn from_usize_opt(v: usize) -> Option { + v.try_into().ok() + } + + #[inline(always)] + fn to_u8_opt(&self) -> Option { + (*self).try_into().ok() + } + #[inline(always)] + fn to_u16_opt(&self) -> Option { + (*self).try_into().ok() + } + #[inline(always)] + fn to_u32_opt(&self) -> Option { + (*self).try_into().ok() + } + #[inline(always)] + fn to_u64_opt(&self) -> Option { + (*self).try_into().ok() + } + #[inline(always)] + fn to_u128_opt(&self) -> Option { + (*self).try_into().ok() + } + #[inline(always)] + fn to_usize_opt(&self) -> Option { + (*self).try_into().ok() + } + } + }; +} +prim!(u8, 8); +prim!(u16, 16); +prim!(u32, 32); +prim!(u64, 64); +prim!(u128, 128); -- cgit v1.2.3