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