一、原碼
這段代碼實現了一個類型級的加一操作(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 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))}
}// ==============================================
// 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 = 1fn 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 = 0fn 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 = ...1fn 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)
六、特點
+ 類型安全:所有操作都在編譯期進行類型檢查+ 零成本抽象:運行時無額外開銷+ 遞歸處理:復合類型的加一操作遞歸處理每一位+ 特殊化處理:對邊界情況(如負數)有特殊處理
這個實現通過在類型系統層面定義數值運算,可以在編譯期捕獲更多錯誤,特別適合需要高安全性和確定性的場景。