ckb_types/conversion/
storage.rs

1use crate::{core, packed, prelude::*};
2
3impl Pack<packed::HeaderView> for core::HeaderView {
4    fn pack(&self) -> packed::HeaderView {
5        packed::HeaderView::new_builder()
6            .data(self.data())
7            .hash(self.hash())
8            .build()
9    }
10}
11
12impl From<core::HeaderView> for packed::HeaderView {
13    fn from(value: core::HeaderView) -> Self {
14        (&value).into()
15    }
16}
17
18impl From<&core::HeaderView> for packed::HeaderView {
19    fn from(value: &core::HeaderView) -> Self {
20        packed::HeaderView::new_builder()
21            .data(value.data())
22            .hash(value.hash())
23            .build()
24    }
25}
26
27impl<'r> Unpack<core::HeaderView> for packed::HeaderViewReader<'r> {
28    fn unpack(&self) -> core::HeaderView {
29        core::HeaderView {
30            data: self.data().to_entity(),
31            hash: self.hash().to_entity(),
32        }
33    }
34}
35impl_conversion_for_entity_unpack!(core::HeaderView, HeaderView);
36
37impl<'r> From<packed::HeaderViewReader<'r>> for core::HeaderView {
38    fn from(value: packed::HeaderViewReader<'r>) -> core::HeaderView {
39        core::HeaderView {
40            data: value.data().to_entity(),
41            hash: value.hash().to_entity(),
42        }
43    }
44}
45impl_conversion_for_entity_from!(core::HeaderView, HeaderView);
46
47impl Pack<packed::UncleBlockVecView> for core::UncleBlockVecView {
48    fn pack(&self) -> packed::UncleBlockVecView {
49        packed::UncleBlockVecView::new_builder()
50            .data(self.data())
51            .hashes(self.hashes())
52            .build()
53    }
54}
55
56impl From<core::UncleBlockVecView> for packed::UncleBlockVecView {
57    fn from(value: core::UncleBlockVecView) -> Self {
58        (&value).into()
59    }
60}
61
62impl From<&core::UncleBlockVecView> for packed::UncleBlockVecView {
63    fn from(value: &core::UncleBlockVecView) -> Self {
64        packed::UncleBlockVecView::new_builder()
65            .data(value.data())
66            .hashes(value.hashes())
67            .build()
68    }
69}
70
71impl<'r> Unpack<core::UncleBlockVecView> for packed::UncleBlockVecViewReader<'r> {
72    fn unpack(&self) -> core::UncleBlockVecView {
73        core::UncleBlockVecView {
74            data: self.data().to_entity(),
75            hashes: self.hashes().to_entity(),
76        }
77    }
78}
79impl_conversion_for_entity_unpack!(core::UncleBlockVecView, UncleBlockVecView);
80
81impl<'r> From<packed::UncleBlockVecViewReader<'r>> for core::UncleBlockVecView {
82    fn from(value: packed::UncleBlockVecViewReader<'r>) -> core::UncleBlockVecView {
83        core::UncleBlockVecView {
84            data: value.data().to_entity(),
85            hashes: value.hashes().to_entity(),
86        }
87    }
88}
89impl_conversion_for_entity_from!(core::UncleBlockVecView, UncleBlockVecView);
90
91impl Pack<packed::TransactionView> for core::TransactionView {
92    fn pack(&self) -> packed::TransactionView {
93        packed::TransactionView::new_builder()
94            .data(self.data())
95            .hash(self.hash())
96            .witness_hash(self.witness_hash())
97            .build()
98    }
99}
100
101impl From<core::TransactionView> for packed::TransactionView {
102    fn from(value: core::TransactionView) -> Self {
103        (&value).into()
104    }
105}
106
107impl From<&core::TransactionView> for packed::TransactionView {
108    fn from(value: &core::TransactionView) -> Self {
109        packed::TransactionView::new_builder()
110            .data(value.data())
111            .hash(value.hash())
112            .witness_hash(value.witness_hash())
113            .build()
114    }
115}
116
117impl<'r> Unpack<core::TransactionView> for packed::TransactionViewReader<'r> {
118    fn unpack(&self) -> core::TransactionView {
119        core::TransactionView {
120            data: self.data().to_entity(),
121            hash: self.hash().to_entity(),
122            witness_hash: self.witness_hash().to_entity(),
123        }
124    }
125}
126impl_conversion_for_entity_unpack!(core::TransactionView, TransactionView);
127
128impl<'r> From<packed::TransactionViewReader<'r>> for core::TransactionView {
129    fn from(value: packed::TransactionViewReader<'r>) -> core::TransactionView {
130        core::TransactionView {
131            data: value.data().to_entity(),
132            hash: value.hash().to_entity(),
133            witness_hash: value.witness_hash().to_entity(),
134        }
135    }
136}
137impl_conversion_for_entity_from!(core::TransactionView, TransactionView);
138
139impl<'r> Unpack<core::BlockExt> for packed::BlockExtReader<'r> {
140    fn unpack(&self) -> core::BlockExt {
141        core::BlockExt {
142            received_at: self.received_at().into(),
143            total_difficulty: self.total_difficulty().into(),
144            total_uncles_count: self.total_uncles_count().into(),
145            verified: self.verified().into(),
146            txs_fees: self.txs_fees().into(),
147            cycles: None,
148            txs_sizes: None,
149        }
150    }
151}
152impl_conversion_for_entity_unpack!(core::BlockExt, BlockExt);
153
154impl<'r> From<packed::BlockExtReader<'r>> for core::BlockExt {
155    fn from(value: packed::BlockExtReader<'r>) -> core::BlockExt {
156        core::BlockExt {
157            received_at: value.received_at().into(),
158            total_difficulty: value.total_difficulty().into(),
159            total_uncles_count: value.total_uncles_count().into(),
160            verified: value.verified().into(),
161            txs_fees: value.txs_fees().into(),
162            cycles: None,
163            txs_sizes: None,
164        }
165    }
166}
167impl_conversion_for_entity_from!(core::BlockExt, BlockExt);
168
169impl Pack<packed::BlockExtV1> for core::BlockExt {
170    fn pack(&self) -> packed::BlockExtV1 {
171        packed::BlockExtV1::new_builder()
172            .received_at(self.received_at)
173            .total_difficulty(&self.total_difficulty)
174            .total_uncles_count(self.total_uncles_count)
175            .verified(self.verified)
176            .txs_fees(&self.txs_fees[..])
177            .cycles(&self.cycles)
178            .txs_sizes(&self.txs_sizes)
179            .build()
180    }
181}
182
183impl From<core::BlockExt> for packed::BlockExtV1 {
184    fn from(value: core::BlockExt) -> Self {
185        (&value).into()
186    }
187}
188
189impl From<&core::BlockExt> for packed::BlockExtV1 {
190    fn from(value: &core::BlockExt) -> Self {
191        packed::BlockExtV1::new_builder()
192            .received_at(value.received_at)
193            .total_difficulty(&value.total_difficulty)
194            .total_uncles_count(value.total_uncles_count)
195            .verified(value.verified)
196            .txs_fees(&value.txs_fees[..])
197            .cycles(&value.cycles)
198            .txs_sizes(&value.txs_sizes)
199            .build()
200    }
201}
202
203impl<'r> Unpack<core::BlockExt> for packed::BlockExtV1Reader<'r> {
204    fn unpack(&self) -> core::BlockExt {
205        core::BlockExt {
206            received_at: self.received_at().into(),
207            total_difficulty: self.total_difficulty().into(),
208            total_uncles_count: self.total_uncles_count().into(),
209            verified: self.verified().into(),
210            txs_fees: self.txs_fees().into(),
211            cycles: self.cycles().into(),
212            txs_sizes: self.txs_sizes().into(),
213        }
214    }
215}
216impl_conversion_for_entity_unpack!(core::BlockExt, BlockExtV1);
217
218impl<'r> From<packed::BlockExtV1Reader<'r>> for core::BlockExt {
219    fn from(value: packed::BlockExtV1Reader<'r>) -> core::BlockExt {
220        core::BlockExt {
221            received_at: value.received_at().into(),
222            total_difficulty: value.total_difficulty().into(),
223            total_uncles_count: value.total_uncles_count().into(),
224            verified: value.verified().into(),
225            txs_fees: value.txs_fees().into(),
226            cycles: value.cycles().into(),
227            txs_sizes: value.txs_sizes().into(),
228        }
229    }
230}
231impl_conversion_for_entity_from!(core::BlockExt, BlockExtV1);
232
233impl Pack<packed::EpochExt> for core::EpochExt {
234    fn pack(&self) -> packed::EpochExt {
235        packed::EpochExt::new_builder()
236            .number(self.number())
237            .base_block_reward(self.base_block_reward())
238            .remainder_reward(self.remainder_reward())
239            .previous_epoch_hash_rate(self.previous_epoch_hash_rate())
240            .last_block_hash_in_previous_epoch(self.last_block_hash_in_previous_epoch())
241            .start_number(self.start_number())
242            .length(self.length())
243            .compact_target(self.compact_target())
244            .build()
245    }
246}
247
248impl From<core::EpochExt> for packed::EpochExt {
249    fn from(value: core::EpochExt) -> Self {
250        (&value).into()
251    }
252}
253
254impl From<&core::EpochExt> for packed::EpochExt {
255    fn from(value: &core::EpochExt) -> Self {
256        packed::EpochExt::new_builder()
257            .number(value.number())
258            .base_block_reward(value.base_block_reward())
259            .remainder_reward(value.remainder_reward())
260            .previous_epoch_hash_rate(value.previous_epoch_hash_rate())
261            .last_block_hash_in_previous_epoch(value.last_block_hash_in_previous_epoch())
262            .start_number(value.start_number())
263            .length(value.length())
264            .compact_target(value.compact_target())
265            .build()
266    }
267}
268
269impl<'r> Unpack<core::EpochExt> for packed::EpochExtReader<'r> {
270    fn unpack(&self) -> core::EpochExt {
271        core::EpochExt {
272            number: self.number().into(),
273            base_block_reward: self.base_block_reward().into(),
274            remainder_reward: self.remainder_reward().into(),
275            previous_epoch_hash_rate: self.previous_epoch_hash_rate().into(),
276            last_block_hash_in_previous_epoch: self.last_block_hash_in_previous_epoch().to_entity(),
277            start_number: self.start_number().into(),
278            length: self.length().into(),
279            compact_target: self.compact_target().into(),
280        }
281    }
282}
283impl_conversion_for_entity_unpack!(core::EpochExt, EpochExt);
284
285impl<'r> From<packed::EpochExtReader<'r>> for core::EpochExt {
286    fn from(value: packed::EpochExtReader<'r>) -> core::EpochExt {
287        core::EpochExt {
288            number: value.number().into(),
289            base_block_reward: value.base_block_reward().into(),
290            remainder_reward: value.remainder_reward().into(),
291            previous_epoch_hash_rate: value.previous_epoch_hash_rate().into(),
292            last_block_hash_in_previous_epoch: value
293                .last_block_hash_in_previous_epoch()
294                .to_entity(),
295            start_number: value.start_number().into(),
296            length: value.length().into(),
297            compact_target: value.compact_target().into(),
298        }
299    }
300}
301impl_conversion_for_entity_from!(core::EpochExt, EpochExt);
302
303impl Pack<packed::TransactionInfo> for core::TransactionInfo {
304    fn pack(&self) -> packed::TransactionInfo {
305        let key = packed::TransactionKey::new_builder()
306            .block_hash(self.block_hash.clone())
307            .index(self.index)
308            .build();
309        packed::TransactionInfo::new_builder()
310            .key(key)
311            .block_number(self.block_number)
312            .block_epoch(self.block_epoch)
313            .build()
314    }
315}
316
317impl From<core::TransactionInfo> for packed::TransactionInfo {
318    fn from(value: core::TransactionInfo) -> Self {
319        (&value).into()
320    }
321}
322
323impl From<&core::TransactionInfo> for packed::TransactionInfo {
324    fn from(value: &core::TransactionInfo) -> Self {
325        let key = packed::TransactionKey::new_builder()
326            .block_hash(value.block_hash.clone())
327            .index(value.index)
328            .build();
329        packed::TransactionInfo::new_builder()
330            .key(key)
331            .block_number(value.block_number)
332            .block_epoch(value.block_epoch)
333            .build()
334    }
335}
336
337impl<'r> Unpack<core::TransactionInfo> for packed::TransactionInfoReader<'r> {
338    fn unpack(&self) -> core::TransactionInfo {
339        core::TransactionInfo {
340            block_hash: self.key().block_hash().to_entity(),
341            index: self.key().index().into(),
342            block_number: self.block_number().into(),
343            block_epoch: self.block_epoch().into(),
344        }
345    }
346}
347impl_conversion_for_entity_unpack!(core::TransactionInfo, TransactionInfo);
348
349impl<'r> From<packed::TransactionInfoReader<'r>> for core::TransactionInfo {
350    fn from(value: packed::TransactionInfoReader<'r>) -> core::TransactionInfo {
351        core::TransactionInfo {
352            block_hash: value.key().block_hash().to_entity(),
353            index: value.key().index().into(),
354            block_number: value.block_number().into(),
355            block_epoch: value.block_epoch().into(),
356        }
357    }
358}
359impl_conversion_for_entity_from!(core::TransactionInfo, TransactionInfo);