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
11pub trait StatisticsType: Copy {
12    const STATISTICS: Statistics;
13}
14
15/// Fermionic statistics marker type
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct Fermionic;
18
19impl StatisticsType for Fermionic {
20    const STATISTICS: Statistics = Statistics::Fermionic;
21}
22
23/// Bosonic statistics marker type
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct Bosonic;
26
27impl StatisticsType for Bosonic {
28    const STATISTICS: Statistics = Statistics::Bosonic;
29}
30
31/// Trait for types that can represent statistics at runtime
32pub trait StatisticsMarker {
33    fn statistics() -> Statistics;
34}
35
36impl StatisticsMarker for Fermionic {
37    fn statistics() -> Statistics {
38        Statistics::Fermionic
39    }
40}
41
42impl StatisticsMarker for Bosonic {
43    fn statistics() -> Statistics {
44        Statistics::Bosonic
45    }
46}
47
48/// Utility functions for statistics
49impl Statistics {
50    /// Check if this statistics type is fermionic
51    pub fn is_fermionic(self) -> bool {
52        matches!(self, Statistics::Fermionic)
53    }
54
55    /// Check if this statistics type is bosonic
56    pub fn is_bosonic(self) -> bool {
57        matches!(self, Statistics::Bosonic)
58    }
59
60    /// Get the string representation of the statistics
61    pub fn as_str(self) -> &'static str {
62        match self {
63            Statistics::Fermionic => "fermionic",
64            Statistics::Bosonic => "bosonic",
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_statistics_type_trait() {
75        assert_eq!(Fermionic::STATISTICS, Statistics::Fermionic);
76        assert_eq!(Bosonic::STATISTICS, Statistics::Bosonic);
77    }
78
79    #[test]
80    fn test_statistics_marker_trait() {
81        assert_eq!(Fermionic::statistics(), Statistics::Fermionic);
82        assert_eq!(Bosonic::statistics(), Statistics::Bosonic);
83    }
84
85    #[test]
86    fn test_statistics_utility_methods() {
87        assert!(Statistics::Fermionic.is_fermionic());
88        assert!(!Statistics::Fermionic.is_bosonic());
89        assert!(!Statistics::Bosonic.is_fermionic());
90        assert!(Statistics::Bosonic.is_bosonic());
91
92        assert_eq!(Statistics::Fermionic.as_str(), "fermionic");
93        assert_eq!(Statistics::Bosonic.as_str(), "bosonic");
94    }
95}