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

【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类型系统实现了编译期数值验证,适用于需要高安全性的数学运算场景。

相关文章:

  • xpath使用_结合python提取页面内容
  • 《AI工程技术栈》:三层结构解析,AI工程如何区别于ML工程与全栈工程
  • 《捕捉桌面存成jpg案例代码》调试中的注意事项
  • 网络 :网络基础【网络框架认识】
  • kml数据生成全球科学研究所地理标记
  • VDK中接收memcpy传递结构体时,interface被访问多次问题
  • Spring事务简单操作
  • 中国地图上标注颜色的方法
  • Ubuntu 20.04安装及配置docker
  • 龙虎榜——20250521
  • ESP32-S3 (ESP IDF 5.4.1 - LVGL 9.2.0)九宫格拼音输入法
  • Java 实现二进制与十进制之间的互相转换
  • 7.数据的预测分析及可视化
  • 网页 HTML布局(详解)
  • Javascript 编程基础(4)函数 | 4.2、this 绑定机制
  • 全球证券交易系统开发方案
  • Pytorch基础操作
  • C#中Task.Run的线程管理最佳实践与并发控制
  • 【Linux系统】第七节—git+cgdb(详解)
  • 更新ubuntu软件源遇到GPG error
  • 设计页面图片/许昌网站seo
  • 杭州设计企业网站高端公司/兰州搜索引擎优化
  • 自动做网页的网站/移动优化课主讲:夫唯老师
  • 手机app制作网站模板/网站seo整站优化