【unitrix】 4.1 类型级加一操作(Add1.rs)

发布于:2025-06-23 ⋅ 阅读:(17) ⋅ 点赞:(0)

一、原码

这段代码实现了一个类型级的加一操作(Add1 trait),用于在Rust的类型系统中进行数值加一运算。

//! 加一操作特质实现 / 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, FixedPoint, Float};
/// 加一特质 / Increment trait
/// 
/// 为类型系统提供加一操作的计算能力
/// Provides increment operation capability for type system
pub trait Add1 {
    /// 加一后的输出类型 / Output type after increment
    type 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不可能为Z0
    type 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))
    }
}

// ==============================================
// FixedPoint的Add1实现
// ==============================================

impl<IntPart: Add1, FracPart> Add1 for FixedPoint<IntPart, FracPart>{
    type Output = FixedPoint<IntPart::Output, FracPart>;
    
    fn add1(self) -> Self::Output {
        FixedPoint::new()
    }
}

二、核心设计

Add1 Trait 定义

pub trait Add1 {
    type Output;
    fn add1(self) -> Self::Output;
}

定义了一个泛型trait,表示类型可以执行加一操作,Output关联类型表示加一后的结果类型。

三、基础类型实现

Z0 (0) 加一

impl Add1 for Z0 {
    type Output = P1;  // 0 + 1 = 1
    fn add1(self) -> Self::Output { P1::new() }
}
  • Z0表示0,加一后变为P1(1)
P1 (1) 加一

impl Add1 for P1 {
    type Output = B0<P1>;  // 1 + 1 = 2 (二进制10)
    fn add1(self) -> Self::Output { B0::new() }
}
  • P1表示1,加一后变为B0(二进制10,即2)
N1 (-1) 加一

impl Add1 for N1 {
    type Output = Z0;  // -1 + 1 = 0
    fn add1(self) -> Self::Output { Z0::new() }
}
  • N1表示-1,加一后变为Z0(0)

四、复合类型实现

B0 (以0结尾的二进制数) 加一

impl<H: NonZero + NonNegOne> Add1 for B0<H> {
    type Output = B1<H>;  // ...0 + 1 = ...1
    fn add1(self) -> Self::Output { B1::new() }
}
  • 将最低位的B0变为B1,不需要进位

  • 例如:B0(2) + 1 = B1(3)

B1 (以1结尾的二进制数) 加一

impl<H: NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H> {
    type Output = B0<H::Output>;  // ...1 + 1 = ...0 (有进位)
    fn add1(self) -> Self::Output { B0::new() }
}
  • 将最低位的B1变为B0,并向高位进位

  • 例如:B1(3) + 1 = B0<B0>(4)

五、特殊处理

B0 (-2) 加一

···rust

impl Add1 for B0 {
type Output = N1; // -2 + 1 = -1
fn add1(self) -> Self::Output { N1::new() }
}
···

  • 特化处理负数情况,保持格式规范
    
  • B0<N1>表示-2,加一后变为N1(-1)
    
定点数(FixedPoint)实现

···rust

impl<IntPart: Add1, FracPart> Add1 for FixedPoint<IntPart, FracPart> {
type Output = FixedPoint<IntPart::Output, FracPart>;
fn add1(self) -> Self::Output { FixedPoint::new() }
}
···
+ 定点数的加一只对整数部分进行加一操作

+ 小数部分保持不变

+ 例如:FixedPoint<P1, B1<Z0>>(1.5) + 1 = FixedPoint<B0<P1>, B1<Z0>>(2.5)

六、特点

+ 类型安全:所有操作都在编译期进行类型检查

+ 零成本抽象:运行时无额外开销

+ 递归处理:复合类型的加一操作递归处理每一位

+ 特殊化处理:对边界情况(如负数)有特殊处理

这个实现通过在类型系统层面定义数值运算,可以在编译期捕获更多错误,特别适合需要高安全性和确定性的场景。


网站公告

今日签到

点亮在社区的每一天
去签到