Skip to main content

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

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