intfinity/
intfinity.rs

1use crate::traits::{Zero, Negate, Unsigned};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
4pub enum int<T> {
5    neginf,
6    finite(T),
7    posinf,
8}
9
10impl<T> int<T>
11where
12    T: Copy,
13{
14    pub fn new(value: T) -> Self {
15        int::finite(value)
16    }
17}
18
19impl<T> int<T>
20where
21    T: Zero + PartialEq,
22{
23    pub fn is_zero(&self) -> bool {
24        match self {
25            int::finite(val) => val.is_zero(),
26            _ => false,
27        }
28    }
29}
30
31impl<T> int<T>
32where
33    T: Copy + Negate,
34{
35    pub fn negate(self) -> Self {
36        match self {
37            int::finite(value) => int::finite(value.negate()), 
38            int::posinf => int::neginf,  
39            int::neginf => int::posinf,  
40        }
41    }
42}
43
44impl<T> core::fmt::Display for int<T>
45where
46    T: core::fmt::Display,
47{
48    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49        match self {
50            int::finite(val) => write!(f, "{}", val),
51            int::posinf => write!(f, "+infinity"),
52            int::neginf => write!(f, "-infinity"),
53        }
54    }
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
58pub enum uint<T: Unsigned> {
59    finite(T),
60    inf,
61}
62
63impl<T> uint<T>
64where
65    T: Copy + Unsigned,
66{
67    pub fn new(value: T) -> Self {
68        uint::finite(value)
69    }
70}
71
72impl<T> uint<T>
73where
74    T: Zero + PartialEq + Unsigned,
75{
76    pub fn is_zero(&self) -> bool {
77        match self {
78            uint::finite(val) => val.is_zero(),
79            _ => false,
80        }
81    }
82}
83
84impl<T> core::fmt::Display for uint<T>
85where
86    T: core::fmt::Display + Unsigned,
87{
88    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
89        match self {
90            uint::finite(val) => write!(f, "{}", val),
91            uint::inf => write!(f, "infinity"),
92        }
93    }
94}