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

相关推荐
superman超哥1 小时前
Rust 错误处理模式:Result、?运算符与 anyhow 的最佳实践
开发语言·后端·rust·运算符·anyhow·rust 错误处理
Tony Bai10 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
哆啦code梦18 小时前
Rust:高性能安全的现代编程语言
开发语言·rust
superman超哥19 小时前
Rust 过程宏开发入门:编译期元编程的深度实践
开发语言·后端·rust·元编程·rust过程宏·编译期
借个火er21 小时前
用 Tauri 2.0 + React + Rust 打造跨平台文件工具箱
react.js·rust
superman超哥1 天前
Rust Link-Time Optimization (LTO):跨边界的全局优化艺术
开发语言·后端·rust·lto·link-time·跨边界·优化艺术
superman超哥1 天前
Rust 编译优化选项配置:释放性能潜力的精细调控
开发语言·后端·rust·rust编译优化·精细调控·编译优化选项
superman超哥1 天前
Rust 日志级别与结构化日志:生产级可观测性实践
开发语言·后端·rust·可观测性·rust日志级别·rust结构化日志
superman超哥1 天前
Rust 减少内存分配策略:性能优化的内存管理艺术
开发语言·后端·性能优化·rust·内存管理·内存分配策略
superman超哥1 天前
Rust 并发性能调优:线程、异步与无锁的深度优化
开发语言·后端·rust·线程·异步·无锁·rust并发性能