]> git.lizzy.rs Git - enumset.git/commitdiff
Merge branch 'master' of github.com:Lymia/enumset
authorAlissa Rao <lymia@lymiahugs.com>
Mon, 25 Jan 2021 22:17:23 +0000 (14:17 -0800)
committerAlissa Rao <lymia@lymiahugs.com>
Mon, 25 Jan 2021 22:17:23 +0000 (14:17 -0800)
enumset/Cargo.toml
enumset/src/lib.rs
enumset/src/repr.rs [new file with mode: 0644]
enumset_derive/src/lib.rs

index 0714fc1d9f548b9775ebc24a3311d52c297af4a3..0ebe1b90ffa4fb7c649f579b6adabeb0c934e715 100644 (file)
@@ -23,7 +23,6 @@ serde = ["serde2", "enumset_derive/serde"]
 
 [dependencies]
 enumset_derive = { version = "0.5.0", path = "../enumset_derive" }
-num-traits = { version = "0.2", default-features = false }
 serde2 = { package = "serde", version = "1.0.91", default-features = false, optional = true }
 
 [dev-dependencies]
index d03767e0e18f7a8b0d9e7a207282314ecee5ba46..55aab1e98e45463004ca2dc16a0c5c98ef2a6d7a 100644 (file)
@@ -75,7 +75,7 @@
 //! assert_eq!(set, Enum::A | Enum::E | Enum::G);
 //! ```
 
-pub use enumset_derive::*;
+pub use enumset_derive::EnumSetType;
 
 use core::cmp::Ordering;
 use core::fmt;
@@ -84,19 +84,11 @@ use core::hash::{Hash, Hasher};
 use core::iter::FromIterator;
 use core::ops::*;
 
-use num_traits::*;
-
 #[doc(hidden)]
 /// Everything in this module is internal API and may change at any time.
 pub mod __internal {
     use super::*;
 
-    /// A struct used to type check [`enum_set!`].
-    pub struct EnumSetSameTypeHack<'a, T: EnumSetType + 'static> {
-        pub unified: &'a [T],
-        pub enum_set: EnumSet<T>,
-    }
-
     /// A reexport of core to allow our macros to be generic to std vs core.
     pub use ::core as core_export;
 
@@ -132,45 +124,8 @@ use crate::__internal::EnumSetTypePrivate;
 #[cfg(feature = "serde")] use crate::__internal::serde;
 #[cfg(feature = "serde")] use crate::serde::{Serialize, Deserialize};
 
-mod private {
-    use super::*;
-
-    /// A trait marking valid underlying bitset storage types and providing the
-    /// operations `EnumSet` and related types use.
-    pub trait EnumSetTypeRepr :
-        PrimInt + WrappingSub + CheckedShl + Debug + Hash + FromPrimitive + ToPrimitive +
-        AsPrimitive<u8> + AsPrimitive<u16> + AsPrimitive<u32> + AsPrimitive<u64> +
-        AsPrimitive<u128> + AsPrimitive<usize>
-    {
-        const WIDTH: u32;
-
-        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;
-    }
-    macro_rules! prim {
-        ($name:ty, $width:expr) => {
-            impl EnumSetTypeRepr for $name {
-                const WIDTH: u32 = $width;
-                fn from_u8(v: u8) -> Self { v.as_() }
-                fn from_u16(v: u16) -> Self { v.as_() }
-                fn from_u32(v: u32) -> Self { v.as_() }
-                fn from_u64(v: u64) -> Self { v.as_() }
-                fn from_u128(v: u128) -> Self { v.as_() }
-                fn from_usize(v: usize) -> Self { v.as_() }
-            }
-        }
-    }
-    prim!(u8  , 8  );
-    prim!(u16 , 16 );
-    prim!(u32 , 32 );
-    prim!(u64 , 64 );
-    prim!(u128, 128);
-}
-use crate::private::EnumSetTypeRepr;
+mod repr;
+use crate::repr::EnumSetTypeRepr;
 
 /// The trait used to define enum types that may be used with [`EnumSet`].
 ///
@@ -262,22 +217,9 @@ pub struct EnumSet<T: EnumSetType> {
     #[doc(hidden)]
     /// This is public due to the [`enum_set!`] macro.
     /// This is **NOT** public API and may change at any time.
-    pub __enumset_underlying: T::Repr
+    pub __priv_repr: T::Repr
 }
 impl <T: EnumSetType> EnumSet<T> {
-    fn mask(bit: u32) -> T::Repr {
-        Shl::<usize>::shl(T::Repr::one(), bit as usize)
-    }
-    fn has_bit(&self, bit: u32) -> bool {
-        let mask = Self::mask(bit);
-        self.__enumset_underlying & mask == mask
-    }
-    fn partial_bits(bits: u32) -> T::Repr {
-        T::Repr::one().checked_shl(bits as u32)
-            .unwrap_or(T::Repr::zero())
-            .wrapping_sub(&T::Repr::one())
-    }
-
     // Returns all bits valid for the enum
     fn all_bits() -> T::Repr {
         T::ALL_BITS
@@ -285,12 +227,14 @@ impl <T: EnumSetType> EnumSet<T> {
 
     /// Creates an empty `EnumSet`.
     pub fn new() -> Self {
-        EnumSet { __enumset_underlying: T::Repr::zero() }
+        EnumSet { __priv_repr: T::Repr::empty() }
     }
 
     /// Returns an `EnumSet` containing a single element.
     pub fn only(t: T) -> Self {
-        EnumSet { __enumset_underlying: Self::mask(t.enum_into_u32()) }
+        let mut set = Self::new();
+        set.insert(t);
+        set
     }
 
     /// Creates an empty `EnumSet`.
@@ -302,7 +246,7 @@ impl <T: EnumSetType> EnumSet<T> {
 
     /// Returns an `EnumSet` containing all valid variants of the enum.
     pub fn all() -> Self {
-        EnumSet { __enumset_underlying: Self::all_bits() }
+        EnumSet { __priv_repr: Self::all_bits() }
     }
 
     /// Total number of bits used by this type. Note that the actual amount of space used is
@@ -324,15 +268,15 @@ impl <T: EnumSetType> EnumSet<T> {
 
     /// Returns the number of elements in this set.
     pub fn len(&self) -> usize {
-        self.__enumset_underlying.count_ones() as usize
+        self.__priv_repr.count_ones() as usize
     }
     /// Returns `true` if the set contains no elements.
     pub fn is_empty(&self) -> bool {
-        self.__enumset_underlying.is_zero()
+        self.__priv_repr.is_empty()
     }
     /// Removes all elements from the set.
     pub fn clear(&mut self) {
-        self.__enumset_underlying = T::Repr::zero()
+        self.__priv_repr = T::Repr::empty()
     }
 
     /// Returns `true` if `self` has no elements in common with `other`. This is equivalent to
@@ -343,7 +287,7 @@ impl <T: EnumSetType> EnumSet<T> {
     /// Returns `true` if the set is a superset of another, i.e., `self` contains at least all the
     /// values in `other`.
     pub fn is_superset(&self, other: Self) -> bool {
-        (*self & other).__enumset_underlying == other.__enumset_underlying
+        (*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`.
@@ -353,29 +297,29 @@ impl <T: EnumSetType> EnumSet<T> {
 
     /// Returns a set containing any elements present in either set.
     pub fn union(&self, other: Self) -> Self {
-        EnumSet { __enumset_underlying: self.__enumset_underlying | other.__enumset_underlying }
+        EnumSet { __priv_repr: self.__priv_repr | other.__priv_repr }
     }
     /// Returns a set containing every element present in both sets.
     pub fn intersection(&self, other: Self) -> Self {
-        EnumSet { __enumset_underlying: self.__enumset_underlying & other.__enumset_underlying }
+        EnumSet { __priv_repr: self.__priv_repr & other.__priv_repr }
     }
     /// Returns a set containing element present in `self` but not in `other`.
     pub fn difference(&self, other: Self) -> Self {
-        EnumSet { __enumset_underlying: self.__enumset_underlying & !other.__enumset_underlying }
+        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.
     pub fn symmetrical_difference(&self, other: Self) -> Self {
-        EnumSet { __enumset_underlying: self.__enumset_underlying ^ other.__enumset_underlying }
+        EnumSet { __priv_repr: self.__priv_repr ^ other.__priv_repr }
     }
     /// Returns a set containing all enum variants not in this set.
     pub fn complement(&self) -> Self {
-        EnumSet { __enumset_underlying: !self.__enumset_underlying & Self::all_bits() }
+        EnumSet { __priv_repr: !self.__priv_repr & Self::all_bits() }
     }
 
     /// Checks whether this set contains a value.
     pub fn contains(&self, value: T) -> bool {
-        self.has_bit(value.enum_into_u32())
+        self.__priv_repr.has_bit(value.enum_into_u32())
     }
 
     /// Adds a value to this set.
@@ -385,23 +329,23 @@ impl <T: EnumSetType> EnumSet<T> {
     /// If the set did have this value present, `false` is returned.
     pub fn insert(&mut self, value: T) -> bool {
         let contains = !self.contains(value);
-        self.__enumset_underlying = self.__enumset_underlying | Self::mask(value.enum_into_u32());
+        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.
     pub fn remove(&mut self, value: T) -> bool {
         let contains = self.contains(value);
-        self.__enumset_underlying = self.__enumset_underlying & !Self::mask(value.enum_into_u32());
+        self.__priv_repr.remove_bit(value.enum_into_u32());
         contains
     }
 
     /// Adds all elements in another set to this one.
     pub fn insert_all(&mut self, other: Self) {
-        self.__enumset_underlying = self.__enumset_underlying | other.__enumset_underlying
+        self.__priv_repr = self.__priv_repr | other.__priv_repr
     }
     /// Removes all values in another set from this one.
     pub fn remove_all(&mut self, other: Self) {
-        self.__enumset_underlying = self.__enumset_underlying & !other.__enumset_underlying
+        self.__priv_repr = self.__priv_repr.and_not(other.__priv_repr);
     }
 
     /// Creates an iterator over the values in this set.
@@ -417,7 +361,8 @@ impl <T: EnumSetType> EnumSet<T> {
 macro_rules! conversion_impls {
     (
         $(for_num!(
-            $underlying:ty, $underlying_str:expr, $from_fn:ident, $to_fn:ident,
+            $underlying:ty, $underlying_str:expr,
+            $from_fn:ident $to_fn:ident $from_fn_opt:ident $to_fn_opt:ident,
             $from:ident $try_from:ident $from_truncated:ident
             $to:ident $try_to:ident $to_truncated:ident
         );)*
@@ -440,7 +385,7 @@ macro_rules! conversion_impls {
             #[doc = $underlying_str]
             #[doc = "`, this method will instead return `None`."]
             pub fn $try_to(&self) -> Option<$underlying> {
-                self.__enumset_underlying.$to_fn()
+                EnumSetTypeRepr::$to_fn_opt(&self.__priv_repr)
             }
 
             #[doc = "Returns a truncated `"]
@@ -450,7 +395,7 @@ macro_rules! conversion_impls {
             #[doc = $underlying_str]
             #[doc = "`, this method will truncate any bits that don't fit."]
             pub fn $to_truncated(&self) -> $underlying {
-                AsPrimitive::<$underlying>::as_(self.__enumset_underlying)
+                EnumSetTypeRepr::$to_fn(&self.__priv_repr)
             }
 
             #[doc = "Constructs a bitset from a `"]
@@ -466,10 +411,10 @@ macro_rules! conversion_impls {
             #[doc = "`.\n\nIf a bit that doesn't correspond to an enum variant is set, this \
                      method will return `None`."]
             pub fn $try_from(bits: $underlying) -> Option<Self> {
-                let bits = <T::Repr as FromPrimitive>::$from_fn(bits);
-                let mask = Self::all().__enumset_underlying;
-                bits.and_then(|bits| if (bits & !mask) == T::Repr::zero() {
-                    Some(EnumSet { __enumset_underlying: bits })
+                let bits = T::Repr::$from_fn_opt(bits);
+                let mask = Self::all().__priv_repr;
+                bits.and_then(|bits| if bits.and_not(mask).is_empty() {
+                    Some(EnumSet { __priv_repr: bits })
                 } else {
                     None
                 })
@@ -481,23 +426,23 @@ macro_rules! conversion_impls {
             pub fn $from_truncated(bits: $underlying) -> Self {
                 let mask = Self::all().$to_truncated();
                 let bits = <T::Repr as EnumSetTypeRepr>::$from_fn(bits & mask);
-                EnumSet { __enumset_underlying: bits }
+                EnumSet { __priv_repr: bits }
             }
         )*}
     }
 }
 conversion_impls! {
-    for_num!(u8, "u8", from_u8, to_u8,
+    for_num!(u8, "u8", from_u8 to_u8 from_u8_opt to_u8_opt,
              from_u8 try_from_u8 from_u8_truncated as_u8 try_as_u8 as_u8_truncated);
-    for_num!(u16, "u16", from_u16, to_u16,
+    for_num!(u16, "u16", from_u16 to_u16 from_u16_opt to_u16_opt,
              from_u16 try_from_u16 from_u16_truncated as_u16 try_as_u16 as_u16_truncated);
-    for_num!(u32, "u32", from_u32, to_u32,
+    for_num!(u32, "u32", from_u32 to_u32 from_u32_opt to_u32_opt,
              from_u32 try_from_u32 from_u32_truncated as_u32 try_as_u32 as_u32_truncated);
-    for_num!(u64, "u64", from_u64, to_u64,
+    for_num!(u64, "u64", from_u64 to_u64 from_u64_opt to_u64_opt,
              from_u64 try_from_u64 from_u64_truncated as_u64 try_as_u64 as_u64_truncated);
-    for_num!(u128, "u128", from_u128, to_u128,
+    for_num!(u128, "u128", from_u128 to_u128 from_u128_opt to_u128_opt,
              from_u128 try_from_u128 from_u128_truncated as_u128 try_as_u128 as_u128_truncated);
-    for_num!(usize, "usize", from_usize, to_usize,
+    for_num!(usize, "usize", from_usize to_usize from_usize_opt to_usize_opt,
              from_usize try_from_usize from_usize_truncated
              as_usize try_as_usize as_usize_truncated);
 }
@@ -579,7 +524,7 @@ impl <T: EnumSetType> From<T> for EnumSet<T> {
 
 impl <T: EnumSetType> PartialEq<T> for EnumSet<T> {
     fn eq(&self, other: &T) -> bool {
-        self.__enumset_underlying == EnumSet::<T>::mask(other.enum_into_u32())
+        self.__priv_repr == EnumSet::only(*other).__priv_repr
     }
 }
 impl <T: EnumSetType + Debug> Debug for EnumSet<T> {
@@ -598,17 +543,17 @@ impl <T: EnumSetType + Debug> Debug for EnumSet<T> {
 
 impl <T: EnumSetType> Hash for EnumSet<T> {
     fn hash<H: Hasher>(&self, state: &mut H) {
-        self.__enumset_underlying.hash(state)
+        self.__priv_repr.hash(state)
     }
 }
 impl <T: EnumSetType> PartialOrd for EnumSet<T> {
     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
-        self.__enumset_underlying.partial_cmp(&other.__enumset_underlying)
+        self.__priv_repr.partial_cmp(&other.__priv_repr)
     }
 }
 impl <T: EnumSetType> Ord for EnumSet<T> {
     fn cmp(&self, other: &Self) -> Ordering {
-        self.__enumset_underlying.cmp(&other.__enumset_underlying)
+        self.__priv_repr.cmp(&other.__priv_repr)
     }
 }
 
@@ -636,15 +581,14 @@ impl <T: EnumSetType> Iterator for EnumSetIter<T> {
         while self.1 < EnumSet::<T>::bit_width() {
             let bit = self.1;
             self.1 += 1;
-            if self.0.has_bit(bit) {
+            if self.0.__priv_repr.has_bit(bit) {
                 return unsafe { Some(T::enum_from_u32(bit)) }
             }
         }
         None
     }
     fn size_hint(&self) -> (usize, Option<usize>) {
-        let left_mask = !EnumSet::<T>::partial_bits(self.1);
-        let left = (self.0.__enumset_underlying & left_mask).count_ones() as usize;
+        let left = self.0.__priv_repr.count_remaining_ones(self.1);
         (left, Some(left))
     }
 }
@@ -684,6 +628,8 @@ impl<T: EnumSetType> FromIterator<EnumSet<T>> for EnumSet<T> {
 /// The syntax used is `enum_set!(Type::A | Type::B | Type::C)`. Each variant must be of the same
 /// type, or a error will occur at compile-time.
 ///
+/// This macro accepts trailing `|`s to allow easier use in other macros.
+///
 /// # Examples
 ///
 /// ```rust
@@ -703,15 +649,17 @@ impl<T: EnumSetType> FromIterator<EnumSet<T>> for EnumSet<T> {
 /// ```
 #[macro_export]
 macro_rules! enum_set {
-    () => {
-        $crate::EnumSet { __enumset_underlying: 0 }
+    ($(|)*) => {
+        $crate::EnumSet { __priv_repr: 0 }
     };
-    ($($value:path)|* $(|)*) => {
-        $crate::__internal::EnumSetSameTypeHack {
-            unified: &[$($value,)*],
-            enum_set: $crate::EnumSet {
-                __enumset_underlying: 0 $(| (1 << ($value as u32)))*
-            },
-        }.enum_set
+    ($value:path $(|)*) => {
+        $value.__impl_enumset_internal__const_only()
+    };
+    ($value:path | $($rest:path)|* $(|)*) => {
+        {
+            #[allow(deprecated)] let value = $value.__impl_enumset_internal__const_only();
+            $(#[allow(deprecated)] let value = $rest.__impl_enumset_internal__const_merge(value);)*
+            value
+        }
     };
 }
diff --git a/enumset/src/repr.rs b/enumset/src/repr.rs
new file mode 100644 (file)
index 0000000..eb0f915
--- /dev/null
@@ -0,0 +1,127 @@
+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<Output = Self> +
+    BitOr<Output = Self> +
+    BitXor<Output = Self> +
+    Not<Output = Self> +
+{
+    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 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<Self>;
+    fn from_u16_opt(v: u16) -> Option<Self>;
+    fn from_u32_opt(v: u32) -> Option<Self>;
+    fn from_u64_opt(v: u64) -> Option<Self>;
+    fn from_u128_opt(v: u128) -> Option<Self>;
+    fn from_usize_opt(v: usize) -> Option<Self>;
+
+    fn to_u8_opt(&self) -> Option<u8>;
+    fn to_u16_opt(&self) -> Option<u16>;
+    fn to_u32_opt(&self) -> Option<u32>;
+    fn to_u64_opt(&self) -> Option<u64>;
+    fn to_u128_opt(&self) -> Option<u128>;
+    fn to_usize_opt(&self) -> Option<usize>;
+}
+macro_rules! prim {
+    ($name:ty, $width:expr) => {
+        impl EnumSetTypeRepr for $name {
+            const WIDTH: u32 = $width;
+
+            fn is_empty(&self) -> bool { *self == 0 }
+            fn empty() -> Self { 0 }
+
+            fn add_bit(&mut self, bit: u32) {
+                *self |= 1 << bit as $name;
+            }
+            fn remove_bit(&mut self, bit: u32) {
+                *self &= !(1 << bit as $name);
+            }
+            fn has_bit(&self, bit: u32) -> bool {
+                (self & (1 << bit as $name)) != 0
+            }
+
+            fn count_ones(&self) -> u32 { (*self).count_ones() }
+            fn leading_zeros(&self) -> u32 { (*self).leading_zeros() }
+
+            fn and_not(&self, other: Self) -> Self { (*self) & !other }
+
+            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
+            }
+
+            fn from_u8(v: u8) -> Self { v as $name }
+            fn from_u16(v: u16) -> Self { v as $name }
+            fn from_u32(v: u32) -> Self { v as $name }
+            fn from_u64(v: u64) -> Self { v as $name }
+            fn from_u128(v: u128) -> Self { v as $name }
+            fn from_usize(v: usize) -> Self { v as $name }
+
+            fn to_u8(&self) -> u8 { (*self) as u8 }
+            fn to_u16(&self) -> u16 { (*self) as u16 }
+            fn to_u32(&self) -> u32 { (*self) as u32 }
+            fn to_u64(&self) -> u64 { (*self) as u64 }
+            fn to_u128(&self) -> u128 { (*self) as u128 }
+            fn to_usize(&self) -> usize { (*self) as usize }
+
+            fn from_u8_opt(v: u8) -> Option<Self> { v.try_into().ok() }
+            fn from_u16_opt(v: u16) -> Option<Self> { v.try_into().ok() }
+            fn from_u32_opt(v: u32) -> Option<Self> { v.try_into().ok() }
+            fn from_u64_opt(v: u64) -> Option<Self> { v.try_into().ok() }
+            fn from_u128_opt(v: u128) -> Option<Self> { v.try_into().ok() }
+            fn from_usize_opt(v: usize) -> Option<Self> { v.try_into().ok() }
+
+            fn to_u8_opt(&self) -> Option<u8> { (*self).try_into().ok() }
+            fn to_u16_opt(&self) -> Option<u16> { (*self).try_into().ok() }
+            fn to_u32_opt(&self) -> Option<u32> { (*self).try_into().ok() }
+            fn to_u64_opt(&self) -> Option<u64> { (*self).try_into().ok() }
+            fn to_u128_opt(&self) -> Option<u128> { (*self).try_into().ok() }
+            fn to_usize_opt(&self) -> Option<usize> { (*self).try_into().ok() }
+        }
+    }
+}
+prim!(u8  , 8  );
+prim!(u16 , 16 );
+prim!(u32 , 32 );
+prim!(u64 , 64 );
+prim!(u128, 128);
index bc479496ae7ba5c4a7947477c77b5794cbcd6f94..597b4cb2cad76eb650d2a382f74664989fa871f5 100644 (file)
@@ -350,7 +350,7 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream {
             fn serialize<S: #serde::Serializer>(
                 set: #enumset::EnumSet<#name>, ser: S,
             ) -> #core::result::Result<S::Ok, S::Error> {
-                #serde::Serialize::serialize(&(set.__enumset_underlying as #serialize_repr), ser)
+                #serde::Serialize::serialize(&(set.__priv_repr as #serialize_repr), ser)
             }
             fn deserialize<'de, D: #serde::Deserializer<'de>>(
                 de: D,
@@ -358,7 +358,7 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream {
                 let value = <#serialize_repr as #serde::Deserialize>::deserialize(de)?;
                 #check_unknown
                 #core::prelude::v1::Ok(#enumset::EnumSet {
-                    __enumset_underlying: (value & #all_variants) as #repr,
+                    __priv_repr: (value & #all_variants) as #repr,
                 })
             }
         }
@@ -431,6 +431,13 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream {
         quote!((*self as u32) == (*other as u32))
     };
 
+    // used in the enum_set! macro `const fn`s.
+    let self_as_repr_mask = if is_uninhabited {
+        quote! { 0 } // impossible anyway
+    } else {
+        quote! { 1 << self as #repr }
+    };
+
     quote! {
         unsafe impl #enumset::__internal::EnumSetTypePrivate for #name {
             type Repr = #repr;
@@ -454,6 +461,28 @@ fn enum_set_type_impl(info: EnumSetInfo) -> SynTokenStream {
         }
         impl #core::marker::Copy for #name { }
 
+        impl #name {
+            /// Creates a new enumset with only this variant.
+            #[deprecated(note = "This method is an internal implementation detail generated by \
+                                 the `enumset` crate's procedural macro. It should not be used \
+                                 directly. Use `EnumSet::only` instead.")]
+            #[doc(hidden)]
+            pub const fn __impl_enumset_internal__const_only(self) -> EnumSet<#name> {
+                EnumSet { __priv_repr: #self_as_repr_mask }
+            }
+
+            /// Creates a new enumset with this variant added.
+            #[deprecated(note = "This method is an internal implementation detail generated by \
+                                 the `enumset` crate's procedural macro. It should not be used \
+                                 directly. Use the `|` operator instead.")]
+            #[doc(hidden)]
+            pub const fn __impl_enumset_internal__const_merge(
+                self, chain: EnumSet<#name>,
+            ) -> EnumSet<#name> {
+                EnumSet { __priv_repr: chain.__priv_repr | #self_as_repr_mask }
+            }
+        }
+
         #ops
     }
 }