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 AsRef<[u8]> for U64 {
61    fn as_ref(&self) -> &[u8] {
62        &self.0
63    }
64}
65
66impl Deref for U64 {
67    type Target = [u8];
68    fn deref(&self) -> &[u8] {
69        &self.0
70    }
71}
72
73impl Debug for U64 {
74    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75        write!(f, "{}", u64::from_be_bytes(self.0))
76    }
77}
78
79impl Display 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
85#[cfg(test)]
86mod tests {
87    use super::*;
88    use commonware_codec::{DecodeExt, Encode};
89
90    #[test]
91    fn test_u64() {
92        let value = 42u64;
93        let array = U64::new(value);
94        assert_eq!(value, U64::decode(array.as_ref()).unwrap().to_u64());
95        assert_eq!(value, U64::from(array.0).to_u64());
96
97        let vec = array.to_vec();
98        assert_eq!(value, U64::decode(vec.as_ref()).unwrap().to_u64());
99    }
100
101    #[test]
102    fn test_codec() {
103        let original = U64::new(42u64);
104
105        let encoded = original.encode();
106        assert_eq!(encoded.len(), U64::SIZE);
107        assert_eq!(encoded, original.as_ref());
108
109        let decoded = U64::decode(encoded).unwrap();
110        assert_eq!(original, decoded);
111    }
112}