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

相关推荐
DongLi011 天前
rustlings 学习笔记 -- exercises/05_vecs
rust
番茄灭世神2 天前
Rust学习笔记第2篇
rust·编程语言
shimly1234562 天前
(done) 速通 rustlings(20) 错误处理1 --- 不涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(19) Option
rust
@atweiwei2 天前
rust所有权机制详解
开发语言·数据结构·后端·rust·内存·所有权
shimly1234562 天前
(done) 速通 rustlings(24) 错误处理2 --- 涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(23) 特性 Traits
rust
shimly1234562 天前
(done) 速通 rustlings(17) 哈希表
rust
shimly1234562 天前
(done) 速通 rustlings(15) 字符串
rust
shimly1234562 天前
(done) 速通 rustlings(22) 泛型
rust