interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / bitflags / spec.md

556 lines · 10768 bytes Raw Blame History Rendered
1# Bitflags
2
3`bitflags` generates flags enums with well-defined semantics and ergonomic end-user APIs.
4
5You can use `bitflags` to:
6
7- provide more user-friendly bindings to C APIs where flags may or may not be fully known in advance.
8- generate efficient options types with string parsing and formatting support.
9
10You can't use `bitflags` to:
11
12- guarantee only bits corresponding to defined flags will ever be set. `bitflags` allows access to the underlying bits type so arbitrary bits may be set.
13- define bitfields. `bitflags` only generates types where set bits denote the presence of some combination of flags.
14
15## Definitions
16
17This section formally defines the terminology and semantics of `bitflags`. It's organized so more fundamental concepts are introduced before those that build on them. It may be helpful to start from the bottom of the section and refer back up to concepts defined earlier.
18
19Examples use `bitflags` syntax with `u8` as the bits type.
20
21### Bits type
22
23A type that defines a fixed number of bits at specific locations.
24
25----
26
27Bits types are typically fixed-width unsigned integers. For example, `u8` is a bits type that defines 8 bits; bit-0 through bit-7.
28
29### Bits value
30
31An instance of a bits type where each bit may be set (`1`) or unset (`0`).
32
33----
34
35Some examples of bits values for the bits type `u8` are:
36
37```rust
380b0000_0000
390b1111_1111
400b1010_0101
41```
42
43#### Equality
44
45Two bits values are equal if their bits are in the same configuration; set bits in one are set in the other, and unset bits in one are unset in the other.
46
47#### Operations
48
49Bits values define the bitwise operators and (`&`), or (`|`), exclusive-or (`^`), and negation (`!`) that apply to each of their bits.
50
51### Flag
52
53A set of bits in a bits type that may have a unique name.
54
55----
56
57Bits are not required to be exclusive to a flag. Bits are not required to be contiguous.
58
59The following is a flag for `u8` with the name `A` that includes bit-0:
60
61```rust
62const A = 0b0000_0001;
63```
64
65The following is a flag for `u8` with the name `B` that includes bit-0, and bit-5:
66
67```rust
68const B = 0b0010_0001;
69```
70
71#### Named flag
72
73A flag with a name.
74
75----
76
77The following is a named flag, where the name is `A`:
78
79```rust
80const A = 0b0000_0001;
81```
82
83#### Unnamed flag
84
85A flag without a name.
86
87----
88
89The following is an unnamed flag:
90
91```rust
92const _ = 0b0000_0001;
93```
94
95#### Zero-bit flag
96
97A flag with a set of zero bits.
98
99----
100
101The following is a zero-bit flag:
102
103```rust
104const ZERO = 0b0000_0000;
105```
106
107#### Single-bit flag
108
109A flag with a set of one bit.
110
111----
112
113The following are single-bit flags:
114
115```rust
116const A = 0b0000_0001;
117const B = 0b0000_0010;
118```
119
120#### Multi-bit flag
121
122A flag with a set of more than one bit.
123
124----
125
126The following are multi-bit flags:
127
128```rust
129const A = 0b0000_0011;
130const B = 0b1111_1111;
131```
132
133### Flags type
134
135A set of defined flags over a specific bits type.
136
137#### Known bit
138
139A bit in any defined flag.
140
141----
142
143In the following flags type:
144
145```rust
146struct Flags {
147 const A = 0b0000_0001;
148 const B = 0b0000_0010;
149 const C = 0b0000_0100;
150}
151```
152
153the known bits are:
154
155```rust
1560b0000_0111
157```
158
159#### Unknown bit
160
161A bit not in any defined flag.
162
163----
164
165In the following flags type:
166
167```rust
168struct Flags {
169 const A = 0b0000_0001;
170 const B = 0b0000_0010;
171 const C = 0b0000_0100;
172}
173```
174
175the unknown bits are:
176
177```rust
1780b1111_1000
179```
180
181### Flags value
182
183An instance of a flags type using its specific bits value for storage.
184
185The flags value of a flag is one where each of its bits is set, and all others are unset.
186
187#### Contains
188
189Whether all set bits in a source flags value are also set in a target flags value.
190
191----
192
193Given the flags value:
194
195```rust
1960b0000_0011
197```
198
199the following flags values are contained:
200
201```rust
2020b0000_0000
2030b0000_0010
2040b0000_0001
2050b0000_0011
206```
207
208but the following flags values are not contained:
209
210```rust
2110b0000_1000
2120b0000_0110
213```
214
215#### Intersects
216
217Whether any set bits in a source flags value are also set in a target flags value.
218
219----
220
221Given the flags value:
222
223```rust
2240b0000_0011
225```
226
227the following flags intersect:
228
229```rust
2300b0000_0010
2310b0000_0001
2320b1111_1111
233```
234
235but the following flags values do not intersect:
236
237```rust
2380b0000_0000
2390b1111_0000
240```
241
242#### Empty
243
244Whether all bits in a flags value are unset.
245
246----
247
248The following flags value is empty:
249
250```rust
2510b0000_0000
252```
253
254The following flags values are not empty:
255
256```rust
2570b0000_0001
2580b0110_0000
259```
260
261#### All
262
263Whether all defined flags are contained in a flags value.
264
265----
266
267Given a flags type:
268
269```rust
270struct Flags {
271 const A = 0b0000_0001;
272 const B = 0b0000_0010;
273}
274```
275
276the following flags values all satisfy all:
277
278```rust
2790b0000_0011
2800b1000_0011
2810b1111_1111
282```
283
284### Operations
285
286Examples in this section all use the given flags type:
287
288```rust
289struct Flags {
290 const A = 0b0000_0001;
291 const B = 0b0000_0010;
292 const C = 0b0000_1100;
293}
294```
295
296#### Truncate
297
298Unset all unknown bits in a flags value.
299
300----
301
302Given the flags value:
303
304```rust
3050b1111_1111
306```
307
308the result of truncation will be:
309
310```rust
3110b0000_1111
312```
313
314----
315
316Truncating doesn't guarantee that a non-empty result will contain any defined flags. Given the following flags type:
317
318```rust
319struct Flags {
320 const A = 0b0000_0101;
321}
322```
323
324and the following flags value:
325
326```rust
3270b0000_1110;
328```
329
330The result of truncation will be:
331
332```rust
3330b0000_0100;
334```
335
336which intersects the flag `A`, but doesn't contain it.
337
338This behavior is possible even when only operating with flags values containing defined flags. Given the following flags type:
339
340```rust
341struct Flags {
342 const A = 0b0000_0101;
343 const B = 0b0000_0001;
344}
345```
346
347The result of `A ^ B` is `0b0000_0100`, which also doesn't contain any defined flag.
348
349----
350
351If all known bits are in the set of at least one defined single-bit flag, then all operations that produce non-empty results will always contain defined flags.
352
353#### Union
354
355The bitwise or (`|`) of the bits in two flags values.
356
357----
358
359The following are examples of the result of unioning flags values:
360
361```rust
3620b0000_0001 | 0b0000_0010 = 0b0000_0011
3630b0000_0000 | 0b1111_1111 = 0b1111_1111
364```
365
366#### Intersection
367
368The bitwise and (`&`) of the bits in two flags values.
369
370----
371
372The following are examples of the result of intersecting flags values:
373
374```rust
3750b0000_0001 & 0b0000_0010 = 0b0000_0000
3760b1111_1100 & 0b1111_0111 = 0b1111_0100
3770b1111_1111 & 0b1111_1111 = 0b1111_1111
378```
379
380#### Symmetric difference
381
382The bitwise exclusive-or (`^`) of the bits in two flags values.
383
384----
385
386The following are examples of the symmetric difference between two flags values:
387
388```rust
3890b0000_0001 ^ 0b0000_0010 = 0b0000_0011
3900b0000_1111 ^ 0b0000_0011 = 0b0000_1100
3910b1100_0000 ^ 0b0011_0000 = 0b1111_0000
392```
393
394#### Complement
395
396The bitwise negation (`!`) of the bits in a flags value, truncating the result.
397
398----
399
400The complement is the only operation that explicitly truncates its result, because it doesn't accept a second flags value as input and so is likely to set unknown bits.
401
402----
403
404The following are examples of the complement of a flags value:
405
406```rust
407!0b0000_0000 = 0b0000_1111
408!0b0000_1111 = 0b0000_0000
409!0b1111_1000 = 0b0000_0111
410```
411
412#### Difference
413
414The bitwise intersection (`&`) of the bits in one flags value and the bitwise negation (`!`) of the bits in another.
415
416----
417
418This operation is not equivalent to the intersection of one flags value with the complement of another (`&!`).
419The former will truncate the result in the complement, where difference will not.
420
421----
422
423The following are examples of the difference between two flags values:
424
425```rust
4260b0000_0001 & !0b0000_0010 = 0b0000_0001
4270b0000_1101 & !0b0000_0011 = 0b0000_1100
4280b1111_1111 & !0b0000_0001 = 0b1111_1110
429```
430
431### Iteration
432
433Yield the bits of a source flags value in a set of contained flags values.
434
435----
436
437To be most useful, each yielded flags value should set exactly the bits of a defined flag contained in the source. Any known bits that aren't in the set of any contained flag should be yielded together as a final flags value.
438
439----
440
441Given the following flags type:
442
443```rust
444struct Flags {
445 const A = 0b0000_0001;
446 const B = 0b0000_0010;
447 const AB = 0b0000_0011;
448}
449```
450
451and the following flags value:
452
453```rust
4540b0000_1111
455```
456
457When iterated it may yield a flags value for `A` and `B`, then a final flag with the unknown bits:
458
459```rust
4600b0000_0001
4610b0000_0010
4620b0000_1100
463```
464
465It may also yield a flags value for `AB`, then a final flag with the unknown bits:
466
467```rust
4680b0000_0011
4690b0000_1100
470```
471
472----
473
474Given the following flags type:
475
476```rust
477struct Flags {
478 const A = 0b0000_0011;
479}
480```
481
482and the following flags value:
483
484```rust
4850b0000_0001
486```
487
488When iterated it will still yield a flags value for the known bit `0b0000_0001` even though it doesn't contain a flag.
489
490### Formatting
491
492Format and parse a flags value as text using the following grammar:
493
494- _Flags:_ (_Whitespace_ _Flag_ _Whitespace_)`|`*
495- _Flag:_ _Name_ | _Hex Number_
496- _Name:_ The name of any defined flag
497- _Hex Number_: `0x`([0-9a-fA-F])*
498- _Whitespace_: (\s)*
499
500Flags values can be formatted as _Flags_ by iterating over them, formatting each yielded flags value as a _Flag_. Any yielded flags value that sets exactly the bits of a defined flag with a name should be formatted as a _Name_. Otherwise it must be formatted as a _Hex Number_.
501
502Formatting and parsing supports three modes:
503
504- **Retain**: Formatting and parsing roundtrips exactly the bits of the source flags value. This is the default behavior.
505- **Truncate**: Flags values are truncated before formatting, and truncated after parsing.
506- **Strict**: A _Flag_ may only be formatted and parsed as a _Name_. _Hex numbers_ are not allowed. A consequence of this is that unknown bits and any bits that aren't in a contained named flag will be ignored. This is recommended for flags values serialized across API boundaries, like web services.
507
508Text that is empty or whitespace is an empty flags value.
509
510----
511
512Given the following flags type:
513
514```rust
515struct Flags {
516 const A = 0b0000_0001;
517 const B = 0b0000_0010;
518 const AB = 0b0000_0011;
519 const C = 0b0000_1100;
520}
521```
522
523The following are examples of how flags values can be formatted using any mode:
524
525```rust
5260b0000_0000 = ""
5270b0000_0001 = "A"
5280b0000_0010 = "B"
5290b0000_0011 = "A | B"
5300b0000_0011 = "AB"
5310b0000_1111 = "A | B | C"
532```
533
534Truncate mode will unset any unknown bits:
535
536```rust
5370b1000_0000 = ""
5380b1111_1111 = "A | B | C"
5390b0000_1000 = "0x8"
540```
541
542Retain mode will include any unknown bits as a final _Flag_:
543
544```rust
5450b1000_0000 = "0x80"
5460b1111_1111 = "A | B | C | 0xf0"
5470b0000_1000 = "0x8"
548```
549
550Strict mode will unset any unknown bits, as well as bits not contained in any defined named flags:
551
552```rust
5530b1000_0000 = ""
5540b1111_1111 = "A | B | C"
5550b0000_1000 = ""
556```