commonware_utils/array/
u64.rs

1use crate::Array;
2use bytes::{Buf, BufMut};
3use commonware_codec::{Error as CodecError, FixedSize, Read, ReadExt, Write};
4use std::{
5    cmp::{Ord, PartialOrd},
6    fmt::{Debug, Display},
7    hash::Hash,
8    ops::Deref,
9};
10use thiserror::Error;
11
12// Errors returned by `U64` functions.
13#[derive(Error, Debug, PartialEq)]
14pub enum Error {
15    #[error("invalid length")]
16    InvalidLength,
17}
18
19/// An `Array` implementation for `u64`.
20#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Default)]
21#[repr(transparent)]
22pub struct U64([u8; u64::SIZE]);
23
24impl U64 {
25    pub fn new(value: u64) -> Self {
26        Self(value.to_be_bytes())
27    }
28
29    pub fn to_u64(&self) -> u64 {
30        u64::from_be_bytes(self.0)
31    }
32}
33
34impl Write for U64 {
35    fn write(&self, buf: &mut impl BufMut) {
36        self.0.write(buf);
37    }
38}
39
40impl Read for U64 {
41    type Cfg = ();
42
43    fn read_cfg(buf: &mut impl Buf, _: &()) -> Result<Self, CodecError> {
44        <[u8; U64::SIZE]>::read(buf).map(Self)
45    }
46}
47
48impl FixedSize for U64 {
49    const SIZE: usize = u64::SIZE;
50}
51
52impl Array for U64 {}
53
54impl From<[u8; U64::SIZE]> for U64 {
55    fn from(value: [u8; U64::SIZE]) -> Self {
56        Self(value)
57    }
58}
59
60impl From<u64> for U64 {
61    fn from(value: u64) -> Self {
62        Self(value.to_be_bytes())
63    }
64}
65
66impl AsRef<[u8]> for U64 {
67    fn as_ref(&self) -> &[u8] {
68        &self.0
69    }
70}
71
72impl Deref for U64 {
73    type Target = [u8];
74    fn deref(&self) -> &[u8] {
75        &self.0
76    }
77}
78
79impl Debug for U64 {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(f, "{}", u64::from_be_bytes(self.0))
82    }
83}
84
85impl Display for U64 {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        write!(f, "{}", u64::from_be_bytes(self.0))
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94    use commonware_codec::{DecodeExt, Encode};
95
96    #[test]
97    fn test_u64() {
98        let value = 42u64;
99        let array = U64::new(value);
100        assert_eq!(value, U64::decode(array.as_ref()).unwrap().to_u64());
101        assert_eq!(value, U64::from(array.0).to_u64());
102
103        let vec = array.to_vec();
104        assert_eq!(value, U64::decode(vec.as_ref()).unwrap().to_u64());
105    }
106
107    #[test]
108    fn test_codec() {
109        let original = U64::new(42u64);
110
111        let encoded = original.encode();
112        assert_eq!(encoded.len(), U64::SIZE);
113        assert_eq!(encoded, original.as_ref());
114
115        let decoded = U64::decode(encoded).unwrap();
116        assert_eq!(original, decoded);
117    }
118}