|
3 | 3 | //! with `#[bitflags]`, and `BitFlags<YourEnum>` will be able to hold arbitrary combinations
|
4 | 4 | //! of your enum within the space of a single integer.
|
5 | 5 | //!
|
| 6 | +//! Unlike other crates, `enumflags2` makes the type-level distinction between |
| 7 | +//! a single flag (`YourEnum`) and a set of flags (`BitFlags<YourEnum>`). |
| 8 | +//! This allows idiomatic handling of bitflags, such as with `match` and `iter`. |
| 9 | +//! |
6 | 10 | //! ## Example
|
7 | 11 | //! ```
|
8 | 12 | //! use enumflags2::{bitflags, make_bitflags, BitFlags};
|
|
58 | 62 | //! **Naming convention:** If a separate, more limited function is provided
|
59 | 63 | //! for usage in a `const fn`, the name is suffixed with `_c`.
|
60 | 64 | //!
|
| 65 | +//! Apart from functions whose name ends with `_c`, the [`make_bitflags!`] macro |
| 66 | +//! is often useful for many `const` and `const fn` usecases. |
| 67 | +//! |
61 | 68 | //! **Blanket implementations:** If you attempt to write a `const fn` ranging
|
62 | 69 | //! over `T: BitFlag`, you will be met with an error explaining that currently,
|
63 | 70 | //! the only allowed trait bound for a `const fn` is `?Sized`. You will probably
|
@@ -298,6 +305,11 @@ pub mod _internal {
|
298 | 305 | ///
|
299 | 306 | /// The values should reflect reality, like they do if the implementation
|
300 | 307 | /// is generated by the procmacro.
|
| 308 | + /// |
| 309 | + /// `bits` must return the same value as |
| 310 | + /// [`transmute_copy`][std::mem::transmute_copy]. |
| 311 | + /// |
| 312 | + /// Representations for all values of `T` must have exactly one bit set. |
301 | 313 | pub unsafe trait RawBitFlags: Copy + Clone + 'static {
|
302 | 314 | /// The underlying integer type.
|
303 | 315 | type Numeric: BitFlagNum;
|
@@ -533,17 +545,20 @@ pub struct BitFlags<T, N = <T as _internal::RawBitFlags>::Numeric> {
|
533 | 545 | /// `BitFlags<T>`. Instead of repeating the name of your type for each flag
|
534 | 546 | /// you want to add, try `make_bitflags!(Flags::{Foo | Bar})`.
|
535 | 547 | /// ```
|
536 |
| -/// use enumflags2::{bitflags, make_bitflags}; |
537 |
| -/// #[bitflags] |
538 |
| -/// #[repr(u8)] |
539 |
| -/// #[derive(Clone, Copy, Debug)] |
540 |
| -/// enum Test { |
541 |
| -/// A = 1 << 0, |
542 |
| -/// B = 1 << 1, |
543 |
| -/// C = 1 << 2, |
544 |
| -/// } |
| 548 | +/// # use enumflags2::{bitflags, BitFlags, make_bitflags}; |
| 549 | +/// # #[bitflags] |
| 550 | +/// # #[repr(u8)] |
| 551 | +/// # #[derive(Clone, Copy, Debug)] |
| 552 | +/// # enum Test { |
| 553 | +/// # A = 1 << 0, |
| 554 | +/// # B = 1 << 1, |
| 555 | +/// # C = 1 << 2, |
| 556 | +/// # } |
545 | 557 | /// let x = make_bitflags!(Test::{A | C});
|
546 | 558 | /// assert_eq!(x, Test::A | Test::C);
|
| 559 | +/// |
| 560 | +/// // Also works in const contexts: |
| 561 | +/// const X: BitFlags<Test> = make_bitflags!(Test::{A | C}); |
547 | 562 | /// ```
|
548 | 563 | #[macro_export]
|
549 | 564 | macro_rules! make_bitflags {
|
|
0 commit comments