1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{NumBytes, Read, Write};

macro_rules! checksum_type {
    ($ident:ident, $bytes:literal) => {
        /// TODO docs
        /// TODO Read, Write, `NumBytes` needs a custom implementation based on `fixed_bytes`
        #[derive(Read, Write, NumBytes, Clone, Copy)]
        #[eosio(crate_path = "crate::bytes")]
        pub struct $ident([u8; $bytes]);

        impl $ident {
            /// TODO docs.
            #[must_use]
            pub const fn as_bytes(&self) -> &[u8; $bytes] {
                &self.0
            }

            /// TODO docs.
            #[must_use]
            pub const fn to_bytes(&self) -> [u8; $bytes] {
                self.0
            }
        }

        impl From<[u8; $bytes]> for $ident {
            #[inline]
            #[must_use]
            fn from(value: [u8; $bytes]) -> Self {
                Self(value)
            }
        }

        impl From<$ident> for [u8; $bytes] {
            #[inline]
            #[must_use]
            fn from(value: $ident) -> Self {
                value.0
            }
        }

        impl Default for $ident {
            fn default() -> Self {
                Self([0; $bytes])
            }
        }

        impl AsRef<$ident> for $ident {
            #[inline]
            fn as_ref(&self) -> &Self {
                self
            }
        }
    };
}

checksum_type!(Checksum160, 20);
checksum_type!(Checksum256, 32);
checksum_type!(Checksum512, 64);