]> git.lizzy.rs Git - enumset.git/blobdiff - enumset_derive/src/lib.rs
Use const fns for enum_set! instead of a weird hack.
[enumset.git] / enumset_derive / src / lib.rs
index 83ac1381cf89f0d44331bc42689bb53ba5a6a82c..3dc9a451772afcb9ff202c964892ae296cbba2e4 100644 (file)
@@ -432,6 +432,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;
@@ -455,6 +462,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 { __enumset_underlying: #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 { __enumset_underlying: chain.__enumset_underlying | #self_as_repr_mask }
+            }
+        }
+
         #ops
     }
 }