ckb_gen_types/conversion/
primitive.rs1#[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<'r> Unpack<bool> for packed::BoolReader<'r> {
16 fn unpack(&self) -> bool {
17 match self.as_slice()[0] {
18 0 => false,
19 1 => true,
20 _ => unreachable!(),
21 }
22 }
23}
24impl_conversion_for_entity_unpack!(bool, Bool);
25
26impl Pack<packed::Uint32> for u32 {
27 fn pack(&self) -> packed::Uint32 {
28 packed::Uint32::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
29 }
30}
31
32impl Pack<packed::Uint64> for u64 {
33 fn pack(&self) -> packed::Uint64 {
34 packed::Uint64::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
35 }
36}
37
38impl Pack<packed::Uint128> for u128 {
39 fn pack(&self) -> packed::Uint128 {
40 packed::Uint128::new_unchecked(Bytes::from(self.to_le_bytes().to_vec()))
41 }
42}
43
44impl Pack<packed::Uint32> for usize {
45 fn pack(&self) -> packed::Uint32 {
46 (*self as u32).pack()
47 }
48}
49
50impl<'r> Unpack<u32> for packed::Uint32Reader<'r> {
51 fn unpack(&self) -> u32 {
52 let mut b = [0u8; 4];
53 b.copy_from_slice(self.as_slice());
54 u32::from_le_bytes(b)
55 }
56}
57impl_conversion_for_entity_unpack!(u32, Uint32);
58
59impl<'r> Unpack<u64> for packed::Uint64Reader<'r> {
60 fn unpack(&self) -> u64 {
61 let mut b = [0u8; 8];
62 b.copy_from_slice(self.as_slice());
63 u64::from_le_bytes(b)
64 }
65}
66impl_conversion_for_entity_unpack!(u64, Uint64);
67
68impl<'r> Unpack<u128> for packed::Uint128Reader<'r> {
69 fn unpack(&self) -> u128 {
70 let mut b = [0u8; 16];
71 b.copy_from_slice(self.as_slice());
72 u128::from_le_bytes(b)
73 }
74}
75impl_conversion_for_entity_unpack!(u128, Uint128);
76
77impl<'r> Unpack<usize> for packed::Uint32Reader<'r> {
78 fn unpack(&self) -> usize {
79 let x: u32 = self.unpack();
80 x as usize
81 }
82}
83impl_conversion_for_entity_unpack!(usize, Uint32);
84
85impl Pack<packed::BeUint32> for u32 {
86 fn pack(&self) -> packed::BeUint32 {
87 packed::BeUint32::new_unchecked(Bytes::from(self.to_be_bytes().to_vec()))
88 }
89}
90
91impl Pack<packed::BeUint64> for u64 {
92 fn pack(&self) -> packed::BeUint64 {
93 packed::BeUint64::new_unchecked(Bytes::from(self.to_be_bytes().to_vec()))
94 }
95}
96
97impl Pack<packed::BeUint32> for usize {
98 fn pack(&self) -> packed::BeUint32 {
99 (*self as u32).pack()
100 }
101}
102
103impl<'r> Unpack<u32> for packed::BeUint32Reader<'r> {
104 fn unpack(&self) -> u32 {
105 let mut b = [0u8; 4];
106 b.copy_from_slice(self.as_slice());
107 u32::from_be_bytes(b)
108 }
109}
110impl_conversion_for_entity_unpack!(u32, BeUint32);
111
112impl<'r> Unpack<u64> for packed::BeUint64Reader<'r> {
113 fn unpack(&self) -> u64 {
114 let mut b = [0u8; 8];
115 b.copy_from_slice(self.as_slice());
116 u64::from_be_bytes(b)
117 }
118}
119impl_conversion_for_entity_unpack!(u64, BeUint64);
120
121impl<'r> Unpack<usize> for packed::BeUint32Reader<'r> {
122 fn unpack(&self) -> usize {
123 let x: u32 = self.unpack();
124 x as usize
125 }
126}
127impl_conversion_for_entity_unpack!(usize, BeUint32);
128
129impl Pack<packed::Bytes> for [u8] {
130 fn pack(&self) -> packed::Bytes {
131 let len = self.len();
132 let mut vec: Vec<u8> = Vec::with_capacity(4 + len);
133 vec.extend_from_slice(&(len as u32).to_le_bytes()[..]);
134 vec.extend_from_slice(self);
135 packed::Bytes::new_unchecked(Bytes::from(vec))
136 }
137}
138
139impl<'r> Unpack<Vec<u8>> for packed::BytesReader<'r> {
140 fn unpack(&self) -> Vec<u8> {
141 self.raw_data().to_owned()
142 }
143}
144impl_conversion_for_entity_unpack!(Vec<u8>, Bytes);
145
146impl Pack<packed::Bytes> for str {
147 fn pack(&self) -> packed::Bytes {
148 self.as_bytes().pack()
149 }
150}
151
152impl<'r> packed::BytesReader<'r> {
153 pub fn as_utf8(&self) -> Result<&str, str::Utf8Error> {
155 str::from_utf8(self.raw_data())
156 }
157
158 pub unsafe fn as_utf8_unchecked(&self) -> &str {
166 str::from_utf8_unchecked(self.raw_data())
167 }
168
169 pub fn is_utf8(&self) -> bool {
171 self.as_utf8().is_ok()
172 }
173}
174
175impl Pack<packed::Bytes> for String {
176 fn pack(&self) -> packed::Bytes {
177 self.as_str().pack()
178 }
179}
180
181impl<'r> Unpack<Option<Vec<u64>>> for packed::Uint64VecOptReader<'r> {
182 fn unpack(&self) -> Option<Vec<u64>> {
183 self.to_opt().map(|x| x.unpack())
184 }
185}
186
187impl_conversion_for_entity_unpack!(Option<Vec<u64>>, Uint64VecOpt);
188
189impl Pack<packed::Uint64VecOpt> for Option<Vec<u64>> {
190 fn pack(&self) -> packed::Uint64VecOpt {
191 if let Some(inner) = self.as_ref() {
192 packed::Uint64VecOptBuilder::default()
193 .set(Some(inner.pack()))
194 .build()
195 } else {
196 packed::Uint64VecOpt::default()
197 }
198 }
199}
200
201impl_conversion_for_option!(bool, BoolOpt, BoolOptReader);
202impl_conversion_for_vector!(u32, Uint32Vec, Uint32VecReader);
203impl_conversion_for_vector!(usize, Uint32Vec, Uint32VecReader);
204impl_conversion_for_vector!(u64, Uint64Vec, Uint64VecReader);
205impl_conversion_for_option_pack!(&str, BytesOpt);
206impl_conversion_for_option_pack!(String, BytesOpt);
207impl_conversion_for_option_pack!(Bytes, BytesOpt);
208impl_conversion_for_packed_optional_pack!(Bytes, BytesOpt);