zink_codegen/
utils.rs

1//! Utils for bytes conversion.
2//!
3//! TODO: move this util to other library
4
5/// Trait for converting type to bytes32.
6pub trait Bytes32: Sized {
7    /// Convert type to the lowest significant bytes 32.
8    fn to_bytes32(&self) -> [u8; 32];
9
10    /// Convert type to vec of bytes.
11    fn to_vec(&self) -> Vec<u8> {
12        self.to_bytes32().to_vec()
13    }
14}
15
16/// Implement Bytes32 for types.
17macro_rules! impl_bytes32 {
18    ($($ty:ident),+) => {
19        $(
20            impl Bytes32 for $ty {
21                fn to_bytes32(&self) -> [u8; 32] {
22                    let mut bytes = [0u8; 32];
23                    let ls_bytes = {
24                        self.to_le_bytes()
25                            .into_iter()
26                            .rev()
27                            .skip_while(|b| *b == 0)
28                            .collect::<Vec<_>>()
29                            .into_iter()
30                            .rev()
31                            .collect::<Vec<_>>()
32                    };
33
34                    bytes[(32 - ls_bytes.len())..].copy_from_slice(&ls_bytes);
35                    bytes
36                }
37
38                fn to_vec(&self) -> Vec<u8> {
39                    self.to_le_bytes().to_vec()
40                }
41            }
42        )+
43    };
44}
45
46impl Bytes32 for Vec<u8> {
47    fn to_bytes32(&self) -> [u8; 32] {
48        let mut bytes = [0u8; 32];
49        bytes[(32 - self.len())..].copy_from_slice(self);
50        bytes
51    }
52
53    fn to_vec(&self) -> Vec<u8> {
54        self.clone()
55    }
56}
57
58impl Bytes32 for [u8; 20] {
59    fn to_bytes32(&self) -> [u8; 32] {
60        let mut bytes = [0u8; 32];
61        bytes[12..].copy_from_slice(self);
62        bytes
63    }
64}
65
66impl Bytes32 for [u8; 32] {
67    fn to_bytes32(&self) -> [u8; 32] {
68        *self
69    }
70
71    fn to_vec(&self) -> Vec<u8> {
72        self.as_ref().into()
73    }
74}
75
76impl Bytes32 for &[u8] {
77    fn to_bytes32(&self) -> [u8; 32] {
78        let mut bytes = [0u8; 32];
79        bytes[(32 - self.len())..].copy_from_slice(self);
80        bytes
81    }
82
83    fn to_vec(&self) -> Vec<u8> {
84        (*self).into()
85    }
86}
87
88impl Bytes32 for () {
89    fn to_bytes32(&self) -> [u8; 32] {
90        [0; 32]
91    }
92
93    fn to_vec(&self) -> Vec<u8> {
94        Default::default()
95    }
96}
97
98impl Bytes32 for &str {
99    fn to_bytes32(&self) -> [u8; 32] {
100        let mut bytes = [0u8; 32];
101        bytes[(32 - self.len())..].copy_from_slice(self.as_bytes());
102        bytes
103    }
104
105    fn to_vec(&self) -> Vec<u8> {
106        self.as_bytes().into()
107    }
108}
109
110impl Bytes32 for bool {
111    fn to_bytes32(&self) -> [u8; 32] {
112        let mut output = [0; 32];
113        if *self {
114            output[31] = 1;
115        }
116
117        output
118    }
119}
120
121impl_bytes32!(i8, u8, i16, u16, i32, u32, usize, i64, u64, i128, u128);