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

相关推荐
维维酱7 小时前
四、VGA 文本缓冲区基础
rust
寻月隐君9 小时前
Rust 实战:从零构建一个多线程 Web 服务器
后端·rust·github
受之以蒙13 小时前
Rust & WebAssembly 性能调优指南:从毫秒级加速到KB级瘦身
笔记·rust·webassembly
Vallelonga18 小时前
关于 Rust 异步(无栈协程)的相关疑问
开发语言·后端·rust
q5673152318 小时前
Rust爬虫与代理池技术解析
开发语言·爬虫·python·rust
RustFS21 小时前
如何用 Trae + RustFS MCP 实现自然语言对对象存储的操作?
rust·trae
浪费笔墨1 天前
基于rust的RGBA颜色混合
rust
咸甜适中1 天前
Rust语言序列化和反序列化vec<u8>,serde库Serialize, Deserialize,bincode库(2025年最新解决方案详细使用)
开发语言·后端·rust
受之以蒙2 天前
web-sys进阶:事件处理、异步操作与 Web API 实践
笔记·rust·webassembly
寻月隐君2 天前
Rust NFT 开发实战:构建生产级的 Pinata IPFS 自动化上传工具
后端·rust·github