当前位置: 首页 > news >正文

【unitrix】 4.1 类型级加一操作(Add1.rs)

一、原码

这段代码实现了一个类型级的加一操作(Add1 trait),用于在Rust的类型系统中进行数值加一运算。

//! 加一操作特质实现 / Increment operation trait implementation
//!
//! 说明:
//!     1. Z0、P1,、N1 + 1,常规计算
//!     2. B0<H> + 1,该位B1,无进位,原高位是N1时要规范格式,即H=N1时要特化,此时源码为B0<N1>
//!     3. B1<H> + 1,该位B0,有进位,当H+1 = Z0时要规范格式,即H=N1时要特化,此时源码为B1<N1>,不是简化格式use crate::number::{NonNegOne, NonZero, Primitive, Var, B0, B1, N1, P1, Z0, FixedPoint, Float};
/// 加一特质 / Increment trait
/// 
/// 为类型系统提供加一操作的计算能力
/// Provides increment operation capability for type system
pub trait Add1 {/// 加一后的输出类型 / Output type after incrementtype Output;fn add1(self) -> Self::Output;
}// ========== 基础类型实现 / Basic Type Implementations ==========/// Z0 (0) 加一实现 / Increment for Z0 (0)
/// 
/// 0 + 1 = 1 (B1<Z0>)
impl Add1 for Z0 {type Output = P1;  //P1替换B1<Z0>#[inline(always)]fn add1(self) -> Self::Output{P1::new()}
}/// P1 (1) 加一实现 / Increment for P1 (+1)
/// 
/// 1 + 1 = 2 (B0<P1>)
impl Add1 for P1 {type Output = B0<P1>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}/// N1 (-1) 加一实现 / Increment for N1 (-1)
/// 
/// -1 + 1 = 0 (Z0)
impl Add1 for N1 {type Output = Z0;#[inline(always)]fn add1(self) -> Self::Output{Z0::new()}
}// ========== 递归类型实现 / Recursive Type Implementations ==========/// B0<H> 加一实现 / Increment for B0<H>
/// 
/// 直接加一无需进位 / Direct increment without carry
/// ...0 + 1 = ...1 / ...0 + 1 = ...1
impl<H:NonZero + NonNegOne> Add1 for B0<H>{type Output = B1<H>;#[inline(always)]fn add1(self) -> Self::Output{B1::new()}
}/// B1<H> 加一实现 / Increment for B1<H>
/// 
/// 处理进位情况 / Handles carry case
/// 0...1 + 1 = 0...(高位进位) / ...1 + 1 = ...0(with carry)
impl<H:NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H>{//P1替代B1<Z0>后,H不可能为Z0type Output = B0<H::Output>;#[inline(always)]fn add1(self) -> Self::Output{B0::new()}
}// 待Float加法完善后考虑其加一实现
/* impl<Mantissa, Exponent> Add1 for Float<Mantissa, Exponent> {type Output = <Float<Mantissa, Exponent> as Add<P1>>::out;#[inline(always)]fn add1(self) -> Self::Output{Float::new()}
} */
// ========== 特化实现 ==========
/// B0<N1> (-2) 加一特化实现 / Specialized increment for B0<N1> (-2)
impl Add1 for B0<N1> {type Output = N1;#[inline(always)]fn add1(self) -> Self::Output{N1::new()}
}// B1<N1> (-1) 加一特化实现,本身不允许B1<N1>出现,其结果也是不规范的格式,目前取消
/* impl Add1 for B1<N1> {type Output = Z0;
} *//// Val<T> 加一实现 / Increment for Val<T>
/// Val<T>
impl<T:Primitive + From<P1>> Add1 for Var<T> {type Output = Self;#[inline(always)]fn add1(self) -> Self::Output{Self(self.0 + T::from(P1))}
}// ==============================================
// FixedPoint的Add1实现
// ==============================================impl<IntPart: Add1, FracPart> Add1 for FixedPoint<IntPart, FracPart>{type Output = FixedPoint<IntPart::Output, FracPart>;fn add1(self) -> Self::Output {FixedPoint::new()}
}

二、核心设计

Add1 Trait 定义

pub trait Add1 {type Output;fn add1(self) -> Self::Output;
}

定义了一个泛型trait,表示类型可以执行加一操作,Output关联类型表示加一后的结果类型。

三、基础类型实现

Z0 (0) 加一

impl Add1 for Z0 {type Output = P1;  // 0 + 1 = 1fn add1(self) -> Self::Output { P1::new() }
}
  • Z0表示0,加一后变为P1(1)
P1 (1) 加一

impl Add1 for P1 {type Output = B0<P1>;  // 1 + 1 = 2 (二进制10)fn add1(self) -> Self::Output { B0::new() }
}
  • P1表示1,加一后变为B0(二进制10,即2)
N1 (-1) 加一

impl Add1 for N1 {type Output = Z0;  // -1 + 1 = 0fn add1(self) -> Self::Output { Z0::new() }
}
  • N1表示-1,加一后变为Z0(0)

四、复合类型实现

B0 (以0结尾的二进制数) 加一

impl<H: NonZero + NonNegOne> Add1 for B0<H> {type Output = B1<H>;  // ...0 + 1 = ...1fn add1(self) -> Self::Output { B1::new() }
}
  • 将最低位的B0变为B1,不需要进位

  • 例如:B0(2) + 1 = B1(3)

B1 (以1结尾的二进制数) 加一

impl<H: NonZero + NonNegOne + Add1<Output: NonZero>> Add1 for B1<H> {type Output = B0<H::Output>;  // ...1 + 1 = ...0 (有进位)fn add1(self) -> Self::Output { B0::new() }
}
  • 将最低位的B1变为B0,并向高位进位

  • 例如:B1(3) + 1 = B0<B0>(4)

五、特殊处理

B0 (-2) 加一

···rust

impl Add1 for B0 {
type Output = N1; // -2 + 1 = -1
fn add1(self) -> Self::Output { N1::new() }
}
···

  • 特化处理负数情况,保持格式规范
    
  • B0<N1>表示-2,加一后变为N1(-1)
    
定点数(FixedPoint)实现

···rust

impl<IntPart: Add1, FracPart> Add1 for FixedPoint<IntPart, FracPart> {
type Output = FixedPoint<IntPart::Output, FracPart>;
fn add1(self) -> Self::Output { FixedPoint::new() }
}
···
+ 定点数的加一只对整数部分进行加一操作

+ 小数部分保持不变+ 例如:FixedPoint<P1, B1<Z0>>(1.5) + 1 = FixedPoint<B0<P1>, B1<Z0>>(2.5)

六、特点

+ 类型安全:所有操作都在编译期进行类型检查+ 零成本抽象:运行时无额外开销+ 递归处理:复合类型的加一操作递归处理每一位+ 特殊化处理:对边界情况(如负数)有特殊处理

这个实现通过在类型系统层面定义数值运算,可以在编译期捕获更多错误,特别适合需要高安全性和确定性的场景。

相关文章:

  • Vmware WorkStation 17.5 安装 Ubuntu 24.04-LTS Server 版本
  • Qt项目,记事本
  • windows桌面连接ubuntu, 设置VNC
  • BERT 模型准备与转换详细操作流程
  • 科学计算库 Numpy
  • 软件工程核心知识全景图:从需求到部署的系统化构建指南
  • 【AI智能体】Spring AI MCP 服务常用开发模式实战详解
  • 命令行中SSH本地端口转发和反向远程端口转发
  • 计算机网络课程设计--基于TCP协议的文件传输系统
  • linux VFS简介
  • 笔式胰岛素简单拆解
  • SAP金属行业解决方案:无锡哲讯科技助力企业数字化转型与高效运营
  • P99延迟:系统性能优化的关键指标
  • 408考研逐题详解:2010年第3题——后序线索二叉树
  • Docker容器自动更新利器:Watchtower
  • 自动化测试01
  • 如何用AI开发完整的小程序<9>—UI自适应与游戏页优化
  • oracle rac - starwind san 磁盘共享篇
  • SpringBoot+Vue服装商城系统 附带详细运行指导视频
  • 设计模式精讲 Day 10:外观模式(Facade Pattern)
  • 企业做网站须要注意些什么/百度非企渠道开户
  • 网站建设合同范本/免费友情链接网站
  • 哪个软件可以做明星视频网站/如何制作一个公司网站
  • vvic网站一起做网店/网络推广及销售
  • 淄博网站建设公司/站长之家关键词查询
  • 深圳网站建设sumaart/长沙正规seo优化价格