@@ -26,7 +26,7 @@ pub fn bitflags_internal(
2626) -> proc_macro:: TokenStream {
2727 let ast: DeriveInput = syn:: parse ( input) . unwrap ( ) ;
2828
29- let impls = match ast. data {
29+ let output = match ast. data {
3030 Data :: Enum ( ref data) => {
3131 gen_enumflags ( & ast. ident , & ast, data)
3232 }
@@ -38,12 +38,13 @@ pub fn bitflags_internal(
3838 }
3939 } ;
4040
41- let impls = impls. unwrap_or_else ( |err| err. to_compile_error ( ) ) ;
42- let combined = quote ! {
43- #ast
44- #impls
45- } ;
46- combined. into ( )
41+ output. unwrap_or_else ( |err| {
42+ let error = err. to_compile_error ( ) ;
43+ quote ! {
44+ #ast
45+ #error
46+ }
47+ } ) . into ( )
4748}
4849
4950/// Try to evaluate the expression given.
@@ -178,10 +179,11 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
178179{
179180 let span = Span :: call_site ( ) ;
180181 // for quote! interpolation
181- let variant_names = data. variants . iter ( ) . map ( |v| & v. ident ) ;
182- let variant_count = data. variants . len ( ) ;
183-
184- let repeated_name = std:: iter:: repeat ( & ident) ;
182+ let variant_names =
183+ data. variants . iter ( )
184+ . map ( |v| & v. ident )
185+ . collect :: < Vec < _ > > ( ) ;
186+ let repeated_name = vec ! [ & ident; data. variants. len( ) ] ;
185187
186188 let variants = collect_flags ( data. variants . iter ( ) ) ?;
187189 let deferred = variants. iter ( )
@@ -192,16 +194,11 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
192194 . ok_or_else ( || syn:: Error :: new_spanned ( & ident,
193195 "repr attribute missing. Add #[repr(u64)] or a similar attribute to specify the size of the bitfield." ) ) ?;
194196 let std_path = quote_spanned ! ( span => :: enumflags2:: _internal:: core) ;
195- let all = if variant_count == 0 {
196- quote ! ( 0 )
197- } else {
198- let repeated_name = repeated_name. clone ( ) ;
199- let variant_names = variant_names. clone ( ) ;
200- quote ! ( #( #repeated_name:: #variant_names as #ty) |* )
201- } ;
202197
203198 Ok ( quote_spanned ! {
204- span => #( #deferred) *
199+ span =>
200+ #item
201+ #( #deferred) *
205202 impl #std_path:: ops:: Not for #ident {
206203 type Output = :: enumflags2:: BitFlags <#ident>;
207204 fn not( self ) -> Self :: Output {
@@ -236,23 +233,17 @@ fn gen_enumflags(ident: &Ident, item: &DeriveInput, data: &DataEnum)
236233 impl :: enumflags2:: _internal:: RawBitFlags for #ident {
237234 type Type = #ty;
238235
239- fn all_bits( ) -> Self :: Type {
240- // make sure it's evaluated at compile time
241- const VALUE : #ty = #all;
242- VALUE
243- }
236+ const ALL_BITS : Self :: Type =
237+ 0 #( | ( #repeated_name:: #variant_names as #ty) ) * ;
244238
245- fn bits( self ) -> Self :: Type {
246- self as #ty
247- }
239+ const FLAG_LIST : & ' static [ Self ] =
240+ & [ #( #repeated_name:: #variant_names) , * ] ;
248241
249- fn flag_list( ) -> & ' static [ Self ] {
250- const VARIANTS : [ #ident; #variant_count] = [ #( #repeated_name :: #variant_names) , * ] ;
251- & VARIANTS
252- }
242+ const BITFLAGS_TYPE_NAME : & ' static str =
243+ concat!( "BitFlags<" , stringify!( #ident) , ">" ) ;
253244
254- fn bitflags_type_name ( ) -> & ' static str {
255- concat! ( "BitFlags<" , stringify! ( #ident ) , ">" )
245+ fn bits ( self ) -> Self :: Type {
246+ self as #ty
256247 }
257248 }
258249
0 commit comments