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

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

相关推荐
鸿乃江边鸟8 小时前
Spark Datafusion Comet 向量化Rust Native--创建Datafusion计划
rust·spark·native
咸甜适中8 小时前
rust的docx-rs库,自定义docx模版批量分页生成一个docx文档(方便打印)(逐行注释)
rust·办公自动化·docx-rs
Vallelonga8 小时前
Rust Option.as_ref() 方法
开发语言·rust
大卫小东(Sheldon)1 天前
GIM 2.0 发布:真正让 AI 提交消息可定制、可控、可项目级优化
git·rust·gim
roamingcode1 天前
我是如何 Vibe Coding,将 AI CLI 工具从 Node.js 迁移到 Rust 并成功发布的
人工智能·rust·node.js·github·claude·github copilot
初恋叫萱萱1 天前
构建高性能生成式AI应用:基于Rust Axum与蓝耘DeepSeek-V3.2大模型服务的全栈开发实战
开发语言·人工智能·rust
superman超哥3 天前
Serde 性能优化的终极武器
开发语言·rust·编程语言·rust serde·serde性能优化·rust开发工具
sayang_shao3 天前
Rust多线程编程学习笔记
笔记·学习·rust
鸿乃江边鸟3 天前
Spark Datafusion Comet 向量化Rust Native--读数据
rust·spark·native·arrow
硬汉嵌入式3 天前
基于Rust构建的单片机Ariel RTOS,支持Cortex-M、RISC-V 和 Xtensa
单片机·rust·risc-v