cranelift_codegen_meta/shared/
types.rs

1//! This module predefines all the Cranelift scalar types.
2
3#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
4pub(crate) enum Int {
5    /// 8-bit int.
6    I8 = 8,
7    /// 16-bit int.
8    I16 = 16,
9    /// 32-bit int.
10    I32 = 32,
11    /// 64-bit int.
12    I64 = 64,
13    /// 128-bit int.
14    I128 = 128,
15}
16
17/// This provides an iterator through all of the supported int variants.
18pub(crate) struct IntIterator {
19    index: u8,
20}
21
22impl IntIterator {
23    pub fn new() -> Self {
24        Self { index: 0 }
25    }
26}
27
28impl Iterator for IntIterator {
29    type Item = Int;
30    fn next(&mut self) -> Option<Self::Item> {
31        let res = match self.index {
32            0 => Some(Int::I8),
33            1 => Some(Int::I16),
34            2 => Some(Int::I32),
35            3 => Some(Int::I64),
36            4 => Some(Int::I128),
37            _ => return None,
38        };
39        self.index += 1;
40        res
41    }
42}
43
44#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
45pub(crate) enum Float {
46    F16 = 16,
47    F32 = 32,
48    F64 = 64,
49    F128 = 128,
50}
51
52/// Iterator through the variants of the Float enum.
53pub(crate) struct FloatIterator {
54    index: u8,
55}
56
57impl FloatIterator {
58    pub fn new() -> Self {
59        Self { index: 0 }
60    }
61}
62
63/// This provides an iterator through all of the supported float variants.
64impl Iterator for FloatIterator {
65    type Item = Float;
66    fn next(&mut self) -> Option<Self::Item> {
67        let res = match self.index {
68            0 => Some(Float::F16),
69            1 => Some(Float::F32),
70            2 => Some(Float::F64),
71            3 => Some(Float::F128),
72            _ => return None,
73        };
74        self.index += 1;
75        res
76    }
77}
78
79#[cfg(test)]
80mod iter_tests {
81    use super::*;
82
83    #[test]
84    fn int_iter_works() {
85        let mut int_iter = IntIterator::new();
86        assert_eq!(int_iter.next(), Some(Int::I8));
87        assert_eq!(int_iter.next(), Some(Int::I16));
88        assert_eq!(int_iter.next(), Some(Int::I32));
89        assert_eq!(int_iter.next(), Some(Int::I64));
90        assert_eq!(int_iter.next(), Some(Int::I128));
91        assert_eq!(int_iter.next(), None);
92    }
93
94    #[test]
95    fn float_iter_works() {
96        let mut float_iter = FloatIterator::new();
97        assert_eq!(float_iter.next(), Some(Float::F16));
98        assert_eq!(float_iter.next(), Some(Float::F32));
99        assert_eq!(float_iter.next(), Some(Float::F64));
100        assert_eq!(float_iter.next(), Some(Float::F128));
101        assert_eq!(float_iter.next(), None);
102    }
103}