third_party_rust_bitflags:基于 Rust 的标志枚举生成工具项目

一个宏,使得在 Rust 中定义和处理位标志更加容易。 | A macro that makes it easy to define and work with bitflags in Rust.

分支232Tags30
文件最后提交记录最后更新时间
8 个月前
8 个月前
8 个月前
8 个月前
8 个月前
4 年前
7 个月前
8 个月前
8 年前
8 个月前
8 个月前
11 年前
11 年前
8 个月前
8 个月前
8 个月前
5 年前
7 个月前
8 个月前

bitflags

Rust 最新版本 文档 许可证

bitflags 用于生成具有明确语义和符合人体工程学的终端用户 API 的标志枚举。

您可以使用 bitflags 来:

  • 为 C API 提供更用户友好的绑定,其中标志可能预先完全已知,也可能并非如此。
  • 生成具有字符串解析和格式化支持的高效选项类型。

您不能使用 bitflags 来:

  • 保证仅设置与已定义标志对应的位。bitflags 允许访问底层位类型,因此可以设置任意位。

  • 定义位域。bitflags 仅生成那些用已设置的位表示某种标志组合存在的类型。

  • 文档

  • 规范

  • 发布说明

用法

将以下内容添加到您的 Cargo.toml 中:

[dependencies]
bitflags = "2.9.1"

并将以下内容添加到您的源代码中:

use bitflags::bitflags;

示例

生成一个标志结构:

use bitflags::bitflags;

// The `bitflags!` macro generates `struct`s that manage a set of flags.
bitflags! {
    /// Represents a set of flags.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    struct Flags: u32 {
        /// The value `A`, at bit position `0`.
        const A = 0b00000001;
        /// The value `B`, at bit position `1`.
        const B = 0b00000010;
        /// The value `C`, at bit position `2`.
        const C = 0b00000100;

        /// The combination of `A`, `B`, and `C`.
        const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
    }
}

fn main() {
    let e1 = Flags::A | Flags::C;
    let e2 = Flags::B | Flags::C;
    assert_eq!((e1 | e2), Flags::ABC);   // union
    assert_eq!((e1 & e2), Flags::C);     // intersection
    assert_eq!((e1 - e2), Flags::A);     // set difference
    assert_eq!(!e2, Flags::A);           // set complement
}

Rust 版本支持

最低支持的 Rust 版本记录在 Cargo.toml 文件中。 必要时,次要版本可能会提升此版本要求。

项目介绍

一个宏,使得在 Rust 中定义和处理位标志更加容易。 | A macro that makes it easy to define and work with bitflags in Rust.

定制我的领域