【unitrix】 6.13 类型级整数的按位取反(Not)操作实现(not.rs)

一、源码

这段代码实现了类型级整数的按位取反操作,支持基本位类型、复合位类型和变量类型。

rust 复制代码
//! 类型级整数的按位取反(Not)操作实现
//!
//! 为基本位类型(O/I)、复合位类型(B的整型部分)和变量类型(V)实现按位取反操作

// $ource 2025-7-23 0.1.0版
use core::ops::Not;
use crate::number::{Null, O, I, B, V, Bit, BaseInt};

// 基本位类型取反
impl Not for O { type Output = I; #[inline] fn not(self) -> I { I } }
impl Not for I { type Output = O; #[inline] fn not(self) -> O { O } }

// 复合位类型取反
impl<L: Bit> Not for B<Null, L> {
    type Output = B<Null, <L as Not>::Output>;
    #[inline]
    fn not(self) -> Self::Output { B { h: Null, l: !self.l } }
}

impl<H, L: Bit, X: Bit> Not for B<B<H, L>, X>
where 
    B<H, L>: Not,
{
    type Output = B<<B<H, L> as Not>::Output, <X as Not>::Output>;
    #[inline]
    fn not(self) -> Self::Output { B { h: !self.h, l: !self.l } }
}

// 变量类型取反
impl<T: BaseInt> Not for V<T> {
    type Output = V<<T as Not>::Output>;
    #[inline]
    fn not(self) -> Self::Output { V(!self.0) }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    // 测试基本位类型取反
    #[test]
    fn test_bit_not() {
        assert_eq!(!O, I);  // !0 = 1
        assert_eq!(!I, O);  // !1 = 0
        
        // 编译时类型检查
        let _: I = !O;
        let _: O = !I;
    }
    
    // 测试单层复合类型(B<Null, L>)取反
    #[test]
    fn test_single_level_not() {
        let zero: B<Null, O> = B { h: Null, l: O };
        let neg_one = !zero;
        assert_eq!(neg_one.l, I);  // !0 = -1 (B<Null, I>)
        
        let neg_one: B<Null, I> = B { h: Null, l: I };
        let zero = !neg_one;
        assert_eq!(zero.l, O);  // !-1 = 0 (B<Null, O>)
    }
    
    // 测试多层复合类型(B<B<H, L>, X>)取反
    #[test]
    fn test_nested_not() {
        // 构造 B<B<Null, O>, I> 表示 0b01 (1)
        let num: B<B<Null, O>, I> = B {
            h: B { h: Null, l: O },
            l: I
        };
        
        let inverted = !num;
        assert_eq!(inverted.h.h, Null);  // 最高位Null保持
        assert_eq!(inverted.h.l, I);     // !O = I
        assert_eq!(inverted.l, O);       // !I = O
        
        // 类型检查
        let _: B<B<Null, I>, O> = inverted;
    }
    
    // 测试变量类型取反
    #[test]
    fn test_var_not() {
        let v: V<u8> = V(0b1010);
        let inverted = !v;
        assert_eq!(inverted.0, 0b11110101);  // !0b1010 = 0b11110101 (u8)
        
        let v: V<i32> = V(-1);
        let inverted = !v;
        assert_eq!(inverted.0, 0);  // !-1 = 0 (i32)
    }
    
    // 测试边界情况
    #[test]
    fn test_edge_cases() {
        // 测试最大/最小值
        let max_u8 = V(u8::MAX);
        assert_eq!((!max_u8).0, 0);
        
        let min_i32 = V(i32::MIN);
        assert_eq!((!min_i32).0, i32::MAX);
    }
    
    // 测试类型约束
    #[test]
    fn test_type_constraints() {
        // 以下代码应该编译失败(测试时需注释掉)
        // let invalid: B<Null, Null> = B { h: Null, l: Null };
        // let _ = !invalid;  // Null未实现Bit
        
        // let invalid: B<B<Null, Null>, O> = B { 
        //     h: B { h: Null, l: Null }, 
        //     l: O 
        // };
        // let _ = !invalid;  // Null未实现Bit
    }
}

二、主要组成部分

  1. 基本位类型取反
rust 复制代码
impl Not for O { type Output = I; #[inline] fn not(self) -> I { I } }
impl Not for I { type Output = O; #[inline] fn not(self) -> O { O } }
  • O 和 I 分别表示二进制位 0 和 1

  • 实现了 Not trait(按位取反操作)

  • !O 返回 I,!I 返回 O

  1. 复合位类型取反

分为两种情况:

单层复合类型 (B<Null, L>)
rust 复制代码
impl<L: Bit> Not for B<Null, L> {
    type Output = B<Null, <L as Not>::Output>;
    fn not(self) -> Self::Output { B { h: Null, l: !self.l } }
}
  • 处理类似 B<Null, O> 这样的单层复合类型

  • 只对低位 l 取反,高位 h 保持 Null

多层复合类型 (B<B<H, L>, X>)
rust 复制代码
impl<H, L: Bit, X: Bit> Not for B<B<H, L>, X>
where 
    B<H, L>: Not,
{
    type Output = B<<B<H, L> as Not>::Output, <X as Not>::Output>;
    fn not(self) -> Self::Output { B { h: !self.h, l: !self.l } }
}
  • 处理嵌套的复合类型,如 B<B<Null, O>, I>

  • 递归地对高位 h 和低位 l 都进行取反

  1. 变量类型取反 (V)
rust 复制代码
impl<T: BaseInt> Not for V<T> {
    type Output = V<<T as Not>::Output>;
    fn not(self) -> Self::Output { V(!self.0) }
}
  • V 包装了一个基础整数类型 T

  • 直接使用 T 的 Not 实现进行取反

三、测试部分

测试用例覆盖了:

  1. 基本位类型取反

  2. 单层复合类型取反

  3. 多层复合类型取反

  4. 变量类型取反

  5. 边界情况(最大/最小值)

  6. 类型约束检查(注释掉的代码用于验证非法情况)

四、设计特点

  1. 类型级编程:在编译时通过类型系统表达和操作整数

  2. 递归实现:复合类型的操作通过递归处理各组成部分

  3. 泛型约束:使用 trait bound (Bit, BaseInt) 确保类型安全

  4. 零成本抽象:使用 #[inline] 确保运行时无额外开销

这段代码展示了一个类型级二进制数的按位取反操作的完整实现,从基本位到复合结构,再到运行时变量,提供了丰富的类型安全操作。

相关推荐
大鱼七成饱11 小时前
Rc和RefCell:严父Rust的叛逆小儿子
rust
l1t13 小时前
使用DeepSeek辅助测试一个rust编写的postgresql协议工具包convergence
开发语言·postgresql·rust·协议·datafusion
Kiri霧15 小时前
Rust数组与向量
开发语言·后端·rust
特立独行的猫a15 小时前
Rust语言入门难,难在哪?所有权、借用检查器、生命周期和泛型介绍
开发语言·后端·rust
yihai-lin19 小时前
Rust/C/C++ 混合构建 - Cmake集成Cargo编译动态库
c语言·c++·rust
fcm1920 小时前
(6) tauri之前端框架性能对比
前端·javascript·rust·前端框架·vue·react
yihailin1 天前
Rust/C/C++ 混合构建 - Cmake集成Cargo编译动态库
rust
李剑一2 天前
为了免受再来一刀的痛苦,我耗时两天开发了一款《提肛助手》
前端·vue.js·rust
红尘散仙2 天前
使用 Tauri Plugin-Store 实现 Zustand 持久化与多窗口数据同步
前端·rust·electron