一、源码
这段代码是用Rust实现的类型级二进制整数加法运算,通过类型系统在编译期完成计算。它定义了一系列trait实现来处理不同类型的二进制数相加的情况。
rust
use core::ops::Add;
use crate::number::{
Z0, O, I, Bin, Equal, Negative,
BinInt, BinPos, BinUnsigned, Bit,
AddOne, BinSignedOpWith, Cmp,
};
// 零相关加法
impl<Int: BinInt> Add<Int> for Z0 {
type Output = Int;
#[inline] fn add(self, rhs: Int) -> Self::Output { rhs }
}
impl<U: BinUnsigned, B: Bit> Add<Z0> for Bin<U, B> where Self: BinPos {
type Output = Self;
#[inline] fn add(self, _: Z0) -> Self::Output { self }
}
impl<P: BinPos> Add<Z0> for Negative<P> {
type Output = Self;
#[inline] fn add(self, _: Z0) -> Self::Output { self }
}
// 非负数加比特位
impl Add<O> for Z0 {
type Output = Z0;
#[inline] fn add(self, _: O) -> Self::Output { Z0 }
}
impl Add<I> for Z0 {
type Output = Bin<Z0, I>;
#[inline] fn add(self, _: I) -> Self::Output { Bin::default() }
}
impl<U: BinUnsigned, B: Bit> Add<O> for Bin<U, B> where Bin<U, B>: BinPos {
type Output = Bin<U, B>;
#[inline] fn add(self, _: O) -> Self::Output { Bin::default() }
}
impl<U: BinUnsigned, B: Bit> Add<I> for Bin<U, B> where Self: AddOne {
type Output = Self::Output;
#[inline] fn add(self, _: I) -> Self::Output { Bin::default() }
}
// 正数间加法
impl<Ul: BinUnsigned, Ur: BinUnsigned, B: Bit> Add<Bin<Ur, O>> for Bin<Ul, B> where Ul: Add<Ur> {
type Output = Bin<<Ul as Add<Ur>>::Output, B>;
#[inline] fn add(self, _: Bin<Ur, O>) -> Self::Output { Bin::default() }
}
impl<Ul: BinUnsigned, Ur: BinUnsigned> Add<Bin<Ur, I>> for Bin<Ul, O> where Ul: Add<Ur> {
type Output = Bin<<Ul as Add<Ur>>::Output, I>;
#[inline] fn add(self, _: Bin<Ur, I>) -> Self::Output { Bin::default() }
}
impl<Ul: BinUnsigned, Ur: BinUnsigned> Add<Bin<Ur, I>> for Bin<Ul, I>
where Ul: Add<Ur>, <Ul as Add<Ur>>::Output: AddOne {
type Output = Bin<<<Ul as Add<Ur>>::Output as AddOne>::Output, O>;
#[inline] fn add(self, _: Bin<Ur, I>) -> Self::Output { Bin::default() }
}
// 负数相关加法
impl<Pl: BinPos, Pr: BinPos> Add<Negative<Pr>> for Negative<Pl> where Pl: Add<Pr>, <Pl as Add<Pr>>::Output: BinPos {
type Output = Negative<<Pl as Add<Pr>>::Output>;
#[inline] fn add(self, _: Negative<Pr>) -> Self::Output { Negative::default() }
}
impl<Ul: BinUnsigned, Bl: Bit, Pr: BinPos> Add<Negative<Pr>> for Bin<Ul, Bl>
where Self: BinPos, Equal: Cmp<Self, Pr>, <Equal as Cmp<Self, Pr>>::Output: BinSignedOpWith<Self, Pr> {
type Output = <<Equal as Cmp<Self, Pr>>::Output as BinSignedOpWith<Self, Pr>>::Output;
#[inline] fn add(self, _: Negative<Pr>) -> Self::Output { Default::default() }
}
impl<Ul: BinPos, Ur: BinPos> Add<Ur> for Negative<Ul>
where Equal: Cmp<Ur, Ul>, <Equal as Cmp<Ur, Ul>>::Output: BinSignedOpWith<Ur, Ul> {
type Output = <<Equal as Cmp<Ur, Ul>>::Output as BinSignedOpWith<Ur, Ul>>::Output;
#[inline] fn add(self, _: Ur) -> Self::Output { Default::default() }
}
二、基本类型说明
-
Z0: 表示数字0,也是数的终止标志
-
O和I: 表示二进制位的0和1
-
Bin<U, B>: 表示二进制数,U是高位部分,B是最低位(0或1)
-
Negative
: 表示负数,P是其绝对值对应的正数
三、零相关加法
rust
impl<Int: BinInt> Add<Int> for Z0 {
type Output = Int;
fn add(self, rhs: Int) -> Self::Output { rhs }
}
任何数加0等于它自己,0加任何数也等于它自己。
四、非负数加比特位
处理0加二进制位0、0加二进制位1的情况,以及正数加二进制位0、加二进制位1的情况:
rust
impl Add<I> for Z0 {
type Output = Bin<Z0, I>; // 0 + 1 = 1
fn add(self, _: I) -> Self::Output { Bin::default() }
}
五、正数间加法
处理两个正二进制数相加的情况,分为三种情况:
-
任意二进制数加偶数(最低位不变)
-
偶数加奇数(最低位为1)
-
奇数加奇数(需要进位)
例如:
rust
impl<Ul: BinUnsigned, Ur: BinUnsigned, B: Bit> Add<Bin<Ur, O>> for Bin<Ul, B> where Ul: Add<Ur> {
type Output = Bin<<Ul as Add<Ur>>::Output, B>;
// 例如: 11(Bin<Bin<Z0,I>,I>) + 10(Bin<Bin<Z0,I>,O>) = 101(Bin<Bin<Bin<Z0,I>,O>,I>)
}
六、负数相关加法
处理负数相加,以及正负数相加的情况:
rust
impl<Pl: BinPos, Pr: BinPos> Add<Negative<Pr>> for Negative<Pl> where Pl: Add<Pr>, <Pl as Add<Pr>>::Output: BinPos {
type Output = Negative<<Pl as Add<Pr>>::Output>;
// 例如: -3 + -2 = -5
}
impl<Ul: BinPos, Ur: BinPos> Add<Ur> for Negative<Ul>
where Equal: Cmp<Ur, Ul>, <Equal as Cmp<Ur, Ul>>::Output: BinSignedOpWith<Ur, Ul> {
type Output = <<Equal as Cmp<Ur, Ul>>::Output as BinSignedOpWith<Ur, Ul>>::Output;
// 例如: -3 + 5 = 2
}
七、特点
-
所有计算都在类型系统层面完成,运行时没有任何计算
-
使用Default::default()作为占位符,实际值由类型系统决定
-
通过trait约束确保类型安全
-
支持正数、负数和零的加法运算
这种类型级编程技术在需要编译期计算的场景中非常有用,如模板元编程、维度检查等。