sparse_ir/
traits.rs

1//! Common trait definitions for SparseIR
2
3/// Statistics type enumeration
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5pub enum Statistics {
6    Fermionic,
7    Bosonic,
8}
9
10/// Statistics type trait for compile-time type-level distinction
11///
12/// # Safety
13/// Types implementing this trait must be zero-sized types (ZST) used only as
14/// compile-time markers. The `Default` bound ensures safe instantiation.
15pub trait StatisticsType: Copy + Default {
16    const STATISTICS: Statistics;
17}
18
19/// Fermionic statistics marker type
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
21pub struct Fermionic;
22
23impl StatisticsType for Fermionic {
24    const STATISTICS: Statistics = Statistics::Fermionic;
25}
26
27/// Bosonic statistics marker type
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
29pub struct Bosonic;
30
31impl StatisticsType for Bosonic {
32    const STATISTICS: Statistics = Statistics::Bosonic;
33}
34
35/// Trait for types that can represent statistics at runtime
36pub trait StatisticsMarker {
37    fn statistics() -> Statistics;
38}
39
40impl StatisticsMarker for Fermionic {
41    fn statistics() -> Statistics {
42        Statistics::Fermionic
43    }
44}
45
46impl StatisticsMarker for Bosonic {
47    fn statistics() -> Statistics {
48        Statistics::Bosonic
49    }
50}
51
52/// Utility functions for statistics
53impl Statistics {
54    /// Check if this statistics type is fermionic
55    pub fn is_fermionic(self) -> bool {
56        matches!(self, Statistics::Fermionic)
57    }
58
59    /// Check if this statistics type is bosonic
60    pub fn is_bosonic(self) -> bool {
61        matches!(self, Statistics::Bosonic)
62    }
63
64    /// Get the string representation of the statistics
65    pub fn as_str(self) -> &'static str {
66        match self {
67            Statistics::Fermionic => "fermionic",
68            Statistics::Bosonic => "bosonic",
69        }
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_statistics_type_trait() {
79        assert_eq!(Fermionic::STATISTICS, Statistics::Fermionic);
80        assert_eq!(Bosonic::STATISTICS, Statistics::Bosonic);
81    }
82
83    #[test]
84    fn test_statistics_marker_trait() {
85        assert_eq!(Fermionic::statistics(), Statistics::Fermionic);
86        assert_eq!(Bosonic::statistics(), Statistics::Bosonic);
87    }
88
89    #[test]
90    fn test_statistics_utility_methods() {
91        assert!(Statistics::Fermionic.is_fermionic());
92        assert!(!Statistics::Fermionic.is_bosonic());
93        assert!(!Statistics::Bosonic.is_fermionic());
94        assert!(Statistics::Bosonic.is_bosonic());
95
96        assert_eq!(Statistics::Fermionic.as_str(), "fermionic");
97        assert_eq!(Statistics::Bosonic.as_str(), "bosonic");
98    }
99}