【unitrix数间混合计算】2.21 二进制整数加法计算(bin_add.rs)

一、源码

这段代码是用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. 任意二进制数加偶数(最低位不变)

  2. 偶数加奇数(最低位为1)

  3. 奇数加奇数(需要进位)

例如:

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
}

七、特点

  1. 所有计算都在类型系统层面完成,运行时没有任何计算

  2. 使用Default::default()作为占位符,实际值由类型系统决定

  3. 通过trait约束确保类型安全

  4. 支持正数、负数和零的加法运算

这种类型级编程技术在需要编译期计算的场景中非常有用,如模板元编程、维度检查等。

相关推荐
小杍随笔4 小时前
2025年Rust GUI框架实战万字避坑指南
开发语言·后端·rust
在水一缸1 天前
当 AI 编码助手遇上 Rust:深入解析 Zerostack 的极简哲学与实战应用
rust·系统架构·开源项目·轻量化·ai编码助手·zerostack
2401_873479401 天前
风控引擎高并发IP查询方案对比:开源Rust库 vs 商业离线库,怎么选?
tcp/ip·rust·开源·ip
花褪残红青杏小2 天前
Rust图像处理第22节-从RGB到YCbCr: 让亮度和颜色分家
rust·webassembly·图形学
花褪残红青杏小2 天前
Rust图像处理第21节-最小二乘回归:用矩阵求逆解"拟合"问题
rust·webassembly·图形学
k4m7v2pz2 天前
Bevy 0.14.2 玩家精灵不渲染(只有背景在动)排查全记录
macos·rust·bevy
老王生涯2 天前
rust开发环境配置-Windows & GNU
开发语言·windows·rust
Yeauty2 天前
自建 HLS 第一问:fMP4 还是 TS?用 Rust 在进程内把两种都跑出来
开发语言·后端·rust
Flynt2 天前
我用Biome替换ESLint+Prettier在项目上踩了不少坑
rust·eslint·前端工程化
Yeauty2 天前
2026 年在 Rust 里处理音视频,该走哪条路?
rust·ffmpeg·音视频·视频