1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// https://github.com/nervosnetwork/ckb/blob/develop/util/types/src/conversion/utilities.rs

macro_rules! impl_conversion_for_entity_unpack {
    ($original:ty, $entity:ident) => {
        impl Unpack<$original> for packed::$entity {
            fn unpack(&self) -> $original {
                self.as_reader().unpack()
            }
        }
    };
}

macro_rules! impl_conversion_for_option_pack {
    ($original:ty, $entity:ident) => {
        impl Pack<packed::$entity> for Option<$original> {
            fn pack(&self) -> packed::$entity {
                if let Some(ref inner) = self {
                    packed::$entity::new_unchecked(inner.pack().as_bytes())
                } else {
                    packed::$entity::default()
                }
            }
        }
    };
}

macro_rules! impl_conversion_for_vector_pack {
    ($original:ty, $entity:ident) => {
        impl Pack<packed::$entity> for [$original] {
            fn pack(&self) -> packed::$entity {
                packed::$entity::new_builder()
                    .set(self.iter().map(|v| v.pack()).collect())
                    .build()
            }
        }
    };
}

macro_rules! impl_conversion_for_vector_unpack {
    ($original:ty, $entity:ident, $reader:ident) => {
        impl<'r> Unpack<Vec<$original>> for packed::$reader<'r> {
            fn unpack(&self) -> Vec<$original> {
                self.iter().map(|x| x.unpack()).collect()
            }
        }
        impl_conversion_for_entity_unpack!(Vec<$original>, $entity);
    };
}

macro_rules! impl_conversion_for_vector {
    ($original:ty, $entity:ident, $reader:ident) => {
        impl_conversion_for_vector_pack!($original, $entity);
        impl_conversion_for_vector_unpack!($original, $entity, $reader);
    };
}

macro_rules! impl_conversion_for_packed_optional_pack {
    ($original:ident, $entity:ident) => {
        impl Pack<packed::$entity> for Option<packed::$original> {
            fn pack(&self) -> packed::$entity {
                if let Some(ref inner) = self {
                    packed::$entity::new_unchecked(inner.as_bytes())
                } else {
                    packed::$entity::default()
                }
            }
        }
    };
}

macro_rules! impl_conversion_for_packed_iterator_pack {
    ($item:ident, $vec:ident) => {
        impl<T> PackVec<packed::$vec, packed::$item> for T
        where
            T: IntoIterator<Item = packed::$item>,
        {
            fn pack(self) -> packed::$vec {
                packed::$vec::new_builder().extend(self).build()
            }
        }
    };
}