【PhysUnits】10 减一操作(sub1.rs)

一、源码

代码实现了一个类型级别的减一操作(Sub1 trait),通过Rust的类型系统在编译期完成数值减一的计算。

rust 复制代码
//! 减一操作特质实现 / Decrement operation trait implementation
//!
//! 提供类型级别的减一计算 / Provides type-level decrement operation

use super::basic::{B0, B1, Z0, N1, Integer, NonZero};

/// 减一特质 / Decrement trait
/// 
/// 为类型系统提供减一操作的计算能力
/// Provides decrement operation capability for type system
pub trait Sub1 {
    /// 减一后的输出类型 / Output type after decrement
    type Output;
}

// ========== 基础类型实现 / Basic Type Implementations ==========

/// Z0 (0) 减一实现 / Decrement for Z0 (0)
/// 
/// 0 - 1 = -1 (N1)
impl Sub1 for Z0 {
    type Output = N1;
}

/// N1 (-1) 减一实现 / Decrement for N1 (-1)
/// 
/// -1 - 1 = -2 (B0<N1>)
impl Sub1 for N1 {
    type Output = B0<N1>;
}

/// B1<Z0> (+1) 减一特化实现 / Specialized decrement for B1<Z0> (+1)
/// 
/// 说明:为表示简洁,高位固定为B0<N1>和B1<Z0>,减1时为b1<B0<N1>>和B0<Z0>,
/// 其B0<Z0>不符合简洁规则,因此B1<Z0>减1必须特化为Z0
/// 
/// Note: For concise representation, high bits are fixed as B0<N1> and B1<Z0>.
/// When decrementing, B0<Z0> doesn't follow concise rules,
/// so B1<Z0> must be specialized to Z0
impl Sub1 for B1<Z0> {
    type Output = Z0;
}

// ========== 递归类型实现 / Recursive Type Implementations ==========

/// B0<H> 减一实现 / Decrement for B0<H>
/// 
/// 处理借位情况 / Handles borrow case
/// ...0 -1 = ...1(高位借位) / ...0 -1 = ...1(with borrow)
impl<H> Sub1 for B0<H>
where
    H: Sub1 + Integer,
{
    type Output = B1<H::Output>;
}

/// B1<H> 减一实现 / Decrement for B1<H>
/// 
/// 直接减一无需借位 / Direct decrement without borrow
/// ...1 -1 = ...0 / ...1 -1 = ...0
impl<H> Sub1 for B1<H>
where
    H: NonZero,
{
    type Output = B0<H>;
}
  • 定义了一个Sub1 trait,只有关联类型Output没有方法

  • 表示"当前类型减一后的结果类型"

三、基础类型实现

零的减一 (Z0 → N1)
rust 复制代码
impl Sub1 for Z0 {
    type Output = N1; // 0 -1 = -1
}
  • 零减一得到负一(N1)
负一的减一 (N1 → B0)
rust 复制代码
impl Sub1 for N1 {
    type Output = B0<N1>; // -1 -1 = -2
}
  • -1减一得到-2,用二进制表示为B0(即...10)
正一的减一 (B1 → Z0)
rust 复制代码
impl Sub1 for B1<Z0> {
    type Output = Z0; // 1 -1 = 0
}
  • 特化处理+1减一的情况,直接返回零(Z0)

  • 注释说明这是为了保持表示的简洁性,避免产生B0这种不符合规范的形式

四、递归类型实现

以0结尾的数 (B0)
rust 复制代码
impl<H> Sub1 for B0<H>
where
    H: Sub1 + Integer,
{
    type Output = B1<H::Output>; // ...0 → ...1(高位借位)
}
  • 处理形如...0的二进制数

  • 需要向高位借位,所以递归调用高位的减一操作

  • 当前位从0变成1(因为借位后2-1相当于1)

以1开头的数 (B1)
rust 复制代码
impl<H> Sub1 for B1<H>
where
    H: NonZero,
{
    type Output = B0<H>; // 0...1 → ...0
}
  • 处理形如...1的二进制数

  • 直接当前位从1变成0,不需要借位

  • 通过NonZero约束排除了B1的情况(已特化处理)

五、设计特点

  1. 类型安全:
  • 所有操作在编译期完成

  • 通过trait系统保证正确性

  1. 二进制补码表示:
  • 正确处理正负数的减一

  • 负数用补码表示(如-1表示为全1)

  1. 递归结构:
  • 通过类型递归处理任意长度的二进制数

  • 自动处理借位问题

  1. 边界条件处理:
  • 专门处理0、-1、+1等边界情况

  • 通过特化实现避免非法状态

六、使用示例

rust 复制代码
type Two = B0<B1<Z0>>;    // +2 (0b10)
type MinusTwo = B0<N1>;    // -2 (0b...1110)

type One = <Two as Sub1>::Output;        // +1 (B1<Z0>)
type MinusThree = <MinusTwo as Sub1>::Output; // -3 (B1<N1>)

这个实现展示了如何利用Rust的类型系统在编译期完成数学运算,是类型级编程的典型应用。

相关推荐
该用户已不存在2 天前
Mojo vs Python vs Rust: 2025年搞AI,该学哪个?
后端·python·rust
大卫小东(Sheldon)2 天前
写了一个BBP算法的实现库,欢迎讨论
数学·rust
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
ftpeak3 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
咸甜适中3 天前
rust语言 (1.88) 学习笔记:客户端和服务器端同在一个项目中
笔记·学习·rust
咸甜适中3 天前
rust语言 (1.88) egui (0.32.2) 学习笔记(逐行注释)(二十八)使用图片控件显示图片
笔记·学习·rust·egui
huli33203 天前
pingora_web:首款基于 Cloudflare Pingora 的企业级 Rust Web 框架
rust
Pomelo_刘金3 天前
如何优雅地抽离 Rust 子工程:以 rumqttd 为例
rust
几颗流星3 天前
Rust 常用语法速记 - 错误处理
后端·rust
向上的车轮3 天前
如何用 Rust 重写 SQLite 数据库(二):是否有市场空间?
数据库·rust·sqlite