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

【PhysUnits】7 类型整数基本结构体(basic.rs)

一、源码

use crate::sealed::Sealed;
use std::marker::PhantomData;// ========== Trait Definitions ==========
// 标记正数 / Marker trait for positive numbers
pub trait Positive: Sealed {}// 标记负数 / Marker trait for negative numbers
pub trait Negative: Sealed {}  // Fixed typo: lowercase 'n' to uppercase 'N'// 非零标记 / Marker trait for non-zero numbers
pub trait NonZero: Sealed {}// ========== Positive Number Implementations ==========
// 正数实现 / Positive number implementations
impl Positive for B1<Z0> {}  // +1: B1<Z0>
impl NonZero for B1<Z0> {}   // +1 is non-zeroimpl<H: Positive> Positive for B1<H> {}  // B1<H> is positive if H is positive
impl<H: Positive> NonZero for B1<H> {}   // All B1<H> are non-zeroimpl<H: Positive> Positive for B0<H> {}  // B0<H> is positive if H is positive
impl<H: Sealed> NonZero for B0<H> {}     // B0<H> is non-zero (note: should probably require H: NonZero)// ========== Negative Number Implementations ==========
// 负数实现 / Negative number implementations
impl Negative for N1 {}      // -1: N1 (base case)
impl NonZero for N1 {}       // -1 is non-zeroimpl<H: Negative> Negative for B1<H> {}  // B1<H> is negative if H is negative
impl<H: Negative> NonZero for B1<H> {}   // All B1<H> are non-zeroimpl<H: Negative> Negative for B0<H> {}  // B0<H> is negative if H is negative
impl<H: Negative> NonZero for B0<H> {}   // All B0<H> are non-zero// ==================== Basic Type Definitions ====================
// ==================== 基础类型定义 ====================/// 二进制0节点 / Binary 0 bit node
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct B0<H>(PhantomData<H>);impl<H> Sealed for B0<H> {}  // 为 B0<H> 实现 Sealed / Implement Sealed for B0<H>impl<H: Integer> B0<H> {#[inline]pub fn new() -> B0<H> {B0(PhantomData)}
}/// 二进制1节点 / Binary 1 bit node
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct B1<H>(PhantomData<H>);impl<H> Sealed for B1<H> {}  // 为 B1<H> 实现 Sealed / Implement Sealed for B1<H>impl<H: Integer> B1<H> {#[inline]pub fn new() -> B1<H> {B1(PhantomData)  // Fixed typo: was B0::default()}
}/// 非负数终止符 / Non-negative terminator (Z0)
/// 表示正数的符号位和更高位的0 / Represents sign bit (positive) and higher-order zeros
/// 单独使用时表示0 / Stands for 0 when used alone
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct Z0;impl Sealed for Z0 {}  // 零类型实现Sealed标记trait / Implement Sealed for Z0impl Z0 {#[inline]pub fn new() -> Z0 {Z0}
}/// 负数终止符 / Negative terminator (N1)
/// 表示负数的符号位和更高位的1 / Represents sign bit (negative) and higher-order ones
/// 单独使用时表示-1 / Stands for -1 when used alone
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct N1;impl Sealed for N1 {}  // 为 N1 实现 Sealed / Implement Sealed for N1impl N1 {#[inline]pub fn new() -> N1 {N1}
}

二、基础结构体定义

/// 二进制0节点 / Binary 0 bit node
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct B0<H>(PhantomData<H>);/// 二进制1节点 / Binary 1 bit node  
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct B1<H>(PhantomData<H>);/// 非负数终止符 / Non-negative terminator
/// 表示正号和高位补零 / Represents positive sign and zero-padding
/// 单独使用表示0 / Standalone represents 0
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct Z0;/// 负数终止符 / Negative terminator  
/// 表示负号和高位补1 / Represents negative sign and one-padding
/// 单独使用表示-1 / Standalone represents -1
#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct N1;

三、类型系统规则

  1. 正数构建规则
  • 从最高有效位(MSB)到最低位(LSB)嵌套

  • 最内层必须是Z0(终止符兼符号位)

  • 数值位用B0/B1表示

示例:
+5 (二进制0101)表示为:

B1<B0<B1<Z0>>>  // LSB→MSB方向嵌套
  1. 负数构建规则
  • 从最高有效位(MSB)到最低位(LSB)嵌套

  • 最内层必须是N1(终止符兼符号位)

  • 数值位用补码表示

示例:
-5 (补码1011)表示为:

B1<B1<B0<N1>>>  // 符号位扩展为1

四、标记特征实现

// 正数标记 / Positive marker
impl Positive for B1<Z0> {}  // +1的基础情况
impl<H: Positive> Positive for B0<H> {}  // 正数的0比特扩展
impl<H: Positive> Positive for B1<H> {}  // 正数的1比特扩展// 负数标记 / Negative marker
impl Negative for N1 {}      // -1的基础情况
impl<H: Negative> Negative for B0<H> {}  // 负数的0比特扩展
impl<H: Negative> Negative for B1<H> {}  // 负数的1比特扩展// 非零标记 / Non-zero marker
impl NonZero for B1<Z0> {}   // +1必然非零
impl<H: NonZero> NonZero for B0<H> {}  // 前导0不影响非零性
impl<H: NonZero> NonZero for B1<H> {}  // 包含1比特即为非零

五、典型数值示例

数值二进制表示类型表达式
+00Z0
+11B1
+50101B1<B0<B1>>
-11…1N1
-51011B1<B1<B0>>

六、设计要点说明

  1. 符号扩展机制
  • 正数通过Z0自动实现高位补零

  • 负数通过N1自动实现高位补一

  1. 类型安全
  • Positive/Negative trait 确保编译期符号检查

  • NonZero trait 避免除零错误

  1. 零成本抽象
    所有类型信息在编译期解析,运行时无开销

该设计通过Rust类型系统实现了编译期数值验证,适用于需要高安全性的数学运算场景。

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

相关文章:

  • 掩膜合并代码
  • 力扣算法---哈希表总结篇
  • 【无标题】Spring AI 1.0 正式发布!核心内容和智能体详解
  • upload-labs通关笔记-第15关 文件上传之getimagesize绕过(图片马)
  • C语言判断素数(附带源码和解析)
  • 第十三届蓝桥杯国赛PythonA题解
  • 贪心算法题目合集2
  • 链表day3
  • Linux电源管理——PSCI初始化流程和多核启动流程
  • 对于final、finally和finalize不一样的理解
  • Java基于SSM的数学辅导微信小程序【附源码、文档说明】
  • 招投标项目记录
  • 一键二次元风格转换:风格转换 ComfyUI 使用教学--
  • 逆向学习笔记1
  • 【性能提升300%】Function Calling高并发实践:gRPC优化+缓存策略+容错设计​
  • 2024正式版企业级在线客服系统源码+语音定位+快捷回复+图片视频传输+安装教程
  • id分页遍历数据漏行问题
  • 猎板PCB如何以高可靠方案护航大国重器?
  • 发布Chrome浏览器插件的几种方法
  • C++进阶--C++11
  • C++ stack对象创建、入栈、获取栈顶
  • MySQL高可用实战:PXC集群原理与部署全解析,让数据库永不宕机
  • vue页面实现table动态拆分列功能
  • 江科大TIM定时器hal库实现
  • 自定义属性面板开发指南:公开属性声明、监听回调与基础类型配置
  • Linux:缓冲区
  • BigFoot (DBM) Deadly Boss Mods
  • DL00988-稀疏增强数据transformer船舶AIS轨迹预测含完整数据集
  • 腾讯文档怎么设置多列筛选条件
  • 固定翼无人机抛投技术分析!