Skip to main content

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

1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 ReifyDB
3
4use super::*;
5
6impl_safe_convert_demote!(i32 => i8, i16);
7impl_safe_convert_promote!(i32 => i64, i128);
8
9impl_safe_convert!(i32 => u8, u16, u32, u64, u128);
10
11impl_safe_convert_signed_to_float!(24; i32 => f32);
12impl_safe_convert_signed_to_float!(53; i32 => f64);
13
14impl_safe_convert_to_int!(i32);
15impl SafeConvert<Uint> for i32 {
16	fn checked_convert(self) -> Option<Uint> {
17		if self >= 0 {
18			Some(Uint(BigInt::from(self)))
19		} else {
20			None
21		}
22	}
23
24	fn saturating_convert(self) -> Uint {
25		if self >= 0 {
26			Uint(BigInt::from(self))
27		} else {
28			Uint::zero()
29		}
30	}
31
32	fn wrapping_convert(self) -> Uint {
33		Uint(BigInt::from(self as u32))
34	}
35}
36
37impl_safe_convert_to_decimal_from_int!(i32);
38
39#[cfg(test)]
40pub mod tests {
41	use super::SafeConvert;
42
43	mod i8 {
44		use super::*;
45
46		#[test]
47		fn test_checked_convert_happy() {
48			let x: i32 = 127;
49			let y: Option<i8> = x.checked_convert();
50			assert_eq!(y, Some(127i8));
51		}
52
53		#[test]
54		fn test_checked_convert_unhappy() {
55			let x: i32 = 128;
56			let y: Option<i8> = x.checked_convert();
57			assert_eq!(y, None);
58		}
59
60		#[test]
61		fn test_saturating_convert_min() {
62			let x: i32 = -129;
63			let y: i8 = x.saturating_convert();
64			assert_eq!(y, i8::MIN);
65		}
66
67		#[test]
68		fn test_saturating_convert_max() {
69			let x: i32 = 128;
70			let y: i8 = x.saturating_convert();
71			assert_eq!(y, i8::MAX);
72		}
73
74		#[test]
75		fn test_wrapping_convert() {
76			let x: i32 = 128;
77			let y: i8 = x.wrapping_convert();
78			assert_eq!(y, -128);
79		}
80	}
81
82	mod i16 {
83		use super::*;
84
85		#[test]
86		fn test_checked_convert_happy() {
87			let x: i32 = 32767;
88			let y: Option<i16> = x.checked_convert();
89			assert_eq!(y, Some(32767i16));
90		}
91
92		#[test]
93		fn test_checked_convert_unhappy() {
94			let x: i32 = 32768;
95			let y: Option<i16> = x.checked_convert();
96			assert_eq!(y, None);
97		}
98
99		#[test]
100		fn test_saturating_convert_min() {
101			let x: i32 = -32769;
102			let y: i16 = x.saturating_convert();
103			assert_eq!(y, i16::MIN);
104		}
105
106		#[test]
107		fn test_saturating_convert_max() {
108			let x: i32 = 32768;
109			let y: i16 = x.saturating_convert();
110			assert_eq!(y, i16::MAX);
111		}
112
113		#[test]
114		fn test_wrapping_convert() {
115			let x: i32 = 32768;
116			let y: i16 = x.wrapping_convert();
117			assert_eq!(y, -32768);
118		}
119	}
120
121	mod i64 {
122		use super::*;
123
124		#[test]
125		fn test_checked_convert() {
126			let x: i32 = -2147483648;
127			let y: Option<i64> = x.checked_convert();
128			assert_eq!(y, Some(-2147483648i64));
129		}
130
131		#[test]
132		fn test_saturating_convert() {
133			let x: i32 = 2147483647;
134			let y: i64 = x.saturating_convert();
135			assert_eq!(y, 2147483647i64);
136		}
137
138		#[test]
139		fn test_wrapping_convert() {
140			let x: i32 = -1;
141			let y: i64 = x.wrapping_convert();
142			assert_eq!(y, -1i64);
143		}
144	}
145
146	mod i128 {
147		use super::*;
148
149		#[test]
150		fn test_checked_convert() {
151			let x: i32 = -2147483648;
152			let y: Option<i128> = x.checked_convert();
153			assert_eq!(y, Some(-2147483648i128));
154		}
155
156		#[test]
157		fn test_saturating_convert() {
158			let x: i32 = 2147483647;
159			let y: i128 = x.saturating_convert();
160			assert_eq!(y, 2147483647i128);
161		}
162
163		#[test]
164		fn test_wrapping_convert() {
165			let x: i32 = -1;
166			let y: i128 = x.wrapping_convert();
167			assert_eq!(y, -1i128);
168		}
169	}
170
171	mod u8 {
172		use crate::value::number::safe::convert::SafeConvert;
173
174		#[test]
175		fn test_checked_convert_happy() {
176			let x: i32 = 42;
177			let y: Option<u8> = SafeConvert::checked_convert(x);
178			assert_eq!(y, Some(42u8));
179		}
180
181		#[test]
182		fn test_checked_convert_unhappy() {
183			let x: i32 = -1;
184			let y: Option<u8> = SafeConvert::checked_convert(x);
185			assert_eq!(y, None);
186		}
187
188		#[test]
189		fn test_saturating_convert() {
190			let x: i32 = -1;
191			let y: u8 = SafeConvert::saturating_convert(x);
192			assert_eq!(y, 0u8);
193		}
194
195		#[test]
196		fn test_wrapping_convert() {
197			let x: i32 = -1;
198			let y: u8 = x.wrapping_convert();
199			assert_eq!(y, 255u8);
200		}
201	}
202	mod u16 {
203		use crate::value::number::safe::convert::SafeConvert;
204
205		#[test]
206		fn test_checked_convert_happy() {
207			let x: i32 = 42;
208			let y: Option<u16> = x.checked_convert();
209			assert_eq!(y, Some(42u16));
210		}
211
212		#[test]
213		fn test_checked_convert_unhappy() {
214			let x: i32 = -1;
215			let y: Option<u16> = x.checked_convert();
216			assert_eq!(y, None);
217		}
218
219		#[test]
220		fn test_saturating_convert() {
221			let x: i32 = -1;
222			let y: u16 = x.saturating_convert();
223			assert_eq!(y, 0u16);
224		}
225
226		#[test]
227		fn test_wrapping_convert() {
228			let x: i32 = -1;
229			let y: u16 = x.wrapping_convert();
230			assert_eq!(y, 65535u16);
231		}
232	}
233	mod u32 {
234		use crate::value::number::safe::convert::SafeConvert;
235
236		#[test]
237		fn test_checked_convert_happy() {
238			let x: i32 = 42;
239			let y: Option<u32> = x.checked_convert();
240			assert_eq!(y, Some(42u32));
241		}
242
243		#[test]
244		fn test_checked_convert_unhappy() {
245			let x: i32 = -1;
246			let y: Option<u32> = x.checked_convert();
247			assert_eq!(y, None);
248		}
249
250		#[test]
251		fn test_saturating_convert() {
252			let x: i32 = -1;
253			let y: u32 = x.saturating_convert();
254			assert_eq!(y, 0u32);
255		}
256
257		#[test]
258		fn test_wrapping_convert() {
259			let x: i32 = -1;
260			let y: u32 = x.wrapping_convert();
261			assert_eq!(y, 4294967295u32);
262		}
263	}
264	mod u64 {
265		use crate::value::number::safe::convert::SafeConvert;
266
267		#[test]
268		fn test_checked_convert_happy() {
269			let x: i32 = 42;
270			let y: Option<u64> = x.checked_convert();
271			assert_eq!(y, Some(42u64));
272		}
273
274		#[test]
275		fn test_checked_convert_unhappy() {
276			let x: i32 = -1;
277			let y: Option<u64> = x.checked_convert();
278			assert_eq!(y, None);
279		}
280
281		#[test]
282		fn test_saturating_convert() {
283			let x: i32 = -1;
284			let y: u64 = x.saturating_convert();
285			assert_eq!(y, 0u64);
286		}
287
288		#[test]
289		fn test_wrapping_convert() {
290			let x: i32 = -1;
291			let y: u64 = x.wrapping_convert();
292			assert_eq!(y, 18446744073709551615u64);
293		}
294	}
295	mod u128 {
296		use crate::value::number::safe::convert::SafeConvert;
297
298		#[test]
299		fn test_checked_convert_happy() {
300			let x: i32 = 42;
301			let y: Option<u128> = x.checked_convert();
302			assert_eq!(y, Some(42u128));
303		}
304
305		#[test]
306		fn test_checked_convert_unhappy() {
307			let x: i32 = -1;
308			let y: Option<u128> = x.checked_convert();
309			assert_eq!(y, None);
310		}
311
312		#[test]
313		fn test_saturating_convert() {
314			let x: i32 = -1;
315			let y: u128 = x.saturating_convert();
316			assert_eq!(y, 0u128);
317		}
318
319		#[test]
320		fn test_wrapping_convert() {
321			let x: i32 = -1;
322			let y: u128 = x.wrapping_convert();
323			assert_eq!(y, 340282366920938463463374607431768211455u128);
324		}
325	}
326	mod f32 {
327		use crate::value::number::safe::convert::SafeConvert;
328
329		#[test]
330		fn test_checked_convert() {
331			let x: i32 = 42;
332			let y: Option<f32> = x.checked_convert();
333			assert_eq!(y, Some(42.0f32));
334		}
335
336		#[test]
337		fn test_saturating_convert() {
338			let x: i32 = 100;
339			let y: f32 = x.saturating_convert();
340			assert_eq!(y, 100.0f32);
341		}
342
343		#[test]
344		fn test_wrapping_convert() {
345			let x: i32 = -1;
346			let y: f32 = x.wrapping_convert();
347			assert_eq!(y, -1.0f32);
348		}
349	}
350	mod f64 {
351		use crate::value::number::safe::convert::SafeConvert;
352
353		#[test]
354		fn test_checked_convert() {
355			let x: i32 = 42;
356			let y: Option<f64> = x.checked_convert();
357			assert_eq!(y, Some(42.0f64));
358		}
359
360		#[test]
361		fn test_saturating_convert() {
362			let x: i32 = 100;
363			let y: f64 = x.saturating_convert();
364			assert_eq!(y, 100.0f64);
365		}
366
367		#[test]
368		fn test_wrapping_convert() {
369			let x: i32 = -1;
370			let y: f64 = x.wrapping_convert();
371			assert_eq!(y, -1.0f64);
372		}
373	}
374
375	mod decimal {
376		use super::*;
377		use crate::value::decimal::Decimal;
378
379		#[test]
380		fn test_checked_convert() {
381			let x: i32 = 42;
382			let y: Option<Decimal> = x.checked_convert();
383			assert!(y.is_some());
384			let decimal = y.unwrap();
385			assert_eq!(decimal.to_string(), "42");
386		}
387
388		#[test]
389		fn test_checked_convert_zero() {
390			let x: i32 = 0;
391			let y: Option<Decimal> = x.checked_convert();
392			assert!(y.is_some());
393			let decimal = y.unwrap();
394			assert_eq!(decimal.to_string(), "0");
395		}
396
397		#[test]
398		fn test_checked_convert_negative() {
399			let x: i32 = -999;
400			let y: Option<Decimal> = x.checked_convert();
401			assert!(y.is_some());
402			let decimal = y.unwrap();
403			assert_eq!(decimal.to_string(), "-999");
404		}
405
406		#[test]
407		fn test_saturating_convert() {
408			let x: i32 = -2147483648;
409			let y: Decimal = x.saturating_convert();
410			assert_eq!(y.to_string(), "-2147483648");
411		}
412
413		#[test]
414		fn test_wrapping_convert() {
415			let x: i32 = 2147483647;
416			let y: Decimal = x.wrapping_convert();
417			assert_eq!(y.to_string(), "2147483647");
418		}
419	}
420
421	mod int {
422		use super::*;
423		use crate::value::int::Int;
424
425		#[test]
426		fn test_checked_convert() {
427			let x: i32 = -2147483648;
428			let y: Option<Int> = x.checked_convert();
429			assert!(y.is_some());
430			assert_eq!(y.unwrap().to_string(), "-2147483648");
431		}
432
433		#[test]
434		fn test_saturating_convert() {
435			let x: i32 = 2147483647;
436			let y: Int = x.saturating_convert();
437			assert_eq!(y.to_string(), "2147483647");
438		}
439
440		#[test]
441		fn test_wrapping_convert() {
442			let x: i32 = -1;
443			let y: Int = x.wrapping_convert();
444			assert_eq!(y.to_string(), "-1");
445		}
446	}
447
448	mod uint {
449		use super::*;
450		use crate::value::uint::Uint;
451
452		#[test]
453		fn test_checked_convert_positive() {
454			let x: i32 = 42;
455			let y: Option<Uint> = x.checked_convert();
456			assert!(y.is_some());
457			assert_eq!(y.unwrap().to_string(), "42");
458		}
459
460		#[test]
461		fn test_checked_convert_negative() {
462			let x: i32 = -1;
463			let y: Option<Uint> = x.checked_convert();
464			assert!(y.is_none());
465		}
466
467		#[test]
468		fn test_saturating_convert() {
469			let x: i32 = -1;
470			let y: Uint = x.saturating_convert();
471			assert_eq!(y.to_string(), "0");
472		}
473
474		#[test]
475		fn test_wrapping_convert() {
476			let x: i32 = -1;
477			let y: Uint = x.wrapping_convert();
478			assert_eq!(y.to_string(), "4294967295");
479		}
480	}
481}