ckb_standalone_types/conversion/
primitive.rs1use crate::{bytes::Bytes, packed, prelude::*};
2
3#[cfg(not(feature = "std"))]
4use alloc::{borrow::ToOwned, str, string::String, vec::Vec};
5#[cfg(feature = "std")]
6use std::str;
7
8impl Pack<packed::Uint32> for u32 {
9 fn pack(&self) -> packed::Uint32 {
10 packed::Uint32::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
11 }
12}
13
14impl Pack<packed::Uint64> for u64 {
15 fn pack(&self) -> packed::Uint64 {
16 packed::Uint64::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
17 }
18}
19
20impl Pack<packed::Uint128> for u128 {
21 fn pack(&self) -> packed::Uint128 {
22 packed::Uint128::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
23 }
24}
25
26impl Pack<packed::Uint32> for usize {
27 fn pack(&self) -> packed::Uint32 {
28 (*self as u32).pack()
29 }
30}
31
32impl<'r> Unpack<u32> for packed::Uint32Reader<'r> {
33 #[allow(clippy::cast_ptr_alignment)]
34 fn unpack(&self) -> u32 {
35 let le = self.as_slice().as_ptr() as *const u32;
36 u32::from_le(unsafe { *le })
37 }
38}
39impl_conversion_for_entity_unpack!(u32, Uint32);
40
41impl<'r> Unpack<u64> for packed::Uint64Reader<'r> {
42 #[allow(clippy::cast_ptr_alignment)]
43 fn unpack(&self) -> u64 {
44 let le = self.as_slice().as_ptr() as *const u64;
45 u64::from_le(unsafe { *le })
46 }
47}
48impl_conversion_for_entity_unpack!(u64, Uint64);
49
50impl<'r> Unpack<u128> for packed::Uint128Reader<'r> {
51 #[allow(clippy::cast_ptr_alignment)]
52 fn unpack(&self) -> u128 {
53 let le = self.as_slice().as_ptr() as *const u128;
54 u128::from_le(unsafe { *le })
55 }
56}
57impl_conversion_for_entity_unpack!(u128, Uint128);
58
59impl<'r> Unpack<usize> for packed::Uint32Reader<'r> {
60 fn unpack(&self) -> usize {
61 let x: u32 = self.unpack();
62 x as usize
63 }
64}
65impl_conversion_for_entity_unpack!(usize, Uint32);
66
67impl Pack<packed::Bytes> for [u8] {
68 fn pack(&self) -> packed::Bytes {
69 let len = self.len();
70 let mut vec: Vec<u8> = Vec::with_capacity(4 + len);
71 vec.extend_from_slice(&(len as u32).to_le_bytes()[..]);
72 vec.extend_from_slice(self);
73 packed::Bytes::new_unchecked(Bytes::from(vec))
74 }
75}
76
77impl<'r> Unpack<Vec<u8>> for packed::BytesReader<'r> {
78 fn unpack(&self) -> Vec<u8> {
79 self.raw_data().to_owned()
80 }
81}
82impl_conversion_for_entity_unpack!(Vec<u8>, Bytes);
83
84impl Pack<packed::Bytes> for str {
85 fn pack(&self) -> packed::Bytes {
86 self.as_bytes().pack()
87 }
88}
89
90impl<'r> packed::BytesReader<'r> {
91 pub fn as_utf8(&self) -> Result<&str, str::Utf8Error> {
92 str::from_utf8(self.raw_data())
93 }
94
95 #[allow(clippy::missing_safety_doc)]
96 pub unsafe fn as_utf8_unchecked(&self) -> &str {
97 str::from_utf8_unchecked(self.raw_data())
98 }
99
100 pub fn is_utf8(&self) -> bool {
101 self.as_utf8().is_ok()
102 }
103}
104
105impl Pack<packed::Bytes> for String {
106 fn pack(&self) -> packed::Bytes {
107 self.as_str().pack()
108 }
109}
110
111impl_conversion_for_option_pack!(&str, BytesOpt);
112impl_conversion_for_option_pack!(String, BytesOpt);
113impl_conversion_for_option_pack!(Bytes, BytesOpt);