]> git.lizzy.rs Git - enumset.git/commitdiff
Expanded API.
authorLymia Aluysia <lymia@lymiahugs.com>
Thu, 24 May 2018 02:30:54 +0000 (21:30 -0500)
committerLymia Aluysia <lymia@lymiahugs.com>
Thu, 24 May 2018 02:30:54 +0000 (21:30 -0500)
src/lib.rs

index 83d8a91f035b64d70508d70a59c7efdee1444db7..7d41ff008e6244280777fa34c19c1d8644b56e56 100644 (file)
@@ -99,6 +99,9 @@ pub trait EnumSetType : Copy {
     fn count_ones(val: Self::Repr) -> usize;
     fn into_u8(self) -> u8;
     fn from_u8(val: u8) -> Self;
+
+    fn repr_to_u128(bits: Self::Repr) -> u128;
+    fn repr_from_u128(bits: u128) -> Self::Repr;
 }
 
 /// An efficient set type for enums created with the [`enum_set_type!`](./macro.enum_set_type.html)
@@ -127,6 +130,21 @@ impl <T : EnumSetType> EnumSet<T> {
         EnumSet { __enumset_underlying: T::ZERO }
     }
 
+    /// Total number of bits this enumset uses. Note that the actual amount of space used is
+    /// rounded up to the next highest integer type (`u8`, `u16`, `u32`, `u64`, or `u128`).
+    pub fn bit_width() -> u8 {
+        T::VARIANT_COUNT as u8
+    }
+
+    /// Returns the raw bits of this set
+    pub fn to_bits(&self) -> u128 {
+        T::repr_to_u128(self.__enumset_underlying)
+    }
+    /// Constructs a bitset from raw bits
+    pub fn from_bits(bits: u128) -> Self {
+        EnumSet { __enumset_underlying: T::repr_from_u128(bits) }
+    }
+
     /// Returns the number of values in this set.
     pub fn len(&self) -> usize {
         T::count_ones(self.__enumset_underlying)
@@ -400,6 +418,13 @@ macro_rules! enum_set_type_internal {
             fn from_u8(val: u8) -> Self {
                 unsafe { ::std::mem::transmute(val) }
             }
+
+            fn repr_to_u128(bits: Self::Repr) -> u128 {
+                bits as u128
+            }
+            fn repr_from_u128(bits: u128) -> Self::Repr {
+                bits as $repr
+            }
         }
         enum_set_type_nightly_impl!($enum_name $repr);
         impl ::std::ops::BitOr<$enum_name> for $enum_name {