15个稳定版本
2.2.4 | 2024年5月17日 |
---|---|
2.2.3 | 2023年11月4日 |
2.2.2 | 2023年8月13日 |
2.2.1 | 2023年7月9日 |
1.1.0 | 2021年3月7日 |
#55 in 过程宏
55,319 每月下载量
在 47 个crate中使用 (21个直接使用)
22KB
313 行
Bitmask-Enum
位掩码枚举属性宏,将枚举转换为位掩码。
位掩码可以有(无)符号整数类型,默认类型是 usize
。
最初创建是因为我想找一个简单的工具,后来受到bitflags crate的启发,你可能想看看这个。
use bitmask_enum::bitmask;
#[bitmask] // usize
enum Bitmask { /* ... */ }
#[bitmask(u8)] // u8
enum BitmaskU8 { /* ... */ }
示例
use bitmask_enum::bitmask;
#[bitmask(u8)]
enum Bitmask {
Flag1, // defaults to 0b00000001
Flag2, // defaults to 0b00000010
Flag3, // defaults to 0b00000100
}
// It is possible to impl on the bitmask and use its bits field
impl Bitmask {
fn _set_to(&mut self, val: u8) {
self.bits = val
}
}
// bitmask has const bitwise operator methods
const CONST_BM: Bitmask = Bitmask::Flag2.or(Bitmask::Flag3);
fn main() {
println!("{:#010b}", CONST_BM); // 0b00000110
// Bitmask that contains Flag1 and Flag3
let bm = Bitmask::Flag1 | Bitmask::Flag3;
println!("{:#010b}", bm); // 0b00000101
// Does bm intersect one of CONST_BM
println!("{}", bm.intersects(CONST_BM)); // true
// Does bm contain all of CONST_BM
println!("{}", bm.contains(CONST_BM)); // false
}
自定义值
您可以给任何标志分配任何自定义值。
use bitmask_enum::bitmask;
#[bitmask(u8)]
enum Bitmask {
Flag1, // defaults to 0b00000001
CustomFlag3 = 0b00000100,
Flag2, // defaults to 0b00000010
Flag3, // defaults to 0b00000100
Flag13_1 = 0b00000001 | 0b00000100,
Flag13_2 = Self::Flag1.or(Self::Flag3).bits,
Flag13_3 = Self::Flag1.bits | Self::CustomFlag3.bits,
Flag123 = {
let flag13 = Self::Flag13_1.bits;
flag13 | Self::Flag2.bits
},
}
fn main() {
let bm = Bitmask::Flag1 | Bitmask::Flag3;
println!("{:#010b}", bm); // 0b00000101
println!("{}", bm == Bitmask::Flag13_1); // true
println!("{:#010b}", Bitmask::Flag123); // 0b00000111
}
位掩码配置
您可以通过 #[bitmask_config(...)]
宏添加自定义位掩码配置选项。 (只需在 #[bitmask]
宏下方添加即可)
use bitmask_enum::bitmask;
#[bitmask(u8)]
#[bitmask_config(inverted_flags)]
enum Bitmask {
Flag1, // defaults to 0b00000001
}
#[bitmask(u8)]
#[bitmask_config(vec_debug)]
enum BitmaskVecDebug {
Flag1,
Flag2,
}
fn main() {
println!("{:#010b}", Bitmask::Flag1); // 0b00000001
println!("{:#010b}", Bitmask::InvertedFlag1); // 0b11111110
println!("{:?}", BitmaskVecDebug::none()); // BitmaskVecDebug[]
println!("{:?}", BitmaskVecDebug::Flag1); // BitmaskVecDebug[Flag1]
println!("{:?}", BitmaskVecDebug::all_flags()); // BitmaskVecDebug[Flag1, Flag2]
}
可用的配置选项
inverted_flags
=> 为每个非反转标志向位掩码添加一个反转标志。vec_debug
=> 用一个自定义的、将位掩码打印为所有匹配值的vec的Debug trait实现替换默认实现。
如果您需要/能想到任何其他配置选项,请随时提出建议,我们可以讨论实现它们。
实现的方法
// Returns the underlying bits of the bitmask.
const fn bits(&self) -> #type;
// Returns a bitmask that contains all values.
//
// This will include bits that do not have any associated flags.
// Use `::all_flags()` if you only want to use flags.
const fn all_bits() -> Self;
// Returns `true` if the bitmask contains all values.
//
// This will check for `bits == !0`,
// use `.is_all_flags()` if you only want to check for all flags
const fn is_all_bits(&self) -> bool;
// Returns a bitmask that does not contain any values.
const fn none() -> Self;
// Returns `true` if the bitmask does not contain any values.
const fn is_none(&self) -> bool;
// Returns a bitmask that contains all flags.
const fn all_flags() -> Self;
// Returns `true` if the bitmask contains all flags.
//
// This will fail if any unused bit is set,
// consider using `.truncate()` first.
const fn is_all_flags(&self) -> bool;
// Returns a bitmask that only has bits corresponding to flags
const fn truncate(&self) -> Self;
// Returns `true` if `self` intersects with any value in `other`,
// or if `other` does not contain any values.
// This is equivalent to `(self & other) != 0 || other == 0`.
const fn intersects(&self, other: Self) -> bool;
// Returns `true` if `self` contains all values of `other`.
// This is equivalent to `(self & other) == other`.
const fn contains(&self, other: Self) -> bool;
// Constant bitwise operations.
const fn not(self) -> Self;
const fn and(self, other: Self) -> Self;
const fn or(self, other: Self) -> Self;
const fn xor(self, other: Self) -> Self;
实现的特质
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
impl core::ops::Not;
impl core::ops::BitAnd;
impl core::ops::BitAndAssign;
impl core::ops::BitOr;
impl core::ops::BitOrAssign;
impl core::ops::BitXor;
impl core::ops::BitXorAssign;
impl From<#type> for #ident;
impl From<#ident> for #type;
impl PartialEq<#type>;
impl core::fmt::Binary;
impl core::fmt::LowerHex;
impl core::fmt::UpperHex;
impl core::fmt::Octal;
依赖关系
~255–700KB
~17K SLoC