【unitrix】 3.7 类型级加一计算(Add1.rs)
一、源码
这段代码实现了一个类型级别的加法操作(Add1 trait),允许在编译时对数字类型进行加一操作。它使用了类型系统来表示数字(如 Z0、P1、N1 等),并定义了它们的加一行为。
//! 加一操作特质实现 / Increment operation trait implementation
//!
//! 说明:
//! 1. Z0、P1,、N1 + 1,常规计算
//! 2. B0<H> + 1,该位B1,无进位,原高位是N1时要规范格式,即H=N1时要特化,此时源码为B0<N1>
//! 3. B1<H> + 1,该位B0,有进位,当H+1 = Z0时要规范格式,即H=N1时要特化,此时源码为B1<N1>,不是简化格式use crate::number::{NonNegOne, NonZero, Primitive, Var, B0, B1, N1, P1, Z0};
/// 加一特质 / Increment trait
///
/// 为类型系统提供加一操作的计算能力
/// Provides increment operation capability for type system
pub trait Add1 {/// 加一后的输出类型 / Output type after incrementtype Output;fn add1(self) -> Self::Output;
}// ========== 基础类型实现 / Basic Type Implementations ==========/// Z0 (0) 加一实现 / Increment for Z0 (0)
///
/// 0 + 1 = 1 (B1<Z0>)
impl Add1 for Z0 {type Output = P1; //P1替换B1<Z0>#[inline(always)]fn add1(self) -> Self::Output{P1::new()}
}/// P1 (1) 加一实现 / Increment for P1 (+1)
///
/// 1 + 1 = 2 (B0<P1>)
impl Add1 for P1 {type Output = B0<P1>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}/// N1 (-1) 加一实现 / Increment for N1 (-1)
///
/// -1 + 1 = 0 (Z0)
impl Add1 for N1 {type Output = Z0;#[inline(always)]fn add1(self) -> Self::Output{Z0::new()}
}// ========== 递归类型实现 / Recursive Type Implementations ==========/// B0<H> 加一实现 / Increment for B0<H>
///
/// 直接加一无需进位 / Direct increment without carry
/// ...0 + 1 = ...1 / ...0 + 1 = ...1
impl<H:NonZero + NonNegOne> Add1 for B0<H>{type Output = B1<H>;#[inline(always)]fn add1(self) -> Self::Output{B1::new()}
}/// B1<H> 加一实现 / Increment for B1<H>
///
/// 处理进位情况 / Handles carry case
/// 0...1 + 1 = 0...(高位进位) / ...1 + 1 = ...0(with carry)
impl<H:NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H>{//P1替代B1<Z0>后,H不可能为Z0type Output = B0<H::Output>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}// 待Float加法完善后考虑其加一实现
/* impl<Mantissa, Exponent> Add1 for Float<Mantissa, Exponent> {type Output = <Float<Mantissa, Exponent> as Add<P1>>::out;#[inline(always)]fn add1(self) -> Self::Output{Float::new()}
} */
// ========== 特化实现 ==========
/// B0<N1> (-2) 加一特化实现 / Specialized increment for B0<N1> (-2)
impl Add1 for B0<N1> {type Output = N1;#[inline(always)]fn add1(self) -> Self::Output{N1::new()}
}// B1<N1> (-1) 加一特化实现,本身不允许B1<N1>出现,其结果也是不规范的格式,目前取消
/* impl Add1 for B1<N1> {type Output = Z0;
} *//// Val<T> 加一实现 / Increment for Val<T>
/// Val<T>
impl<T:Primitive + From<P1>> Add1 for Var<T> {type Output = Self;#[inline(always)]fn add1(self) -> Self::Output{Self(self.0 + T::from(P1))}
}
二、源码分析
- Add1 Trait 定义
pub trait Add1 {type Output;fn add1(self) -> Self::Output;
}
-
作用:定义了一个类型级别的加一操作。
-
Output:表示加一后的结果类型。
-
add1(self):执行加一操作的方法。
- 基本数字类型的加一实现
(1) Z0(零)加一
impl Add1 for Z0 {type Output = P1; // 0 + 1 = 1fn add1(self) -> P1 { P1::new() }
}
- Z0 表示 0,加一后变为 P1(正一)。
(2) P1(正一)加一
impl Add1 for P1 {type Output = B0<P1>; // 1 + 1 = 2 (二进制 10)fn add1(self) -> B0<P1> { B0::new() }
}
- P1 表示 1,加一后变为 B0(二进制 10,即十进制 2)。
(3) N1(负一)加一
impl Add1 for N1 {type Output = Z0; // -1 + 1 = 0fn add1(self) -> Z0 { Z0::new() }
}
- N1 表示 -1,加一后变为 Z0(0)。
- 二进制数字类型的加一实现
(1) B0(最低位 0)加一
impl<H: NonZero + NonNegOne> Add1 for B0<H> {type Output = B1<H>; // ...0 + 1 = ...1fn add1(self) -> B1<H> { B1::new() }
}
-
B0 表示一个二进制数,最低位是 0,高位是 H(非零)。
-
加一后最低位变为 1,即 B1。
(2) B1(最低位 1)加一
impl<H: NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H> {type Output = B0<H::Output>; // ...1 + 1 = ...0 (进位)fn add1(self) -> B0<H::Output> { B0::new() }
}
-
B1 表示一个二进制数,最低位是 1,高位是 H(非零)。
-
加一后最低位变为 0,并进位(H + 1),即 B0<H::Output>。
- 特化实现
(1) B0(-2)加一
impl Add1 for B0<N1> {type Output = N1; // -2 + 1 = -1fn add1(self) -> N1 { N1::new() }
}
-
B0 表示二进制 …0(高位是 N1,即 -1),实际表示 -2。
-
加一后变为 N1(-1)。
(2) B1(-1)加一(已注释)
/* impl Add1 for B1<N1> {type Output = Z0; // -1 + 1 = 0
} */
-
B1 表示二进制 …1(高位是 N1,即 -1),实际表示 -1。
-
加一后应为 Z0(0),但代码中注释掉,为了避免使用非标准格式。
- 运行时数值类型 Var 的加一
impl<T: Primitive + From<P1>> Add1 for Var<T> {type Output = Self;fn add1(self) -> Self { Self(self.0 + T::from(P1)) }
}
-
Var 是一个运行时数值包装类型(如 Var)。
-
加一操作直接调用 + 1(P1 转换为 T 类型后相加)。
- 关键点
-
类型级计算:所有操作在编译期完成,无运行时开销。
-
二进制表示:
-
B0 表示 …0(H * 2)。
-
B1 表示 …1(H * 2 + 1)。
-
-
进位处理:
- B1 + 1 会进位(H + 1)。
-
特化情况:
- B0 和 B1 用于处理负数。
三、总结
这段代码实现了一个类型安全的加法系统,支持:
-
基本数字(Z0、P1、N1)。
-
二进制数字(B0、B1)。
-
运行时数值(Var)。
适用于需要编译期计算的场景,如静态数组大小计算、类型安全的单位系统等。