一、源碼
這段代碼是用Rust語言實現的一個庫,主要功能是對數字進行位取反操作(按位NOT運算)。
/*庫數字取反* 編制人: $ource* 修改版次:0版完成版* 本版次創建時間: 2025年6月25日* 最后修改時間: 無* 待完善問題:無*/
use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};// ==================== 位取反運算實現 ====================// 基礎類型實現
impl Not for Z0 { // !0 = -1type Output = N1;fn not(self) -> Self::Output { N1 }
}impl Not for P1 { // !1 = -2 (二進制表示為 B0<N1>)type Output = B0<N1>;fn not(self) -> Self::Output { B0::new() }
}impl Not for N1 { // !(-1) = 0type Output = Z0;fn not(self) -> Self::Output { Z0 }
}// 遞歸類型實現
impl<Other: NonZero + NonNegOne + Not> Not for B0<Other> { // !B0<T> = B1<!T>type Output = B1<Other::Output>;fn not(self) -> Self::Output { B1::new() }
}impl<Other: NonZero + Not> Not for B1<Other> { // !B1<T> = B0<!T>type Output = B0<Other::Output>;fn not(self) -> Self::Output { B0::new() }
}// 特殊處理
impl Not for B0<N1> { // !(-2) = 1 特例type Output = P1;fn not(self) -> Self::Output { P1 }
}/* 注意:
1. 小數類型未實現取反,因為小數部分取反會產生無限尾部1
2. 浮點類型不支持位取反操作(無實際意義)
*/// 變量類型取反
impl<T: PrimitiveInt + Not> Not for Var<T> { // !Var<T> = Var<!T>type Output = Var<<T as Not>::Output>;#[inline(always)]fn not(self) -> Self::Output { Var(!self.0) }
}#[cfg(test)]
mod tests {use super::*;#[test]fn test_basic_not() {assert_eq!(!Z0, N1);assert_eq!(!P1, B0::<N1>::new());assert_eq!(!N1, Z0);}#[test]fn test_recursive_not() {let b0n1 = B0::<N1>::new();assert_eq!(!b0n1, P1); // 特殊處理let b1z0 = B1::<P1>::new();assert_eq!(!b1z0, B0::<B0<N1>>::new());}#[test]fn test_var_not() {let var = Var(42i32);let res = !var;assert_eq!(res.0, !42i32);}
}
二、代碼分析
- 導入依賴
use core::ops::Not;
use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero, Var, PrimitiveInt};
導入了Rust核心庫中的Not trait(用于實現取反操作)和自定義的數字類型。
2. 基礎類型實現
impl Not for Z0 { // !0 = -1type Output = N1;fn not(self) -> Self::Output { N1 }
}impl Not for P1 { // !1 = -2 (二進制表示為 B0<N1>)type Output = B0<N1>;fn not(self) -> Self::Output { B0::new() }
}impl Not for N1 { // !(-1) = 0type Output = Z0;fn not(self) -> Self::Output { Z0 }
}
這部分為基本數字類型實現了Not trait:
-
Z0(零):取反結果為N1(負一)
-
P1(正一):取反結果為B0(負二)
-
N1(負一):取反結果為Z0(零)
- 遞歸類型實現
impl<Other: NonZero + NonNegOne + Not> Not for B0<Other> { // !B0<T> = B1<!T>type Output = B1<Other::Output>;fn not(self) -> Self::Output { B1::new() }
}impl<Other: NonZero + Not> Not for B1<Other> { // !B1<T> = B0<!T>type Output = B0<Other::Output>;fn not(self) -> Self::Output { B0::new() }
}
這部分為遞歸數字類型實現了Not trait:
-
B0(二進制0前綴的數):取反結果為B1<!T>
-
B1(二進制1前綴的數):取反結果為B0<!T>
- 特殊處理
impl Not for B0<N1> { // !(-2) = 1 特例type Output = P1;fn not(self) -> Self::Output { P1 }
}
對B0(即-2)做了特殊處理,直接返回P1(1),因為這是遞歸實現的邊界情況。
5. 變量類型實現
impl<T: PrimitiveInt + Not> Not for Var<T> { // !Var<T> = Var<!T>type Output = Var<<T as Not>::Output>;#[inline(always)]fn not(self) -> Self::Output { Var(!self.0) }
}
為Var類型實現了Not trait,它包裝了一個原始整數類型,直接調用該類型的取反操作。
6. 測試模塊
#[cfg(test)]
mod tests {use super::*;#[test]fn test_basic_not() {assert_eq!(!Z0, N1);assert_eq!(!P1, B0::<N1>::new());assert_eq!(!N1, Z0);}#[test]fn test_recursive_not() {let b0n1 = B0::<N1>::new();assert_eq!(!b0n1, P1); // 特殊處理let b1z0 = B1::<P1>::new();assert_eq!(!b1z0, B0::<B0<N1>>::new());}#[test]fn test_var_not() {let var = Var(42i32);let res = !var;assert_eq!(res.0, !42i32);}
}
測試模塊包含三個測試:
-
測試基本類型的取反操作
-
測試遞歸類型的取反操作
-
測試變量類型的取反操作
三、總結
這段代碼實現了一個類型安全的數字取反操作庫,特點包括:
-
支持基本數字類型(零、正一、負一)的取反
-
支持遞歸定義的二進制數的取反
-
對特定邊界情況做了特殊處理
-
提供了變量類型的取反支持
-
通過泛型和trait實現了類型安全的操作
這個庫是類型級數學計算的一部分,使用了Rust的類型系統來保證數字操作的安全性。