Skip to main content

ckb_standalone_types/conversion/
blockchain.rs

1use crate::{bytes::Bytes, packed, prelude::*, vec::Vec};
2impl Pack<packed::Byte32> for [u8; 32] {
3    fn pack(&self) -> packed::Byte32 {
4        packed::Byte32::from_slice(&self[..]).expect("impossible: fail to pack [u8; 32]")
5    }
6}
7
8impl<'r> Unpack<[u8; 32]> for packed::Byte32Reader<'r> {
9    fn unpack(&self) -> [u8; 32] {
10        let ptr = self.as_slice().as_ptr() as *const [u8; 32];
11        unsafe { *ptr }
12    }
13}
14impl_conversion_for_entity_unpack!([u8; 32], Byte32);
15
16impl Pack<packed::ProposalShortId> for [u8; 10] {
17    fn pack(&self) -> packed::ProposalShortId {
18        packed::ProposalShortId::from_slice(&self[..])
19            .expect("impossible: fail to pack to ProposalShortId")
20    }
21}
22
23impl<'r> Unpack<[u8; 10]> for packed::ProposalShortIdReader<'r> {
24    fn unpack(&self) -> [u8; 10] {
25        let ptr = self.as_slice().as_ptr() as *const [u8; 10];
26        unsafe { *ptr }
27    }
28}
29impl_conversion_for_entity_unpack!([u8; 10], ProposalShortId);
30
31impl Pack<packed::Bytes> for Bytes {
32    fn pack(&self) -> packed::Bytes {
33        let len = (self.len() as u32).to_le_bytes();
34        let mut v = Vec::with_capacity(4 + self.len());
35        v.extend_from_slice(&len[..]);
36        v.extend_from_slice(&self[..]);
37        packed::Bytes::new_unchecked(v.into())
38    }
39}
40
41impl<'r> Unpack<Bytes> for packed::BytesReader<'r> {
42    fn unpack(&self) -> Bytes {
43        Bytes::from(self.raw_data().to_vec())
44    }
45}
46
47impl Unpack<Bytes> for packed::Bytes {
48    fn unpack(&self) -> Bytes {
49        self.raw_data()
50    }
51}
52
53impl_conversion_for_vector!(Bytes, BytesVec, BytesVecReader);
54impl_conversion_for_packed_optional_pack!(Script, ScriptOpt);
55impl_conversion_for_packed_iterator_pack!(ProposalShortId, ProposalShortIdVec);
56impl_conversion_for_packed_iterator_pack!(Bytes, BytesVec);
57impl_conversion_for_packed_iterator_pack!(Transaction, TransactionVec);
58impl_conversion_for_packed_iterator_pack!(CellDep, CellDepVec);
59impl_conversion_for_packed_iterator_pack!(CellOutput, CellOutputVec);
60impl_conversion_for_packed_iterator_pack!(CellInput, CellInputVec);
61impl_conversion_for_packed_iterator_pack!(UncleBlock, UncleBlockVec);
62impl_conversion_for_packed_iterator_pack!(Byte32, Byte32Vec);