【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. 支持正数、负数和零的加法运算

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

相关推荐
古城小栈1 小时前
Rust 网络请求库:reqwest
开发语言·网络·rust
WaterRun2 小时前
一个由Rust实现的, 好得多的Windows tree命令: tree++项目简介
rust·github
大厂技术总监下海3 小时前
Rust的“一发逆转弹”:Dioxus 如何用一套代码横扫 Web、桌面、移动与后端?
前端·rust·开源
superman超哥6 小时前
Rust 异步时间管理核心:Tokio 定时器实现机制深度剖析
开发语言·rust·编程语言·rust异步时间管理核心·tokio定时器实现机制·tokio定时器
古城小栈6 小时前
Rust 的 validator 库
开发语言·后端·rust
古城小栈6 小时前
Rust 的 redis-rs 库
开发语言·redis·rust
superman超哥7 小时前
Rust 异步递归的解决方案
开发语言·后端·rust·编程语言·rust异步递归
Mr -老鬼7 小时前
Rust 的优雅和其他语言的不同之处
java·开发语言·rust
weixin_531651817 小时前
Rust 的所有权机制
java·开发语言·rust
古城小栈9 小时前
Rust 丰富&好用的 格式化语法
前端·算法·rust