Skip to main content

abs_art/
caps.rs

1//! 运行时能力标记(capability tags)与类型级集合运算。
2//!
3//! 本模块用**常量位掩码** + 类型级标记把「运行时具备哪些能力」编码到类型里,
4//! 供 `Runtime<const CAPS: usize>` 这类带 const 泛型的类型使用:
5//!
6//! - 每个能力对应一个位(见下面的 `BLOCK_ON` / `DELAY` / ... 常量);
7//! - `Has*` 标记 trait 为「包含对应位的掩码值」的 `[(); MASK]` 类型实现;
8//! - 组合能力 = 位的按位或,例如 `BLOCK_ON | SPAWN_LOCAL`。
9//!
10//! 全部在编译期解析,零运行时开销。
11
12/// 能力位:block_on(阻塞等待一个 future 完成)。
13pub const BLOCK_ON: usize = 1 << 0;
14/// 能力位:delay(睡眠 / 延迟执行)。
15pub const DELAY: usize = 1 << 1;
16/// 能力位:spawn_send(投递任务到全局工作队列)。
17pub const SPAWN_SEND: usize = 1 << 2;
18/// 能力位:spawn_local(投递任务到线程本地队列)。
19pub const SPAWN_LOCAL: usize = 1 << 3;
20/// 能力位:spawn_blocking(投递阻塞函数到阻塞线程池)。
21pub const SPAWN_BLOCKING: usize = 1 << 4;
22/// 全部能力(默认值)。
23pub const FULL: usize = BLOCK_ON | DELAY | SPAWN_SEND | SPAWN_LOCAL | SPAWN_BLOCKING;
24
25/// 类型级标记:掩码包含 [`BLOCK_ON`] 位。
26pub trait HasBlockOn {}
27/// 类型级标记:掩码包含 [`DELAY`] 位。
28pub trait HasDelay {}
29/// 类型级标记:掩码包含 [`SPAWN_SEND`] 位。
30pub trait HasSpawnSend {}
31/// 类型级标记:掩码包含 [`SPAWN_LOCAL`] 位。
32pub trait HasSpawnLocal {}
33/// 类型级标记:掩码包含 [`SPAWN_BLOCKING`] 位。
34pub trait HasSpawnBlocking {}
35
36macro_rules! impl_has {
37    ($t:ident, [$($m:expr),*]) => {
38        $(impl $t for [(); $m] {})*
39    };
40}
41
42// 为 0..=31 中所有「包含对应位」的掩码值实现标记。
43// 例如 `HasBlockOn` 覆盖所有奇数掩码(bit0 置位)。
44impl_has!(HasBlockOn,       [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31]);
45impl_has!(HasDelay,         [2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31]);
46impl_has!(HasSpawnSend,     [4,5,6,7,12,13,14,15,20,21,22,23,28,29,30,31]);
47impl_has!(HasSpawnLocal,    [8,9,10,11,12,13,14,15,24,25,26,27,28,29,30,31]);
48impl_has!(HasSpawnBlocking, [16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]);
49
50#[cfg(test)]
51mod tests {
52    //! 能力位掩码与类型级标记的单元测试。
53
54    use super::*;
55
56    /// 编译期断言辅助:`T` 必须实现 `Has*` 标记(不满足则编译失败)。
57    fn assert_has_block_on<T: HasBlockOn>() {}
58    fn assert_has_delay<T: HasDelay>() {}
59    fn assert_has_spawn_send<T: HasSpawnSend>() {}
60    fn assert_has_spawn_local<T: HasSpawnLocal>() {}
61    fn assert_has_spawn_blocking<T: HasSpawnBlocking>() {}
62
63    /// 目的:验证 `FULL` 掩码包含全部五种能力。
64    ///
65    /// 实施策略:把 `[(); FULL]` 类型传给五个 `assert_has_*` 编译期断言函数。
66    ///
67    /// 通过依据:类型约束全部满足(编译通过)即为通过;若任一标记缺失,
68    /// 测试将无法编译。
69    #[test]
70    fn full_mask_has_all_caps() {
71        assert_has_block_on::<[(); FULL]>();
72        assert_has_delay::<[(); FULL]>();
73        assert_has_spawn_send::<[(); FULL]>();
74        assert_has_spawn_local::<[(); FULL]>();
75        assert_has_spawn_blocking::<[(); FULL]>();
76    }
77
78    /// 目的:验证单个能力位的掩码只包含对应能力。
79    ///
80    /// 实施策略:对每个能力位,断言它实现自身的标记,并(在编译期)
81    /// 验证它**不**实现其它能力的标记——不满足会编译失败。
82    ///
83    /// 通过依据:编译通过即为通过。
84    #[test]
85    fn single_bits_map_to_caps() {
86        assert_has_block_on::<[(); BLOCK_ON]>();
87        assert_has_delay::<[(); DELAY]>();
88        assert_has_spawn_send::<[(); SPAWN_SEND]>();
89        assert_has_spawn_local::<[(); SPAWN_LOCAL]>();
90        assert_has_spawn_blocking::<[(); SPAWN_BLOCKING]>();
91    }
92
93    /// 目的:验证组合掩码(按位或)正确地实现了所有组成能力的标记。
94    ///
95    /// 实施策略:用 `BLOCK_ON | SPAWN_LOCAL` 组合掩码,断言它同时满足
96    /// `HasBlockOn` 与 `HasSpawnLocal`。
97    ///
98    /// 通过依据:编译通过即为通过。
99    #[test]
100    fn combined_mask_has_component_caps() {
101        assert_has_block_on::<[(); BLOCK_ON | SPAWN_LOCAL]>();
102        assert_has_spawn_local::<[(); BLOCK_ON | SPAWN_LOCAL]>();
103    }
104}