【PhysUnits】15 类型整数基本结构体补充P1(basic.rs)
一、源码
这段代码定义了一个类型系统,用于表示不同种类的数字(正数、负数、零、非零数等),主要使用了Rust的类型系统和泛型编程。
use crate::sealed::Sealed;
use core::marker::PhantomData;// ========== 基础类型定义 ==========
#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub struct B0<H>(pub PhantomData<H>);#[derive(Eq, PartialEq, Clone, Copy, Debug)]
pub struct B1<H>(pub PhantomData<H>);#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct Z0;#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct P1;//新增#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct N1;impl<H> Default for B0<H>{fn default() -> Self{B0(PhantomData)}
}impl<H> Default for B1<H>{fn default() -> Self{B1(PhantomData)}
}// ========== Sealed 实现 ==========
impl<H> Sealed for B0<H> {}
impl<H> Sealed for B1<H> {}
impl Sealed for Z0 {}
impl Sealed for P1 {}
impl Sealed for N1 {}// ========== 标记特质定义 ==========
pub trait Positive: Sealed {}
pub trait Negative: Sealed {}
pub trait Integer: Sealed + Copy + Default + 'static {}
pub trait NonZero: Integer {}
pub trait NonNegOne: Integer {}
pub trait Unsigned: Integer {}// ========== Positive 实现 ==========
impl Positive for P1 {} //原先B1<Z0>替换为P1
impl<H: Positive> Positive for B0<H> {}
impl<H: Positive> Positive for B1<H> {}// ========== Negative 实现 ==========
impl Negative for N1 {}
impl<H: Negative> Negative for B0<H> {}
impl<H: Negative> Negative for B1<H> {}// ========== Integer 实现 ==========
impl Integer for P1 {}
impl Integer for N1 {}
impl<H: Integer> Integer for B0<H> {}
impl<H: Integer> Integer for B1<H> {}
impl Integer for Z0 {} //现在Z0仅单独使用// ========== NonZero 实现 ==========
impl NonZero for P1 {}
impl NonZero for N1 {}
impl<H: NonZero> NonZero for B0<H> {}
impl<H: NonZero> NonZero for B1<H> {}// ========== NonNegOne 实现 ==========
impl NonNegOne for Z0 {}
impl NonNegOne for P1 {}
impl<H: Integer> NonNegOne for B0<H> {}
impl<H: Integer> NonNegOne for B1<H> {}// ========== Unsigned 实现 ==========
impl Unsigned for P1 {}
impl<H: Unsigned> Unsigned for B0<H> {}
impl<H: Unsigned> Unsigned for B1<H> {}
impl Unsigned for Z0 {}// ========== 构造函数 ==========
impl<H> B0<H> {#[inline]pub fn new() -> Self {B0(PhantomData)}
}impl<H> B1<H> {#[inline]pub fn new() -> Self {B1(PhantomData)}
}impl P1 {#[inline]pub fn new() -> Self {P1}
}impl N1 {#[inline]pub fn new() -> Self {N1}
}impl Z0 {#[inline]pub fn new() -> Self {Z0}
}
二、基础类型定义
-
B0 和 B1:泛型结构体,用于构建更复杂的数字类型。PhantomData表示这些类型在运行时不会实际占用内存,仅用于编译时类型检查。
-
Z0:表示数字0
-
P1:表示数字+1(新增的)
-
N1:表示数字-1
三、Sealed实现
Sealed是一个标记trait(通常定义在其他模块中),用于限制哪些类型可以实现特定的trait。这里为所有基础类型实现了Sealed,意味着只有这些类型可以实现后续定义的trait。
四、标记特质定义
定义了一系列trait来分类数字类型:
-
Positive:正数
-
Negative:负数
-
Integer:所有整数(包括正、负、零)
-
NonZero:非零数
-
NonNegOne:非负一数(即不是-1的数)
-
Unsigned:无符号数(非负数)
五、特质实现
-
Positive:P1是正数,如果H是正数,那么B0和B1也是正数
-
Negative:N1是负数,如果H是负数,那么B0和B1也是负数
-
Integer:所有定义的类型都是整数
-
NonZero:除了Z0,其他都是非零数
-
NonNegOne:除了N1,其他都是非负一数
-
Unsigned:P1和它们的组合是无符号数,Z0也是
六、构造函数
为每个类型提供了new()方法,方便创建实例。
七、设计目的
这个系统用来在类型级别表示和操作数字的,用于:
-
在编译时进行数字验证
-
实现类型安全的算术运算
-
构建依赖数字类型的复杂类型系统
八、特点
-
零(Z0)现在是独立类型,不再像之前可能作为B0、B1的符号位
-
新增了P1直接表示+1,简化了正数的表示,即与B0、B1配合使用时表示符号为正,该位为1,更高位都是0。
-
使用类型参数H可以构建更复杂的数字(如B1<B0>表示5的二进制形式数字)
这种设计常见于依赖类型编程或类型级编程的场景,可以在编译期捕获更多错误,但会增加类型系统的复杂性。