bitgrep/types/
bit_type.rs

1use std::fmt::Display;
2use std::str::FromStr;
3
4use super::endian::{FromBigEndian, FromLittleEndian};
5
6// TODO(danilan): Migrate from own implementations to num crate float/num/int types
7
8/// A general marker trait that represents a type that bitgrep supports.
9/// Used as part of the generics black magic
10pub trait BitType:
11    num::Num + FromStr + Copy + PartialOrd + Display + FromLittleEndian + FromBigEndian
12{
13}
14
15impl BitType for f32 {}
16impl BitType for f64 {}
17
18impl BitType for i8 {}
19impl BitType for i16 {}
20impl BitType for i32 {}
21impl BitType for i64 {}
22impl BitType for i128 {}
23
24impl BitType for u8 {}
25impl BitType for u16 {}
26impl BitType for u32 {}
27impl BitType for u64 {}
28impl BitType for u128 {}
29
30pub trait Float: BitType + approx::UlpsEq {
31    fn is_nan(self) -> bool;
32    fn is_pos_infinity(self) -> bool;
33    fn is_neg_infinity(self) -> bool;
34}
35
36impl Float for f32 {
37    fn is_nan(self) -> bool {
38        return self.is_nan();
39    }
40
41    fn is_pos_infinity(self) -> bool {
42        return self.is_infinite() && self.is_sign_positive();
43    }
44
45    fn is_neg_infinity(self) -> bool {
46        return self.is_infinite() && self.is_sign_negative();
47    }
48}
49impl Float for f64 {
50    fn is_nan(self) -> bool {
51        return self.is_nan();
52    }
53
54    fn is_pos_infinity(self) -> bool {
55        return self.is_infinite() && self.is_sign_positive();
56    }
57
58    fn is_neg_infinity(self) -> bool {
59        return self.is_infinite() && self.is_sign_negative();
60    }
61}