-/// The default representation serializes enumsets as an `u8`, `u16`, `u32`, `u64`, or `u128`,
-/// whichever is the smallest that can contain all bits that are part of the set.
+/// By default, `EnumSet`s are serialized as an unsigned integer of the same width as used to store
+/// it in memory.
///
/// Unknown bits are ignored, and are simply dropped. To override this behavior, you can add a
/// `#[enumset(serialize_deny_unknown)]` annotation to your enum.
///
///
/// Unknown bits are ignored, and are simply dropped. To override this behavior, you can add a
/// `#[enumset(serialize_deny_unknown)]` annotation to your enum.
///
-/// You can add a `#[enumset(serialize_repr = "u8")]` annotation to your enum to explicitly set
-/// the bit width the `EnumSet` is serialized as. This can be used to avoid breaking changes in
-/// certain serialization formats (such as `bincode`).
+/// You can add a `#[enumset(serialize_repr = "u8")]` annotation to your enum to manually set
+/// the number width the `EnumSet` is serialized as. Only unsigned integer types may be used. This
+/// can be used to avoid breaking changes in certain serialization formats (such as `bincode`).
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct EnumSet<T : EnumSetType> {
#[doc(hidden)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct EnumSet<T : EnumSetType> {
#[doc(hidden)]
/// rounded up to the next highest integer type (`u8`, `u16`, `u32`, `u64`, or `u128`).
///
/// This is the same as [`EnumSet::variant_count`] except in enums with "sparse" variants.
/// rounded up to the next highest integer type (`u8`, `u16`, `u32`, `u64`, or `u128`).
///
/// This is the same as [`EnumSet::variant_count`] except in enums with "sparse" variants.
///
/// This is the same as [`EnumSet::bit_width`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
///
/// This is the same as [`EnumSet::bit_width`] except in enums with "sparse" variants.
/// (e.g. `enum Foo { A = 10, B = 20 }`)
pub fn to_bits(&self) -> u128 {
self.__enumset_underlying.to_u128()
.expect("Impossible: Bits cannot be to converted into i128?")
pub fn to_bits(&self) -> u128 {
self.__enumset_underlying.to_u128()
.expect("Impossible: Bits cannot be to converted into i128?")
- /// Returns the number of values in this set.
+ /// Constructs a bitset from raw bits, ignoring any unknown variants.
+ pub fn from_bits_safe(bits: u128) -> Self {
+ Self::form_bits(bits & Self::all().to_bits())
+ }
+
+ /// Returns the number of elements in this set.
pub fn is_disjoint(&self, other: Self) -> bool {
(*self & other).is_empty()
}
pub fn is_disjoint(&self, other: Self) -> bool {
(*self & other).is_empty()
}
pub fn is_superset(&self, other: Self) -> bool {
(*self & other).__enumset_underlying == other.__enumset_underlying
}
pub fn is_superset(&self, other: Self) -> bool {
(*self & other).__enumset_underlying == other.__enumset_underlying
}
pub fn union(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying | other.__enumset_underlying }
}
pub fn union(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying | other.__enumset_underlying }
}
pub fn intersection(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying & other.__enumset_underlying }
}
pub fn intersection(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying & other.__enumset_underlying }
}
pub fn difference(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying & !other.__enumset_underlying }
}
pub fn difference(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying & !other.__enumset_underlying }
}
pub fn symmetrical_difference(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying ^ other.__enumset_underlying }
}
pub fn symmetrical_difference(&self, other: Self) -> Self {
EnumSet { __enumset_underlying: self.__enumset_underlying ^ other.__enumset_underlying }
}
pub fn complement(&self) -> Self {
EnumSet { __enumset_underlying: !self.__enumset_underlying & Self::all_bits() }
}
pub fn complement(&self) -> Self {
EnumSet { __enumset_underlying: !self.__enumset_underlying & Self::all_bits() }
}
pub fn insert(&mut self, value: T) -> bool {
let contains = self.contains(value);
self.__enumset_underlying = self.__enumset_underlying | Self::mask(value.enum_into_u8());
contains
}
pub fn insert(&mut self, value: T) -> bool {
let contains = self.contains(value);
self.__enumset_underlying = self.__enumset_underlying | Self::mask(value.enum_into_u8());
contains
}
pub fn remove(&mut self, value: T) -> bool {
let contains = self.contains(value);
self.__enumset_underlying = self.__enumset_underlying & !Self::mask(value.enum_into_u8());
pub fn remove(&mut self, value: T) -> bool {
let contains = self.contains(value);
self.__enumset_underlying = self.__enumset_underlying & !Self::mask(value.enum_into_u8());
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
pub struct EnumSetIter<T : EnumSetType>(EnumSet<T>, u8);
impl <T : EnumSetType> Iterator for EnumSetIter<T> {
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Debug)]
pub struct EnumSetIter<T : EnumSetType>(EnumSet<T>, u8);
impl <T : EnumSetType> Iterator for EnumSetIter<T> {