xrpl_common_stdlib/types/
uint.rs1use crate::fields::decoder::{FieldDecoder, FromCurrentTx, FromLedger, decode_exact};
4use crate::types::decode_error::DecodeError;
5
6#[repr(C)]
23#[derive(Debug, Clone, PartialEq, Eq)]
24pub struct UInt<const N: usize>(pub [u8; N]);
25
26impl<const N: usize> From<[u8; N]> for UInt<N> {
27 fn from(bytes: [u8; N]) -> Self {
28 Self(bytes)
29 }
30}
31
32impl<const N: usize> UInt<N> {
33 pub fn as_bytes(&self) -> &[u8; N] {
35 &self.0
36 }
37}
38
39pub const UINT128_SIZE: usize = 16;
41pub const UINT160_SIZE: usize = 20;
42pub const UINT192_SIZE: usize = 24;
43pub const UINT256_SIZE: usize = 32;
44
45pub const HASH128_SIZE: usize = UINT128_SIZE;
47pub const HASH160_SIZE: usize = UINT160_SIZE;
48pub const HASH192_SIZE: usize = UINT192_SIZE;
49pub const HASH256_SIZE: usize = UINT256_SIZE;
50
51pub type UInt128 = UInt<UINT128_SIZE>;
53pub type UInt160 = UInt<UINT160_SIZE>;
54pub type UInt192 = UInt<UINT192_SIZE>;
55pub type UInt256 = UInt<UINT256_SIZE>;
56
57pub type Hash128 = UInt128;
59pub type Hash160 = UInt160;
60pub type Hash192 = UInt192;
61pub type Hash256 = UInt256;
62
63impl<const N: usize> FieldDecoder for UInt<N> {
67 type Buffer = [u8; N];
68
69 #[inline]
70 fn empty_buffer() -> Self::Buffer {
71 [0u8; N]
72 }
73
74 #[inline]
75 fn decode(buf: Self::Buffer, bytes_written: usize) -> core::result::Result<Self, DecodeError> {
76 decode_exact(buf, bytes_written)
77 }
78}
79
80impl FromCurrentTx for Hash128 {}
81impl FromCurrentTx for Hash160 {}
82impl FromCurrentTx for Hash192 {}
83impl FromCurrentTx for Hash256 {}
84impl FromLedger for Hash128 {}
85impl FromLedger for Hash160 {}
86impl FromLedger for Hash192 {}
87impl FromLedger for Hash256 {}
88
89#[cfg(test)]
90mod tests {
91 use super::*;
92
93 #[test]
95 fn test_uint_creation_generic() {
96 let bytes = [1u8, 2, 3, 4, 5, 6, 7, 8];
98 let uint = UInt::<8>(bytes);
99
100 assert_eq!(uint.0, bytes);
102 }
103
104 #[test]
105 fn test_uint_from_bytes_generic() {
106 let bytes = [0xAB, 0xCD, 0xEF, 0x12];
108
109 let uint = UInt::<4>::from(bytes);
111
112 assert_eq!(uint.as_bytes(), &bytes);
114 }
115
116 #[test]
117 fn test_uint_as_bytes() {
118 let bytes = [0xFF, 0x00, 0xFF, 0x00, 0xAA, 0xBB];
120 let uint = UInt::<6>::from(bytes);
121
122 let retrieved_bytes = uint.as_bytes();
124
125 assert_eq!(retrieved_bytes, &bytes);
127 }
128
129 #[test]
130 fn test_uint_equality() {
131 let bytes1 = [1u8, 2, 3, 4];
133 let uint1 = UInt::<4>::from(bytes1);
134 let uint2 = UInt::<4>::from(bytes1);
135
136 let bytes2 = [5u8, 6, 7, 8];
138 let uint3 = UInt::<4>::from(bytes2);
139
140 assert_eq!(uint1, uint2);
142 assert_ne!(uint1, uint3);
143 }
144
145 #[test]
146 #[allow(clippy::clone_on_copy)]
147 fn test_uint_clone() {
148 let bytes = [0x11, 0x22, 0x33, 0x44];
150 let uint1 = UInt::<4>::from(bytes);
151
152 let uint2 = uint1.clone();
154
155 assert_eq!(uint1, uint2);
157 assert_eq!(uint1.as_bytes(), uint2.as_bytes());
158 }
159
160 #[test]
161 fn test_uint_copy() {
162 let bytes = [0xDE, 0xAD, 0xBE, 0xEF];
164 let uint1 = UInt::<4>::from(bytes);
165
166 let uint2 = uint1.clone();
168
169 assert_eq!(uint1, uint2);
171 assert_eq!(uint1.as_bytes(), uint2.as_bytes());
172 }
173
174 #[test]
175 fn test_uint_debug() {
176 let bytes = [0x01, 0x02];
178 let uint = UInt::<2>::from(bytes);
179
180 let _ = uint; }
184
185 #[test]
187 fn test_uint128_creation() {
188 let bytes = [1u8; 16];
190 let uint128 = UInt128::from(bytes);
191
192 assert_eq!(uint128.as_bytes(), &bytes);
194 assert_eq!(uint128.as_bytes().len(), UINT128_SIZE);
195 }
196
197 #[test]
198 fn test_uint128_all_zeros() {
199 let bytes = [0u8; 16];
201 let uint128 = UInt128::from(bytes);
202
203 assert_eq!(uint128.as_bytes(), &[0u8; 16]);
205 }
206
207 #[test]
208 fn test_uint128_all_ones() {
209 let bytes = [0xFFu8; 16];
211 let uint128 = UInt128::from(bytes);
212
213 assert_eq!(uint128.as_bytes(), &[0xFFu8; 16]);
215 }
216
217 #[test]
219 fn test_uint160_creation() {
220 let bytes = [2u8; 20];
222 let uint160 = UInt160::from(bytes);
223
224 assert_eq!(uint160.as_bytes(), &bytes);
226 assert_eq!(uint160.as_bytes().len(), UINT160_SIZE);
227 }
228
229 #[test]
230 fn test_uint160_pattern() {
231 let mut bytes = [0u8; 20];
233 for (i, byte) in bytes.iter_mut().enumerate() {
234 *byte = (i % 256) as u8;
235 }
236 let uint160 = UInt160::from(bytes);
237
238 assert_eq!(uint160.as_bytes(), &bytes);
240 }
241
242 #[test]
244 fn test_uint192_creation() {
245 let bytes = [3u8; 24];
247 let uint192 = UInt192::from(bytes);
248
249 assert_eq!(uint192.as_bytes(), &bytes);
251 assert_eq!(uint192.as_bytes().len(), UINT192_SIZE);
252 }
253
254 #[test]
255 fn test_uint192_alternating_pattern() {
256 let mut bytes = [0u8; 24];
258 for (i, byte) in bytes.iter_mut().enumerate() {
259 *byte = if i % 2 == 0 { 0xAA } else { 0x55 };
260 }
261 let uint192 = UInt192::from(bytes);
262
263 assert_eq!(uint192.as_bytes(), &bytes);
265 }
266
267 #[test]
269 fn test_uint256_creation() {
270 let bytes = [4u8; 32];
272 let uint256 = UInt256::from(bytes);
273
274 assert_eq!(uint256.as_bytes(), &bytes);
276 assert_eq!(uint256.as_bytes().len(), UINT256_SIZE);
277 }
278
279 #[test]
280 fn test_uint256_specific_value() {
281 let mut bytes = [0u8; 32];
283 bytes[0] = 0x12;
284 bytes[15] = 0x34;
285 bytes[31] = 0x56;
286 let uint256 = UInt256::from(bytes);
287
288 assert_eq!(uint256.as_bytes()[0], 0x12);
290 assert_eq!(uint256.as_bytes()[15], 0x34);
291 assert_eq!(uint256.as_bytes()[31], 0x56);
292 }
293
294 #[test]
296 fn test_hash128_alias() {
297 let bytes = [0xAB; 16];
299 let hash128 = Hash128::from(bytes);
300 let uint128 = UInt128::from(bytes);
301
302 assert_eq!(hash128, uint128);
304 assert_eq!(hash128.as_bytes(), uint128.as_bytes());
305 }
306
307 #[test]
308 fn test_hash160_alias() {
309 let bytes = [0xCD; 20];
311 let hash160 = Hash160::from(bytes);
312 let uint160 = UInt160::from(bytes);
313
314 assert_eq!(hash160, uint160);
316 assert_eq!(hash160.as_bytes(), uint160.as_bytes());
317 }
318
319 #[test]
320 fn test_hash192_alias() {
321 let bytes = [0xEF; 24];
323 let hash192 = Hash192::from(bytes);
324 let uint192 = UInt192::from(bytes);
325
326 assert_eq!(hash192, uint192);
328 assert_eq!(hash192.as_bytes(), uint192.as_bytes());
329 }
330
331 #[test]
332 fn test_hash256_alias() {
333 let bytes = [0x12; 32];
335 let hash256 = Hash256::from(bytes);
336 let uint256 = UInt256::from(bytes);
337
338 assert_eq!(hash256, uint256);
340 assert_eq!(hash256.as_bytes(), uint256.as_bytes());
341 }
342
343 #[test]
345 fn test_uint_constants() {
346 assert_eq!(UINT128_SIZE, 16);
348 assert_eq!(UINT160_SIZE, 20);
349 assert_eq!(UINT192_SIZE, 24);
350 assert_eq!(UINT256_SIZE, 32);
351 }
352
353 #[test]
354 fn test_hash_constants() {
355 assert_eq!(HASH128_SIZE, UINT128_SIZE);
357 assert_eq!(HASH160_SIZE, UINT160_SIZE);
358 assert_eq!(HASH192_SIZE, UINT192_SIZE);
359 assert_eq!(HASH256_SIZE, UINT256_SIZE);
360
361 assert_eq!(HASH128_SIZE, 16);
363 assert_eq!(HASH160_SIZE, 20);
364 assert_eq!(HASH192_SIZE, 24);
365 assert_eq!(HASH256_SIZE, 32);
366 }
367
368 #[test]
370 fn test_uint_single_byte() {
371 let bytes = [0x42];
373 let uint = UInt::<1>::from(bytes);
374
375 assert_eq!(uint.as_bytes(), &[0x42]);
376 }
377
378 #[test]
379 fn test_uint_large_size() {
380 let bytes = [0x99; 64];
382 let uint = UInt::<64>::from(bytes);
383
384 assert_eq!(uint.as_bytes().len(), 64);
385 assert_eq!(uint.as_bytes(), &[0x99; 64]);
386 }
387
388 #[test]
389 fn test_uint_equality_different_sizes() {
390 let uint4 = UInt::<4>::from([1, 2, 3, 4]);
393 let uint8 = UInt::<8>::from([1, 2, 3, 4, 5, 6, 7, 8]);
394
395 assert_eq!(uint4.as_bytes().len(), 4);
397 assert_eq!(uint8.as_bytes().len(), 8);
398 }
399
400 #[test]
401 fn test_uint_repr_c() {
402 use core::mem;
405
406 assert_eq!(mem::size_of::<UInt<16>>(), 16);
408 assert_eq!(mem::size_of::<UInt<20>>(), 20);
409 assert_eq!(mem::size_of::<UInt<24>>(), 24);
410 assert_eq!(mem::size_of::<UInt<32>>(), 32);
411
412 assert_eq!(mem::align_of::<UInt<16>>(), 1);
414 }
415
416 #[test]
417 fn test_uint_from_array_direct() {
418 let uint = UInt([0x01, 0x02, 0x03, 0x04]);
420
421 assert_eq!(uint.as_bytes(), &[0x01, 0x02, 0x03, 0x04]);
422 }
423
424 #[test]
425 fn test_multiple_uint_instances() {
426 let uint1 = UInt128::from([1u8; 16]);
428 let uint2 = UInt128::from([2u8; 16]);
429 let uint3 = UInt128::from([3u8; 16]);
430
431 assert_ne!(uint1, uint2);
433 assert_ne!(uint2, uint3);
434 assert_ne!(uint1, uint3);
435
436 assert_eq!(uint1.as_bytes(), &[1u8; 16]);
438 assert_eq!(uint2.as_bytes(), &[2u8; 16]);
439 assert_eq!(uint3.as_bytes(), &[3u8; 16]);
440 }
441}