Skip to main content

ckb_gen_types/conversion/
primitive.rs

1#[cfg(not(feature = "std"))]
2use alloc::{borrow::ToOwned, str, string::String};
3#[cfg(feature = "std")]
4use std::str;
5
6use crate::{bytes::Bytes, generated::packed, prelude::*, vec, vec::Vec};
7
8impl Pack<packed::Bool> for bool {
9    fn pack(&self) -> packed::Bool {
10        let b = u8::from(*self);
11        packed::Bool::new_unchecked(Bytes::from(vec![b]))
12    }
13}
14
15impl From<bool> for packed::Bool {
16    fn from(value: bool) -> Self {
17        (&value).into()
18    }
19}
20
21impl From<&bool> for packed::Bool {
22    fn from(value: &bool) -> Self {
23        let b = u8::from(*value);
24        packed::Bool::new_unchecked(Bytes::from(vec![b]))
25    }
26}
27
28impl<'r> From<packed::BoolReader<'r>> for bool {
29    fn from(value: packed::BoolReader<'r>) -> bool {
30        value.as_slice()[0] != 0
31    }
32}
33impl_conversion_for_entity_from!(bool, Bool);
34impl<'r> Unpack<bool> for packed::BoolReader<'r> {
35    fn unpack(&self) -> bool {
36        self.as_slice()[0] != 0
37    }
38}
39impl_conversion_for_entity_unpack!(bool, Bool);
40
41impl Pack<packed::Uint32> for u32 {
42    fn pack(&self) -> packed::Uint32 {
43        packed::Uint32::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
44    }
45}
46
47impl From<u32> for packed::Uint32 {
48    fn from(value: u32) -> Self {
49        (&value).into()
50    }
51}
52
53impl From<&u32> for packed::Uint32 {
54    fn from(value: &u32) -> Self {
55        packed::Uint32::new_unchecked(Bytes::from(value.to_le_bytes().to_vec()))
56    }
57}
58
59impl Pack<packed::Uint64> for u64 {
60    fn pack(&self) -> packed::Uint64 {
61        packed::Uint64::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
62    }
63}
64
65impl From<u64> for packed::Uint64 {
66    fn from(value: u64) -> Self {
67        (&value).into()
68    }
69}
70
71impl From<&u64> for packed::Uint64 {
72    fn from(value: &u64) -> Self {
73        packed::Uint64::new_unchecked(Bytes::from(value.to_le_bytes().to_vec()))
74    }
75}
76
77impl Pack<packed::Uint128> for u128 {
78    fn pack(&self) -> packed::Uint128 {
79        packed::Uint128::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
80    }
81}
82
83impl From<u128> for packed::Uint128 {
84    fn from(value: u128) -> Self {
85        (&value).into()
86    }
87}
88
89impl From<&u128> for packed::Uint128 {
90    fn from(value: &u128) -> Self {
91        packed::Uint128::new_unchecked(Bytes::from(value.to_le_bytes().to_vec()))
92    }
93}
94
95impl Pack<packed::Uint32> for usize {
96    fn pack(&self) -> packed::Uint32 {
97        (*self as u32).into()
98    }
99}
100
101impl From<usize> for packed::Uint32 {
102    fn from(value: usize) -> Self {
103        (value as u32).into()
104    }
105}
106
107impl From<&usize> for packed::Uint32 {
108    fn from(value: &usize) -> Self {
109        (*value as u32).into()
110    }
111}
112
113impl<'r> From<packed::Uint32Reader<'r>> for u32 {
114    fn from(value: packed::Uint32Reader<'r>) -> u32 {
115        let mut b = [0u8; 4];
116        b.copy_from_slice(value.as_slice());
117        u32::from_le_bytes(b)
118    }
119}
120impl_conversion_for_entity_from!(u32, Uint32);
121impl<'r> Unpack<u32> for packed::Uint32Reader<'r> {
122    fn unpack(&self) -> u32 {
123        let mut b = [0u8; 4];
124        b.copy_from_slice(self.as_slice());
125        u32::from_le_bytes(b)
126    }
127}
128impl_conversion_for_entity_unpack!(u32, Uint32);
129
130impl<'r> From<packed::Uint64Reader<'r>> for u64 {
131    fn from(value: packed::Uint64Reader<'r>) -> u64 {
132        let mut b = [0u8; 8];
133        b.copy_from_slice(value.as_slice());
134        u64::from_le_bytes(b)
135    }
136}
137impl_conversion_for_entity_from!(u64, Uint64);
138impl<'r> Unpack<u64> for packed::Uint64Reader<'r> {
139    fn unpack(&self) -> u64 {
140        let mut b = [0u8; 8];
141        b.copy_from_slice(self.as_slice());
142        u64::from_le_bytes(b)
143    }
144}
145impl_conversion_for_entity_unpack!(u64, Uint64);
146
147impl<'r> From<packed::Uint128Reader<'r>> for u128 {
148    fn from(value: packed::Uint128Reader<'r>) -> u128 {
149        let mut b = [0u8; 16];
150        b.copy_from_slice(value.as_slice());
151        u128::from_le_bytes(b)
152    }
153}
154impl_conversion_for_entity_from!(u128, Uint128);
155impl<'r> Unpack<u128> for packed::Uint128Reader<'r> {
156    fn unpack(&self) -> u128 {
157        let mut b = [0u8; 16];
158        b.copy_from_slice(self.as_slice());
159        u128::from_le_bytes(b)
160    }
161}
162impl_conversion_for_entity_unpack!(u128, Uint128);
163
164impl<'r> From<packed::Uint32Reader<'r>> for usize {
165    fn from(value: packed::Uint32Reader<'r>) -> usize {
166        let x: u32 = value.into();
167        x as usize
168    }
169}
170impl_conversion_for_entity_from!(usize, Uint32);
171impl<'r> Unpack<usize> for packed::Uint32Reader<'r> {
172    fn unpack(&self) -> usize {
173        let x: u32 = self.unpack();
174        x as usize
175    }
176}
177impl_conversion_for_entity_unpack!(usize, Uint32);
178
179impl Pack<packed::BeUint32> for u32 {
180    fn pack(&self) -> packed::BeUint32 {
181        packed::BeUint32::new_unchecked(Bytes::from(self.to_be_bytes().to_vec()))
182    }
183}
184
185impl From<u32> for packed::BeUint32 {
186    fn from(value: u32) -> Self {
187        (&value).into()
188    }
189}
190
191impl From<&u32> for packed::BeUint32 {
192    fn from(value: &u32) -> Self {
193        packed::BeUint32::new_unchecked(Bytes::from(value.to_be_bytes().to_vec()))
194    }
195}
196
197impl Pack<packed::BeUint64> for u64 {
198    fn pack(&self) -> packed::BeUint64 {
199        packed::BeUint64::new_unchecked(Bytes::from(self.to_be_bytes().to_vec()))
200    }
201}
202
203impl From<u64> for packed::BeUint64 {
204    fn from(value: u64) -> Self {
205        (&value).into()
206    }
207}
208
209impl From<&u64> for packed::BeUint64 {
210    fn from(value: &u64) -> Self {
211        packed::BeUint64::new_unchecked(Bytes::from(value.to_be_bytes().to_vec()))
212    }
213}
214
215impl Pack<packed::BeUint32> for usize {
216    fn pack(&self) -> packed::BeUint32 {
217        (*self as u32).into()
218    }
219}
220
221impl From<usize> for packed::BeUint32 {
222    fn from(value: usize) -> Self {
223        (value as u32).into()
224    }
225}
226
227impl From<&usize> for packed::BeUint32 {
228    fn from(value: &usize) -> Self {
229        (*value as u32).into()
230    }
231}
232
233impl<'r> From<packed::BeUint32Reader<'r>> for u32 {
234    fn from(value: packed::BeUint32Reader<'r>) -> u32 {
235        let mut b = [0u8; 4];
236        b.copy_from_slice(value.as_slice());
237        u32::from_be_bytes(b)
238    }
239}
240impl_conversion_for_entity_from!(u32, BeUint32);
241impl<'r> Unpack<u32> for packed::BeUint32Reader<'r> {
242    fn unpack(&self) -> u32 {
243        let mut b = [0u8; 4];
244        b.copy_from_slice(self.as_slice());
245        u32::from_be_bytes(b)
246    }
247}
248impl_conversion_for_entity_unpack!(u32, BeUint32);
249
250impl<'r> From<packed::BeUint64Reader<'r>> for u64 {
251    fn from(value: packed::BeUint64Reader<'r>) -> u64 {
252        let mut b = [0u8; 8];
253        b.copy_from_slice(value.as_slice());
254        u64::from_be_bytes(b)
255    }
256}
257impl_conversion_for_entity_from!(u64, BeUint64);
258impl<'r> Unpack<u64> for packed::BeUint64Reader<'r> {
259    fn unpack(&self) -> u64 {
260        let mut b = [0u8; 8];
261        b.copy_from_slice(self.as_slice());
262        u64::from_be_bytes(b)
263    }
264}
265impl_conversion_for_entity_unpack!(u64, BeUint64);
266
267impl<'r> From<packed::BeUint32Reader<'r>> for usize {
268    fn from(value: packed::BeUint32Reader<'r>) -> usize {
269        let x: u32 = value.into();
270        x as usize
271    }
272}
273impl_conversion_for_entity_from!(usize, BeUint32);
274impl<'r> Unpack<usize> for packed::BeUint32Reader<'r> {
275    fn unpack(&self) -> usize {
276        let x: u32 = self.unpack();
277        x as usize
278    }
279}
280impl_conversion_for_entity_unpack!(usize, BeUint32);
281
282impl Pack<packed::Bytes> for [u8] {
283    fn pack(&self) -> packed::Bytes {
284        let len = self.len();
285        let mut vec: Vec<u8> = Vec::with_capacity(4 + len);
286        vec.extend_from_slice(&(len as u32).to_le_bytes()[..]);
287        vec.extend_from_slice(self);
288        packed::Bytes::new_unchecked(Bytes::from(vec))
289    }
290}
291
292impl From<&[u8]> for packed::Bytes {
293    fn from(value: &[u8]) -> Self {
294        let len = value.len();
295        let mut vec: Vec<u8> = Vec::with_capacity(4 + len);
296        vec.extend_from_slice(&(len as u32).to_le_bytes()[..]);
297        vec.extend_from_slice(value);
298        packed::Bytes::new_unchecked(Bytes::from(vec))
299    }
300}
301
302impl<const N: usize> From<[u8; N]> for packed::Bytes {
303    fn from(value: [u8; N]) -> Self {
304        (&value[..]).into()
305    }
306}
307
308impl<const N: usize> From<&[u8; N]> for packed::Bytes {
309    fn from(value: &[u8; N]) -> Self {
310        (&value[..]).into()
311    }
312}
313
314impl<'r> From<packed::BytesReader<'r>> for Vec<u8> {
315    fn from(value: packed::BytesReader<'r>) -> Vec<u8> {
316        value.raw_data().to_owned()
317    }
318}
319impl_conversion_for_entity_from!(Vec<u8>, Bytes);
320impl<'r> Unpack<Vec<u8>> for packed::BytesReader<'r> {
321    fn unpack(&self) -> Vec<u8> {
322        self.raw_data().to_owned()
323    }
324}
325impl_conversion_for_entity_unpack!(Vec<u8>, Bytes);
326
327impl Pack<packed::Bytes> for str {
328    fn pack(&self) -> packed::Bytes {
329        self.as_bytes().into()
330    }
331}
332
333impl From<&str> for packed::Bytes {
334    fn from(value: &str) -> Self {
335        value.as_bytes().into()
336    }
337}
338
339impl<'r> packed::BytesReader<'r> {
340    /// Converts self to a string slice.
341    pub fn as_utf8(&self) -> Result<&str, str::Utf8Error> {
342        str::from_utf8(self.raw_data())
343    }
344
345    /// Converts self to a string slice without checking that the string contains valid UTF-8.
346    ///
347    /// # Safety
348    ///
349    /// This function is unsafe because it does not check that the bytes passed to
350    /// it are valid UTF-8. If this constraint is violated, undefined behavior
351    /// results, as the rest of Rust assumes that [`&str`]s are valid UTF-8.
352    pub unsafe fn as_utf8_unchecked(&self) -> &str {
353        str::from_utf8_unchecked(self.raw_data())
354    }
355
356    /// Checks whether self is contains valid UTF-8 binary data.
357    pub fn is_utf8(&self) -> bool {
358        self.as_utf8().is_ok()
359    }
360}
361
362impl Pack<packed::Bytes> for String {
363    fn pack(&self) -> packed::Bytes {
364        self.as_str().into()
365    }
366}
367
368impl From<String> for packed::Bytes {
369    fn from(value: String) -> Self {
370        value.as_str().into()
371    }
372}
373
374impl<'r> Unpack<Option<Vec<u64>>> for packed::Uint64VecOptReader<'r> {
375    fn unpack(&self) -> Option<Vec<u64>> {
376        self.to_opt().map(|x| x.unpack())
377    }
378}
379
380impl<'r> From<packed::Uint64VecOptReader<'r>> for Option<Vec<u64>> {
381    fn from(value: packed::Uint64VecOptReader<'r>) -> Option<Vec<u64>> {
382        value.to_opt().map(|x| x.into())
383    }
384}
385
386impl_conversion_for_entity_unpack!(Option<Vec<u64>>, Uint64VecOpt);
387
388impl Pack<packed::Uint64VecOpt> for Option<Vec<u64>> {
389    fn pack(&self) -> packed::Uint64VecOpt {
390        if let Some(inner) = self.as_ref() {
391            packed::Uint64VecOptBuilder::default()
392                .set(Some(inner.pack()))
393                .build()
394        } else {
395            packed::Uint64VecOpt::default()
396        }
397    }
398}
399
400impl From<Option<Vec<u64>>> for packed::Uint64VecOpt {
401    fn from(value: Option<Vec<u64>>) -> Self {
402        (&value).into()
403    }
404}
405
406impl From<&Option<Vec<u64>>> for packed::Uint64VecOpt {
407    fn from(value: &Option<Vec<u64>>) -> Self {
408        if let Some(inner) = value {
409            packed::Uint64VecOptBuilder::default()
410                .set(Some(inner.as_slice().into()))
411                .build()
412        } else {
413            packed::Uint64VecOpt::default()
414        }
415    }
416}
417
418impl_conversion_for_option!(bool, BoolOpt, BoolOptReader);
419impl_conversion_for_vector!(u32, Uint32Vec, Uint32VecReader);
420impl_conversion_for_vector!(usize, Uint32Vec, Uint32VecReader);
421impl_conversion_for_vector!(u64, Uint64Vec, Uint64VecReader);
422impl_conversion_for_option_pack!(&str, BytesOpt);
423impl_conversion_for_option_pack!(String, BytesOpt);
424impl_conversion_for_option_pack!(Bytes, BytesOpt);
425impl_conversion_for_packed_optional_pack!(Bytes, BytesOpt);
426
427impl_conversion_for_option_from_into!(bool, BoolOpt, BoolOptReader, Bool);
428impl_conversion_for_vector_from_into!(u32, Uint32Vec, Uint32VecReader);
429impl_conversion_for_vector_from_into!(usize, Uint32Vec, Uint32VecReader);
430impl_conversion_for_vector_from_into!(u64, Uint64Vec, Uint64VecReader);
431
432impl_conversion_for_option_from!(&str, BytesOpt, Bytes);
433impl_conversion_for_option_from!(String, BytesOpt, Bytes);
434impl_conversion_for_option_from!(Bytes, BytesOpt, Bytes);
435impl_conversion_for_packed_optional_from!(Bytes, BytesOpt);