reifydb_type/value/int/
mod.rs1use std::{
5 cmp::Ordering,
6 fmt::{Display, Formatter},
7 hash::{Hash, Hasher},
8 ops::Deref,
9};
10
11use num_bigint::BigInt as StdBigInt;
12use serde::{Deserialize, Serialize};
13
14pub mod parse;
15pub use parse::parse_int;
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<'_>) -> std::fmt::Result {
150 write!(f, "{}", self.0)
151 }
152}
153
154#[cfg(test)]
155mod tests {
156 use super::*;
157
158 #[test]
159 fn test_int_create() {
160 let int = Int::from_i64(42);
161 assert_eq!(format!("{}", int), "42");
162 }
163
164 #[test]
165 fn test_int_equality() {
166 let a = Int::from_i64(100);
167 let b = Int::from_i64(100);
168 let c = Int::from_i64(200);
169
170 assert_eq!(a, b);
171 assert_ne!(a, c);
172 }
173
174 #[test]
175 fn test_int_ordering() {
176 let a = Int::from_i64(10);
177 let b = Int::from_i64(20);
178 let c = Int::from_i64(20);
179
180 assert!(a < b);
181 assert!(b > a);
182 assert_eq!(b.cmp(&c), Ordering::Equal);
183 }
184
185 #[test]
186 fn test_int_large_values() {
187 let large = Int::from_i128(i128::MAX);
188 let larger = Int::from(StdBigInt::from(i128::MAX) + 1);
189
190 assert!(large < larger);
191 }
192
193 #[test]
194 fn test_int_display() {
195 let int = Int::from_i64(-12345);
196 assert_eq!(format!("{}", int), "-12345");
197 }
198
199 #[test]
200 fn test_int_hash() {
201 use std::collections::HashSet;
202
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}