1. 位域
1.1 定义并操作位域风格的类型
需要安装bitflags
库,可通过cargo add bitflags
命令安装
[dependencies] bitflags = "2.1.0"
|
如下实例在 bitflags!
宏的帮助下创建类型安全的位域类型 MyFlags
,并为其实现基本的清理操作(clear
方法)以及 Display trait
。随后,展示了基本的按位操作和格式化。
use bitflags::bitflags; use std::fmt;
bitflags! { #[derive(Clone, Copy, PartialEq, Eq, Debug )] struct MyFlags: u32 { const FLAG_A = 0b00000001; const FLAG_B = 0b00000010; const FLAG_C = 0b00000100; const FLAG_ABC = Self::FLAG_A.bits() | Self::FLAG_B.bits() | Self::FLAG_C.bits(); } }
impl MyFlags { pub fn clear(&mut self) -> &mut MyFlags { *self.0.bits_mut() = 0; self } }
impl std::fmt::Display for MyFlags { fn fmt(&self, f: &mut std::fmt::Formatter) -> fmt::Result { write!(f, "{:032b}", self.bits()) } }
fn main() { let e1 = MyFlags::FLAG_A | MyFlags::FLAG_C; let e2 = MyFlags::FLAG_B | MyFlags::FLAG_C; assert_eq!((e1 | e2), MyFlags::FLAG_ABC); assert_eq!((e1 & e2), MyFlags::FLAG_C); assert_eq!((e1 - e2), MyFlags::FLAG_A); assert_eq!(!e2, MyFlags::FLAG_A);
let mut flags = MyFlags::FLAG_ABC; assert_eq!(format!("{}", flags), "00000000000000000000000000000111"); assert_eq!( format!("{}", flags.clear()), "00000000000000000000000000000000" ); assert_eq!(format!("{:?}", MyFlags::FLAG_B), "MyFlags(FLAG_B)"); assert_eq!( format!("{:?}", MyFlags::FLAG_A | MyFlags::FLAG_B), "MyFlags(FLAG_A | FLAG_B)" ); }
|