一、源码
这段代码实现了一个类型级别的二进制数减法系统,包含标准减法和带借位减法。
use core::ops::{Neg, Not, Sub};
use super::basic::{Z0, N1, P1, B0, B1, Integer, NonZero};
use super::add1::Add1;
use super::sub1::Sub1;
use super::standardization::{IfB0, IfB1};// ==================== 带借位减法 Trait ====================
/// 带借位减法运算
/// Subtraction with borrow operation
///
/// 表示 a - b - 1 的运算 (相当于 a - (b + 1))
/// Represents the operation of a - b - 1 (equivalent to a - (b + 1))
/// 说明:有借位表示有低位数,本位 NonZero
pub trait SubWithBorrow<Rhs> {type Output;
}// ==================== 带借位减法实现 ====================// ========== 带借位P1 - NonZero ==========// P1 - I (带借位) = P1-I-1=-I
impl<I:NonZero + Neg> SubWithBorrow<I> for P1 {type Output = I::Output;
}// ========== 带借位N1 - NonZero ==========
// N1 - I (带借位,非0) = !I-1 (即N1+(!I+1)-1)
impl<I: NonZero + Not> SubWithBorrow<I> for N1
where<I as Not>::Output:Sub1,
{type Output = <I::Output as Sub1>::Output;
}// ========== 带借位B0 - NonZero ==========
// B0 - P1 (带借位)
impl<H: NonZero + Sub1> SubWithBorrow<P1> for B0<H>
whereH::Output: IfB0,
{type Output = <H::Output as IfB0>::Output;
}// B0 - N1 (带借位)
impl<H: NonZero> SubWithBorrow<N1> for B0<H>
{type Output = Self;
}// B0 - B0 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B0<H2>> for B0<H1>
whereH1::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;
}// B0 - B1 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B1<H2>> for B0<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;
}// ========== 带借位B1 - NonZero ==========
// B1 - P1 (带借位)
impl<H: NonZero + Sub1> SubWithBorrow<P1> for B1<H>
whereH::Output: IfB1,
{type Output = <H::Output as IfB1>::Output;
}// B1 - N1 (带借位)
impl<H: NonZero> SubWithBorrow<N1> for B1<H>
{type Output = Self;
}// B1 - B0 (带借位)
impl<H1: NonZero + Sub<H2>, H2: NonZero> SubWithBorrow<B0<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;
}// B1 - B1 (带借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> SubWithBorrow<B1<H2>> for B1<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;
}// ==================== 标准减法实现 (Sub trait) ====================// ========== Z0 - All ==========
// Z0 - I = -I
impl<I: Integer + Neg> Sub<I> for Z0 {type Output = I::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== P1 - All ==========
// P1 - I = -(I-P1)
impl<I: Integer + Sub1> Sub<I> for P1
where<I as Sub1>::Output: Neg,
{type Output = <I::Output as Neg>::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== N1 - All ==========
// N1 - I = -(I+P1)
impl<I: Integer + Add1> Sub<I> for N1
where<I as Add1>::Output: Neg,
{type Output = <I::Output as Neg>::Output;fn sub(self, _: I) -> Self::Output {unreachable!("Type-level operation")}
}// ========== B0 - All ==========
// B0 - Z0
impl<H: NonZero> Sub<Z0> for B0<H> {type Output = Self;fn sub(self, _: Z0) -> Self::Output {self}
}// B0 - P1
impl<H: NonZero + Sub1> Sub<P1> for B0<H>
where<H as Sub1>::Output: IfB1,
{type Output = <H::Output as IfB1>::Output;fn sub(self, _: P1) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - N1
impl<H: NonZero + IfB1> Sub<N1> for B0<H>{type Output = H::Output;fn sub(self, _: N1) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - B0
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B0<H2>> for B0<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;fn sub(self, _: B0<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// B0 - B1 (需要借位)
impl<H1: NonZero + SubWithBorrow<H2>, H2: NonZero> Sub<B1<H2>> for B0<H1>
where<H1 as SubWithBorrow<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;fn sub(self, _: B1<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// ========== B1 - All ==========
// B1-Z0
impl<H: NonZero> Sub<Z0> for B1<H> {type Output = Self;fn sub(self, _: Z0) -> Self::Output {self}
}// B1 - P1
impl<H: NonZero + IfB0> Sub<P1> for B1<H> {type Output = H::Output;fn sub(self, _: P1) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - N1
impl<H: NonZero + Add1> Sub<N1> for B1<H>
where <H as Add1>::Output: IfB0,
{type Output = <H::Output as IfB0>::Output;fn sub(self, _: N1) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - B0
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B0<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB1,
{type Output = <H1::Output as IfB1>::Output;fn sub(self, _: B0<H2>) -> Self::Output {unreachable!("Type-level operation")}
}// B1 - B1
impl<H1: NonZero + Sub<H2>, H2: NonZero> Sub<B1<H2>> for B1<H1>
where<H1 as Sub<H2>>::Output: IfB0,
{type Output = <H1::Output as IfB0>::Output;fn sub(self, _: B1<H2>) -> Self::Output {unreachable!("Type-level operation")}
}
二、核心设计
类型系统基础
use super::basic::{Z0, N1, P1, B0, B1}; // 基础类型:
// Z0 = 零, N1 = -1, P1 = +1
// B0<H> = 二进制数低位为0 (如 10)
// B1<H> = 二进制数低位为1 (如 11)
关键Trait定义
pub trait SubWithBorrow<Rhs> { // 带借位减法(a-b-1)type Output;
}impl core::ops::Sub<Rhs> { // 标准减法(a-b)type Output;
}
三、带借位减法实现
特殊值处理
// P1 - I (带借位) = -I
impl<I: NonZero + Neg> SubWithBorrow<I> for P1 {type Output = I::Output; // 直接取负
}// N1 - I (带借位) = !I - 1
impl<I: NonZero + Not> SubWithBorrow<I> for N1
whereI::Output: Sub1 // 按位取反后减1
{type Output = <I::Output as Sub1>::Output;
}
二进制数处理
// B0 - B1 的递归借位处理
impl<H1, H2> SubWithBorrow<B1<H2>> for B0<H1>
whereH1: SubWithBorrow<H2>, // 高位递归H1::Output: IfB1 // 结果标准化
{type Output = B1<H1::Output>; // 借位后变B1
}// B1 - B1 的递归处理
impl<H1, H2> SubWithBorrow<B1<H2>> for B1<H1>
whereH1: Sub<H2>, // 普通减法H1::Output: IfB0 // 结果标准化
{type Output = B0<H1::Output>;
}
四、标准减法实现
运算符重载模式
impl<I> Sub<I> for Z0 { // 零减任何数type Output = I::Neg; // 直接取负fn sub(self, _: I) -> Self::Output { unreachable!() }
}impl<H> Sub<Z0> for B0<H> { // 二进制数减零type Output = Self;fn sub(self, _: Z0) -> Self { self }
}
递归减法
// B1 - B0 的递归处理
impl<H1, H2> Sub<B0<H2>> for B1<H1>
whereH1: Sub<H2>, // 高位相减H1::Output: IfB1 // 结果标准化
{type Output = B1<H1::Output>;
}
五、标准化处理
通过 IfB0/IfB1 trait 处理特殊情况:
B0<Z0> => Z0 // 消除前导零
B1<N1> => N1 // 二进制补码优化
B1<Z0> => P1 // 单比特正数
六、设计亮点
- 双重减法系统:
-
标准减法:a - b
-
带借位减法:a - b - 1(处理低位借位)
- 完备的数学规则:
// 负数减法转换为加法
impl<I: Add1> Sub<I> for N1 {type Output = Neg<I::Add1>;
}
- 递归类型处理:
// 处理二进制数的递归减法
impl<H1, H2> Sub<B1<H2>> for B1<H1> {type Output = B0<H1::Sub<H2>>;
}
七、典型运算示例
type A = B1<B0<Z0>>; // 二进制10(2)
type B = B1<Z0>; // 二进制1(1)
type Diff = <A as Sub<B>>::Output; // 得到P1 (1)// 带借位减法示例
type C = B0<B1<Z0>>; // 二进制01(1)
type D = B1<Z0>; // 二进制1(1)
type BorrowDiff = <C as SubWithBorrow<D>>::Output; // 得到N1 (-1)
八、异常处理机制
所有运行时方法都标记为:
fn sub(self, _: Rhs) -> Self::Output {unreachable!("Type-level operation") // 确保不会实际执行
}
这个系统通过编译期类型计算实现了:
-
任意长度二进制数减法
-
自动借位处理
-
结果标准化
-
零运行时开销
适用于需要编译期数值验证的场景,如协议编解码等。