一、源码
这段代码实现了类型级整数的按位取反操作,支持基本位类型、复合位类型和变量类型。
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
}
}
二、主要组成部分
- 基本位类型取反
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
- 复合位类型取反
分为两种情况:
单层复合类型 (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 都进行取反
- 变量类型取反 (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 实现进行取反
三、测试部分
测试用例覆盖了:
-
基本位类型取反
-
单层复合类型取反
-
多层复合类型取反
-
变量类型取反
-
边界情况(最大/最小值)
-
类型约束检查(注释掉的代码用于验证非法情况)
四、设计特点
-
类型级编程:在编译时通过类型系统表达和操作整数
-
递归实现:复合类型的操作通过递归处理各组成部分
-
泛型约束:使用 trait bound (Bit, BaseInt) 确保类型安全
-
零成本抽象:使用 #[inline] 确保运行时无额外开销
这段代码展示了一个类型级二进制数的按位取反操作的完整实现,从基本位到复合结构,再到运行时变量,提供了丰富的类型安全操作。