一、源碼
這段代碼實現了一個二進制數字標準化系統,用于將二進制數字類型(B0/B1)轉換為更簡潔的表示形式。
//! 二進制數字標準化模塊 / Binary Number Normalization Module
//!
//! 提供將二進制數字(B0/B1)標準化為更簡潔表示形式的功能
//! Provides functionality to normalize binary numbers (B0/B1) into more concise representations
//!
//! 例如 B0<Z0> => Z0, B1<Z0> => P1, B1<N1> => N1
//! e.g. B0<Z0> => Z0, B1<Z0> => P1, B1<N1> => N1use crate::number::{Z0, P1, N1, B0, B1, NonNegOne, NonZero};/// 處理 B0<H> 類型的標準化 / Standardization for B0<H> types
///
/// 這個 trait 定義了將 B0<H> 類型數字標準化的行為。
/// This trait defines the behavior for normalizing B0<H> type numbers.
///
/// 當高位 H 為 Z0 時,將 B0<Z0> 轉換為 Z0;
/// Converts B0<Z0> to Z0 when higher bit H is Z0;
/// 對于其他非零高位,保持 B0<H> 結構不變。
/// maintains B0<H> structure for other non-zero higher bits.
pub trait IfB0 {type Output;fn b0() -> Self::Output;
}/// 處理 B1<H> 類型的標準化 / Standardization for B1<H> types
///
/// 這個 trait 定義了將 B1<H> 類型數字標準化的行為。
/// This trait defines the behavior for normalizing B1<H> type numbers.
///
/// 當高位 H 為 N1 時,將 B1<N1> 轉換為 N1;
/// Converts B1<N1> to N1 when higher bit H is N1;
/// 當高位 H 為 Z0 時,將 B1<Z0> 轉換為 P1;
/// Converts B1<Z0> to P1 when higher bit H is Z0;
/// 對于其他非零非負一高位,保持 B1<H> 結構不變。
/// maintains B1<H> structure for other non-zero non-negative-one higher bits.
pub trait IfB1 {type Output;fn b1() -> Self::Output;
}// ==================== IfB0 實現 / IfB0 Implementations ====================/// 為所有非零類型實現 IfB0 / IfB0 implementation for all non-zero types
///
/// 保持 B0<H> 結構不變,其中 H 是非零類型
/// Maintains B0<H> structure where H is non-zero type
impl<I: NonZero> IfB0 for I {type Output = B0<I>;#[inline(always)]fn b0() -> Self::Output {B0::new()}
}/// 為零類型 Z0 實現 IfB0 / IfB0 implementation for zero type Z0
///
/// 將 B0<Z0> 轉換為 Z0
/// Converts B0<Z0> to Z0
impl IfB0 for Z0 {// B0<Z0> => Z0type Output = Z0;#[inline(always)]fn b0() -> Self::Output {Z0::new()}
}// ==================== IfB1 實現 / IfB1 Implementations ====================/// 為非零非負一類型實現 IfB1 / IfB1 implementation for non-zero non-negative-one types
///
/// 保持 B1<H> 結構不變,其中 H 是非零非負一類型
/// Maintains B1<H> structure where H is non-zero non-negative-one type
impl<I: NonZero + NonNegOne> IfB1 for I {type Output = B1<I>;#[inline(always)]fn b1() -> Self::Output {B1::new()}
}/// 為負一類型 N1 實現 IfB1 / IfB1 implementation for negative-one type N1
///
/// 將 B1<N1> 轉換為 N1
/// Converts B1<N1> to N1
impl IfB1 for N1 {// B1<N1> => N1type Output = N1;#[inline(always)]fn b1() -> Self::Output {N1::new()}
}/// 為零類型 Z0 實現 IfB1 / IfB1 implementation for zero type Z0
///
/// 將 B1<Z0> 轉換為 P1
/// Converts B1<Z0> to P1
impl IfB1 for Z0 {// B1<Z0> => P1type Output = P1;#[inline(always)]fn b1() -> Self::Output {P1::new()}
}
二、模塊功能
-
提供將二進制數字(B0/B1)標準化為更簡潔表示形式的功能
-
例如:
-
B0 => Z0(二進制0后跟零 → 零)
-
B1 => P1(二進制1后跟零 → 正一)
-
B1 => N1(二進制1后跟負一 → 負一)
三、核心Trait定義
- IfB0 Trait
-
處理B0類型的標準化
-
規則:
-
當高位H為Z0時:B0 → Z0
-
其他非零高位:保持B0結構不變
-
-
方法:
type Output;
fn b0() -> Self::Output;
- IfB1 Trait
-
處理B1類型的標準化
-
規則:
-
當高位H為N1時:B1 → N1
-
當高位H為Z0時:B1 → P1
-
其他非零非負一高位:保持B1結構不變
-
-
方法:
type Output;
fn b1() -> Self::Output;
四、具體實現
IfB0實現
- 對于所有非零類型:
-
保持B0結構不變
-
實現:
impl<I: NonZero> IfB0 for I {type Output = B0<I>;fn b0() -> Self::Output { B0::new() }
}
- 對于零類型Z0:
-
將B0轉換為Z0
-
實現:
impl IfB0 for Z0 {type Output = Z0;fn b0() -> Self::Output { Z0::new() }
}
IfB1實現
- 對于非零非負一類型:
-
保持B1結構不變
-
實現:
impl<I: NonZero + NonNegOne> IfB1 for I {type Output = B1<I>;fn b1() -> Self::Output { B1::new() }
}
- 對于負一類型N1:
-
將B1轉換為N1
-
實現:
impl IfB1 for N1 {type Output = N1;fn b1() -> Self::Output { N1::new() }
}
- 對于零類型Z0:
-
將B1轉換為P1
-
實現:
impl IfB1 for Z0 {type Output = P1;fn b1() -> Self::Output { P1::new() }
}
五、技術細節
- 類型約束:
-
使用NonZero和NonNegOne trait bound來限制實現類型
-
確保只有符合條件的類型才能實現相應的trait
- 性能優化:
-
所有方法都標記為#[inline(always)],確保內聯優化
-
使用new()方法創建實例,以便編譯時計算
- 類型轉換:
-
通過關聯類型Output指定轉換結果類型
-
轉換過程完全在類型系統層面進行,不涉及運行時開銷
六、設計目的
這個系統的主要目的是:
-
簡化二進制數字的類型表示
-
消除冗余的類型結構(如B0其實就是Z0)
-
為類型系統提供更簡潔的表示形式
-
支持編譯時的數值計算和轉換
這種設計常用于需要類型級數值計算的場景,如:
-
物理單位系統
-
矩陣維度計算
-
編譯時資源分配
-
類型安全的API設計