当前位置: 首页 > java >正文

【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
  1. 最外层 B1:最低有效位 (LSB=1)

  2. 中间 B0:第二位 (0)

  3. 内层 B1:第三位 (1)

  4. 终止符 Z0:符号位+更高位终止

四、负数表示规则

关键特点:
  • 终止符:N1 表示负数的终止,同时隐含更高位为1

  • 补码计算:绝对值取反加一

  • 嵌套结构:同样从外到内是 LSB → MSB

示例解析:
-5 => B1<B1<B0<N1>>>  // 1011

计算过程:

  1. 绝对值5 → 0101

  2. 取反 → 1010

  3. 加1 → 1011

  4. 类型表示:

  • 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 类型系统的强大表达能力。

http://www.xdnf.cn/news/8475.html

相关文章:

  • Oracle 11g导出数据库结构和数据
  • 基于Java的仓库库存管理系统的设计与实现|参考|1w字+
  • 每日一练,冲进国赛!全国青少年信息素养大赛-图形化编程—省赛真题——小鸡吃东西
  • Java注解运行时访问与处理技术详解
  • 修改nmeaLib库增加北斗(BD)和格洛纳斯(GNSS)解析
  • PostGIS实现栅格数据导出TIFF应用实践【ST_AsTiff】
  • 图纸加密软件的核心优势解析
  • Python多线程编程详解
  • 信号与系统02-信号的时域分析
  • Python训练营打卡 Day25
  • 电路图识图基础知识-电气符号(二)
  • 图片压缩工具 | 需求思考及桌面应用开发技术选型
  • 2025电工杯数学建模竞赛A题 光伏电站发电功率日前预测问题 完整论文+python代码发布!
  • git 暂存功能使用
  • 从数学融智学视域系统地理解《道德经》:前三十七章,道法自然
  • Linux `clear` 命令与 Ctrl+L 快捷键的深度解析与高阶应用指南
  • 爬虫IP代理技术深度解析:场景、选型与实战应用
  • 缓存穿透解析
  • 20250523-BUG:无法加载“GameLib/Framework.h“头文件(已解决)
  • 【window QT开发】简易的对称密钥加解密工具(包含图形应用工具和命令行工具)
  • esp32-idf框架学习笔记/教程
  • 力扣509题:斐波那契数列的解法与代码注释
  • pytdx数据获取:在线获取和离线获取(8年前的东西,还能用吗?)
  • RESTful API 在前后端交互中的作用与实践
  • 晶圆隐裂检测提高半导体行业效率
  • Python之PyCharm安装及使用教程
  • MySQL强化关键_015_存储过程
  • YOLOv8检测头代码详解(示例展示数据变换过程)
  • 【信息系统项目管理师】第17章:项目干系人管理 - 43个经典题目及详解
  • PyTorch高阶技巧:构建非线性分类器与梯度优化全解析​