【PhysUnits】8 关联常量泛型(constant/mod.rs)
一、源码
这是一个二进制补码整数表示的精巧设计。
//! 类型级整数表示模块
//!
//! 本模块提供了一种在类型系统中表示整数的方法,使用二进制补码形式。
//! 正数和负数有不同的表示规则,详见下面的文档说明。
mod basic;
pub use basic::*;//mod integer;
//pub use integer::Integer;/// 类型级整数别名
///
/// 将编译时常量 N 转换为对应的类型表示
/// 例如:`Int<42>` 会展开为 `B0<B1<B0<B1<B0<Z0>>>>>`
pub type Int<const N: i32> = <Const<N> as ToInt>::Output;/// 用于在泛型上下文中表示整数的包装类型
pub struct Const<const N: i32>;/// 将整数常量转换为类型表示的 trait
pub trait ToInt {/// 对应的类型表示type Output;
}/* ========== 正数表示规则 ==========* 1. 最高有效位(MSB)在内,最低位(LSB)在外,依次嵌套* 2. 最内层必须是 Z0,表示正号,也表示本位和更高位是0* 3. 数值部分使用 B0/B1 表示二进制位* * 例如 +5 (101):* * Z0 (符号位和终止符,表示本位为0,更高位也是0)* B1 <- MSB* B0* B1 <- LSB* 表示为: B1<B0<B1<Z0>>>*/
impl ToInt for Const<0> {type Output = Z0; // 0 的特殊表示
}impl ToInt for Const<1> {type Output = B1<Z0>;
}impl ToInt for Const<2> {type Output = B0<Z0>;
}impl ToInt for Const<3> {type Output = B1<Z0>;
}impl ToInt for Const<4> {type Output = B0<B0<Z0>>;
}impl ToInt for Const<5> {type Output = B1<B0<Z0>>;
}impl ToInt for Const<6> {type Output = B0<B1<Z0>>;
}impl ToInt for Const<7> {type Output = B1<B1<Z0>>;
}impl ToInt for Const<8> {type Output = B0<B0<B0<Z0>>>;
}impl ToInt for Const<9> {type Output = B1<B0<B0<Z0>>>;
}impl ToInt for Const<10> {type Output = B0<B1<B0<Z0>>>;
}impl ToInt for Const<11> {type Output = B1<B1<B0<Z0>>>;
}impl ToInt for Const<12> {type Output = B0<B0<B1<Z0>>>;
}impl ToInt for Const<13> {type Output = B1<B0<B1<Z0>>>;
}impl ToInt for Const<14> {type Output = B0<B1<B1<Z0>>>;
}impl ToInt for Const<15> {type Output = B1<B1<B1<Z0>>>;
}impl ToInt for Const<16> {type Output = B0<B0<B0<B0<Z0>>>>;
}impl ToInt for Const<17> {type Output = B1<B0<B0<B0<Z0>>>>;
}impl ToInt for Const<18> {type Output = B0<B1<B0<B0<Z0>>>>;
}impl ToInt for Const<19> {type Output = B1<B1<B0<B0<Z0>>>>;
}impl ToInt for Const<20> {type Output = B0<B0<B1<B0<Z0>>>>;
}impl ToInt for Const<21> {type Output = B1<B0<B1<B0<Z0>>>>;
}impl ToInt for Const<22> {type Output = B0<B1<B1<B0<Z0>>>>;
}impl ToInt for Const<23> {type Output = B1<B1<B1<B0<Z0>>>>;
}impl ToInt for Const<24> {type Output = B0<B0<B0<B1<Z0>>>>;
}impl ToInt for Const<25> {type Output = B1<B0<B0<B1<Z0>>>>;
}impl ToInt for Const<26> {type Output = B0<B1<B0<B1<Z0>>>>;
}impl ToInt for Const<27> {type Output = B1<B1<B0<B1<Z0>>>>;
}impl ToInt for Const<28> {type Output = B0<B0<B1<B1<Z0>>>>;
}impl ToInt for Const<29> {type Output = B1<B0<B1<B1<Z0>>>>;
}impl ToInt for Const<30> {type Output = B0<B1<B1<B1<Z0>>>>;
}impl ToInt for Const<31> {type Output = B1<B1<B1<B1<Z0>>>>;
}impl ToInt for Const<32> {type Output = B0<B0<B0<B0<B0<Z0>>>>>;
}impl ToInt for Const<33> {type Output = B1<B0<B0<B0<B0<Z0>>>>>;
}impl ToInt for Const<34> {type Output = B0<B1<B0<B0<B0<Z0>>>>>;
}impl ToInt for Const<35> {type Output = B1<B1<B0<B0<B0<Z0>>>>>;
}impl ToInt for Const<36> {type Output = B0<B0<B1<B0<B0<Z0>>>>>;
}impl ToInt for Const<37> {type Output = B1<B0<B1<B0<B0<Z0>>>>>;
}impl ToInt for Const<38> {type Output = B0<B1<B1<B0<B0<Z0>>>>>;
}impl ToInt for Const<39> {type Output = B1<B1<B1<B0<B0<Z0>>>>>;
}impl ToInt for Const<40> {type Output = B0<B0<B0<B1<B0<Z0>>>>>;
}impl ToInt for Const<41> {type Output = B1<B0<B0<B1<B0<Z0>>>>>;
}impl ToInt for Const<42> {type Output = B0<B1<B0<B1<B0<Z0>>>>>;
}impl ToInt for Const<43> {type Output = B1<B1<B0<B1<B0<Z0>>>>>;
}impl ToInt for Const<44> {type Output = B0<B0<B1<B1<B0<Z0>>>>>;
}impl ToInt for Const<45> {type Output = B1<B0<B1<B1<B0<Z0>>>>>;
}impl ToInt for Const<46> {type Output = B0<B1<B1<B1<B0<Z0>>>>>;
}impl ToInt for Const<47> {type Output = B1<B1<B1<B1<B0<Z0>>>>>;
}impl ToInt for Const<48> {type Output = B0<B0<B0<B0<B1<Z0>>>>>;
}impl ToInt for Const<49> {type Output = B1<B0<B0<B0<B1<Z0>>>>>;
}impl ToInt for Const<50> {type Output = B0<B1<B0<B0<B1<Z0>>>>>;
}impl ToInt for Const<51> {type Output = B1<B1<B0<B0<B1<Z0>>>>>;
}impl ToInt for Const<52> {type Output = B0<B0<B1<B0<B1<Z0>>>>>;
}impl ToInt for Const<53> {type Output = B1<B0<B1<B0<B1<Z0>>>>>;
}impl ToInt for Const<54> {type Output = B0<B1<B1<B0<B1<Z0>>>>>;
}impl ToInt for Const<55> {type Output = B1<B1<B1<B0<B1<Z0>>>>>;
}impl ToInt for Const<56> {type Output = B0<B0<B0<B1<B1<Z0>>>>>;
}impl ToInt for Const<57> {type Output = B1<B0<B0<B1<B1<Z0>>>>>;
}impl ToInt for Const<58> {type Output = B0<B1<B0<B1<B1<Z0>>>>>;
}impl ToInt for Const<59> {type Output = B1<B1<B0<B1<B1<Z0>>>>>;
}impl ToInt for Const<60> {type Output = B0<B0<B1<B1<B1<Z0>>>>>;
}impl ToInt for Const<61> {type Output = B1<B0<B1<B1<B1<Z0>>>>>;
}impl ToInt for Const<62> {type Output = B0<B1<B1<B1<B1<Z0>>>>>;
}impl ToInt for Const<63> {type Output = B1<B1<B1<B1<B1<Z0>>>>>;
}impl ToInt for Const<64> {type Output = B0<B0<B0<B0<B0<B0<Z0>>>>>>;
}/* ========== 负数表示规则 ==========* 1. 最高有效位(MSB)在内,最低位(LSB)在外,依次嵌套* 2. 最内层必须是 N1,表示负数的负号位,也表示本位及更高位为1* 3. 数值部分使用补码表示,使用 B0/B1 表示二进制位* * 例如 -5 (补码 1011):* N1 <- MSB (符号位和终止符,也表示本位为1,更高位都是1)* B0* B1* B1 <- LSB* 表示为: B1<B1<B0<N1>>>*/
impl ToInt for Const<-1> {type Output = N1;
}impl ToInt for Const<-2> {type Output = B0<N1>;
}impl ToInt for Const<-3> {type Output = B1<B0<N1>>;
}impl ToInt for Const<-4> {type Output = B0<B0<N1>>;
}impl ToInt for Const<-5> {type Output = B1<B1<B0<N1>>>;
}impl ToInt for Const<-6> {type Output = B0<B1<B0<N1>>>;
}impl ToInt for Const<-7> {type Output = B1<B0<B0<N1>>>;
}impl ToInt for Const<-8> {type Output = B0<B0<B0<N1>>>;
}impl ToInt for Const<-9> {type Output = B1<B1<B1<B0<N1>>>>;
}impl ToInt for Const<-10> {type Output = B0<B1<B1<B0<N1>>>>;
}impl ToInt for Const<-11> {type Output = B1<B0<B1<B0<N1>>>>;
}impl ToInt for Const<-12> {type Output = B0<B0<B1<B0<N1>>>>;
}impl ToInt for Const<-13> {type Output = B1<B1<B0<B0<N1>>>>;
}impl ToInt for Const<-14> {type Output = B0<B1<B0<B0<N1>>>>;
}impl ToInt for Const<-15> {type Output = B1<B0<B0<B0<N1>>>>;
}impl ToInt for Const<-16> {type Output = B0<B0<B0<B0<N1>>>>;
}impl ToInt for Const<-17> {type Output = B1<B1<B1<B1<B0<N1>>>>>;
}impl ToInt for Const<-18> {type Output = B0<B1<B1<B1<B0<N1>>>>>;
}impl ToInt for Const<-19> {type Output = B1<B0<B1<B1<B0<N1>>>>>;
}impl ToInt for Const<-20> {type Output = B0<B0<B1<B1<B0<N1>>>>>;
}impl ToInt for Const<-21> {type Output = B1<B1<B0<B1<B0<N1>>>>>;
}impl ToInt for Const<-22> {type Output = B0<B1<B0<B1<B0<N1>>>>>;
}impl ToInt for Const<-23> {type Output = B1<B0<B0<B1<B0<N1>>>>>;
}impl ToInt for Const<-24> {type Output = B0<B0<B0<B1<B0<N1>>>>>;
}impl ToInt for Const<-25> {type Output = B1<B1<B1<B0<B0<N1>>>>>;
}impl ToInt for Const<-26> {type Output = B0<B1<B1<B0<B0<N1>>>>>;
}impl ToInt for Const<-27> {type Output = B1<B0<B1<B0<B0<N1>>>>>;
}impl ToInt for Const<-28> {type Output = B0<B0<B1<B0<B0<N1>>>>>;
}impl ToInt for Const<-29> {type Output = B1<B1<B0<B0<B0<N1>>>>>;
}impl ToInt for Const<-30> {type Output = B0<B1<B0<B0<B0<N1>>>>>;
}impl ToInt for Const<-31> {type Output = B1<B0<B0<B0<B0<N1>>>>>;
}impl ToInt for Const<-32> {type Output = B0<B0<B0<B0<B0<N1>>>>>;
}impl ToInt for Const<-33> {type Output = B1<B1<B1<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-34> {type Output = B0<B1<B1<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-35> {type Output = B1<B0<B1<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-36> {type Output = B0<B0<B1<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-37> {type Output = B1<B1<B0<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-38> {type Output = B0<B1<B0<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-39> {type Output = B1<B0<B0<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-40> {type Output = B0<B0<B0<B1<B1<B0<N1>>>>>>;
}impl ToInt for Const<-41> {type Output = B1<B1<B1<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-42> {type Output = B0<B1<B1<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-43> {type Output = B1<B0<B1<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-44> {type Output = B0<B0<B1<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-45> {type Output = B1<B1<B0<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-46> {type Output = B0<B1<B0<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-47> {type Output = B1<B0<B0<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-48> {type Output = B0<B0<B0<B0<B1<B0<N1>>>>>>;
}impl ToInt for Const<-49> {type Output = B1<B1<B1<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-50> {type Output = B0<B1<B1<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-51> {type Output = B1<B0<B1<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-52> {type Output = B0<B0<B1<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-53> {type Output = B1<B1<B0<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-54> {type Output = B0<B1<B0<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-55> {type Output = B1<B0<B0<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-56> {type Output = B0<B0<B0<B1<B0<B0<N1>>>>>>;
}impl ToInt for Const<-57> {type Output = B1<B1<B1<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-58> {type Output = B0<B1<B1<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-59> {type Output = B1<B0<B1<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-60> {type Output = B0<B0<B1<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-61> {type Output = B1<B1<B0<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-62> {type Output = B0<B1<B0<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-63> {type Output = B1<B0<B0<B0<B0<B0<N1>>>>>>;
}impl ToInt for Const<-64> {type Output = B0<B0<B0<B0<B0<B0<N1>>>>>>;
}
二、基本架构
pub type Int<const N: i32> = <Const<N> as ToInt>::Output;
-
核心思想:将整数值 N 转换为类型级别的表示
-
通过 ToInt trait 实现具体转换逻辑
-
最终类型表示存储在关联类型 Output 中
三、正数表示规则
关键特点:
-
终止符:Z0 表示正数的终止,同时隐含该位及更高位为0
-
嵌套结构:从外到内依次是 LSB → MSB
-
二进制位:B0 表示0,B1 表示1
示例解析:
+5 => B1<B0<B1<Z0>>> // 101
-
最外层 B1:最低有效位 (LSB=1)
-
中间 B0:第二位 (0)
-
内层 B1:第三位 (1)
-
终止符 Z0:符号位+更高位终止
四、负数表示规则
关键特点:
-
终止符:N1 表示负数的终止,同时隐含更高位为1
-
补码计算:绝对值取反加一
-
嵌套结构:同样从外到内是 LSB → MSB
示例解析:
-5 => B1<B1<B0<N1>>> // 1011
计算过程:
-
绝对值5 → 0101
-
取反 → 1010
-
加1 → 1011
-
类型表示:
-
B1 (LSB=1)
-
B1 (第二位=1)
-
B0 (第三位=0)
-
N1 (终止符+更高位1)
五、特殊值处理
-
0:直接使用 Z0
-
-1:直接使用 N1 (补码为全1)
六、设计优势
-
类型安全:在编译期完成整数表示
-
空间效率:自动处理高位扩展
-
扩展性:可支持任意位宽的整数
七、使用示例
let _: Int<5> = ...; // 类型表示为 B1<B0<B1<Z0>>>
let _: Int<-3> = ...; // 类型表示为 B1<B0<N1>>
八、注意事项
-
正负数表示对称但规则不同
-
嵌套顺序与二进制位顺序相反
-
高位扩展由终止符自动处理
这种设计常见于类型级算术运算、维度检查等需要编译期计算的场景,展示了 Rust 类型系统的强大表达能力。