| 1 |
//! An example of implementing the `BitFlags` trait manually for a flags type. |
| 2 |
|
| 3 |
use std::str; |
| 4 |
|
| 5 |
use bitflags::bitflags; |
| 6 |
|
| 7 |
// Define a flags type outside of the `bitflags` macro as a newtype |
| 8 |
// It can accept custom derives for libaries `bitflags` doesn't support natively |
| 9 |
#[derive(zerocopy::AsBytes, zerocopy::FromBytes)] |
| 10 |
#[repr(transparent)] |
| 11 |
pub struct ManualFlags(u32); |
| 12 |
|
| 13 |
// Next: use `impl Flags` instead of `struct Flags` |
| 14 |
bitflags! { |
| 15 |
impl ManualFlags: u32 { |
| 16 |
const A = 0b00000001; |
| 17 |
const B = 0b00000010; |
| 18 |
const C = 0b00000100; |
| 19 |
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
fn main() {} |