ink_primitives/sol/
bytes.rs
1use core::{
16 borrow::Borrow,
17 ops::Deref,
18};
19
20use alloy_sol_types::{
21 abi::token::{
22 PackedSeqToken,
23 WordToken,
24 },
25 sol_data,
26 SolType as AlloySolType,
27};
28use ink_prelude::{
29 boxed::Box,
30 vec::Vec,
31};
32use scale::{
33 Decode,
34 Encode,
35};
36#[cfg(feature = "std")]
37use scale_info::TypeInfo;
38
39use crate::sol::{
40 encodable::{
41 DynSizeDefault,
42 FixedSizeDefault,
43 },
44 types::SolTokenType,
45 Error,
46 SolDecode,
47 SolEncode,
48 SolTypeDecode,
49 SolTypeEncode,
50};
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
57#[cfg_attr(feature = "std", derive(TypeInfo))]
58#[repr(transparent)]
59pub struct FixedBytes<const N: usize>(pub [u8; N]);
60
61impl<const N: usize> SolTypeDecode for FixedBytes<N>
63where
64 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes,
65{
66 type AlloyType = sol_data::FixedBytes<N>;
67
68 fn detokenize(
69 token: <Self::AlloyType as AlloySolType>::Token<'_>,
70 ) -> Result<Self, Error> {
71 Ok(Self(
76 token.0 .0[..N]
77 .try_into()
78 .expect("Expected a slice of N bytes"),
79 ))
80 }
81}
82
83impl<const N: usize> SolTypeEncode for FixedBytes<N>
84where
85 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes,
86{
87 type AlloyType = sol_data::FixedBytes<N>;
88
89 const DEFAULT_VALUE: Self::DefaultType = FixedSizeDefault::WORD;
90
91 fn tokenize(&self) -> Self::TokenType<'_> {
92 let mut word = [0; 32];
95 word[..N].copy_from_slice(self.0.as_slice());
96 WordToken::from(word)
97 }
98}
99
100impl<const N: usize> SolTokenType for FixedBytes<N>
101where
102 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes,
103{
104 type TokenType<'enc> = WordToken;
105
106 type DefaultType = FixedSizeDefault;
107}
108
109impl<const N: usize> crate::sol::types::private::Sealed for FixedBytes<N> where
110 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes
111{
112}
113
114impl<const N: usize> SolDecode for FixedBytes<N>
116where
117 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes,
118{
119 type SolType = FixedBytes<N>;
120
121 fn from_sol_type(value: Self::SolType) -> Result<Self, Error> {
122 Ok(value)
123 }
124}
125
126impl<'a, const N: usize> SolEncode<'a> for FixedBytes<N>
127where
128 sol_data::ByteCount<N>: sol_data::SupportedFixedBytes,
129{
130 type SolType = &'a FixedBytes<N>;
131
132 fn to_sol_type(&'a self) -> Self::SolType {
133 self
134 }
135}
136
137impl<const N: usize> From<[u8; N]> for FixedBytes<N> {
139 fn from(value: [u8; N]) -> Self {
140 Self(value)
141 }
142}
143
144impl From<u8> for FixedBytes<1> {
145 fn from(value: u8) -> Self {
146 Self([value; 1])
147 }
148}
149
150impl<const N: usize> Deref for FixedBytes<N> {
151 type Target = [u8; N];
152
153 fn deref(&self) -> &Self::Target {
154 &self.0
155 }
156}
157
158impl<const N: usize> Borrow<[u8; N]> for FixedBytes<N> {
159 fn borrow(&self) -> &[u8; N] {
160 &self.0
161 }
162}
163
164impl<const N: usize> AsRef<[u8; N]> for FixedBytes<N> {
165 fn as_ref(&self) -> &[u8; N] {
166 &self.0
167 }
168}
169
170#[derive(Debug, Clone, PartialEq, Eq, Default, Encode, Decode)]
175#[cfg_attr(feature = "std", derive(TypeInfo))]
176#[repr(transparent)]
177pub struct DynBytes(pub Vec<u8>);
178
179impl DynBytes {
180 pub const fn new() -> Self {
182 Self(Vec::new())
183 }
184}
185
186impl SolTypeDecode for DynBytes {
188 type AlloyType = sol_data::Bytes;
189
190 fn detokenize(
191 token: <Self::AlloyType as AlloySolType>::Token<'_>,
192 ) -> Result<Self, Error> {
193 Ok(Self(token.into_vec()))
197 }
198}
199
200impl SolTypeEncode for DynBytes {
201 type AlloyType = sol_data::Bytes;
202
203 const DEFAULT_VALUE: Self::DefaultType = DynSizeDefault;
204
205 fn tokenize(&self) -> Self::TokenType<'_> {
206 PackedSeqToken(self.0.as_slice())
209 }
210}
211
212impl SolTokenType for DynBytes {
213 type TokenType<'enc> = PackedSeqToken<'enc>;
214
215 type DefaultType = DynSizeDefault;
216}
217
218impl crate::sol::types::private::Sealed for DynBytes {}
219
220impl SolDecode for DynBytes {
222 type SolType = DynBytes;
223
224 fn from_sol_type(value: Self::SolType) -> Result<Self, Error> {
225 Ok(value)
226 }
227}
228
229impl<'a> SolEncode<'a> for DynBytes {
230 type SolType = &'a DynBytes;
231
232 fn to_sol_type(&'a self) -> Self::SolType {
233 self
234 }
235}
236
237impl From<Vec<u8>> for DynBytes {
239 fn from(value: Vec<u8>) -> Self {
240 Self(value)
241 }
242}
243
244impl From<Box<[u8]>> for DynBytes {
245 fn from(value: Box<[u8]>) -> Self {
246 Self(value.into_vec())
248 }
249}
250
251impl Deref for DynBytes {
252 type Target = [u8];
253
254 fn deref(&self) -> &Self::Target {
255 &self.0
256 }
257}
258
259impl Borrow<[u8]> for DynBytes {
260 fn borrow(&self) -> &[u8] {
261 &self.0
262 }
263}
264
265impl AsRef<[u8]> for DynBytes {
266 fn as_ref(&self) -> &[u8] {
267 &self.0
268 }
269}