【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] 确保运行时无额外开销

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

相关推荐
我是前端小学生2 天前
一文梳理Rust语言中的可变结构体实例
rust
Source.Liu2 天前
【unitrix数间混合计算】2.21 二进制整数加法计算(bin_add.rs)
rust
Include everything2 天前
Rust学习笔记(二)|变量、函数与控制流
笔记·学习·rust
Source.Liu2 天前
【unitrix数间混合计算】2.20 比较计算(cmp.rs)
rust
许野平2 天前
Rust:构造函数 new() 如何进行错误处理?
开发语言·后端·rust
许野平2 天前
Rust:专业级错误处理工具 thiserror 详解
rust·error·错误处理·result·thiserror
蒋星熠2 天前
Rust 异步生态实战:Tokio 调度、Pin/Unpin 与零拷贝 I/O
人工智能·后端·python·深度学习·rust
Include everything2 天前
Rust学习笔记(一)|Rust初体验 猜数游戏
笔记·学习·rust
华科云商xiao徐3 天前
Rust+Python双核爬虫:高并发采集与智能解析实战
数据库·python·rust