bitflags_attr_macros/
lib.rs

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
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{Error, Result};
use typed::{Args, Bitflag};

mod typed;

/// An attribute macro that transforms an C-like enum into a bitflag struct type implementing an API
/// similar to the `bitflags` crate, and implementing many helpful traits (listed in more details
/// below).
///
/// The attribute requires that the [`Clone`] and [`Copy`] traits are derived for the type.
///
/// [`Clone`]: ::core::clone::Clone
/// [`Copy`]: ::core::marker::Copy
///
/// ## Examples
///
/// Generate a flags type using `u8` as the bits type:
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 0b0000_0100,
/// }
/// ```
///
/// Flags may refer to other flags using their names:
///
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 0b0000_0100,
///     AB = A | B,
/// }
/// ```
///
/// Flags may also refer to other flags using their `bits` method value, like `bitflags` crate:
///
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 0b0000_0100,
///     AB = Flags::A.bits() | Flags::B.bits(),
/// }
/// ```
///
/// It's possible to use more derives and attributes by simply adding them
///
/// ```rust
/// # use core::fmt::Debug as ExternalDerive
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ExternalDerive)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 0b0000_0100,
///     AB = Flags::A.bits() | Flags::B.bits(),
/// }
/// ```
///
/// ## Known and unknown flags
///
/// The variant of the enum are flags. They will be expanded to type-associated constants. Every
/// variant value is a known flag, while every not specified value is a unknown flag.
///
/// There are operation that will truncate out the unknown values. But tha can be configured if
/// desired; more on that on [Externally defined flags](#externally-defined-flags)
///
/// ## Externally defined flags
///
/// If you're generating flags types for an external source, such as a C API, you can use the
/// `non_exhaustive` attribute to communicate to the bitflags macro that there may be more valid
/// flags then the known flags.
///
/// Without extra configuration, it defaults to `!0` (all bits set) as a mask of all bits the
/// external source may ever set, i.e. all bits are considered as possible values.
///
/// ```
/// use bitflag_attr::bitflag;
///
/// #[bitflag(u32)]
/// #[non_exhaustive] // All bits are considered as possible values.
/// #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// But you can also configure it using the helper attribute `extra_valid_bits` with the value of
/// valid bits that the external source may ever set.
///
/// ```
/// use bitflag_attr::bitflag;
///
/// #[bitflag(u32)]
/// #[non_exhaustive] // Communicate there is more potential valid flags than the known flags
/// #[extra_valid_bits = 0b001001111] // Specify the extra bits to take into consideration.
/// #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// Why should you do this? Generated methods like `all` and truncating operators like `!` only
/// consider bits in defined flags. Adding an unnamed flag makes those methods consider additional
/// bits, without generating additional constants for them. It helps compatibility when the external
/// source may start setting additional bits at any time.
///
/// ## Type representation
///
/// By default, the generated flag type will be `#[repr(transparent)]`, but you can explicit it on
/// the definition as long is one of the supported ones (`C`, `Rust` and `transparent`):
///
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[repr(C)]
/// #[bitflag(u8)]
/// #[derive(Clone, Copy)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 1 << 2,
/// }
/// ```
///
/// ## Generated trait implementations
///
/// This macro generates some trait implementations: [`ops:Not`], [`ops:BitAnd`],
/// [`ops:BitOr`], [`ops:BitXor`], [`ops:BitAndAssign`], [`ops:BitOrAssign`], [`ops:BitXorAssign`],
/// [`fmt::Binary`], [`fmt::LowerHex`], [`fmt::UpperHex`], [`fmt::Octal`], [`From`], [`Extend`],
/// [`FromIterator`], [`FromStr`] and [`IntoIterator`].
///
/// The custom [`fmt::Debug`] implementation will only be generated if it is included in the
/// `#[derive(...)]` parameters.
///
/// The custom [`Default`] implementation will only be generated if it is included in the
/// `#[derive(...)]` parameters.
///
/// [`IntoIterator`]: core::iter::IntoIterator
/// [`Default`]: core::default::Default
///
/// ### Default derive
///
/// The `bitflag` macro handles the [`Default`] if specified in the derive list. Without specifying
/// a default variant, the default implementation is the same as a empty flag:
///
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy, Default)] // Default is the same as `Flags::empty()`
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     C = 1 << 2,
/// }
/// ```
///
/// But it can be specified like deriving [`Default`] on enum, using the `#[default]` helper attribute:
///
/// ```rust
/// # use bitflag_attr::bitflag;
///
/// #[bitflag(u8)]
/// #[derive(Clone, Copy, Default)]
/// enum Flags {
///     A = 1,
///     B = 1 << 1,
///     #[default] // `Flags::C` are the default value returned by `Default::default()`
///     C = 1 << 2,
/// }
/// ```
///
/// ### Serde feature
///
/// If the crate is compiled with the `serde` feature, this crate will generate implementations for
/// the `serde::{Serialize, Deserialize}` traits if they are included in the `#[derive(...)]`
/// parameters, but it will not import/re-export these traits, your project must have `serde` as
/// a direct dependency.
///
/// ```no_run
/// use bitflag_attr::bitflag;
/// use serde::{Serialize, Deserialize};
///
/// #[bitflag(u32)]
/// #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// ### Arbitrary feature
///
/// If the crate is compiled with the `arbitrary` feature, this crate will generate implementations for
/// the `arbitrary::Arbitrary` traits if they are included in the `#[derive(...)]`
/// parameters, but it will not import/re-export these traits, your project must have `arbitrary` as
/// a direct dependency.
///
/// ```no_run
/// use bitflag_attr::bitflag;
/// use arbitrary::Arbitrary;
///
/// #[bitflag(u32)]
/// #[derive(Clone, Copy, Arbitrary)]
/// enum Color {
///     RED = 0x1,
///     GREEN = 0x02,
///     BLUE = 0x4,
/// }
/// ```
///
/// ### Bytemuck feature
///
/// If the crate is compiled with the `bytemuck` feature, this crate will generate implementations for
/// the `bytemuck::{Pod, Zeroable}` traits if they are included in the `#[derive(...)]`
/// parameters, but it will not import/re-export these traits, your project must have `bytemuck` as
/// a direct dependency.
///
/// ```no_run
/// use bitflag_attr::bitflag;
/// use bytemuck::{Pod, Zeroable};
///
/// #[bitflag(u32)]
/// #[derive(Debug, Clone, Copy, Pod, Zeroable)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// ### `const-mut-ref` feature
///
/// If the crate is compiled with the `const-mut-ref` feature, all type-associated API that takes
/// `&mut self` will be generated as **const-fn**, meaning they can be used on `const` context.
///
/// **Note:** `&mut` on const function was stabilized on Rust 1.83.0, so using this feature flag on
/// Rust versions below that will cause compilation errors
///
/// ### Custom types feature
///
/// If the crate is compiled with the `custom-types` feature, it allows to use more than the types
/// defined in Rust `core` (`i8`,`u8`,`i16`,`u16`,`i32`,`u32`,`i64`,`u64`,`i128`,`u128`,`isize`,
/// `usize`,`c_char`,`c_schar`,`c_uchar`,`c_short`,`c_ushort`,`c_int`,`c_uint`,`c_long`,`c_ulong`,
/// `c_longlong`,`c_ulonglong`) as long as it is a type alias to one of those types.
///
/// The reason it is behind a feature flag is that to ensure the validity of such constrain, we have
/// to pay the price of having much worse error messages. With this feature enabled, a invalid type
/// will cause a massive wall of error message.
///
///
/// # More Examples
///
/// ```
/// use bitflag_attr::bitflag;
///
/// #[bitflag(u32)]
/// #[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// Without generating [`fmt::Debug`]:
///
/// ```
/// use bitflag_attr::bitflag;
///
/// #[bitflag(u32)]
/// #[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash)]
/// pub enum Flags {
///     /// The value `A`, at bit position `0`.
///     A = 0b00000001,
///     /// The value `B`, at bit position `1`.
///     B = 0b00000010,
///     /// The value `C`, at bit position `2`.
///     C = 0b00000100,
///
///     /// The combination of `A`, `B`, and `C`.
///     ABC = A | B | C,
/// }
/// ```
///
/// # Syntax
///
/// ```text
/// #[bitflag($ty)]
/// $visibility enum $StructName {
///     FlagOne = flag1_value_expr,
///     FlagTwo = flag2_value_expr,
///     // ...
///     FlagN = flagn_value_expr,
/// }
/// ```
///
/// [`fmt::Debug`]: core::fmt::Debug
/// [`ops:Not`]: core::ops::Not
/// [`ops:BitAnd`]: core::ops::BitAnd
/// [`ops:BitOr`]: core::ops::BitOr
/// [`ops:BitXor`]: core::ops::BitXor
/// [`ops:BitAndAssign`]: core::ops::BitAndAssign
/// [`ops:BitOrAssign`]: core::ops::BitOrAssign
/// [`ops:BitXorAssign`]: core::ops::BitXorAssign
/// [`fmt::Binary`]: core::fmt::Binary
/// [`fmt::LowerHex`]: core::fmt::LowerHex
/// [`fmt::UpperHex`]: core::fmt::UpperHex
/// [`fmt::Octal`]: core::fmt::Octal
/// [`From`]: From
/// [`FromStr`]: core::str::FromStr
#[proc_macro_attribute]
pub fn bitflag(attr: TokenStream, item: TokenStream) -> TokenStream {
    match bitflag_impl(attr, item) {
        Ok(ts) => ts,
        Err(err) => err.into_compile_error().into(),
    }
}

fn bitflag_impl(attr: TokenStream, item: TokenStream) -> Result<TokenStream> {
    let args: Args = syn::parse(attr)
        .map_err(|err| Error::new(err.span(), "unexpected token: expected a `{integer}` type"))?;

    let bitflag = Bitflag::parse(args, item)?;

    Ok(bitflag.to_token_stream().into())
}