【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;
三、类型系统规则
- 正数构建规则
-
从最高有效位(MSB)到最低位(LSB)嵌套
-
最内层必须是Z0(终止符兼符号位)
-
数值位用B0/B1表示
示例:
+5 (二进制0101)表示为:
B1<B0<B1<Z0>>> // LSB→MSB方向嵌套
- 负数构建规则
-
从最高有效位(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比特即为非零
五、典型数值示例
数值 | 二进制表示 | 类型表达式 |
---|---|---|
+0 | 0 | Z0 |
+1 | 1 | B1 |
+5 | 0101 | B1<B0<B1>> |
-1 | 1…1 | N1 |
-5 | 1011 | B1<B1<B0>> |
六、设计要点说明
- 符号扩展机制
-
正数通过Z0自动实现高位补零
-
负数通过N1自动实现高位补一
- 类型安全
-
Positive/Negative trait 确保编译期符号检查
-
NonZero trait 避免除零错误
- 零成本抽象
所有类型信息在编译期解析,运行时无开销
该设计通过Rust类型系统实现了编译期数值验证,适用于需要高安全性的数学运算场景。