Skip to main content

reifydb_type/value/int/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use std::{
5	cmp::Ordering,
6	fmt,
7	fmt::{Display, Formatter},
8	hash::{Hash, Hasher},
9	ops::Deref,
10};
11
12use num_bigint::BigInt as StdBigInt;
13use serde::{Deserialize, Serialize};
14
15pub mod parse;
16
17#[repr(transparent)]
18#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
19pub struct Int(pub StdBigInt);
20
21impl Int {
22	pub fn from_i64(value: i64) -> Self {
23		Int(StdBigInt::from(value))
24	}
25
26	pub fn from_i128(value: i128) -> Self {
27		Int(StdBigInt::from(value))
28	}
29
30	pub fn zero() -> Self {
31		Int(StdBigInt::from(0))
32	}
33
34	pub fn one() -> Self {
35		Int(StdBigInt::from(1))
36	}
37}
38
39impl Default for Int {
40	fn default() -> Self {
41		Self::zero()
42	}
43}
44
45impl Deref for Int {
46	type Target = StdBigInt;
47
48	fn deref(&self) -> &Self::Target {
49		&self.0
50	}
51}
52
53impl Hash for Int {
54	fn hash<H: Hasher>(&self, state: &mut H) {
55		self.0.hash(state);
56	}
57}
58
59impl PartialOrd for Int {
60	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
61		Some(self.cmp(other))
62	}
63}
64
65impl Ord for Int {
66	fn cmp(&self, other: &Self) -> Ordering {
67		self.0.cmp(&other.0)
68	}
69}
70
71impl From<i8> for Int {
72	fn from(value: i8) -> Self {
73		Int(StdBigInt::from(value))
74	}
75}
76
77impl From<i16> for Int {
78	fn from(value: i16) -> Self {
79		Int(StdBigInt::from(value))
80	}
81}
82
83impl From<i32> for Int {
84	fn from(value: i32) -> Self {
85		Int(StdBigInt::from(value))
86	}
87}
88
89impl From<i64> for Int {
90	fn from(value: i64) -> Self {
91		Int(StdBigInt::from(value))
92	}
93}
94
95impl From<i128> for Int {
96	fn from(value: i128) -> Self {
97		Int(StdBigInt::from(value))
98	}
99}
100
101impl From<u8> for Int {
102	fn from(value: u8) -> Self {
103		Int(StdBigInt::from(value))
104	}
105}
106
107impl From<u16> for Int {
108	fn from(value: u16) -> Self {
109		Int(StdBigInt::from(value))
110	}
111}
112
113impl From<u32> for Int {
114	fn from(value: u32) -> Self {
115		Int(StdBigInt::from(value))
116	}
117}
118
119impl From<u64> for Int {
120	fn from(value: u64) -> Self {
121		Int(StdBigInt::from(value))
122	}
123}
124
125impl From<u128> for Int {
126	fn from(value: u128) -> Self {
127		Int(StdBigInt::from(value))
128	}
129}
130
131impl From<StdBigInt> for Int {
132	fn from(value: StdBigInt) -> Self {
133		Int(value)
134	}
135}
136
137impl From<Int> for StdBigInt {
138	fn from(int: Int) -> Self {
139		int.0
140	}
141}
142
143impl Display for Int {
144	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
145		write!(f, "{}", self.0)
146	}
147}
148
149#[cfg(test)]
150pub mod tests {
151	use std::collections::HashSet;
152
153	use super::*;
154
155	#[test]
156	fn test_int_create() {
157		let int = Int::from_i64(42);
158		assert_eq!(format!("{}", int), "42");
159	}
160
161	#[test]
162	fn test_int_equality() {
163		let a = Int::from_i64(100);
164		let b = Int::from_i64(100);
165		let c = Int::from_i64(200);
166
167		assert_eq!(a, b);
168		assert_ne!(a, c);
169	}
170
171	#[test]
172	fn test_int_ordering() {
173		let a = Int::from_i64(10);
174		let b = Int::from_i64(20);
175		let c = Int::from_i64(20);
176
177		assert!(a < b);
178		assert!(b > a);
179		assert_eq!(b.cmp(&c), Ordering::Equal);
180	}
181
182	#[test]
183	fn test_int_large_values() {
184		let large = Int::from_i128(i128::MAX);
185		let larger = Int::from(StdBigInt::from(i128::MAX) + 1);
186
187		assert!(large < larger);
188	}
189
190	#[test]
191	fn test_int_display() {
192		let int = Int::from_i64(-12345);
193		assert_eq!(format!("{}", int), "-12345");
194	}
195
196	#[test]
197	fn test_int_hash() {
198		let a = Int::from_i64(42);
199		let b = Int::from_i64(42);
200
201		let mut set = HashSet::new();
202		set.insert(a);
203		assert!(set.contains(&b));
204	}
205}