88` enumflags2 ` defines a ` BitFlags<T> ` type, which is a ` Set<T> `
99for enums without associated data.
1010
11+ This means that the type of a single flag is separate from a set of flags.
12+
1113## Usage
1214
1315In your ` Cargo.toml ` :
@@ -25,33 +27,38 @@ enumflags2 = "^0.6"
2527- [x] The debug formatter prints the binary flag value as well as the flag enums: ` BitFlags(0b1111, [A, B, C, D]) ` .
2628- [x] Optional support for serialization with the [ ` serde ` ] ( https://serde.rs/ ) feature flag.
2729
28- ### Example
29-
30- ``` rust
31- use enumflags2 :: BitFlags ;
30+ ## Example
31+ ```
32+ use enumflags2::{bitflags, make_bitflags, BitFlags};
3233
33- #[derive( BitFlags , Copy , Clone , Debug , PartialEq ) ]
34+ #[bitflags ]
3435#[repr(u8)]
36+ #[derive(Copy, Clone, Debug, PartialEq)]
3537enum Test {
3638 A = 0b0001,
3739 B = 0b0010,
3840 C = 0b0100,
3941 D = 0b1000,
4042}
4143
42- let a_b = Test :: A | Test :: B ; // BitFlags<Test>
44+ // Flags can be combined with |, this creates a BitFlags of your type:
45+ let a_b: BitFlags<Test> = Test::A | Test::B;
4346let a_c = Test::A | Test::C;
44- let b_c_d = Test :: C | Test :: B | Test :: D ;
47+ let b_c_d = make_bitflags!(Test::{B | C | D});
48+
49+ // The debug output lets you inspect both the numeric value and
50+ // the actual flags:
4551
4652// BitFlags<Test>(0b11, [A, B])
4753println!("{:?}", a_b);
4854
4955// BitFlags<Test>(0b1, [A])
5056println!("{:?}", a_b & a_c);
5157
52- // Iterate over the flags like a normal set!
58+ // Iterate over the flags like a normal set
5359assert_eq!(a_b.iter().collect::<Vec<_>>(), &[Test::A, Test::B]);
5460
61+ // Query the contents with contains and intersects
5562assert!(a_b.contains(Test::A));
5663assert!(b_c_d.contains(Test::B | Test::C));
5764assert!(!(b_c_d.contains(a_b)));
@@ -60,37 +67,28 @@ assert!(a_b.intersects(a_c));
6067assert!(!(a_b.intersects(Test::C | Test::D)));
6168```
6269
63- ### Note
64-
65- By default, the ` BitFlags ` are ` usize ` -sized. If you want them to be smaller,
66- specify a ` repr ` on your enum as in the example above.
67-
68- ### Migrating from 0.5
69-
70- The minimum rustc version has been bumped to 1.34.0, because of ` syn 1.0 ` . The
71- version policy from now on will be "what's available on Debian stable", [ because
72- Debian is famously slow with new software versions] [ debian-snailpace ] .
73-
74- You should no longer depend on ` enumflags2_derive ` directly.
75- Use the reexport from the ` enumflags2 ` crate.
76- semver guarantees will be violated if you depend on the derive crate directly.
70+ ## Optional Feature Flags
7771
78- The derive macro has been renamed to ` BitFlags ` , to make it clearer what the
79- derive does.
72+ - [ ` serde ` ] ( https://serde.rs/ ) implements ` Serialize ` and ` Deserialize `
73+ for ` BitFlags<T> ` .
74+ - ` std ` implements ` std::error::Error ` for ` FromBitsError ` .
8075
81- The ` nostd ` feature flag has been removed. The crate now only depends on ` libcore `
82- by default. Enable the ` std ` flag to get an implementation of ` std::error::Error `
83- on error types.
76+ ## ` const fn ` -compatible APIs
8477
85- Flags more than one bit set have been found to have inconsistent semantics .
86- They are now rejected at compile-time. The same applies to flags without any
87- bit set. If you were relying on this in your code, please [ open an issue ] [ issue ]
88- and explain your usecase .
78+ ** Background: ** The subset of ` const fn ` features currently stabilized is pretty limited .
79+ Most notably, [ const traits are still at the RFC stage ] [ const-trait-rfc ] ,
80+ which makes it impossible to use any overloaded operators in a const
81+ context .
8982
90- ` BitFlags::from_bits ` returns a ` Result ` instead of an ` Option ` . This might
91- necessitate some minor changes in your code .
83+ ** Naming convention: ** If a separate, more limited function is provided
84+ for usage in a ` const fn ` , the name is suffixed with ` _c ` .
9285
93- ` BitFlags::not ` has been removed. Use the ` ! ` operator instead.
86+ ** Blanket implementations:** If you attempt to write a ` const fn ` ranging
87+ over ` T: BitFlag ` , you will be met with an error explaining that currently,
88+ the only allowed trait bound for a ` const fn ` is ` ?Sized ` . You will probably
89+ want to write a separate implementation for ` BitFlags<T, u8> ` ,
90+ ` BitFlags<T, u16> ` , etc — probably generated by a macro.
91+ This strategy is often used by ` enumflags2 ` itself; to avoid clutter, only
92+ one of the copies is shown in the documentation.
9493
95- [ debian-snailpace ] : https://www.jwz.org/blog/2016/04/i-would-like-debian-to-stop-shipping-xscreensaver/
96- [ issue ] : https://github.com/NieDzejkob/enumflags2/issues/new
94+ [ const-trait-rfc ] : https://github.com/rust-lang/rfcs/pull/2632
0 commit comments