Skip to main content

predsync_core/
slot.rs

1//! `Slot` is the uniform scalar representation at the core API boundary.
2
3/// One scalar value: unsigned integer, sign-extended signed integer, float bits or 0/1.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub struct Slot(pub u64);
6
7/// The scalar types that can be carried in a `Slot`.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum ScalarType {
10    Bool,
11    U8,
12    U16,
13    U32,
14    U64,
15    I8,
16    I16,
17    I32,
18    I64,
19    F32,
20    F64,
21}
22
23impl Slot {
24    pub fn from_bool(value: bool) -> Self {
25        Self(u64::from(value))
26    }
27
28    pub fn from_u8(value: u8) -> Self {
29        Self(u64::from(value))
30    }
31
32    pub fn from_u16(value: u16) -> Self {
33        Self(u64::from(value))
34    }
35
36    pub fn from_u32(value: u32) -> Self {
37        Self(u64::from(value))
38    }
39
40    pub fn from_u64(value: u64) -> Self {
41        Self(value)
42    }
43
44    pub fn from_i8(value: i8) -> Self {
45        Self(i64::from(value) as u64)
46    }
47
48    pub fn from_i16(value: i16) -> Self {
49        Self(i64::from(value) as u64)
50    }
51
52    pub fn from_i32(value: i32) -> Self {
53        Self(i64::from(value) as u64)
54    }
55
56    pub fn from_i64(value: i64) -> Self {
57        Self(value as u64)
58    }
59
60    pub fn from_f32(value: f32) -> Self {
61        Self(u64::from(value.to_bits()))
62    }
63
64    pub fn from_f64(value: f64) -> Self {
65        Self(value.to_bits())
66    }
67
68    pub fn as_bool(self) -> bool {
69        self.0 != 0
70    }
71
72    pub fn as_u8(self) -> u8 {
73        self.0 as u8
74    }
75
76    pub fn as_u16(self) -> u16 {
77        self.0 as u16
78    }
79
80    pub fn as_u32(self) -> u32 {
81        self.0 as u32
82    }
83
84    pub fn as_u64(self) -> u64 {
85        self.0
86    }
87
88    pub fn as_i8(self) -> i8 {
89        self.0 as i64 as i8
90    }
91
92    pub fn as_i16(self) -> i16 {
93        self.0 as i64 as i16
94    }
95
96    pub fn as_i32(self) -> i32 {
97        self.0 as i64 as i32
98    }
99
100    pub fn as_i64(self) -> i64 {
101        self.0 as i64
102    }
103
104    pub fn as_f32(self) -> f32 {
105        f32::from_bits(self.0 as u32)
106    }
107
108    pub fn as_f64(self) -> f64 {
109        f64::from_bits(self.0)
110    }
111
112    /// The predictor input representation defined in the protocol.
113    pub fn to_f64(self, ty: ScalarType) -> f64 {
114        match ty {
115            ScalarType::Bool => f64::from(u8::from(self.as_bool())),
116            ScalarType::U8 => f64::from(self.as_u8()),
117            ScalarType::U16 => f64::from(self.as_u16()),
118            ScalarType::U32 => f64::from(self.as_u32()),
119            ScalarType::U64 => self.as_u64() as f64,
120            ScalarType::I8 => f64::from(self.as_i8()),
121            ScalarType::I16 => f64::from(self.as_i16()),
122            ScalarType::I32 => f64::from(self.as_i32()),
123            ScalarType::I64 => self.as_i64() as f64,
124            ScalarType::F32 => f64::from(self.as_f32()),
125            ScalarType::F64 => self.as_f64(),
126        }
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133
134    #[test]
135    fn slot_roundtrip_every_type() {
136        assert!(!Slot::from_bool(false).as_bool());
137        assert!(Slot::from_bool(true).as_bool());
138        assert_eq!(Slot::from_bool(true).to_f64(ScalarType::Bool), 1.0);
139        assert_eq!(Slot::from_bool(false).to_f64(ScalarType::Bool), 0.0);
140
141        macro_rules! check_int {
142            ($from:ident, $get:ident, $ty:expr, $values:expr) => {
143                for value in $values {
144                    let slot = Slot::$from(value);
145                    assert_eq!(slot.$get(), value);
146                    assert_eq!(slot.to_f64($ty), value as f64);
147                }
148            };
149        }
150        check_int!(from_u8, as_u8, ScalarType::U8, [0u8, 1, 127, 255]);
151        check_int!(from_u16, as_u16, ScalarType::U16, [0u16, 1, 32767, 65535]);
152        check_int!(from_u32, as_u32, ScalarType::U32, [0u32, 1, u32::MAX]);
153        check_int!(from_u64, as_u64, ScalarType::U64, [0u64, 1, u64::MAX]);
154        check_int!(from_i8, as_i8, ScalarType::I8, [-128i8, -1, 0, 127]);
155        check_int!(from_i16, as_i16, ScalarType::I16, [-32768i16, -1, 0, 32767]);
156        check_int!(
157            from_i32,
158            as_i32,
159            ScalarType::I32,
160            [i32::MIN, -1, 0, i32::MAX]
161        );
162        check_int!(
163            from_i64,
164            as_i64,
165            ScalarType::I64,
166            [i64::MIN, -1, 0, i64::MAX]
167        );
168
169        assert_eq!(Slot::from_i8(-1).0, u64::MAX);
170        assert_eq!(Slot::from_i64(-2).0, (-2i64) as u64);
171
172        for value in [0.0f32, -0.0, 1.5, -2.25, f32::MIN, f32::MAX] {
173            let slot = Slot::from_f32(value);
174            assert_eq!(slot.as_f32().to_bits(), value.to_bits());
175            assert_eq!(slot.to_f64(ScalarType::F32), value as f64);
176        }
177        for value in [0.0f64, -0.0, 1.5, -2.25, f64::MIN, f64::MAX] {
178            let slot = Slot::from_f64(value);
179            assert_eq!(slot.as_f64().to_bits(), value.to_bits());
180            assert_eq!(slot.to_f64(ScalarType::F64), value);
181        }
182    }
183}