Skip to main content

reifydb_value/value/number/safe/convert/
u8.rs

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 ReifyDB
3
4use super::*;
5
6impl_safe_convert_promote!(u8 => u16, u32, u64, u128);
7
8impl_safe_unsigned_convert!(u8 => i8, i16, i32, i64, i128);
9
10impl_safe_convert_unsigned_to_float!(24; u8 => f32);
11impl_safe_convert_unsigned_to_float!(53; u8 => f64);
12
13impl_safe_convert_to_int!(u8);
14impl_safe_convert_unsigned_to_uint!(u8);
15
16impl_safe_convert_to_decimal_from_int!(u8);
17
18#[cfg(test)]
19pub mod tests {
20	use super::SafeConvert;
21
22	mod i8 {
23		use super::*;
24
25		#[test]
26		fn test_checked_convert_happy() {
27			let x: u8 = 42;
28			let y: Option<i8> = x.checked_convert();
29			assert_eq!(y, Some(42i8));
30		}
31
32		#[test]
33		fn test_checked_convert_unhappy() {
34			let x: u8 = 200;
35			let y: Option<i8> = x.checked_convert();
36			assert_eq!(y, None);
37		}
38
39		#[test]
40		fn test_saturating_convert() {
41			let x: u8 = 200;
42			let y: i8 = x.saturating_convert();
43			assert_eq!(y, i8::MAX);
44		}
45
46		#[test]
47		fn test_wrapping_convert() {
48			let x: u8 = 200;
49			let y: i8 = x.wrapping_convert();
50			assert_eq!(y, -56i8);
51		}
52	}
53
54	mod i16 {
55		use super::*;
56
57		#[test]
58		fn test_checked_convert_happy() {
59			let x: u8 = 42;
60			let y: Option<i16> = x.checked_convert();
61			assert_eq!(y, Some(42i16));
62		}
63
64		#[test]
65		fn test_checked_convert_max() {
66			let x: u8 = u8::MAX;
67			let y: Option<i16> = x.checked_convert();
68			assert_eq!(y, Some(255i16));
69		}
70
71		#[test]
72		fn test_saturating_convert() {
73			let x: u8 = u8::MAX;
74			let y: i16 = x.saturating_convert();
75			assert_eq!(y, 255i16);
76		}
77
78		#[test]
79		fn test_wrapping_convert() {
80			let x: u8 = u8::MAX;
81			let y: i16 = x.wrapping_convert();
82			assert_eq!(y, 255i16);
83		}
84	}
85
86	mod i32 {
87		use super::*;
88
89		#[test]
90		fn test_checked_convert_happy() {
91			let x: u8 = 42;
92			let y: Option<i32> = x.checked_convert();
93			assert_eq!(y, Some(42i32));
94		}
95
96		#[test]
97		fn test_checked_convert_max() {
98			let x: u8 = u8::MAX;
99			let y: Option<i32> = x.checked_convert();
100			assert_eq!(y, Some(255i32));
101		}
102
103		#[test]
104		fn test_saturating_convert() {
105			let x: u8 = u8::MAX;
106			let y: i32 = x.saturating_convert();
107			assert_eq!(y, 255i32);
108		}
109
110		#[test]
111		fn test_wrapping_convert() {
112			let x: u8 = u8::MAX;
113			let y: i32 = x.wrapping_convert();
114			assert_eq!(y, 255i32);
115		}
116	}
117
118	mod i64 {
119		use super::*;
120
121		#[test]
122		fn test_checked_convert_happy() {
123			let x: u8 = 42;
124			let y: Option<i64> = x.checked_convert();
125			assert_eq!(y, Some(42i64));
126		}
127
128		#[test]
129		fn test_checked_convert_max() {
130			let x: u8 = u8::MAX;
131			let y: Option<i64> = x.checked_convert();
132			assert_eq!(y, Some(255i64));
133		}
134
135		#[test]
136		fn test_saturating_convert() {
137			let x: u8 = u8::MAX;
138			let y: i64 = x.saturating_convert();
139			assert_eq!(y, 255i64);
140		}
141
142		#[test]
143		fn test_wrapping_convert() {
144			let x: u8 = u8::MAX;
145			let y: i64 = x.wrapping_convert();
146			assert_eq!(y, 255i64);
147		}
148	}
149
150	mod i128 {
151		use super::*;
152
153		#[test]
154		fn test_checked_convert_happy() {
155			let x: u8 = 42;
156			let y: Option<i128> = x.checked_convert();
157			assert_eq!(y, Some(42i128));
158		}
159
160		#[test]
161		fn test_checked_convert_max() {
162			let x: u8 = u8::MAX;
163			let y: Option<i128> = x.checked_convert();
164			assert_eq!(y, Some(255i128));
165		}
166
167		#[test]
168		fn test_saturating_convert() {
169			let x: u8 = u8::MAX;
170			let y: i128 = x.saturating_convert();
171			assert_eq!(y, 255i128);
172		}
173
174		#[test]
175		fn test_wrapping_convert() {
176			let x: u8 = u8::MAX;
177			let y: i128 = x.wrapping_convert();
178			assert_eq!(y, 255i128);
179		}
180	}
181
182	mod f32 {
183		use super::*;
184
185		#[test]
186		fn test_checked_convert() {
187			let x: u8 = 42;
188			let y: Option<f32> = x.checked_convert();
189			assert_eq!(y, Some(42.0f32));
190		}
191
192		#[test]
193		fn test_saturating_convert() {
194			let x: u8 = 100;
195			let y: f32 = x.saturating_convert();
196			assert_eq!(y, 100.0f32);
197		}
198
199		#[test]
200		fn test_wrapping_convert() {
201			let x: u8 = u8::MAX;
202			let y: f32 = x.wrapping_convert();
203			assert_eq!(y, 255.0f32);
204		}
205	}
206
207	mod f64 {
208		use super::*;
209
210		#[test]
211		fn test_checked_convert() {
212			let x: u8 = 42;
213			let y: Option<f64> = x.checked_convert();
214			assert_eq!(y, Some(42.0f64));
215		}
216
217		#[test]
218		fn test_saturating_convert() {
219			let x: u8 = 100;
220			let y: f64 = x.saturating_convert();
221			assert_eq!(y, 100.0f64);
222		}
223
224		#[test]
225		fn test_wrapping_convert() {
226			let x: u8 = u8::MAX;
227			let y: f64 = x.wrapping_convert();
228			assert_eq!(y, 255.0f64);
229		}
230	}
231
232	mod decimal {
233		use super::*;
234		use crate::value::decimal::Decimal;
235
236		#[test]
237		fn test_checked_convert() {
238			let x: u8 = 42;
239			let y: Option<Decimal> = x.checked_convert();
240			assert!(y.is_some());
241			let decimal = y.unwrap();
242			assert_eq!(decimal.to_string(), "42");
243		}
244
245		#[test]
246		fn test_checked_convert_zero() {
247			let x: u8 = 0;
248			let y: Option<Decimal> = x.checked_convert();
249			assert!(y.is_some());
250			let decimal = y.unwrap();
251			assert_eq!(decimal.to_string(), "0");
252		}
253
254		#[test]
255		fn test_checked_convert_max() {
256			let x: u8 = u8::MAX;
257			let y: Option<Decimal> = x.checked_convert();
258			assert!(y.is_some());
259			let decimal = y.unwrap();
260			assert_eq!(decimal.to_string(), "255");
261		}
262
263		#[test]
264		fn test_saturating_convert() {
265			let x: u8 = 100;
266			let y: Decimal = x.saturating_convert();
267			assert_eq!(y.to_string(), "100");
268		}
269
270		#[test]
271		fn test_wrapping_convert() {
272			let x: u8 = 99;
273			let y: Decimal = x.wrapping_convert();
274			assert_eq!(y.to_string(), "99");
275		}
276	}
277}