【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的类型系统在编译期完成数学运算,是类型级编程的典型应用。

相关推荐
明月看潮生1 天前
青少年编程与数学 02-019 Rust 编程基础 23课题、web服务器
服务器·开发语言·青少年编程·rust
UestcXiye2 天前
Rust 学习笔记:泛型
rust
进击的愤怒2 天前
GIM发布新版本了 (附rust CLI制作brew bottle流程)
开发语言·后端·rust
x-cmd2 天前
x-cmd install | cargo-selector:优雅管理 Rust 项目二进制与示例,开发体验升级
开发语言·后端·rust·cargo·示例
UestcXiye2 天前
Rust 学习笔记:关于泛型的练习题
rust
Source.Liu2 天前
【PhysUnits】4.4 零类型(Z0)及其算术运算(zero.rs)
rust
Dingyin HU2 天前
使用RUST在Arduino上进行编程(MacOS,mega板)
macos·rust·arduino
无名之逆3 天前
Build High-Performance Web Services with Hyperlane
服务器·开发语言·前端·http·rust
UestcXiye3 天前
Rust 学习笔记:关于生命周期的练习题
rust