bitmask-enum 2.2.5

A bitmask enum attribute macro
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use proc_macro::{Span, TokenStream};
use syn::{
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    Error, Ident, ItemEnum, Result, Token,
};

pub fn parse(attr: TokenStream, mut item: ItemEnum) -> Result<TokenStream> {
    let typ = parse_typ(attr)?;

    let config = if let Some(idx) = item
        .attrs
        .iter()
        .enumerate()
        .find_map(|(idx, attr)| attr.path().is_ident("bitmask_config").then_some(idx))
    {
        item.attrs.remove(idx).parse_args::<Config>()?
    } else {
        Config::new()
    };

    let vis = item.vis;
    let attrs = item.attrs;
    let ident = item.ident;

    let mut flags_amount = item.variants.len();

    if config.inverted_flags {
        flags_amount *= 2;
    }

    let mut all_flags = Vec::with_capacity(flags_amount);
    let mut all_flags_names = Vec::with_capacity(flags_amount);

    let mut i: usize = 0;
    let mut flags = Vec::with_capacity(flags_amount);
    for v in item.variants.iter() {
        let v_attrs = &v.attrs;
        let v_ident = &v.ident;

        all_flags.push(v_ident.clone());
        all_flags_names.push(quote::quote!(stringify!(#v_ident)));

        let expr = if let Some((_, expr)) = v.discriminant.as_ref() {
            quote::quote!(#expr)
        } else {
            let expr = quote::quote!(1 << #i);
            i += 1;
            expr
        };

        let i_flag = config
            .inverted_flags
            .then(|| {
                let i_ident = Ident::new(&format!("Inverted{}", v_ident), v_ident.span());

                all_flags.push(i_ident.clone());
                all_flags_names.push(quote::quote!(stringify!(#i_ident)));

                quote::quote!(
                    #(#v_attrs)*
                    #vis const #i_ident: #ident = Self { bits: (#expr) ^ !0 };
                )
            })
            .into_iter();

        flags.push(quote::quote!(
            #(#v_attrs)*
            #vis const #v_ident: #ident = Self { bits: #expr };

            #(#i_flag)*
        ))
    }

    let flags_iter = config.flags_iter.then(|| {
        quote::quote!(
            /// Returns an iterator over all flags of the bitmask.
            /// Where each Item = (name, flag).
            #vis fn flags() -> impl core::iter::Iterator<Item = &'static (&'static str, Self)> {
                static FLAGS: [(&'static str, #ident); #flags_amount] = [
                    #((#all_flags_names, #ident::#all_flags),)*
                ];

                FLAGS.iter()
            }
        )
    }).into_iter();

    let debug_impl = if config.vec_debug {
        quote::quote! {
            impl core::fmt::Debug for #ident {
                fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                    write!(f, "{}[", stringify!(#ident))?;

                    let mut has_flags = false;
                    #(if self.contains(Self::#all_flags) {
                        if has_flags {
                            write!(f, ", {}", #all_flags_names)?;
                        } else {
                            write!(f, "{}", #all_flags_names)?;
                            has_flags = true;
                        }
                    })*

                    write!(f, "]")
                }
            }
        }
    } else {
        quote::quote! {
            impl core::fmt::Debug for #ident {
                fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
                    f.debug_struct(stringify!(#ident))
                        .field("bits", &self.bits)
                        .finish()
                }
            }
        }
    };

    Ok(TokenStream::from(quote::quote! {
        #(#attrs)*
        #[repr(transparent)]
        #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        #vis struct #ident {
            bits: #typ,
        }

        #[allow(non_upper_case_globals)]
        impl #ident {
            #(#flags)*

            #(#flags_iter)*

            /// Returns the underlying bits of the bitmask.
            #[inline]
            #vis const fn bits(&self) -> #typ {
                self.bits
            }

            /// Returns a bitmask that contains all values.
            ///
            /// This will include bits that do not have any flags.
            /// Use `::all_flags()` if you only want to use flags.
            #[inline]
            #vis const fn all_bits() -> Self {
                Self { bits: !0 }
            }

            /// Returns a bitmask that contains all flags.
            #[inline]
            #vis const fn all_flags() -> Self {
                Self { bits: #(Self::#all_flags.bits |)* 0 }
            }

            /// Returns `true` if the bitmask contains all values.
            ///
            /// This will check for `bits == !0`,
            /// use `.is_all_flags()` if you only want to check for all flags
            #[inline]
            #vis const fn is_all_bits(&self) -> bool {
                self.bits == !0
            }

            /// Returns `true` if the bitmask contains all flags.
            ///
            /// This will fail if any unused bit is set,
            /// consider using `.truncate()` first.
            #[inline]
            #vis const fn is_all_flags(&self) -> bool {
                self.bits == Self::all_flags().bits
            }

            /// Returns a bitmask that contains all values.
            ///
            /// This will include bits that do not have any flags.
            /// Use `::all_flags()` if you only want to use flags.
            #[inline]
            #[deprecated(note = "Please use the `::all_bits()` constructor")]
            #vis const fn all() -> Self {
                Self::all_bits()
            }

            /// Returns `true` if the bitmask contains all values.
            ///
            /// This will check for `bits == !0`,
            /// use `.is_all_flags()` if you only want to check for all flags
            #[inline]
            #[deprecated(note = "Please use the `.is_all_bits()` method")]
            #vis const fn is_all(&self) -> bool {
                self.is_all_bits()
            }


            /// Returns a bitmask that contains all flags.
            #[inline]
            #[deprecated(note = "Please use the `::all_flags()` constructor")]
            #vis const fn full() -> Self {
                Self::all_flags()
            }

            /// Returns `true` if the bitmask contains all flags.
            ///
            /// This will fail if any unused bit is set,
            /// consider using `.truncate()` first.
            #[inline]
            #[deprecated(note = "Please use the `.is_all_flags()` method")]
            #vis const fn is_full(&self) -> bool {
                self.is_all_flags()
            }

            /// Returns a bitmask that does not contain any values.
            #[inline]
            #vis const fn none() -> Self {
                Self { bits: 0 }
            }

            /// Returns `true` if the bitmask does not contain any values.
            #[inline]
            #vis const fn is_none(&self) -> bool {
                self.bits == 0
            }

            /// Returns a bitmask that only has bits corresponding to flags
            #[inline]
            #vis const fn truncate(&self) -> Self {
                Self { bits: self.bits & Self::all_flags().bits }
            }

            /// Returns `true` if `self` intersects with any value in `other`,
            /// or if `other` does not contain any values.
            ///
            /// This is equivalent to `(self & other) != 0 || other == 0`.
            #[inline]
            #vis const fn intersects(&self, other: Self) -> bool {
                (self.bits & other.bits) != 0 || other.bits == 0
            }

            /// Returns `true` if `self` contains all values of `other`.
            ///
            /// This is equivalent to  `(self & other) == other`.
            #[inline]
            #vis const fn contains(&self, other: Self) -> bool {
                (self.bits & other.bits) == other.bits
            }

            /// Returns the bitwise NOT of the bitmask.
            #[inline]
            #vis const fn not(self) -> Self {
                Self { bits: !self.bits }
            }

            /// Returns the bitwise AND of the bitmask.
            #[inline]
            #vis const fn and(self, other: Self) -> Self {
                Self { bits: self.bits & other.bits }
            }

            /// Returns the bitwise OR of the bitmask.
            #[inline]
            #vis const fn or(self, other: Self) -> Self {
                Self { bits: self.bits | other.bits }
            }

            /// Returns the bitwise XOR of the bitmask.
            #[inline]
            #vis const fn xor(self, other: Self) -> Self {
                Self { bits: self.bits ^ other.bits }
            }
        }

        impl core::ops::Not for #ident {
            type Output = Self;
            #[inline]
            fn not(self) -> Self::Output {
                Self { bits: core::ops::Not::not(self.bits) }
            }
        }

        impl core::ops::BitAnd for #ident {
            type Output = Self;
            #[inline]
            fn bitand(self, rhs: Self) -> Self::Output {
                Self { bits: core::ops::BitAnd::bitand(self.bits, rhs.bits) }
            }
        }

        impl core::ops::BitAndAssign for #ident {
            #[inline]
            fn bitand_assign(&mut self, rhs: Self) {
                core::ops::BitAndAssign::bitand_assign(&mut self.bits, rhs.bits)
            }
        }

        impl core::ops::BitOr for #ident {
            type Output = Self;
            #[inline]
            fn bitor(self, rhs: Self) -> Self::Output {
                Self { bits: core::ops::BitOr::bitor(self.bits, rhs.bits) }
            }
        }

        impl core::ops::BitOrAssign for #ident {
            #[inline]
            fn bitor_assign(&mut self, rhs: Self) {
                core::ops::BitOrAssign::bitor_assign(&mut self.bits, rhs.bits)
            }
        }

        impl core::ops::BitXor for #ident {
            type Output = Self;
            #[inline]
            fn bitxor(self, rhs: Self) -> Self::Output {
                Self { bits: core::ops::BitXor::bitxor(self.bits, rhs.bits) }
            }
        }

        impl core::ops::BitXorAssign for #ident {
            #[inline]
            fn bitxor_assign(&mut self, rhs: Self) {
                core::ops::BitXorAssign::bitxor_assign(&mut self.bits, rhs.bits)
            }
        }

        impl From<#typ> for #ident {
            #[inline]
            fn from(val: #typ) -> Self {
                Self { bits: val }
            }
        }

        impl From<#ident> for #typ {
            #[inline]
            fn from(val: #ident) -> #typ {
                val.bits
            }
        }

        impl PartialEq<#typ> for #ident {
            #[inline]
            fn eq(&self, other: &#typ) -> bool {
                self.bits == *other
            }
        }

        #debug_impl

        impl core::fmt::Binary for #ident {
            #[inline]
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Binary::fmt(&self.bits, f)
            }
        }

        impl core::fmt::LowerHex for #ident {
            #[inline]
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::LowerHex::fmt(&self.bits, f)
            }
        }

        impl core::fmt::UpperHex for #ident {
            #[inline]
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::UpperHex::fmt(&self.bits, f)
            }
        }

        impl core::fmt::Octal for #ident {
            #[inline]
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Octal::fmt(&self.bits, f)
            }
        }
    }))
}

fn parse_typ(attr: TokenStream) -> Result<Ident> {
    if attr.is_empty() {
        Ok(Ident::new("usize", Span::call_site().into()))
    } else {
        let ident = syn::parse::<Ident>(attr)?;
        match ident.to_string().as_str() {
            #[rustfmt::skip]
            "u8" | "u16" | "u32" | "u64" | "u128" | "usize" |
            "i8" | "i16" | "i32" | "i64" | "i128" | "isize" => Ok(ident),
            _ => Err(Error::new_spanned(
                ident,
                "type can only be an (un)signed integer",
            )),
        }
    }
}

struct Config {
    inverted_flags: bool,
    vec_debug: bool,
    flags_iter: bool,
}

impl Config {
    fn new() -> Self {
        Self {
            inverted_flags: false,
            vec_debug: false,
            flags_iter: false,
        }
    }
}

impl Parse for Config {
    fn parse(input: ParseStream) -> Result<Self> {
        let args = Punctuated::<Ident, Token![,]>::parse_terminated(input)?;
        let mut config = Self::new();
        for arg in args {
            match arg.to_string().as_str() {
                "inverted_flags" => config.inverted_flags = true,
                "vec_debug" => config.vec_debug = true,
                "flags_iter" => config.flags_iter = true,
                _ => return Err(Error::new_spanned(arg, "unknown config option")),
            }
        }
        Ok(config)
    }
}