}
impl <T: EnumSetType> EnumSet<T> {
// Returns all bits valid for the enum
+ #[inline(always)]
fn all_bits() -> T::Repr {
T::ALL_BITS
}
/// Creates an empty `EnumSet`.
+ #[inline(always)]
pub fn new() -> Self {
EnumSet { __priv_repr: T::Repr::empty() }
}
/// Returns an `EnumSet` containing a single element.
+ #[inline(always)]
pub fn only(t: T) -> Self {
let mut set = Self::new();
set.insert(t);
/// Creates an empty `EnumSet`.
///
/// This is an alias for [`EnumSet::new`].
+ #[inline(always)]
pub fn empty() -> Self {
Self::new()
}
/// Returns an `EnumSet` containing all valid variants of the enum.
+ #[inline(always)]
pub fn all() -> Self {
EnumSet { __priv_repr: Self::all_bits() }
}
///
/// This is the same as [`EnumSet::variant_count`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
+ #[inline(always)]
pub fn bit_width() -> u32 {
T::Repr::WIDTH - T::ALL_BITS.leading_zeros()
}
///
/// This is the same as [`EnumSet::bit_width`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
+ #[inline(always)]
pub fn variant_count() -> u32 {
T::ALL_BITS.count_ones()
}
/// Returns the number of elements in this set.
+ #[inline(always)]
pub fn len(&self) -> usize {
self.__priv_repr.count_ones() as usize
}
/// Returns `true` if the set contains no elements.
+ #[inline(always)]
pub fn is_empty(&self) -> bool {
self.__priv_repr.is_empty()
}
/// Removes all elements from the set.
+ #[inline(always)]
pub fn clear(&mut self) {
self.__priv_repr = T::Repr::empty()
}
/// Returns `true` if `self` has no elements in common with `other`. This is equivalent to
/// checking for an empty intersection.
+ #[inline(always)]
pub fn is_disjoint(&self, other: Self) -> bool {
(*self & other).is_empty()
}
/// Returns `true` if the set is a superset of another, i.e., `self` contains at least all the
/// values in `other`.
+ #[inline(always)]
pub fn is_superset(&self, other: Self) -> bool {
(*self & other).__priv_repr == other.__priv_repr
}
/// Returns `true` if the set is a subset of another, i.e., `other` contains at least all
/// the values in `self`.
+ #[inline(always)]
pub fn is_subset(&self, other: Self) -> bool {
other.is_superset(*self)
}
/// Returns a set containing any elements present in either set.
+ #[inline(always)]
pub fn union(&self, other: Self) -> Self {
EnumSet { __priv_repr: self.__priv_repr | other.__priv_repr }
}
/// Returns a set containing every element present in both sets.
+ #[inline(always)]
pub fn intersection(&self, other: Self) -> Self {
EnumSet { __priv_repr: self.__priv_repr & other.__priv_repr }
}
/// Returns a set containing element present in `self` but not in `other`.
+ #[inline(always)]
pub fn difference(&self, other: Self) -> Self {
EnumSet { __priv_repr: self.__priv_repr.and_not(other.__priv_repr) }
}
/// Returns a set containing every element present in either `self` or `other`, but is not
/// present in both.
+ #[inline(always)]
pub fn symmetrical_difference(&self, other: Self) -> Self {
EnumSet { __priv_repr: self.__priv_repr ^ other.__priv_repr }
}
/// Returns a set containing all enum variants not in this set.
+ #[inline(always)]
pub fn complement(&self) -> Self {
EnumSet { __priv_repr: !self.__priv_repr & Self::all_bits() }
}
/// Checks whether this set contains a value.
+ #[inline(always)]
pub fn contains(&self, value: T) -> bool {
self.__priv_repr.has_bit(value.enum_into_u32())
}
/// If the set did not have this value present, `true` is returned.
///
/// If the set did have this value present, `false` is returned.
+ #[inline(always)]
pub fn insert(&mut self, value: T) -> bool {
let contains = !self.contains(value);
self.__priv_repr.add_bit(value.enum_into_u32());
contains
}
/// Removes a value from this set. Returns whether the value was present in the set.
+ #[inline(always)]
pub fn remove(&mut self, value: T) -> bool {
let contains = self.contains(value);
self.__priv_repr.remove_bit(value.enum_into_u32());
}
/// Adds all elements in another set to this one.
+ #[inline(always)]
pub fn insert_all(&mut self, other: Self) {
self.__priv_repr = self.__priv_repr | other.__priv_repr
}
/// Removes all values in another set from this one.
+ #[inline(always)]
pub fn remove_all(&mut self, other: Self) {
self.__priv_repr = self.__priv_repr.and_not(other.__priv_repr);
}
not fit in a `"]
#[doc = $underlying_str]
#[doc = "`, this method will panic."]
+ #[inline(always)]
pub fn $to(&self) -> $underlying {
self.$try_to().expect("Bitset will not fit into this type.")
}
not fit in a `"]
#[doc = $underlying_str]
#[doc = "`, this method will instead return `None`."]
+ #[inline(always)]
pub fn $try_to(&self) -> Option<$underlying> {
EnumSetTypeRepr::$to_fn_opt(&self.__priv_repr)
}
not fit in a `"]
#[doc = $underlying_str]
#[doc = "`, this method will truncate any bits that don't fit."]
+ #[inline(always)]
pub fn $to_truncated(&self) -> $underlying {
EnumSetTypeRepr::$to_fn(&self.__priv_repr)
}
#[doc = $underlying_str]
#[doc = "`.\n\nIf a bit that doesn't correspond to an enum variant is set, this \
method will panic."]
+ #[inline(always)]
pub fn $from(bits: $underlying) -> Self {
Self::$try_from(bits).expect("Bitset contains invalid variants.")
}
#[doc = $underlying_str]
#[doc = "`.\n\nIf a bit that doesn't correspond to an enum variant is set, this \
method will return `None`."]
+ #[inline(always)]
pub fn $try_from(bits: $underlying) -> Option<Self> {
let bits = T::Repr::$from_fn_opt(bits);
let mask = Self::all().__priv_repr;
#[doc = "Constructs a bitset from a `"]
#[doc = $underlying_str]
#[doc = "`, ignoring invalid variants."]
+ #[inline(always)]
pub fn $from_truncated(bits: $underlying) -> Self {
let mask = Self::all().$to_truncated();
let bits = <T::Repr as EnumSetTypeRepr>::$from_fn(bits & mask);
impl <T: EnumSetType, O: Into<EnumSet<T>>> Sub<O> for EnumSet<T> {
type Output = Self;
+ #[inline(always)]
fn sub(self, other: O) -> Self::Output {
self.difference(other.into())
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitAnd<O> for EnumSet<T> {
type Output = Self;
+ #[inline(always)]
fn bitand(self, other: O) -> Self::Output {
self.intersection(other.into())
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitOr<O> for EnumSet<T> {
type Output = Self;
+ #[inline(always)]
fn bitor(self, other: O) -> Self::Output {
self.union(other.into())
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitXor<O> for EnumSet<T> {
type Output = Self;
+ #[inline(always)]
fn bitxor(self, other: O) -> Self::Output {
self.symmetrical_difference(other.into())
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> SubAssign<O> for EnumSet<T> {
+ #[inline(always)]
fn sub_assign(&mut self, rhs: O) {
*self = *self - rhs;
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitAndAssign<O> for EnumSet<T> {
+ #[inline(always)]
fn bitand_assign(&mut self, rhs: O) {
*self = *self & rhs;
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitOrAssign<O> for EnumSet<T> {
+ #[inline(always)]
fn bitor_assign(&mut self, rhs: O) {
*self = *self | rhs;
}
}
impl <T: EnumSetType, O: Into<EnumSet<T>>> BitXorAssign<O> for EnumSet<T> {
+ #[inline(always)]
fn bitxor_assign(&mut self, rhs: O) {
*self = *self ^ rhs;
}
impl <T: EnumSetType> Not for EnumSet<T> {
type Output = Self;
+ #[inline(always)]
fn not(self) -> Self::Output {
self.complement()
}
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<Self> { v.try_into().ok() }
+ #[inline(always)]
fn from_u16_opt(v: u16) -> Option<Self> { v.try_into().ok() }
+ #[inline(always)]
fn from_u32_opt(v: u32) -> Option<Self> { v.try_into().ok() }
+ #[inline(always)]
fn from_u64_opt(v: u64) -> Option<Self> { v.try_into().ok() }
+ #[inline(always)]
fn from_u128_opt(v: u128) -> Option<Self> { v.try_into().ok() }
+ #[inline(always)]
fn from_usize_opt(v: usize) -> Option<Self> { v.try_into().ok() }
+ #[inline(always)]
fn to_u8_opt(&self) -> Option<u8> { (*self).try_into().ok() }
+ #[inline(always)]
fn to_u16_opt(&self) -> Option<u16> { (*self).try_into().ok() }
+ #[inline(always)]
fn to_u32_opt(&self) -> Option<u32> { (*self).try_into().ok() }
+ #[inline(always)]
fn to_u64_opt(&self) -> Option<u64> { (*self).try_into().ok() }
+ #[inline(always)]
fn to_u128_opt(&self) -> Option<u128> { (*self).try_into().ok() }
+ #[inline(always)]
fn to_usize_opt(&self) -> Option<usize> { (*self).try_into().ok() }
}
}