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