An enum
in Rust is not intended to be used as bit flags. PublicFlags
can take only the values indicated in the enumeration (and not in combination). So, for example, the following match operator is exhaustive:
let flags: PublicFlags;
...
match flags {
PublicFlagVersion => {...}
PublicFlagReset => {...}
NoncePresent => {...}
IdPresent => {...}
PktNumLen4 => {...}
PktNumLen2 => {...}
PktNumLen1 => {...}
Multipath => {...}
}
It is not possible to have a variable PublicFlags
with a combination of flags.
, u8
, . , , , bitflags . , -:
extern crate bitflags;
bitflags! {
flags PublicFlags: u8 {
const PUBLIC_FLAG_VERSION = 0x01,
const PUBLIC_FLAG_RESET = 0x02,
const NONCE_PRESENT = 0x04,
const ID_PRESENT = 0x08,
const PKT_NUM_LEN_4 = 0x30,
const PKT_NUM_LEN_2 = 0x20,
const PKT_NUM_LEN_1 = 0x10,
const MULTIPATH = 0x40,
}
}
fn main() {
let flag = PUBLIC_FLAG_VERSION | ID_PRESENT;
assert!((flag & MULTIPATH).is_empty());
assert!(flag.contains(ID_PRESENT));
}