1use molecule::prelude::*;
4#[derive(Clone)]
5pub struct Uint32(molecule::bytes::Bytes);
6impl ::core::fmt::LowerHex for Uint32 {
7 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8 use molecule::hex_string;
9 if f.alternate() {
10 write!(f, "0x")?;
11 }
12 write!(f, "{}", hex_string(self.as_slice()))
13 }
14}
15impl ::core::fmt::Debug for Uint32 {
16 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
17 write!(f, "{}({:#x})", Self::NAME, self)
18 }
19}
20impl ::core::fmt::Display for Uint32 {
21 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
22 use molecule::hex_string;
23 let raw_data = hex_string(&self.raw_data());
24 write!(f, "{}(0x{})", Self::NAME, raw_data)
25 }
26}
27impl ::core::default::Default for Uint32 {
28 fn default() -> Self {
29 let v: Vec<u8> = vec![0, 0, 0, 0];
30 Uint32::new_unchecked(v.into())
31 }
32}
33impl Uint32 {
34 pub const TOTAL_SIZE: usize = 4;
35 pub const ITEM_SIZE: usize = 1;
36 pub const ITEM_COUNT: usize = 4;
37 pub fn nth0(&self) -> Byte {
38 Byte::new_unchecked(self.0.slice(0..1))
39 }
40 pub fn nth1(&self) -> Byte {
41 Byte::new_unchecked(self.0.slice(1..2))
42 }
43 pub fn nth2(&self) -> Byte {
44 Byte::new_unchecked(self.0.slice(2..3))
45 }
46 pub fn nth3(&self) -> Byte {
47 Byte::new_unchecked(self.0.slice(3..4))
48 }
49 pub fn raw_data(&self) -> molecule::bytes::Bytes {
50 self.as_bytes()
51 }
52 pub fn as_reader<'r>(&'r self) -> Uint32Reader<'r> {
53 Uint32Reader::new_unchecked(self.as_slice())
54 }
55}
56impl molecule::prelude::Entity for Uint32 {
57 type Builder = Uint32Builder;
58 const NAME: &'static str = "Uint32";
59 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
60 Uint32(data)
61 }
62 fn as_bytes(&self) -> molecule::bytes::Bytes {
63 self.0.clone()
64 }
65 fn as_slice(&self) -> &[u8] {
66 &self.0[..]
67 }
68 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
69 Uint32Reader::from_slice(slice).map(|reader| reader.to_entity())
70 }
71 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
72 Uint32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
73 }
74 fn new_builder() -> Self::Builder {
75 ::core::default::Default::default()
76 }
77 fn as_builder(self) -> Self::Builder {
78 Self::new_builder().set([self.nth0(), self.nth1(), self.nth2(), self.nth3()])
79 }
80}
81#[derive(Clone, Copy)]
82pub struct Uint32Reader<'r>(&'r [u8]);
83impl<'r> ::core::fmt::LowerHex for Uint32Reader<'r> {
84 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
85 use molecule::hex_string;
86 if f.alternate() {
87 write!(f, "0x")?;
88 }
89 write!(f, "{}", hex_string(self.as_slice()))
90 }
91}
92impl<'r> ::core::fmt::Debug for Uint32Reader<'r> {
93 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
94 write!(f, "{}({:#x})", Self::NAME, self)
95 }
96}
97impl<'r> ::core::fmt::Display for Uint32Reader<'r> {
98 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
99 use molecule::hex_string;
100 let raw_data = hex_string(&self.raw_data());
101 write!(f, "{}(0x{})", Self::NAME, raw_data)
102 }
103}
104impl<'r> Uint32Reader<'r> {
105 pub const TOTAL_SIZE: usize = 4;
106 pub const ITEM_SIZE: usize = 1;
107 pub const ITEM_COUNT: usize = 4;
108 pub fn nth0(&self) -> ByteReader<'r> {
109 ByteReader::new_unchecked(&self.as_slice()[0..1])
110 }
111 pub fn nth1(&self) -> ByteReader<'r> {
112 ByteReader::new_unchecked(&self.as_slice()[1..2])
113 }
114 pub fn nth2(&self) -> ByteReader<'r> {
115 ByteReader::new_unchecked(&self.as_slice()[2..3])
116 }
117 pub fn nth3(&self) -> ByteReader<'r> {
118 ByteReader::new_unchecked(&self.as_slice()[3..4])
119 }
120 pub fn raw_data(&self) -> &'r [u8] {
121 self.as_slice()
122 }
123}
124impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> {
125 type Entity = Uint32;
126 const NAME: &'static str = "Uint32Reader";
127 fn to_entity(&self) -> Self::Entity {
128 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
129 }
130 fn new_unchecked(slice: &'r [u8]) -> Self {
131 Uint32Reader(slice)
132 }
133 fn as_slice(&self) -> &'r [u8] {
134 self.0
135 }
136 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
137 use molecule::verification_error as ve;
138 let slice_len = slice.len();
139 if slice_len != Self::TOTAL_SIZE {
140 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
141 }
142 Ok(())
143 }
144}
145pub struct Uint32Builder(pub(crate) [Byte; 4]);
146impl ::core::fmt::Debug for Uint32Builder {
147 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
148 write!(f, "{}({:?})", Self::NAME, &self.0[..])
149 }
150}
151impl ::core::default::Default for Uint32Builder {
152 fn default() -> Self {
153 Uint32Builder([
154 Byte::default(),
155 Byte::default(),
156 Byte::default(),
157 Byte::default(),
158 ])
159 }
160}
161impl Uint32Builder {
162 pub const TOTAL_SIZE: usize = 4;
163 pub const ITEM_SIZE: usize = 1;
164 pub const ITEM_COUNT: usize = 4;
165 pub fn set(mut self, v: [Byte; 4]) -> Self {
166 self.0 = v;
167 self
168 }
169 pub fn nth0(mut self, v: Byte) -> Self {
170 self.0[0] = v;
171 self
172 }
173 pub fn nth1(mut self, v: Byte) -> Self {
174 self.0[1] = v;
175 self
176 }
177 pub fn nth2(mut self, v: Byte) -> Self {
178 self.0[2] = v;
179 self
180 }
181 pub fn nth3(mut self, v: Byte) -> Self {
182 self.0[3] = v;
183 self
184 }
185}
186impl molecule::prelude::Builder for Uint32Builder {
187 type Entity = Uint32;
188 const NAME: &'static str = "Uint32Builder";
189 fn expected_length(&self) -> usize {
190 Self::TOTAL_SIZE
191 }
192 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
193 writer.write_all(self.0[0].as_slice())?;
194 writer.write_all(self.0[1].as_slice())?;
195 writer.write_all(self.0[2].as_slice())?;
196 writer.write_all(self.0[3].as_slice())?;
197 Ok(())
198 }
199 fn build(&self) -> Self::Entity {
200 let mut inner = Vec::with_capacity(self.expected_length());
201 self.write(&mut inner)
202 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
203 Uint32::new_unchecked(inner.into())
204 }
205}
206#[derive(Clone)]
207pub struct Uint64(molecule::bytes::Bytes);
208impl ::core::fmt::LowerHex for Uint64 {
209 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
210 use molecule::hex_string;
211 if f.alternate() {
212 write!(f, "0x")?;
213 }
214 write!(f, "{}", hex_string(self.as_slice()))
215 }
216}
217impl ::core::fmt::Debug for Uint64 {
218 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
219 write!(f, "{}({:#x})", Self::NAME, self)
220 }
221}
222impl ::core::fmt::Display for Uint64 {
223 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
224 use molecule::hex_string;
225 let raw_data = hex_string(&self.raw_data());
226 write!(f, "{}(0x{})", Self::NAME, raw_data)
227 }
228}
229impl ::core::default::Default for Uint64 {
230 fn default() -> Self {
231 let v: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0];
232 Uint64::new_unchecked(v.into())
233 }
234}
235impl Uint64 {
236 pub const TOTAL_SIZE: usize = 8;
237 pub const ITEM_SIZE: usize = 1;
238 pub const ITEM_COUNT: usize = 8;
239 pub fn nth0(&self) -> Byte {
240 Byte::new_unchecked(self.0.slice(0..1))
241 }
242 pub fn nth1(&self) -> Byte {
243 Byte::new_unchecked(self.0.slice(1..2))
244 }
245 pub fn nth2(&self) -> Byte {
246 Byte::new_unchecked(self.0.slice(2..3))
247 }
248 pub fn nth3(&self) -> Byte {
249 Byte::new_unchecked(self.0.slice(3..4))
250 }
251 pub fn nth4(&self) -> Byte {
252 Byte::new_unchecked(self.0.slice(4..5))
253 }
254 pub fn nth5(&self) -> Byte {
255 Byte::new_unchecked(self.0.slice(5..6))
256 }
257 pub fn nth6(&self) -> Byte {
258 Byte::new_unchecked(self.0.slice(6..7))
259 }
260 pub fn nth7(&self) -> Byte {
261 Byte::new_unchecked(self.0.slice(7..8))
262 }
263 pub fn raw_data(&self) -> molecule::bytes::Bytes {
264 self.as_bytes()
265 }
266 pub fn as_reader<'r>(&'r self) -> Uint64Reader<'r> {
267 Uint64Reader::new_unchecked(self.as_slice())
268 }
269}
270impl molecule::prelude::Entity for Uint64 {
271 type Builder = Uint64Builder;
272 const NAME: &'static str = "Uint64";
273 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
274 Uint64(data)
275 }
276 fn as_bytes(&self) -> molecule::bytes::Bytes {
277 self.0.clone()
278 }
279 fn as_slice(&self) -> &[u8] {
280 &self.0[..]
281 }
282 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
283 Uint64Reader::from_slice(slice).map(|reader| reader.to_entity())
284 }
285 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
286 Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
287 }
288 fn new_builder() -> Self::Builder {
289 ::core::default::Default::default()
290 }
291 fn as_builder(self) -> Self::Builder {
292 Self::new_builder().set([
293 self.nth0(),
294 self.nth1(),
295 self.nth2(),
296 self.nth3(),
297 self.nth4(),
298 self.nth5(),
299 self.nth6(),
300 self.nth7(),
301 ])
302 }
303}
304#[derive(Clone, Copy)]
305pub struct Uint64Reader<'r>(&'r [u8]);
306impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> {
307 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
308 use molecule::hex_string;
309 if f.alternate() {
310 write!(f, "0x")?;
311 }
312 write!(f, "{}", hex_string(self.as_slice()))
313 }
314}
315impl<'r> ::core::fmt::Debug for Uint64Reader<'r> {
316 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
317 write!(f, "{}({:#x})", Self::NAME, self)
318 }
319}
320impl<'r> ::core::fmt::Display for Uint64Reader<'r> {
321 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
322 use molecule::hex_string;
323 let raw_data = hex_string(&self.raw_data());
324 write!(f, "{}(0x{})", Self::NAME, raw_data)
325 }
326}
327impl<'r> Uint64Reader<'r> {
328 pub const TOTAL_SIZE: usize = 8;
329 pub const ITEM_SIZE: usize = 1;
330 pub const ITEM_COUNT: usize = 8;
331 pub fn nth0(&self) -> ByteReader<'r> {
332 ByteReader::new_unchecked(&self.as_slice()[0..1])
333 }
334 pub fn nth1(&self) -> ByteReader<'r> {
335 ByteReader::new_unchecked(&self.as_slice()[1..2])
336 }
337 pub fn nth2(&self) -> ByteReader<'r> {
338 ByteReader::new_unchecked(&self.as_slice()[2..3])
339 }
340 pub fn nth3(&self) -> ByteReader<'r> {
341 ByteReader::new_unchecked(&self.as_slice()[3..4])
342 }
343 pub fn nth4(&self) -> ByteReader<'r> {
344 ByteReader::new_unchecked(&self.as_slice()[4..5])
345 }
346 pub fn nth5(&self) -> ByteReader<'r> {
347 ByteReader::new_unchecked(&self.as_slice()[5..6])
348 }
349 pub fn nth6(&self) -> ByteReader<'r> {
350 ByteReader::new_unchecked(&self.as_slice()[6..7])
351 }
352 pub fn nth7(&self) -> ByteReader<'r> {
353 ByteReader::new_unchecked(&self.as_slice()[7..8])
354 }
355 pub fn raw_data(&self) -> &'r [u8] {
356 self.as_slice()
357 }
358}
359impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> {
360 type Entity = Uint64;
361 const NAME: &'static str = "Uint64Reader";
362 fn to_entity(&self) -> Self::Entity {
363 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
364 }
365 fn new_unchecked(slice: &'r [u8]) -> Self {
366 Uint64Reader(slice)
367 }
368 fn as_slice(&self) -> &'r [u8] {
369 self.0
370 }
371 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
372 use molecule::verification_error as ve;
373 let slice_len = slice.len();
374 if slice_len != Self::TOTAL_SIZE {
375 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
376 }
377 Ok(())
378 }
379}
380pub struct Uint64Builder(pub(crate) [Byte; 8]);
381impl ::core::fmt::Debug for Uint64Builder {
382 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
383 write!(f, "{}({:?})", Self::NAME, &self.0[..])
384 }
385}
386impl ::core::default::Default for Uint64Builder {
387 fn default() -> Self {
388 Uint64Builder([
389 Byte::default(),
390 Byte::default(),
391 Byte::default(),
392 Byte::default(),
393 Byte::default(),
394 Byte::default(),
395 Byte::default(),
396 Byte::default(),
397 ])
398 }
399}
400impl Uint64Builder {
401 pub const TOTAL_SIZE: usize = 8;
402 pub const ITEM_SIZE: usize = 1;
403 pub const ITEM_COUNT: usize = 8;
404 pub fn set(mut self, v: [Byte; 8]) -> Self {
405 self.0 = v;
406 self
407 }
408 pub fn nth0(mut self, v: Byte) -> Self {
409 self.0[0] = v;
410 self
411 }
412 pub fn nth1(mut self, v: Byte) -> Self {
413 self.0[1] = v;
414 self
415 }
416 pub fn nth2(mut self, v: Byte) -> Self {
417 self.0[2] = v;
418 self
419 }
420 pub fn nth3(mut self, v: Byte) -> Self {
421 self.0[3] = v;
422 self
423 }
424 pub fn nth4(mut self, v: Byte) -> Self {
425 self.0[4] = v;
426 self
427 }
428 pub fn nth5(mut self, v: Byte) -> Self {
429 self.0[5] = v;
430 self
431 }
432 pub fn nth6(mut self, v: Byte) -> Self {
433 self.0[6] = v;
434 self
435 }
436 pub fn nth7(mut self, v: Byte) -> Self {
437 self.0[7] = v;
438 self
439 }
440}
441impl molecule::prelude::Builder for Uint64Builder {
442 type Entity = Uint64;
443 const NAME: &'static str = "Uint64Builder";
444 fn expected_length(&self) -> usize {
445 Self::TOTAL_SIZE
446 }
447 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
448 writer.write_all(self.0[0].as_slice())?;
449 writer.write_all(self.0[1].as_slice())?;
450 writer.write_all(self.0[2].as_slice())?;
451 writer.write_all(self.0[3].as_slice())?;
452 writer.write_all(self.0[4].as_slice())?;
453 writer.write_all(self.0[5].as_slice())?;
454 writer.write_all(self.0[6].as_slice())?;
455 writer.write_all(self.0[7].as_slice())?;
456 Ok(())
457 }
458 fn build(&self) -> Self::Entity {
459 let mut inner = Vec::with_capacity(self.expected_length());
460 self.write(&mut inner)
461 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
462 Uint64::new_unchecked(inner.into())
463 }
464}
465#[derive(Clone)]
466pub struct Uint128(molecule::bytes::Bytes);
467impl ::core::fmt::LowerHex for Uint128 {
468 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
469 use molecule::hex_string;
470 if f.alternate() {
471 write!(f, "0x")?;
472 }
473 write!(f, "{}", hex_string(self.as_slice()))
474 }
475}
476impl ::core::fmt::Debug for Uint128 {
477 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
478 write!(f, "{}({:#x})", Self::NAME, self)
479 }
480}
481impl ::core::fmt::Display for Uint128 {
482 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
483 use molecule::hex_string;
484 let raw_data = hex_string(&self.raw_data());
485 write!(f, "{}(0x{})", Self::NAME, raw_data)
486 }
487}
488impl ::core::default::Default for Uint128 {
489 fn default() -> Self {
490 let v: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
491 Uint128::new_unchecked(v.into())
492 }
493}
494impl Uint128 {
495 pub const TOTAL_SIZE: usize = 16;
496 pub const ITEM_SIZE: usize = 1;
497 pub const ITEM_COUNT: usize = 16;
498 pub fn nth0(&self) -> Byte {
499 Byte::new_unchecked(self.0.slice(0..1))
500 }
501 pub fn nth1(&self) -> Byte {
502 Byte::new_unchecked(self.0.slice(1..2))
503 }
504 pub fn nth2(&self) -> Byte {
505 Byte::new_unchecked(self.0.slice(2..3))
506 }
507 pub fn nth3(&self) -> Byte {
508 Byte::new_unchecked(self.0.slice(3..4))
509 }
510 pub fn nth4(&self) -> Byte {
511 Byte::new_unchecked(self.0.slice(4..5))
512 }
513 pub fn nth5(&self) -> Byte {
514 Byte::new_unchecked(self.0.slice(5..6))
515 }
516 pub fn nth6(&self) -> Byte {
517 Byte::new_unchecked(self.0.slice(6..7))
518 }
519 pub fn nth7(&self) -> Byte {
520 Byte::new_unchecked(self.0.slice(7..8))
521 }
522 pub fn nth8(&self) -> Byte {
523 Byte::new_unchecked(self.0.slice(8..9))
524 }
525 pub fn nth9(&self) -> Byte {
526 Byte::new_unchecked(self.0.slice(9..10))
527 }
528 pub fn nth10(&self) -> Byte {
529 Byte::new_unchecked(self.0.slice(10..11))
530 }
531 pub fn nth11(&self) -> Byte {
532 Byte::new_unchecked(self.0.slice(11..12))
533 }
534 pub fn nth12(&self) -> Byte {
535 Byte::new_unchecked(self.0.slice(12..13))
536 }
537 pub fn nth13(&self) -> Byte {
538 Byte::new_unchecked(self.0.slice(13..14))
539 }
540 pub fn nth14(&self) -> Byte {
541 Byte::new_unchecked(self.0.slice(14..15))
542 }
543 pub fn nth15(&self) -> Byte {
544 Byte::new_unchecked(self.0.slice(15..16))
545 }
546 pub fn raw_data(&self) -> molecule::bytes::Bytes {
547 self.as_bytes()
548 }
549 pub fn as_reader<'r>(&'r self) -> Uint128Reader<'r> {
550 Uint128Reader::new_unchecked(self.as_slice())
551 }
552}
553impl molecule::prelude::Entity for Uint128 {
554 type Builder = Uint128Builder;
555 const NAME: &'static str = "Uint128";
556 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
557 Uint128(data)
558 }
559 fn as_bytes(&self) -> molecule::bytes::Bytes {
560 self.0.clone()
561 }
562 fn as_slice(&self) -> &[u8] {
563 &self.0[..]
564 }
565 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
566 Uint128Reader::from_slice(slice).map(|reader| reader.to_entity())
567 }
568 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
569 Uint128Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
570 }
571 fn new_builder() -> Self::Builder {
572 ::core::default::Default::default()
573 }
574 fn as_builder(self) -> Self::Builder {
575 Self::new_builder().set([
576 self.nth0(),
577 self.nth1(),
578 self.nth2(),
579 self.nth3(),
580 self.nth4(),
581 self.nth5(),
582 self.nth6(),
583 self.nth7(),
584 self.nth8(),
585 self.nth9(),
586 self.nth10(),
587 self.nth11(),
588 self.nth12(),
589 self.nth13(),
590 self.nth14(),
591 self.nth15(),
592 ])
593 }
594}
595#[derive(Clone, Copy)]
596pub struct Uint128Reader<'r>(&'r [u8]);
597impl<'r> ::core::fmt::LowerHex for Uint128Reader<'r> {
598 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
599 use molecule::hex_string;
600 if f.alternate() {
601 write!(f, "0x")?;
602 }
603 write!(f, "{}", hex_string(self.as_slice()))
604 }
605}
606impl<'r> ::core::fmt::Debug for Uint128Reader<'r> {
607 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
608 write!(f, "{}({:#x})", Self::NAME, self)
609 }
610}
611impl<'r> ::core::fmt::Display for Uint128Reader<'r> {
612 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
613 use molecule::hex_string;
614 let raw_data = hex_string(&self.raw_data());
615 write!(f, "{}(0x{})", Self::NAME, raw_data)
616 }
617}
618impl<'r> Uint128Reader<'r> {
619 pub const TOTAL_SIZE: usize = 16;
620 pub const ITEM_SIZE: usize = 1;
621 pub const ITEM_COUNT: usize = 16;
622 pub fn nth0(&self) -> ByteReader<'r> {
623 ByteReader::new_unchecked(&self.as_slice()[0..1])
624 }
625 pub fn nth1(&self) -> ByteReader<'r> {
626 ByteReader::new_unchecked(&self.as_slice()[1..2])
627 }
628 pub fn nth2(&self) -> ByteReader<'r> {
629 ByteReader::new_unchecked(&self.as_slice()[2..3])
630 }
631 pub fn nth3(&self) -> ByteReader<'r> {
632 ByteReader::new_unchecked(&self.as_slice()[3..4])
633 }
634 pub fn nth4(&self) -> ByteReader<'r> {
635 ByteReader::new_unchecked(&self.as_slice()[4..5])
636 }
637 pub fn nth5(&self) -> ByteReader<'r> {
638 ByteReader::new_unchecked(&self.as_slice()[5..6])
639 }
640 pub fn nth6(&self) -> ByteReader<'r> {
641 ByteReader::new_unchecked(&self.as_slice()[6..7])
642 }
643 pub fn nth7(&self) -> ByteReader<'r> {
644 ByteReader::new_unchecked(&self.as_slice()[7..8])
645 }
646 pub fn nth8(&self) -> ByteReader<'r> {
647 ByteReader::new_unchecked(&self.as_slice()[8..9])
648 }
649 pub fn nth9(&self) -> ByteReader<'r> {
650 ByteReader::new_unchecked(&self.as_slice()[9..10])
651 }
652 pub fn nth10(&self) -> ByteReader<'r> {
653 ByteReader::new_unchecked(&self.as_slice()[10..11])
654 }
655 pub fn nth11(&self) -> ByteReader<'r> {
656 ByteReader::new_unchecked(&self.as_slice()[11..12])
657 }
658 pub fn nth12(&self) -> ByteReader<'r> {
659 ByteReader::new_unchecked(&self.as_slice()[12..13])
660 }
661 pub fn nth13(&self) -> ByteReader<'r> {
662 ByteReader::new_unchecked(&self.as_slice()[13..14])
663 }
664 pub fn nth14(&self) -> ByteReader<'r> {
665 ByteReader::new_unchecked(&self.as_slice()[14..15])
666 }
667 pub fn nth15(&self) -> ByteReader<'r> {
668 ByteReader::new_unchecked(&self.as_slice()[15..16])
669 }
670 pub fn raw_data(&self) -> &'r [u8] {
671 self.as_slice()
672 }
673}
674impl<'r> molecule::prelude::Reader<'r> for Uint128Reader<'r> {
675 type Entity = Uint128;
676 const NAME: &'static str = "Uint128Reader";
677 fn to_entity(&self) -> Self::Entity {
678 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
679 }
680 fn new_unchecked(slice: &'r [u8]) -> Self {
681 Uint128Reader(slice)
682 }
683 fn as_slice(&self) -> &'r [u8] {
684 self.0
685 }
686 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
687 use molecule::verification_error as ve;
688 let slice_len = slice.len();
689 if slice_len != Self::TOTAL_SIZE {
690 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
691 }
692 Ok(())
693 }
694}
695pub struct Uint128Builder(pub(crate) [Byte; 16]);
696impl ::core::fmt::Debug for Uint128Builder {
697 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
698 write!(f, "{}({:?})", Self::NAME, &self.0[..])
699 }
700}
701impl ::core::default::Default for Uint128Builder {
702 fn default() -> Self {
703 Uint128Builder([
704 Byte::default(),
705 Byte::default(),
706 Byte::default(),
707 Byte::default(),
708 Byte::default(),
709 Byte::default(),
710 Byte::default(),
711 Byte::default(),
712 Byte::default(),
713 Byte::default(),
714 Byte::default(),
715 Byte::default(),
716 Byte::default(),
717 Byte::default(),
718 Byte::default(),
719 Byte::default(),
720 ])
721 }
722}
723impl Uint128Builder {
724 pub const TOTAL_SIZE: usize = 16;
725 pub const ITEM_SIZE: usize = 1;
726 pub const ITEM_COUNT: usize = 16;
727 pub fn set(mut self, v: [Byte; 16]) -> Self {
728 self.0 = v;
729 self
730 }
731 pub fn nth0(mut self, v: Byte) -> Self {
732 self.0[0] = v;
733 self
734 }
735 pub fn nth1(mut self, v: Byte) -> Self {
736 self.0[1] = v;
737 self
738 }
739 pub fn nth2(mut self, v: Byte) -> Self {
740 self.0[2] = v;
741 self
742 }
743 pub fn nth3(mut self, v: Byte) -> Self {
744 self.0[3] = v;
745 self
746 }
747 pub fn nth4(mut self, v: Byte) -> Self {
748 self.0[4] = v;
749 self
750 }
751 pub fn nth5(mut self, v: Byte) -> Self {
752 self.0[5] = v;
753 self
754 }
755 pub fn nth6(mut self, v: Byte) -> Self {
756 self.0[6] = v;
757 self
758 }
759 pub fn nth7(mut self, v: Byte) -> Self {
760 self.0[7] = v;
761 self
762 }
763 pub fn nth8(mut self, v: Byte) -> Self {
764 self.0[8] = v;
765 self
766 }
767 pub fn nth9(mut self, v: Byte) -> Self {
768 self.0[9] = v;
769 self
770 }
771 pub fn nth10(mut self, v: Byte) -> Self {
772 self.0[10] = v;
773 self
774 }
775 pub fn nth11(mut self, v: Byte) -> Self {
776 self.0[11] = v;
777 self
778 }
779 pub fn nth12(mut self, v: Byte) -> Self {
780 self.0[12] = v;
781 self
782 }
783 pub fn nth13(mut self, v: Byte) -> Self {
784 self.0[13] = v;
785 self
786 }
787 pub fn nth14(mut self, v: Byte) -> Self {
788 self.0[14] = v;
789 self
790 }
791 pub fn nth15(mut self, v: Byte) -> Self {
792 self.0[15] = v;
793 self
794 }
795}
796impl molecule::prelude::Builder for Uint128Builder {
797 type Entity = Uint128;
798 const NAME: &'static str = "Uint128Builder";
799 fn expected_length(&self) -> usize {
800 Self::TOTAL_SIZE
801 }
802 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
803 writer.write_all(self.0[0].as_slice())?;
804 writer.write_all(self.0[1].as_slice())?;
805 writer.write_all(self.0[2].as_slice())?;
806 writer.write_all(self.0[3].as_slice())?;
807 writer.write_all(self.0[4].as_slice())?;
808 writer.write_all(self.0[5].as_slice())?;
809 writer.write_all(self.0[6].as_slice())?;
810 writer.write_all(self.0[7].as_slice())?;
811 writer.write_all(self.0[8].as_slice())?;
812 writer.write_all(self.0[9].as_slice())?;
813 writer.write_all(self.0[10].as_slice())?;
814 writer.write_all(self.0[11].as_slice())?;
815 writer.write_all(self.0[12].as_slice())?;
816 writer.write_all(self.0[13].as_slice())?;
817 writer.write_all(self.0[14].as_slice())?;
818 writer.write_all(self.0[15].as_slice())?;
819 Ok(())
820 }
821 fn build(&self) -> Self::Entity {
822 let mut inner = Vec::with_capacity(self.expected_length());
823 self.write(&mut inner)
824 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
825 Uint128::new_unchecked(inner.into())
826 }
827}
828#[derive(Clone)]
829pub struct Byte32(molecule::bytes::Bytes);
830impl ::core::fmt::LowerHex for Byte32 {
831 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
832 use molecule::hex_string;
833 if f.alternate() {
834 write!(f, "0x")?;
835 }
836 write!(f, "{}", hex_string(self.as_slice()))
837 }
838}
839impl ::core::fmt::Debug for Byte32 {
840 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
841 write!(f, "{}({:#x})", Self::NAME, self)
842 }
843}
844impl ::core::fmt::Display for Byte32 {
845 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
846 use molecule::hex_string;
847 let raw_data = hex_string(&self.raw_data());
848 write!(f, "{}(0x{})", Self::NAME, raw_data)
849 }
850}
851impl ::core::default::Default for Byte32 {
852 fn default() -> Self {
853 let v: Vec<u8> = vec![
854 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
855 0, 0, 0,
856 ];
857 Byte32::new_unchecked(v.into())
858 }
859}
860impl Byte32 {
861 pub const TOTAL_SIZE: usize = 32;
862 pub const ITEM_SIZE: usize = 1;
863 pub const ITEM_COUNT: usize = 32;
864 pub fn nth0(&self) -> Byte {
865 Byte::new_unchecked(self.0.slice(0..1))
866 }
867 pub fn nth1(&self) -> Byte {
868 Byte::new_unchecked(self.0.slice(1..2))
869 }
870 pub fn nth2(&self) -> Byte {
871 Byte::new_unchecked(self.0.slice(2..3))
872 }
873 pub fn nth3(&self) -> Byte {
874 Byte::new_unchecked(self.0.slice(3..4))
875 }
876 pub fn nth4(&self) -> Byte {
877 Byte::new_unchecked(self.0.slice(4..5))
878 }
879 pub fn nth5(&self) -> Byte {
880 Byte::new_unchecked(self.0.slice(5..6))
881 }
882 pub fn nth6(&self) -> Byte {
883 Byte::new_unchecked(self.0.slice(6..7))
884 }
885 pub fn nth7(&self) -> Byte {
886 Byte::new_unchecked(self.0.slice(7..8))
887 }
888 pub fn nth8(&self) -> Byte {
889 Byte::new_unchecked(self.0.slice(8..9))
890 }
891 pub fn nth9(&self) -> Byte {
892 Byte::new_unchecked(self.0.slice(9..10))
893 }
894 pub fn nth10(&self) -> Byte {
895 Byte::new_unchecked(self.0.slice(10..11))
896 }
897 pub fn nth11(&self) -> Byte {
898 Byte::new_unchecked(self.0.slice(11..12))
899 }
900 pub fn nth12(&self) -> Byte {
901 Byte::new_unchecked(self.0.slice(12..13))
902 }
903 pub fn nth13(&self) -> Byte {
904 Byte::new_unchecked(self.0.slice(13..14))
905 }
906 pub fn nth14(&self) -> Byte {
907 Byte::new_unchecked(self.0.slice(14..15))
908 }
909 pub fn nth15(&self) -> Byte {
910 Byte::new_unchecked(self.0.slice(15..16))
911 }
912 pub fn nth16(&self) -> Byte {
913 Byte::new_unchecked(self.0.slice(16..17))
914 }
915 pub fn nth17(&self) -> Byte {
916 Byte::new_unchecked(self.0.slice(17..18))
917 }
918 pub fn nth18(&self) -> Byte {
919 Byte::new_unchecked(self.0.slice(18..19))
920 }
921 pub fn nth19(&self) -> Byte {
922 Byte::new_unchecked(self.0.slice(19..20))
923 }
924 pub fn nth20(&self) -> Byte {
925 Byte::new_unchecked(self.0.slice(20..21))
926 }
927 pub fn nth21(&self) -> Byte {
928 Byte::new_unchecked(self.0.slice(21..22))
929 }
930 pub fn nth22(&self) -> Byte {
931 Byte::new_unchecked(self.0.slice(22..23))
932 }
933 pub fn nth23(&self) -> Byte {
934 Byte::new_unchecked(self.0.slice(23..24))
935 }
936 pub fn nth24(&self) -> Byte {
937 Byte::new_unchecked(self.0.slice(24..25))
938 }
939 pub fn nth25(&self) -> Byte {
940 Byte::new_unchecked(self.0.slice(25..26))
941 }
942 pub fn nth26(&self) -> Byte {
943 Byte::new_unchecked(self.0.slice(26..27))
944 }
945 pub fn nth27(&self) -> Byte {
946 Byte::new_unchecked(self.0.slice(27..28))
947 }
948 pub fn nth28(&self) -> Byte {
949 Byte::new_unchecked(self.0.slice(28..29))
950 }
951 pub fn nth29(&self) -> Byte {
952 Byte::new_unchecked(self.0.slice(29..30))
953 }
954 pub fn nth30(&self) -> Byte {
955 Byte::new_unchecked(self.0.slice(30..31))
956 }
957 pub fn nth31(&self) -> Byte {
958 Byte::new_unchecked(self.0.slice(31..32))
959 }
960 pub fn raw_data(&self) -> molecule::bytes::Bytes {
961 self.as_bytes()
962 }
963 pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> {
964 Byte32Reader::new_unchecked(self.as_slice())
965 }
966}
967impl molecule::prelude::Entity for Byte32 {
968 type Builder = Byte32Builder;
969 const NAME: &'static str = "Byte32";
970 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
971 Byte32(data)
972 }
973 fn as_bytes(&self) -> molecule::bytes::Bytes {
974 self.0.clone()
975 }
976 fn as_slice(&self) -> &[u8] {
977 &self.0[..]
978 }
979 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
980 Byte32Reader::from_slice(slice).map(|reader| reader.to_entity())
981 }
982 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
983 Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
984 }
985 fn new_builder() -> Self::Builder {
986 ::core::default::Default::default()
987 }
988 fn as_builder(self) -> Self::Builder {
989 Self::new_builder().set([
990 self.nth0(),
991 self.nth1(),
992 self.nth2(),
993 self.nth3(),
994 self.nth4(),
995 self.nth5(),
996 self.nth6(),
997 self.nth7(),
998 self.nth8(),
999 self.nth9(),
1000 self.nth10(),
1001 self.nth11(),
1002 self.nth12(),
1003 self.nth13(),
1004 self.nth14(),
1005 self.nth15(),
1006 self.nth16(),
1007 self.nth17(),
1008 self.nth18(),
1009 self.nth19(),
1010 self.nth20(),
1011 self.nth21(),
1012 self.nth22(),
1013 self.nth23(),
1014 self.nth24(),
1015 self.nth25(),
1016 self.nth26(),
1017 self.nth27(),
1018 self.nth28(),
1019 self.nth29(),
1020 self.nth30(),
1021 self.nth31(),
1022 ])
1023 }
1024}
1025#[derive(Clone, Copy)]
1026pub struct Byte32Reader<'r>(&'r [u8]);
1027impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> {
1028 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1029 use molecule::hex_string;
1030 if f.alternate() {
1031 write!(f, "0x")?;
1032 }
1033 write!(f, "{}", hex_string(self.as_slice()))
1034 }
1035}
1036impl<'r> ::core::fmt::Debug for Byte32Reader<'r> {
1037 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1038 write!(f, "{}({:#x})", Self::NAME, self)
1039 }
1040}
1041impl<'r> ::core::fmt::Display for Byte32Reader<'r> {
1042 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1043 use molecule::hex_string;
1044 let raw_data = hex_string(&self.raw_data());
1045 write!(f, "{}(0x{})", Self::NAME, raw_data)
1046 }
1047}
1048impl<'r> Byte32Reader<'r> {
1049 pub const TOTAL_SIZE: usize = 32;
1050 pub const ITEM_SIZE: usize = 1;
1051 pub const ITEM_COUNT: usize = 32;
1052 pub fn nth0(&self) -> ByteReader<'r> {
1053 ByteReader::new_unchecked(&self.as_slice()[0..1])
1054 }
1055 pub fn nth1(&self) -> ByteReader<'r> {
1056 ByteReader::new_unchecked(&self.as_slice()[1..2])
1057 }
1058 pub fn nth2(&self) -> ByteReader<'r> {
1059 ByteReader::new_unchecked(&self.as_slice()[2..3])
1060 }
1061 pub fn nth3(&self) -> ByteReader<'r> {
1062 ByteReader::new_unchecked(&self.as_slice()[3..4])
1063 }
1064 pub fn nth4(&self) -> ByteReader<'r> {
1065 ByteReader::new_unchecked(&self.as_slice()[4..5])
1066 }
1067 pub fn nth5(&self) -> ByteReader<'r> {
1068 ByteReader::new_unchecked(&self.as_slice()[5..6])
1069 }
1070 pub fn nth6(&self) -> ByteReader<'r> {
1071 ByteReader::new_unchecked(&self.as_slice()[6..7])
1072 }
1073 pub fn nth7(&self) -> ByteReader<'r> {
1074 ByteReader::new_unchecked(&self.as_slice()[7..8])
1075 }
1076 pub fn nth8(&self) -> ByteReader<'r> {
1077 ByteReader::new_unchecked(&self.as_slice()[8..9])
1078 }
1079 pub fn nth9(&self) -> ByteReader<'r> {
1080 ByteReader::new_unchecked(&self.as_slice()[9..10])
1081 }
1082 pub fn nth10(&self) -> ByteReader<'r> {
1083 ByteReader::new_unchecked(&self.as_slice()[10..11])
1084 }
1085 pub fn nth11(&self) -> ByteReader<'r> {
1086 ByteReader::new_unchecked(&self.as_slice()[11..12])
1087 }
1088 pub fn nth12(&self) -> ByteReader<'r> {
1089 ByteReader::new_unchecked(&self.as_slice()[12..13])
1090 }
1091 pub fn nth13(&self) -> ByteReader<'r> {
1092 ByteReader::new_unchecked(&self.as_slice()[13..14])
1093 }
1094 pub fn nth14(&self) -> ByteReader<'r> {
1095 ByteReader::new_unchecked(&self.as_slice()[14..15])
1096 }
1097 pub fn nth15(&self) -> ByteReader<'r> {
1098 ByteReader::new_unchecked(&self.as_slice()[15..16])
1099 }
1100 pub fn nth16(&self) -> ByteReader<'r> {
1101 ByteReader::new_unchecked(&self.as_slice()[16..17])
1102 }
1103 pub fn nth17(&self) -> ByteReader<'r> {
1104 ByteReader::new_unchecked(&self.as_slice()[17..18])
1105 }
1106 pub fn nth18(&self) -> ByteReader<'r> {
1107 ByteReader::new_unchecked(&self.as_slice()[18..19])
1108 }
1109 pub fn nth19(&self) -> ByteReader<'r> {
1110 ByteReader::new_unchecked(&self.as_slice()[19..20])
1111 }
1112 pub fn nth20(&self) -> ByteReader<'r> {
1113 ByteReader::new_unchecked(&self.as_slice()[20..21])
1114 }
1115 pub fn nth21(&self) -> ByteReader<'r> {
1116 ByteReader::new_unchecked(&self.as_slice()[21..22])
1117 }
1118 pub fn nth22(&self) -> ByteReader<'r> {
1119 ByteReader::new_unchecked(&self.as_slice()[22..23])
1120 }
1121 pub fn nth23(&self) -> ByteReader<'r> {
1122 ByteReader::new_unchecked(&self.as_slice()[23..24])
1123 }
1124 pub fn nth24(&self) -> ByteReader<'r> {
1125 ByteReader::new_unchecked(&self.as_slice()[24..25])
1126 }
1127 pub fn nth25(&self) -> ByteReader<'r> {
1128 ByteReader::new_unchecked(&self.as_slice()[25..26])
1129 }
1130 pub fn nth26(&self) -> ByteReader<'r> {
1131 ByteReader::new_unchecked(&self.as_slice()[26..27])
1132 }
1133 pub fn nth27(&self) -> ByteReader<'r> {
1134 ByteReader::new_unchecked(&self.as_slice()[27..28])
1135 }
1136 pub fn nth28(&self) -> ByteReader<'r> {
1137 ByteReader::new_unchecked(&self.as_slice()[28..29])
1138 }
1139 pub fn nth29(&self) -> ByteReader<'r> {
1140 ByteReader::new_unchecked(&self.as_slice()[29..30])
1141 }
1142 pub fn nth30(&self) -> ByteReader<'r> {
1143 ByteReader::new_unchecked(&self.as_slice()[30..31])
1144 }
1145 pub fn nth31(&self) -> ByteReader<'r> {
1146 ByteReader::new_unchecked(&self.as_slice()[31..32])
1147 }
1148 pub fn raw_data(&self) -> &'r [u8] {
1149 self.as_slice()
1150 }
1151}
1152impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> {
1153 type Entity = Byte32;
1154 const NAME: &'static str = "Byte32Reader";
1155 fn to_entity(&self) -> Self::Entity {
1156 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1157 }
1158 fn new_unchecked(slice: &'r [u8]) -> Self {
1159 Byte32Reader(slice)
1160 }
1161 fn as_slice(&self) -> &'r [u8] {
1162 self.0
1163 }
1164 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1165 use molecule::verification_error as ve;
1166 let slice_len = slice.len();
1167 if slice_len != Self::TOTAL_SIZE {
1168 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1169 }
1170 Ok(())
1171 }
1172}
1173pub struct Byte32Builder(pub(crate) [Byte; 32]);
1174impl ::core::fmt::Debug for Byte32Builder {
1175 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1176 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1177 }
1178}
1179impl ::core::default::Default for Byte32Builder {
1180 fn default() -> Self {
1181 Byte32Builder([
1182 Byte::default(),
1183 Byte::default(),
1184 Byte::default(),
1185 Byte::default(),
1186 Byte::default(),
1187 Byte::default(),
1188 Byte::default(),
1189 Byte::default(),
1190 Byte::default(),
1191 Byte::default(),
1192 Byte::default(),
1193 Byte::default(),
1194 Byte::default(),
1195 Byte::default(),
1196 Byte::default(),
1197 Byte::default(),
1198 Byte::default(),
1199 Byte::default(),
1200 Byte::default(),
1201 Byte::default(),
1202 Byte::default(),
1203 Byte::default(),
1204 Byte::default(),
1205 Byte::default(),
1206 Byte::default(),
1207 Byte::default(),
1208 Byte::default(),
1209 Byte::default(),
1210 Byte::default(),
1211 Byte::default(),
1212 Byte::default(),
1213 Byte::default(),
1214 ])
1215 }
1216}
1217impl Byte32Builder {
1218 pub const TOTAL_SIZE: usize = 32;
1219 pub const ITEM_SIZE: usize = 1;
1220 pub const ITEM_COUNT: usize = 32;
1221 pub fn set(mut self, v: [Byte; 32]) -> Self {
1222 self.0 = v;
1223 self
1224 }
1225 pub fn nth0(mut self, v: Byte) -> Self {
1226 self.0[0] = v;
1227 self
1228 }
1229 pub fn nth1(mut self, v: Byte) -> Self {
1230 self.0[1] = v;
1231 self
1232 }
1233 pub fn nth2(mut self, v: Byte) -> Self {
1234 self.0[2] = v;
1235 self
1236 }
1237 pub fn nth3(mut self, v: Byte) -> Self {
1238 self.0[3] = v;
1239 self
1240 }
1241 pub fn nth4(mut self, v: Byte) -> Self {
1242 self.0[4] = v;
1243 self
1244 }
1245 pub fn nth5(mut self, v: Byte) -> Self {
1246 self.0[5] = v;
1247 self
1248 }
1249 pub fn nth6(mut self, v: Byte) -> Self {
1250 self.0[6] = v;
1251 self
1252 }
1253 pub fn nth7(mut self, v: Byte) -> Self {
1254 self.0[7] = v;
1255 self
1256 }
1257 pub fn nth8(mut self, v: Byte) -> Self {
1258 self.0[8] = v;
1259 self
1260 }
1261 pub fn nth9(mut self, v: Byte) -> Self {
1262 self.0[9] = v;
1263 self
1264 }
1265 pub fn nth10(mut self, v: Byte) -> Self {
1266 self.0[10] = v;
1267 self
1268 }
1269 pub fn nth11(mut self, v: Byte) -> Self {
1270 self.0[11] = v;
1271 self
1272 }
1273 pub fn nth12(mut self, v: Byte) -> Self {
1274 self.0[12] = v;
1275 self
1276 }
1277 pub fn nth13(mut self, v: Byte) -> Self {
1278 self.0[13] = v;
1279 self
1280 }
1281 pub fn nth14(mut self, v: Byte) -> Self {
1282 self.0[14] = v;
1283 self
1284 }
1285 pub fn nth15(mut self, v: Byte) -> Self {
1286 self.0[15] = v;
1287 self
1288 }
1289 pub fn nth16(mut self, v: Byte) -> Self {
1290 self.0[16] = v;
1291 self
1292 }
1293 pub fn nth17(mut self, v: Byte) -> Self {
1294 self.0[17] = v;
1295 self
1296 }
1297 pub fn nth18(mut self, v: Byte) -> Self {
1298 self.0[18] = v;
1299 self
1300 }
1301 pub fn nth19(mut self, v: Byte) -> Self {
1302 self.0[19] = v;
1303 self
1304 }
1305 pub fn nth20(mut self, v: Byte) -> Self {
1306 self.0[20] = v;
1307 self
1308 }
1309 pub fn nth21(mut self, v: Byte) -> Self {
1310 self.0[21] = v;
1311 self
1312 }
1313 pub fn nth22(mut self, v: Byte) -> Self {
1314 self.0[22] = v;
1315 self
1316 }
1317 pub fn nth23(mut self, v: Byte) -> Self {
1318 self.0[23] = v;
1319 self
1320 }
1321 pub fn nth24(mut self, v: Byte) -> Self {
1322 self.0[24] = v;
1323 self
1324 }
1325 pub fn nth25(mut self, v: Byte) -> Self {
1326 self.0[25] = v;
1327 self
1328 }
1329 pub fn nth26(mut self, v: Byte) -> Self {
1330 self.0[26] = v;
1331 self
1332 }
1333 pub fn nth27(mut self, v: Byte) -> Self {
1334 self.0[27] = v;
1335 self
1336 }
1337 pub fn nth28(mut self, v: Byte) -> Self {
1338 self.0[28] = v;
1339 self
1340 }
1341 pub fn nth29(mut self, v: Byte) -> Self {
1342 self.0[29] = v;
1343 self
1344 }
1345 pub fn nth30(mut self, v: Byte) -> Self {
1346 self.0[30] = v;
1347 self
1348 }
1349 pub fn nth31(mut self, v: Byte) -> Self {
1350 self.0[31] = v;
1351 self
1352 }
1353}
1354impl molecule::prelude::Builder for Byte32Builder {
1355 type Entity = Byte32;
1356 const NAME: &'static str = "Byte32Builder";
1357 fn expected_length(&self) -> usize {
1358 Self::TOTAL_SIZE
1359 }
1360 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1361 writer.write_all(self.0[0].as_slice())?;
1362 writer.write_all(self.0[1].as_slice())?;
1363 writer.write_all(self.0[2].as_slice())?;
1364 writer.write_all(self.0[3].as_slice())?;
1365 writer.write_all(self.0[4].as_slice())?;
1366 writer.write_all(self.0[5].as_slice())?;
1367 writer.write_all(self.0[6].as_slice())?;
1368 writer.write_all(self.0[7].as_slice())?;
1369 writer.write_all(self.0[8].as_slice())?;
1370 writer.write_all(self.0[9].as_slice())?;
1371 writer.write_all(self.0[10].as_slice())?;
1372 writer.write_all(self.0[11].as_slice())?;
1373 writer.write_all(self.0[12].as_slice())?;
1374 writer.write_all(self.0[13].as_slice())?;
1375 writer.write_all(self.0[14].as_slice())?;
1376 writer.write_all(self.0[15].as_slice())?;
1377 writer.write_all(self.0[16].as_slice())?;
1378 writer.write_all(self.0[17].as_slice())?;
1379 writer.write_all(self.0[18].as_slice())?;
1380 writer.write_all(self.0[19].as_slice())?;
1381 writer.write_all(self.0[20].as_slice())?;
1382 writer.write_all(self.0[21].as_slice())?;
1383 writer.write_all(self.0[22].as_slice())?;
1384 writer.write_all(self.0[23].as_slice())?;
1385 writer.write_all(self.0[24].as_slice())?;
1386 writer.write_all(self.0[25].as_slice())?;
1387 writer.write_all(self.0[26].as_slice())?;
1388 writer.write_all(self.0[27].as_slice())?;
1389 writer.write_all(self.0[28].as_slice())?;
1390 writer.write_all(self.0[29].as_slice())?;
1391 writer.write_all(self.0[30].as_slice())?;
1392 writer.write_all(self.0[31].as_slice())?;
1393 Ok(())
1394 }
1395 fn build(&self) -> Self::Entity {
1396 let mut inner = Vec::with_capacity(self.expected_length());
1397 self.write(&mut inner)
1398 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1399 Byte32::new_unchecked(inner.into())
1400 }
1401}
1402#[derive(Clone)]
1403pub struct Uint256(molecule::bytes::Bytes);
1404impl ::core::fmt::LowerHex for Uint256 {
1405 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1406 use molecule::hex_string;
1407 if f.alternate() {
1408 write!(f, "0x")?;
1409 }
1410 write!(f, "{}", hex_string(self.as_slice()))
1411 }
1412}
1413impl ::core::fmt::Debug for Uint256 {
1414 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1415 write!(f, "{}({:#x})", Self::NAME, self)
1416 }
1417}
1418impl ::core::fmt::Display for Uint256 {
1419 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1420 use molecule::hex_string;
1421 let raw_data = hex_string(&self.raw_data());
1422 write!(f, "{}(0x{})", Self::NAME, raw_data)
1423 }
1424}
1425impl ::core::default::Default for Uint256 {
1426 fn default() -> Self {
1427 let v: Vec<u8> = vec![
1428 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1429 0, 0, 0,
1430 ];
1431 Uint256::new_unchecked(v.into())
1432 }
1433}
1434impl Uint256 {
1435 pub const TOTAL_SIZE: usize = 32;
1436 pub const ITEM_SIZE: usize = 1;
1437 pub const ITEM_COUNT: usize = 32;
1438 pub fn nth0(&self) -> Byte {
1439 Byte::new_unchecked(self.0.slice(0..1))
1440 }
1441 pub fn nth1(&self) -> Byte {
1442 Byte::new_unchecked(self.0.slice(1..2))
1443 }
1444 pub fn nth2(&self) -> Byte {
1445 Byte::new_unchecked(self.0.slice(2..3))
1446 }
1447 pub fn nth3(&self) -> Byte {
1448 Byte::new_unchecked(self.0.slice(3..4))
1449 }
1450 pub fn nth4(&self) -> Byte {
1451 Byte::new_unchecked(self.0.slice(4..5))
1452 }
1453 pub fn nth5(&self) -> Byte {
1454 Byte::new_unchecked(self.0.slice(5..6))
1455 }
1456 pub fn nth6(&self) -> Byte {
1457 Byte::new_unchecked(self.0.slice(6..7))
1458 }
1459 pub fn nth7(&self) -> Byte {
1460 Byte::new_unchecked(self.0.slice(7..8))
1461 }
1462 pub fn nth8(&self) -> Byte {
1463 Byte::new_unchecked(self.0.slice(8..9))
1464 }
1465 pub fn nth9(&self) -> Byte {
1466 Byte::new_unchecked(self.0.slice(9..10))
1467 }
1468 pub fn nth10(&self) -> Byte {
1469 Byte::new_unchecked(self.0.slice(10..11))
1470 }
1471 pub fn nth11(&self) -> Byte {
1472 Byte::new_unchecked(self.0.slice(11..12))
1473 }
1474 pub fn nth12(&self) -> Byte {
1475 Byte::new_unchecked(self.0.slice(12..13))
1476 }
1477 pub fn nth13(&self) -> Byte {
1478 Byte::new_unchecked(self.0.slice(13..14))
1479 }
1480 pub fn nth14(&self) -> Byte {
1481 Byte::new_unchecked(self.0.slice(14..15))
1482 }
1483 pub fn nth15(&self) -> Byte {
1484 Byte::new_unchecked(self.0.slice(15..16))
1485 }
1486 pub fn nth16(&self) -> Byte {
1487 Byte::new_unchecked(self.0.slice(16..17))
1488 }
1489 pub fn nth17(&self) -> Byte {
1490 Byte::new_unchecked(self.0.slice(17..18))
1491 }
1492 pub fn nth18(&self) -> Byte {
1493 Byte::new_unchecked(self.0.slice(18..19))
1494 }
1495 pub fn nth19(&self) -> Byte {
1496 Byte::new_unchecked(self.0.slice(19..20))
1497 }
1498 pub fn nth20(&self) -> Byte {
1499 Byte::new_unchecked(self.0.slice(20..21))
1500 }
1501 pub fn nth21(&self) -> Byte {
1502 Byte::new_unchecked(self.0.slice(21..22))
1503 }
1504 pub fn nth22(&self) -> Byte {
1505 Byte::new_unchecked(self.0.slice(22..23))
1506 }
1507 pub fn nth23(&self) -> Byte {
1508 Byte::new_unchecked(self.0.slice(23..24))
1509 }
1510 pub fn nth24(&self) -> Byte {
1511 Byte::new_unchecked(self.0.slice(24..25))
1512 }
1513 pub fn nth25(&self) -> Byte {
1514 Byte::new_unchecked(self.0.slice(25..26))
1515 }
1516 pub fn nth26(&self) -> Byte {
1517 Byte::new_unchecked(self.0.slice(26..27))
1518 }
1519 pub fn nth27(&self) -> Byte {
1520 Byte::new_unchecked(self.0.slice(27..28))
1521 }
1522 pub fn nth28(&self) -> Byte {
1523 Byte::new_unchecked(self.0.slice(28..29))
1524 }
1525 pub fn nth29(&self) -> Byte {
1526 Byte::new_unchecked(self.0.slice(29..30))
1527 }
1528 pub fn nth30(&self) -> Byte {
1529 Byte::new_unchecked(self.0.slice(30..31))
1530 }
1531 pub fn nth31(&self) -> Byte {
1532 Byte::new_unchecked(self.0.slice(31..32))
1533 }
1534 pub fn raw_data(&self) -> molecule::bytes::Bytes {
1535 self.as_bytes()
1536 }
1537 pub fn as_reader<'r>(&'r self) -> Uint256Reader<'r> {
1538 Uint256Reader::new_unchecked(self.as_slice())
1539 }
1540}
1541impl molecule::prelude::Entity for Uint256 {
1542 type Builder = Uint256Builder;
1543 const NAME: &'static str = "Uint256";
1544 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1545 Uint256(data)
1546 }
1547 fn as_bytes(&self) -> molecule::bytes::Bytes {
1548 self.0.clone()
1549 }
1550 fn as_slice(&self) -> &[u8] {
1551 &self.0[..]
1552 }
1553 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1554 Uint256Reader::from_slice(slice).map(|reader| reader.to_entity())
1555 }
1556 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1557 Uint256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1558 }
1559 fn new_builder() -> Self::Builder {
1560 ::core::default::Default::default()
1561 }
1562 fn as_builder(self) -> Self::Builder {
1563 Self::new_builder().set([
1564 self.nth0(),
1565 self.nth1(),
1566 self.nth2(),
1567 self.nth3(),
1568 self.nth4(),
1569 self.nth5(),
1570 self.nth6(),
1571 self.nth7(),
1572 self.nth8(),
1573 self.nth9(),
1574 self.nth10(),
1575 self.nth11(),
1576 self.nth12(),
1577 self.nth13(),
1578 self.nth14(),
1579 self.nth15(),
1580 self.nth16(),
1581 self.nth17(),
1582 self.nth18(),
1583 self.nth19(),
1584 self.nth20(),
1585 self.nth21(),
1586 self.nth22(),
1587 self.nth23(),
1588 self.nth24(),
1589 self.nth25(),
1590 self.nth26(),
1591 self.nth27(),
1592 self.nth28(),
1593 self.nth29(),
1594 self.nth30(),
1595 self.nth31(),
1596 ])
1597 }
1598}
1599#[derive(Clone, Copy)]
1600pub struct Uint256Reader<'r>(&'r [u8]);
1601impl<'r> ::core::fmt::LowerHex for Uint256Reader<'r> {
1602 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1603 use molecule::hex_string;
1604 if f.alternate() {
1605 write!(f, "0x")?;
1606 }
1607 write!(f, "{}", hex_string(self.as_slice()))
1608 }
1609}
1610impl<'r> ::core::fmt::Debug for Uint256Reader<'r> {
1611 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1612 write!(f, "{}({:#x})", Self::NAME, self)
1613 }
1614}
1615impl<'r> ::core::fmt::Display for Uint256Reader<'r> {
1616 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1617 use molecule::hex_string;
1618 let raw_data = hex_string(&self.raw_data());
1619 write!(f, "{}(0x{})", Self::NAME, raw_data)
1620 }
1621}
1622impl<'r> Uint256Reader<'r> {
1623 pub const TOTAL_SIZE: usize = 32;
1624 pub const ITEM_SIZE: usize = 1;
1625 pub const ITEM_COUNT: usize = 32;
1626 pub fn nth0(&self) -> ByteReader<'r> {
1627 ByteReader::new_unchecked(&self.as_slice()[0..1])
1628 }
1629 pub fn nth1(&self) -> ByteReader<'r> {
1630 ByteReader::new_unchecked(&self.as_slice()[1..2])
1631 }
1632 pub fn nth2(&self) -> ByteReader<'r> {
1633 ByteReader::new_unchecked(&self.as_slice()[2..3])
1634 }
1635 pub fn nth3(&self) -> ByteReader<'r> {
1636 ByteReader::new_unchecked(&self.as_slice()[3..4])
1637 }
1638 pub fn nth4(&self) -> ByteReader<'r> {
1639 ByteReader::new_unchecked(&self.as_slice()[4..5])
1640 }
1641 pub fn nth5(&self) -> ByteReader<'r> {
1642 ByteReader::new_unchecked(&self.as_slice()[5..6])
1643 }
1644 pub fn nth6(&self) -> ByteReader<'r> {
1645 ByteReader::new_unchecked(&self.as_slice()[6..7])
1646 }
1647 pub fn nth7(&self) -> ByteReader<'r> {
1648 ByteReader::new_unchecked(&self.as_slice()[7..8])
1649 }
1650 pub fn nth8(&self) -> ByteReader<'r> {
1651 ByteReader::new_unchecked(&self.as_slice()[8..9])
1652 }
1653 pub fn nth9(&self) -> ByteReader<'r> {
1654 ByteReader::new_unchecked(&self.as_slice()[9..10])
1655 }
1656 pub fn nth10(&self) -> ByteReader<'r> {
1657 ByteReader::new_unchecked(&self.as_slice()[10..11])
1658 }
1659 pub fn nth11(&self) -> ByteReader<'r> {
1660 ByteReader::new_unchecked(&self.as_slice()[11..12])
1661 }
1662 pub fn nth12(&self) -> ByteReader<'r> {
1663 ByteReader::new_unchecked(&self.as_slice()[12..13])
1664 }
1665 pub fn nth13(&self) -> ByteReader<'r> {
1666 ByteReader::new_unchecked(&self.as_slice()[13..14])
1667 }
1668 pub fn nth14(&self) -> ByteReader<'r> {
1669 ByteReader::new_unchecked(&self.as_slice()[14..15])
1670 }
1671 pub fn nth15(&self) -> ByteReader<'r> {
1672 ByteReader::new_unchecked(&self.as_slice()[15..16])
1673 }
1674 pub fn nth16(&self) -> ByteReader<'r> {
1675 ByteReader::new_unchecked(&self.as_slice()[16..17])
1676 }
1677 pub fn nth17(&self) -> ByteReader<'r> {
1678 ByteReader::new_unchecked(&self.as_slice()[17..18])
1679 }
1680 pub fn nth18(&self) -> ByteReader<'r> {
1681 ByteReader::new_unchecked(&self.as_slice()[18..19])
1682 }
1683 pub fn nth19(&self) -> ByteReader<'r> {
1684 ByteReader::new_unchecked(&self.as_slice()[19..20])
1685 }
1686 pub fn nth20(&self) -> ByteReader<'r> {
1687 ByteReader::new_unchecked(&self.as_slice()[20..21])
1688 }
1689 pub fn nth21(&self) -> ByteReader<'r> {
1690 ByteReader::new_unchecked(&self.as_slice()[21..22])
1691 }
1692 pub fn nth22(&self) -> ByteReader<'r> {
1693 ByteReader::new_unchecked(&self.as_slice()[22..23])
1694 }
1695 pub fn nth23(&self) -> ByteReader<'r> {
1696 ByteReader::new_unchecked(&self.as_slice()[23..24])
1697 }
1698 pub fn nth24(&self) -> ByteReader<'r> {
1699 ByteReader::new_unchecked(&self.as_slice()[24..25])
1700 }
1701 pub fn nth25(&self) -> ByteReader<'r> {
1702 ByteReader::new_unchecked(&self.as_slice()[25..26])
1703 }
1704 pub fn nth26(&self) -> ByteReader<'r> {
1705 ByteReader::new_unchecked(&self.as_slice()[26..27])
1706 }
1707 pub fn nth27(&self) -> ByteReader<'r> {
1708 ByteReader::new_unchecked(&self.as_slice()[27..28])
1709 }
1710 pub fn nth28(&self) -> ByteReader<'r> {
1711 ByteReader::new_unchecked(&self.as_slice()[28..29])
1712 }
1713 pub fn nth29(&self) -> ByteReader<'r> {
1714 ByteReader::new_unchecked(&self.as_slice()[29..30])
1715 }
1716 pub fn nth30(&self) -> ByteReader<'r> {
1717 ByteReader::new_unchecked(&self.as_slice()[30..31])
1718 }
1719 pub fn nth31(&self) -> ByteReader<'r> {
1720 ByteReader::new_unchecked(&self.as_slice()[31..32])
1721 }
1722 pub fn raw_data(&self) -> &'r [u8] {
1723 self.as_slice()
1724 }
1725}
1726impl<'r> molecule::prelude::Reader<'r> for Uint256Reader<'r> {
1727 type Entity = Uint256;
1728 const NAME: &'static str = "Uint256Reader";
1729 fn to_entity(&self) -> Self::Entity {
1730 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1731 }
1732 fn new_unchecked(slice: &'r [u8]) -> Self {
1733 Uint256Reader(slice)
1734 }
1735 fn as_slice(&self) -> &'r [u8] {
1736 self.0
1737 }
1738 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1739 use molecule::verification_error as ve;
1740 let slice_len = slice.len();
1741 if slice_len != Self::TOTAL_SIZE {
1742 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1743 }
1744 Ok(())
1745 }
1746}
1747pub struct Uint256Builder(pub(crate) [Byte; 32]);
1748impl ::core::fmt::Debug for Uint256Builder {
1749 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1750 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1751 }
1752}
1753impl ::core::default::Default for Uint256Builder {
1754 fn default() -> Self {
1755 Uint256Builder([
1756 Byte::default(),
1757 Byte::default(),
1758 Byte::default(),
1759 Byte::default(),
1760 Byte::default(),
1761 Byte::default(),
1762 Byte::default(),
1763 Byte::default(),
1764 Byte::default(),
1765 Byte::default(),
1766 Byte::default(),
1767 Byte::default(),
1768 Byte::default(),
1769 Byte::default(),
1770 Byte::default(),
1771 Byte::default(),
1772 Byte::default(),
1773 Byte::default(),
1774 Byte::default(),
1775 Byte::default(),
1776 Byte::default(),
1777 Byte::default(),
1778 Byte::default(),
1779 Byte::default(),
1780 Byte::default(),
1781 Byte::default(),
1782 Byte::default(),
1783 Byte::default(),
1784 Byte::default(),
1785 Byte::default(),
1786 Byte::default(),
1787 Byte::default(),
1788 ])
1789 }
1790}
1791impl Uint256Builder {
1792 pub const TOTAL_SIZE: usize = 32;
1793 pub const ITEM_SIZE: usize = 1;
1794 pub const ITEM_COUNT: usize = 32;
1795 pub fn set(mut self, v: [Byte; 32]) -> Self {
1796 self.0 = v;
1797 self
1798 }
1799 pub fn nth0(mut self, v: Byte) -> Self {
1800 self.0[0] = v;
1801 self
1802 }
1803 pub fn nth1(mut self, v: Byte) -> Self {
1804 self.0[1] = v;
1805 self
1806 }
1807 pub fn nth2(mut self, v: Byte) -> Self {
1808 self.0[2] = v;
1809 self
1810 }
1811 pub fn nth3(mut self, v: Byte) -> Self {
1812 self.0[3] = v;
1813 self
1814 }
1815 pub fn nth4(mut self, v: Byte) -> Self {
1816 self.0[4] = v;
1817 self
1818 }
1819 pub fn nth5(mut self, v: Byte) -> Self {
1820 self.0[5] = v;
1821 self
1822 }
1823 pub fn nth6(mut self, v: Byte) -> Self {
1824 self.0[6] = v;
1825 self
1826 }
1827 pub fn nth7(mut self, v: Byte) -> Self {
1828 self.0[7] = v;
1829 self
1830 }
1831 pub fn nth8(mut self, v: Byte) -> Self {
1832 self.0[8] = v;
1833 self
1834 }
1835 pub fn nth9(mut self, v: Byte) -> Self {
1836 self.0[9] = v;
1837 self
1838 }
1839 pub fn nth10(mut self, v: Byte) -> Self {
1840 self.0[10] = v;
1841 self
1842 }
1843 pub fn nth11(mut self, v: Byte) -> Self {
1844 self.0[11] = v;
1845 self
1846 }
1847 pub fn nth12(mut self, v: Byte) -> Self {
1848 self.0[12] = v;
1849 self
1850 }
1851 pub fn nth13(mut self, v: Byte) -> Self {
1852 self.0[13] = v;
1853 self
1854 }
1855 pub fn nth14(mut self, v: Byte) -> Self {
1856 self.0[14] = v;
1857 self
1858 }
1859 pub fn nth15(mut self, v: Byte) -> Self {
1860 self.0[15] = v;
1861 self
1862 }
1863 pub fn nth16(mut self, v: Byte) -> Self {
1864 self.0[16] = v;
1865 self
1866 }
1867 pub fn nth17(mut self, v: Byte) -> Self {
1868 self.0[17] = v;
1869 self
1870 }
1871 pub fn nth18(mut self, v: Byte) -> Self {
1872 self.0[18] = v;
1873 self
1874 }
1875 pub fn nth19(mut self, v: Byte) -> Self {
1876 self.0[19] = v;
1877 self
1878 }
1879 pub fn nth20(mut self, v: Byte) -> Self {
1880 self.0[20] = v;
1881 self
1882 }
1883 pub fn nth21(mut self, v: Byte) -> Self {
1884 self.0[21] = v;
1885 self
1886 }
1887 pub fn nth22(mut self, v: Byte) -> Self {
1888 self.0[22] = v;
1889 self
1890 }
1891 pub fn nth23(mut self, v: Byte) -> Self {
1892 self.0[23] = v;
1893 self
1894 }
1895 pub fn nth24(mut self, v: Byte) -> Self {
1896 self.0[24] = v;
1897 self
1898 }
1899 pub fn nth25(mut self, v: Byte) -> Self {
1900 self.0[25] = v;
1901 self
1902 }
1903 pub fn nth26(mut self, v: Byte) -> Self {
1904 self.0[26] = v;
1905 self
1906 }
1907 pub fn nth27(mut self, v: Byte) -> Self {
1908 self.0[27] = v;
1909 self
1910 }
1911 pub fn nth28(mut self, v: Byte) -> Self {
1912 self.0[28] = v;
1913 self
1914 }
1915 pub fn nth29(mut self, v: Byte) -> Self {
1916 self.0[29] = v;
1917 self
1918 }
1919 pub fn nth30(mut self, v: Byte) -> Self {
1920 self.0[30] = v;
1921 self
1922 }
1923 pub fn nth31(mut self, v: Byte) -> Self {
1924 self.0[31] = v;
1925 self
1926 }
1927}
1928impl molecule::prelude::Builder for Uint256Builder {
1929 type Entity = Uint256;
1930 const NAME: &'static str = "Uint256Builder";
1931 fn expected_length(&self) -> usize {
1932 Self::TOTAL_SIZE
1933 }
1934 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1935 writer.write_all(self.0[0].as_slice())?;
1936 writer.write_all(self.0[1].as_slice())?;
1937 writer.write_all(self.0[2].as_slice())?;
1938 writer.write_all(self.0[3].as_slice())?;
1939 writer.write_all(self.0[4].as_slice())?;
1940 writer.write_all(self.0[5].as_slice())?;
1941 writer.write_all(self.0[6].as_slice())?;
1942 writer.write_all(self.0[7].as_slice())?;
1943 writer.write_all(self.0[8].as_slice())?;
1944 writer.write_all(self.0[9].as_slice())?;
1945 writer.write_all(self.0[10].as_slice())?;
1946 writer.write_all(self.0[11].as_slice())?;
1947 writer.write_all(self.0[12].as_slice())?;
1948 writer.write_all(self.0[13].as_slice())?;
1949 writer.write_all(self.0[14].as_slice())?;
1950 writer.write_all(self.0[15].as_slice())?;
1951 writer.write_all(self.0[16].as_slice())?;
1952 writer.write_all(self.0[17].as_slice())?;
1953 writer.write_all(self.0[18].as_slice())?;
1954 writer.write_all(self.0[19].as_slice())?;
1955 writer.write_all(self.0[20].as_slice())?;
1956 writer.write_all(self.0[21].as_slice())?;
1957 writer.write_all(self.0[22].as_slice())?;
1958 writer.write_all(self.0[23].as_slice())?;
1959 writer.write_all(self.0[24].as_slice())?;
1960 writer.write_all(self.0[25].as_slice())?;
1961 writer.write_all(self.0[26].as_slice())?;
1962 writer.write_all(self.0[27].as_slice())?;
1963 writer.write_all(self.0[28].as_slice())?;
1964 writer.write_all(self.0[29].as_slice())?;
1965 writer.write_all(self.0[30].as_slice())?;
1966 writer.write_all(self.0[31].as_slice())?;
1967 Ok(())
1968 }
1969 fn build(&self) -> Self::Entity {
1970 let mut inner = Vec::with_capacity(self.expected_length());
1971 self.write(&mut inner)
1972 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1973 Uint256::new_unchecked(inner.into())
1974 }
1975}
1976#[derive(Clone)]
1977pub struct Bytes(molecule::bytes::Bytes);
1978impl ::core::fmt::LowerHex for Bytes {
1979 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1980 use molecule::hex_string;
1981 if f.alternate() {
1982 write!(f, "0x")?;
1983 }
1984 write!(f, "{}", hex_string(self.as_slice()))
1985 }
1986}
1987impl ::core::fmt::Debug for Bytes {
1988 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1989 write!(f, "{}({:#x})", Self::NAME, self)
1990 }
1991}
1992impl ::core::fmt::Display for Bytes {
1993 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1994 use molecule::hex_string;
1995 let raw_data = hex_string(&self.raw_data());
1996 write!(f, "{}(0x{})", Self::NAME, raw_data)
1997 }
1998}
1999impl ::core::default::Default for Bytes {
2000 fn default() -> Self {
2001 let v: Vec<u8> = vec![0, 0, 0, 0];
2002 Bytes::new_unchecked(v.into())
2003 }
2004}
2005impl Bytes {
2006 pub const ITEM_SIZE: usize = 1;
2007 pub fn total_size(&self) -> usize {
2008 molecule::NUMBER_SIZE * (self.item_count() + 1)
2009 }
2010 pub fn item_count(&self) -> usize {
2011 molecule::unpack_number(self.as_slice()) as usize
2012 }
2013 pub fn len(&self) -> usize {
2014 self.item_count()
2015 }
2016 pub fn is_empty(&self) -> bool {
2017 self.len() == 0
2018 }
2019 pub fn get(&self, idx: usize) -> Option<Byte> {
2020 if idx >= self.len() {
2021 None
2022 } else {
2023 Some(self.get_unchecked(idx))
2024 }
2025 }
2026 pub fn get_unchecked(&self, idx: usize) -> Byte {
2027 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2028 let end = start + Self::ITEM_SIZE;
2029 Byte::new_unchecked(self.0.slice(start..end))
2030 }
2031 pub fn raw_data(&self) -> molecule::bytes::Bytes {
2032 self.0.slice(molecule::NUMBER_SIZE..)
2033 }
2034 pub fn as_reader<'r>(&'r self) -> BytesReader<'r> {
2035 BytesReader::new_unchecked(self.as_slice())
2036 }
2037}
2038impl molecule::prelude::Entity for Bytes {
2039 type Builder = BytesBuilder;
2040 const NAME: &'static str = "Bytes";
2041 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2042 Bytes(data)
2043 }
2044 fn as_bytes(&self) -> molecule::bytes::Bytes {
2045 self.0.clone()
2046 }
2047 fn as_slice(&self) -> &[u8] {
2048 &self.0[..]
2049 }
2050 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2051 BytesReader::from_slice(slice).map(|reader| reader.to_entity())
2052 }
2053 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2054 BytesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2055 }
2056 fn new_builder() -> Self::Builder {
2057 ::core::default::Default::default()
2058 }
2059 fn as_builder(self) -> Self::Builder {
2060 Self::new_builder().extend(self.into_iter())
2061 }
2062}
2063#[derive(Clone, Copy)]
2064pub struct BytesReader<'r>(&'r [u8]);
2065impl<'r> ::core::fmt::LowerHex for BytesReader<'r> {
2066 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2067 use molecule::hex_string;
2068 if f.alternate() {
2069 write!(f, "0x")?;
2070 }
2071 write!(f, "{}", hex_string(self.as_slice()))
2072 }
2073}
2074impl<'r> ::core::fmt::Debug for BytesReader<'r> {
2075 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2076 write!(f, "{}({:#x})", Self::NAME, self)
2077 }
2078}
2079impl<'r> ::core::fmt::Display for BytesReader<'r> {
2080 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2081 use molecule::hex_string;
2082 let raw_data = hex_string(&self.raw_data());
2083 write!(f, "{}(0x{})", Self::NAME, raw_data)
2084 }
2085}
2086impl<'r> BytesReader<'r> {
2087 pub const ITEM_SIZE: usize = 1;
2088 pub fn total_size(&self) -> usize {
2089 molecule::NUMBER_SIZE * (self.item_count() + 1)
2090 }
2091 pub fn item_count(&self) -> usize {
2092 molecule::unpack_number(self.as_slice()) as usize
2093 }
2094 pub fn len(&self) -> usize {
2095 self.item_count()
2096 }
2097 pub fn is_empty(&self) -> bool {
2098 self.len() == 0
2099 }
2100 pub fn get(&self, idx: usize) -> Option<ByteReader<'r>> {
2101 if idx >= self.len() {
2102 None
2103 } else {
2104 Some(self.get_unchecked(idx))
2105 }
2106 }
2107 pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> {
2108 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2109 let end = start + Self::ITEM_SIZE;
2110 ByteReader::new_unchecked(&self.as_slice()[start..end])
2111 }
2112 pub fn raw_data(&self) -> &'r [u8] {
2113 &self.as_slice()[molecule::NUMBER_SIZE..]
2114 }
2115}
2116impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> {
2117 type Entity = Bytes;
2118 const NAME: &'static str = "BytesReader";
2119 fn to_entity(&self) -> Self::Entity {
2120 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2121 }
2122 fn new_unchecked(slice: &'r [u8]) -> Self {
2123 BytesReader(slice)
2124 }
2125 fn as_slice(&self) -> &'r [u8] {
2126 self.0
2127 }
2128 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2129 use molecule::verification_error as ve;
2130 let slice_len = slice.len();
2131 if slice_len < molecule::NUMBER_SIZE {
2132 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2133 }
2134 let item_count = molecule::unpack_number(slice) as usize;
2135 if item_count == 0 {
2136 if slice_len != molecule::NUMBER_SIZE {
2137 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2138 }
2139 return Ok(());
2140 }
2141 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2142 if slice_len != total_size {
2143 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2144 }
2145 Ok(())
2146 }
2147}
2148#[derive(Debug, Default)]
2149pub struct BytesBuilder(pub(crate) Vec<Byte>);
2150impl BytesBuilder {
2151 pub const ITEM_SIZE: usize = 1;
2152 pub fn set(mut self, v: Vec<Byte>) -> Self {
2153 self.0 = v;
2154 self
2155 }
2156 pub fn push(mut self, v: Byte) -> Self {
2157 self.0.push(v);
2158 self
2159 }
2160 pub fn extend<T: ::core::iter::IntoIterator<Item = Byte>>(mut self, iter: T) -> Self {
2161 for elem in iter {
2162 self.0.push(elem);
2163 }
2164 self
2165 }
2166}
2167impl molecule::prelude::Builder for BytesBuilder {
2168 type Entity = Bytes;
2169 const NAME: &'static str = "BytesBuilder";
2170 fn expected_length(&self) -> usize {
2171 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2172 }
2173 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2174 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2175 for inner in &self.0[..] {
2176 writer.write_all(inner.as_slice())?;
2177 }
2178 Ok(())
2179 }
2180 fn build(&self) -> Self::Entity {
2181 let mut inner = Vec::with_capacity(self.expected_length());
2182 self.write(&mut inner)
2183 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2184 Bytes::new_unchecked(inner.into())
2185 }
2186}
2187pub struct BytesIterator(Bytes, usize, usize);
2188impl ::core::iter::Iterator for BytesIterator {
2189 type Item = Byte;
2190 fn next(&mut self) -> Option<Self::Item> {
2191 if self.1 >= self.2 {
2192 None
2193 } else {
2194 let ret = self.0.get_unchecked(self.1);
2195 self.1 += 1;
2196 Some(ret)
2197 }
2198 }
2199}
2200impl ::core::iter::ExactSizeIterator for BytesIterator {
2201 fn len(&self) -> usize {
2202 self.2 - self.1
2203 }
2204}
2205impl ::core::iter::IntoIterator for Bytes {
2206 type Item = Byte;
2207 type IntoIter = BytesIterator;
2208 fn into_iter(self) -> Self::IntoIter {
2209 let len = self.len();
2210 BytesIterator(self, 0, len)
2211 }
2212}
2213#[derive(Clone)]
2214pub struct BytesOpt(molecule::bytes::Bytes);
2215impl ::core::fmt::LowerHex for BytesOpt {
2216 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2217 use molecule::hex_string;
2218 if f.alternate() {
2219 write!(f, "0x")?;
2220 }
2221 write!(f, "{}", hex_string(self.as_slice()))
2222 }
2223}
2224impl ::core::fmt::Debug for BytesOpt {
2225 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2226 write!(f, "{}({:#x})", Self::NAME, self)
2227 }
2228}
2229impl ::core::fmt::Display for BytesOpt {
2230 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2231 if let Some(v) = self.to_opt() {
2232 write!(f, "{}(Some({}))", Self::NAME, v)
2233 } else {
2234 write!(f, "{}(None)", Self::NAME)
2235 }
2236 }
2237}
2238impl ::core::default::Default for BytesOpt {
2239 fn default() -> Self {
2240 let v: Vec<u8> = vec![];
2241 BytesOpt::new_unchecked(v.into())
2242 }
2243}
2244impl BytesOpt {
2245 pub fn is_none(&self) -> bool {
2246 self.0.is_empty()
2247 }
2248 pub fn is_some(&self) -> bool {
2249 !self.0.is_empty()
2250 }
2251 pub fn to_opt(&self) -> Option<Bytes> {
2252 if self.is_none() {
2253 None
2254 } else {
2255 Some(Bytes::new_unchecked(self.0.clone()))
2256 }
2257 }
2258 pub fn as_reader<'r>(&'r self) -> BytesOptReader<'r> {
2259 BytesOptReader::new_unchecked(self.as_slice())
2260 }
2261}
2262impl molecule::prelude::Entity for BytesOpt {
2263 type Builder = BytesOptBuilder;
2264 const NAME: &'static str = "BytesOpt";
2265 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2266 BytesOpt(data)
2267 }
2268 fn as_bytes(&self) -> molecule::bytes::Bytes {
2269 self.0.clone()
2270 }
2271 fn as_slice(&self) -> &[u8] {
2272 &self.0[..]
2273 }
2274 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2275 BytesOptReader::from_slice(slice).map(|reader| reader.to_entity())
2276 }
2277 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2278 BytesOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2279 }
2280 fn new_builder() -> Self::Builder {
2281 ::core::default::Default::default()
2282 }
2283 fn as_builder(self) -> Self::Builder {
2284 Self::new_builder().set(self.to_opt())
2285 }
2286}
2287#[derive(Clone, Copy)]
2288pub struct BytesOptReader<'r>(&'r [u8]);
2289impl<'r> ::core::fmt::LowerHex for BytesOptReader<'r> {
2290 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2291 use molecule::hex_string;
2292 if f.alternate() {
2293 write!(f, "0x")?;
2294 }
2295 write!(f, "{}", hex_string(self.as_slice()))
2296 }
2297}
2298impl<'r> ::core::fmt::Debug for BytesOptReader<'r> {
2299 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2300 write!(f, "{}({:#x})", Self::NAME, self)
2301 }
2302}
2303impl<'r> ::core::fmt::Display for BytesOptReader<'r> {
2304 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2305 if let Some(v) = self.to_opt() {
2306 write!(f, "{}(Some({}))", Self::NAME, v)
2307 } else {
2308 write!(f, "{}(None)", Self::NAME)
2309 }
2310 }
2311}
2312impl<'r> BytesOptReader<'r> {
2313 pub fn is_none(&self) -> bool {
2314 self.0.is_empty()
2315 }
2316 pub fn is_some(&self) -> bool {
2317 !self.0.is_empty()
2318 }
2319 pub fn to_opt(&self) -> Option<BytesReader<'r>> {
2320 if self.is_none() {
2321 None
2322 } else {
2323 Some(BytesReader::new_unchecked(self.as_slice()))
2324 }
2325 }
2326}
2327impl<'r> molecule::prelude::Reader<'r> for BytesOptReader<'r> {
2328 type Entity = BytesOpt;
2329 const NAME: &'static str = "BytesOptReader";
2330 fn to_entity(&self) -> Self::Entity {
2331 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2332 }
2333 fn new_unchecked(slice: &'r [u8]) -> Self {
2334 BytesOptReader(slice)
2335 }
2336 fn as_slice(&self) -> &'r [u8] {
2337 self.0
2338 }
2339 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2340 if !slice.is_empty() {
2341 BytesReader::verify(&slice[..], compatible)?;
2342 }
2343 Ok(())
2344 }
2345}
2346#[derive(Debug, Default)]
2347pub struct BytesOptBuilder(pub(crate) Option<Bytes>);
2348impl BytesOptBuilder {
2349 pub fn set(mut self, v: Option<Bytes>) -> Self {
2350 self.0 = v;
2351 self
2352 }
2353}
2354impl molecule::prelude::Builder for BytesOptBuilder {
2355 type Entity = BytesOpt;
2356 const NAME: &'static str = "BytesOptBuilder";
2357 fn expected_length(&self) -> usize {
2358 self.0
2359 .as_ref()
2360 .map(|ref inner| inner.as_slice().len())
2361 .unwrap_or(0)
2362 }
2363 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2364 self.0
2365 .as_ref()
2366 .map(|ref inner| writer.write_all(inner.as_slice()))
2367 .unwrap_or(Ok(()))
2368 }
2369 fn build(&self) -> Self::Entity {
2370 let mut inner = Vec::with_capacity(self.expected_length());
2371 self.write(&mut inner)
2372 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2373 BytesOpt::new_unchecked(inner.into())
2374 }
2375}
2376#[derive(Clone)]
2377pub struct BytesVec(molecule::bytes::Bytes);
2378impl ::core::fmt::LowerHex for BytesVec {
2379 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2380 use molecule::hex_string;
2381 if f.alternate() {
2382 write!(f, "0x")?;
2383 }
2384 write!(f, "{}", hex_string(self.as_slice()))
2385 }
2386}
2387impl ::core::fmt::Debug for BytesVec {
2388 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2389 write!(f, "{}({:#x})", Self::NAME, self)
2390 }
2391}
2392impl ::core::fmt::Display for BytesVec {
2393 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2394 write!(f, "{} [", Self::NAME)?;
2395 for i in 0..self.len() {
2396 if i == 0 {
2397 write!(f, "{}", self.get_unchecked(i))?;
2398 } else {
2399 write!(f, ", {}", self.get_unchecked(i))?;
2400 }
2401 }
2402 write!(f, "]")
2403 }
2404}
2405impl ::core::default::Default for BytesVec {
2406 fn default() -> Self {
2407 let v: Vec<u8> = vec![4, 0, 0, 0];
2408 BytesVec::new_unchecked(v.into())
2409 }
2410}
2411impl BytesVec {
2412 pub fn total_size(&self) -> usize {
2413 molecule::unpack_number(self.as_slice()) as usize
2414 }
2415 pub fn item_count(&self) -> usize {
2416 if self.total_size() == molecule::NUMBER_SIZE {
2417 0
2418 } else {
2419 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2420 }
2421 }
2422 pub fn len(&self) -> usize {
2423 self.item_count()
2424 }
2425 pub fn is_empty(&self) -> bool {
2426 self.len() == 0
2427 }
2428 pub fn get(&self, idx: usize) -> Option<Bytes> {
2429 if idx >= self.len() {
2430 None
2431 } else {
2432 Some(self.get_unchecked(idx))
2433 }
2434 }
2435 pub fn get_unchecked(&self, idx: usize) -> Bytes {
2436 let slice = self.as_slice();
2437 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
2438 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
2439 if idx == self.len() - 1 {
2440 Bytes::new_unchecked(self.0.slice(start..))
2441 } else {
2442 let end_idx = start_idx + molecule::NUMBER_SIZE;
2443 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
2444 Bytes::new_unchecked(self.0.slice(start..end))
2445 }
2446 }
2447 pub fn as_reader<'r>(&'r self) -> BytesVecReader<'r> {
2448 BytesVecReader::new_unchecked(self.as_slice())
2449 }
2450}
2451impl molecule::prelude::Entity for BytesVec {
2452 type Builder = BytesVecBuilder;
2453 const NAME: &'static str = "BytesVec";
2454 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2455 BytesVec(data)
2456 }
2457 fn as_bytes(&self) -> molecule::bytes::Bytes {
2458 self.0.clone()
2459 }
2460 fn as_slice(&self) -> &[u8] {
2461 &self.0[..]
2462 }
2463 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2464 BytesVecReader::from_slice(slice).map(|reader| reader.to_entity())
2465 }
2466 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2467 BytesVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2468 }
2469 fn new_builder() -> Self::Builder {
2470 ::core::default::Default::default()
2471 }
2472 fn as_builder(self) -> Self::Builder {
2473 Self::new_builder().extend(self.into_iter())
2474 }
2475}
2476#[derive(Clone, Copy)]
2477pub struct BytesVecReader<'r>(&'r [u8]);
2478impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> {
2479 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2480 use molecule::hex_string;
2481 if f.alternate() {
2482 write!(f, "0x")?;
2483 }
2484 write!(f, "{}", hex_string(self.as_slice()))
2485 }
2486}
2487impl<'r> ::core::fmt::Debug for BytesVecReader<'r> {
2488 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2489 write!(f, "{}({:#x})", Self::NAME, self)
2490 }
2491}
2492impl<'r> ::core::fmt::Display for BytesVecReader<'r> {
2493 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2494 write!(f, "{} [", Self::NAME)?;
2495 for i in 0..self.len() {
2496 if i == 0 {
2497 write!(f, "{}", self.get_unchecked(i))?;
2498 } else {
2499 write!(f, ", {}", self.get_unchecked(i))?;
2500 }
2501 }
2502 write!(f, "]")
2503 }
2504}
2505impl<'r> BytesVecReader<'r> {
2506 pub fn total_size(&self) -> usize {
2507 molecule::unpack_number(self.as_slice()) as usize
2508 }
2509 pub fn item_count(&self) -> usize {
2510 if self.total_size() == molecule::NUMBER_SIZE {
2511 0
2512 } else {
2513 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
2514 }
2515 }
2516 pub fn len(&self) -> usize {
2517 self.item_count()
2518 }
2519 pub fn is_empty(&self) -> bool {
2520 self.len() == 0
2521 }
2522 pub fn get(&self, idx: usize) -> Option<BytesReader<'r>> {
2523 if idx >= self.len() {
2524 None
2525 } else {
2526 Some(self.get_unchecked(idx))
2527 }
2528 }
2529 pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> {
2530 let slice = self.as_slice();
2531 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
2532 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
2533 if idx == self.len() - 1 {
2534 BytesReader::new_unchecked(&self.as_slice()[start..])
2535 } else {
2536 let end_idx = start_idx + molecule::NUMBER_SIZE;
2537 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
2538 BytesReader::new_unchecked(&self.as_slice()[start..end])
2539 }
2540 }
2541}
2542impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> {
2543 type Entity = BytesVec;
2544 const NAME: &'static str = "BytesVecReader";
2545 fn to_entity(&self) -> Self::Entity {
2546 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2547 }
2548 fn new_unchecked(slice: &'r [u8]) -> Self {
2549 BytesVecReader(slice)
2550 }
2551 fn as_slice(&self) -> &'r [u8] {
2552 self.0
2553 }
2554 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
2555 use molecule::verification_error as ve;
2556 let slice_len = slice.len();
2557 if slice_len < molecule::NUMBER_SIZE {
2558 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2559 }
2560 let total_size = molecule::unpack_number(slice) as usize;
2561 if slice_len != total_size {
2562 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2563 }
2564 if slice_len == molecule::NUMBER_SIZE {
2565 return Ok(());
2566 }
2567 if slice_len < molecule::NUMBER_SIZE * 2 {
2568 return ve!(
2569 Self,
2570 TotalSizeNotMatch,
2571 molecule::NUMBER_SIZE * 2,
2572 slice_len
2573 );
2574 }
2575 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
2576 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
2577 return ve!(Self, OffsetsNotMatch);
2578 }
2579 if slice_len < offset_first {
2580 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
2581 }
2582 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
2583 .chunks_exact(molecule::NUMBER_SIZE)
2584 .map(|x| molecule::unpack_number(x) as usize)
2585 .collect();
2586 offsets.push(total_size);
2587 if offsets.windows(2).any(|i| i[0] > i[1]) {
2588 return ve!(Self, OffsetsNotMatch);
2589 }
2590 for pair in offsets.windows(2) {
2591 let start = pair[0];
2592 let end = pair[1];
2593 BytesReader::verify(&slice[start..end], compatible)?;
2594 }
2595 Ok(())
2596 }
2597}
2598#[derive(Debug, Default)]
2599pub struct BytesVecBuilder(pub(crate) Vec<Bytes>);
2600impl BytesVecBuilder {
2601 pub fn set(mut self, v: Vec<Bytes>) -> Self {
2602 self.0 = v;
2603 self
2604 }
2605 pub fn push(mut self, v: Bytes) -> Self {
2606 self.0.push(v);
2607 self
2608 }
2609 pub fn extend<T: ::core::iter::IntoIterator<Item = Bytes>>(mut self, iter: T) -> Self {
2610 for elem in iter {
2611 self.0.push(elem);
2612 }
2613 self
2614 }
2615}
2616impl molecule::prelude::Builder for BytesVecBuilder {
2617 type Entity = BytesVec;
2618 const NAME: &'static str = "BytesVecBuilder";
2619 fn expected_length(&self) -> usize {
2620 molecule::NUMBER_SIZE * (self.0.len() + 1)
2621 + self
2622 .0
2623 .iter()
2624 .map(|inner| inner.as_slice().len())
2625 .sum::<usize>()
2626 }
2627 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2628 let item_count = self.0.len();
2629 if item_count == 0 {
2630 writer.write_all(&molecule::pack_number(
2631 molecule::NUMBER_SIZE as molecule::Number,
2632 ))?;
2633 } else {
2634 let (total_size, offsets) = self.0.iter().fold(
2635 (
2636 molecule::NUMBER_SIZE * (item_count + 1),
2637 Vec::with_capacity(item_count),
2638 ),
2639 |(start, mut offsets), inner| {
2640 offsets.push(start);
2641 (start + inner.as_slice().len(), offsets)
2642 },
2643 );
2644 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
2645 for offset in offsets.into_iter() {
2646 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
2647 }
2648 for inner in self.0.iter() {
2649 writer.write_all(inner.as_slice())?;
2650 }
2651 }
2652 Ok(())
2653 }
2654 fn build(&self) -> Self::Entity {
2655 let mut inner = Vec::with_capacity(self.expected_length());
2656 self.write(&mut inner)
2657 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2658 BytesVec::new_unchecked(inner.into())
2659 }
2660}
2661pub struct BytesVecIterator(BytesVec, usize, usize);
2662impl ::core::iter::Iterator for BytesVecIterator {
2663 type Item = Bytes;
2664 fn next(&mut self) -> Option<Self::Item> {
2665 if self.1 >= self.2 {
2666 None
2667 } else {
2668 let ret = self.0.get_unchecked(self.1);
2669 self.1 += 1;
2670 Some(ret)
2671 }
2672 }
2673}
2674impl ::core::iter::ExactSizeIterator for BytesVecIterator {
2675 fn len(&self) -> usize {
2676 self.2 - self.1
2677 }
2678}
2679impl ::core::iter::IntoIterator for BytesVec {
2680 type Item = Bytes;
2681 type IntoIter = BytesVecIterator;
2682 fn into_iter(self) -> Self::IntoIter {
2683 let len = self.len();
2684 BytesVecIterator(self, 0, len)
2685 }
2686}
2687impl<'r> BytesVecReader<'r> {
2688 pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> {
2689 BytesVecReaderIterator(&self, 0, self.len())
2690 }
2691}
2692pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize);
2693impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> {
2694 type Item = BytesReader<'t>;
2695 fn next(&mut self) -> Option<Self::Item> {
2696 if self.1 >= self.2 {
2697 None
2698 } else {
2699 let ret = self.0.get_unchecked(self.1);
2700 self.1 += 1;
2701 Some(ret)
2702 }
2703 }
2704}
2705impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, 'r> {
2706 fn len(&self) -> usize {
2707 self.2 - self.1
2708 }
2709}
2710#[derive(Clone)]
2711pub struct Byte32Vec(molecule::bytes::Bytes);
2712impl ::core::fmt::LowerHex for Byte32Vec {
2713 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2714 use molecule::hex_string;
2715 if f.alternate() {
2716 write!(f, "0x")?;
2717 }
2718 write!(f, "{}", hex_string(self.as_slice()))
2719 }
2720}
2721impl ::core::fmt::Debug for Byte32Vec {
2722 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2723 write!(f, "{}({:#x})", Self::NAME, self)
2724 }
2725}
2726impl ::core::fmt::Display for Byte32Vec {
2727 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2728 write!(f, "{} [", Self::NAME)?;
2729 for i in 0..self.len() {
2730 if i == 0 {
2731 write!(f, "{}", self.get_unchecked(i))?;
2732 } else {
2733 write!(f, ", {}", self.get_unchecked(i))?;
2734 }
2735 }
2736 write!(f, "]")
2737 }
2738}
2739impl ::core::default::Default for Byte32Vec {
2740 fn default() -> Self {
2741 let v: Vec<u8> = vec![0, 0, 0, 0];
2742 Byte32Vec::new_unchecked(v.into())
2743 }
2744}
2745impl Byte32Vec {
2746 pub const ITEM_SIZE: usize = 32;
2747 pub fn total_size(&self) -> usize {
2748 molecule::NUMBER_SIZE * (self.item_count() + 1)
2749 }
2750 pub fn item_count(&self) -> usize {
2751 molecule::unpack_number(self.as_slice()) as usize
2752 }
2753 pub fn len(&self) -> usize {
2754 self.item_count()
2755 }
2756 pub fn is_empty(&self) -> bool {
2757 self.len() == 0
2758 }
2759 pub fn get(&self, idx: usize) -> Option<Byte32> {
2760 if idx >= self.len() {
2761 None
2762 } else {
2763 Some(self.get_unchecked(idx))
2764 }
2765 }
2766 pub fn get_unchecked(&self, idx: usize) -> Byte32 {
2767 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2768 let end = start + Self::ITEM_SIZE;
2769 Byte32::new_unchecked(self.0.slice(start..end))
2770 }
2771 pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> {
2772 Byte32VecReader::new_unchecked(self.as_slice())
2773 }
2774}
2775impl molecule::prelude::Entity for Byte32Vec {
2776 type Builder = Byte32VecBuilder;
2777 const NAME: &'static str = "Byte32Vec";
2778 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2779 Byte32Vec(data)
2780 }
2781 fn as_bytes(&self) -> molecule::bytes::Bytes {
2782 self.0.clone()
2783 }
2784 fn as_slice(&self) -> &[u8] {
2785 &self.0[..]
2786 }
2787 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2788 Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity())
2789 }
2790 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2791 Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2792 }
2793 fn new_builder() -> Self::Builder {
2794 ::core::default::Default::default()
2795 }
2796 fn as_builder(self) -> Self::Builder {
2797 Self::new_builder().extend(self.into_iter())
2798 }
2799}
2800#[derive(Clone, Copy)]
2801pub struct Byte32VecReader<'r>(&'r [u8]);
2802impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> {
2803 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2804 use molecule::hex_string;
2805 if f.alternate() {
2806 write!(f, "0x")?;
2807 }
2808 write!(f, "{}", hex_string(self.as_slice()))
2809 }
2810}
2811impl<'r> ::core::fmt::Debug for Byte32VecReader<'r> {
2812 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2813 write!(f, "{}({:#x})", Self::NAME, self)
2814 }
2815}
2816impl<'r> ::core::fmt::Display for Byte32VecReader<'r> {
2817 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2818 write!(f, "{} [", Self::NAME)?;
2819 for i in 0..self.len() {
2820 if i == 0 {
2821 write!(f, "{}", self.get_unchecked(i))?;
2822 } else {
2823 write!(f, ", {}", self.get_unchecked(i))?;
2824 }
2825 }
2826 write!(f, "]")
2827 }
2828}
2829impl<'r> Byte32VecReader<'r> {
2830 pub const ITEM_SIZE: usize = 32;
2831 pub fn total_size(&self) -> usize {
2832 molecule::NUMBER_SIZE * (self.item_count() + 1)
2833 }
2834 pub fn item_count(&self) -> usize {
2835 molecule::unpack_number(self.as_slice()) as usize
2836 }
2837 pub fn len(&self) -> usize {
2838 self.item_count()
2839 }
2840 pub fn is_empty(&self) -> bool {
2841 self.len() == 0
2842 }
2843 pub fn get(&self, idx: usize) -> Option<Byte32Reader<'r>> {
2844 if idx >= self.len() {
2845 None
2846 } else {
2847 Some(self.get_unchecked(idx))
2848 }
2849 }
2850 pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> {
2851 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2852 let end = start + Self::ITEM_SIZE;
2853 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
2854 }
2855}
2856impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> {
2857 type Entity = Byte32Vec;
2858 const NAME: &'static str = "Byte32VecReader";
2859 fn to_entity(&self) -> Self::Entity {
2860 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2861 }
2862 fn new_unchecked(slice: &'r [u8]) -> Self {
2863 Byte32VecReader(slice)
2864 }
2865 fn as_slice(&self) -> &'r [u8] {
2866 self.0
2867 }
2868 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2869 use molecule::verification_error as ve;
2870 let slice_len = slice.len();
2871 if slice_len < molecule::NUMBER_SIZE {
2872 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2873 }
2874 let item_count = molecule::unpack_number(slice) as usize;
2875 if item_count == 0 {
2876 if slice_len != molecule::NUMBER_SIZE {
2877 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2878 }
2879 return Ok(());
2880 }
2881 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2882 if slice_len != total_size {
2883 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2884 }
2885 Ok(())
2886 }
2887}
2888#[derive(Debug, Default)]
2889pub struct Byte32VecBuilder(pub(crate) Vec<Byte32>);
2890impl Byte32VecBuilder {
2891 pub const ITEM_SIZE: usize = 32;
2892 pub fn set(mut self, v: Vec<Byte32>) -> Self {
2893 self.0 = v;
2894 self
2895 }
2896 pub fn push(mut self, v: Byte32) -> Self {
2897 self.0.push(v);
2898 self
2899 }
2900 pub fn extend<T: ::core::iter::IntoIterator<Item = Byte32>>(mut self, iter: T) -> Self {
2901 for elem in iter {
2902 self.0.push(elem);
2903 }
2904 self
2905 }
2906}
2907impl molecule::prelude::Builder for Byte32VecBuilder {
2908 type Entity = Byte32Vec;
2909 const NAME: &'static str = "Byte32VecBuilder";
2910 fn expected_length(&self) -> usize {
2911 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2912 }
2913 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2914 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2915 for inner in &self.0[..] {
2916 writer.write_all(inner.as_slice())?;
2917 }
2918 Ok(())
2919 }
2920 fn build(&self) -> Self::Entity {
2921 let mut inner = Vec::with_capacity(self.expected_length());
2922 self.write(&mut inner)
2923 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2924 Byte32Vec::new_unchecked(inner.into())
2925 }
2926}
2927pub struct Byte32VecIterator(Byte32Vec, usize, usize);
2928impl ::core::iter::Iterator for Byte32VecIterator {
2929 type Item = Byte32;
2930 fn next(&mut self) -> Option<Self::Item> {
2931 if self.1 >= self.2 {
2932 None
2933 } else {
2934 let ret = self.0.get_unchecked(self.1);
2935 self.1 += 1;
2936 Some(ret)
2937 }
2938 }
2939}
2940impl ::core::iter::ExactSizeIterator for Byte32VecIterator {
2941 fn len(&self) -> usize {
2942 self.2 - self.1
2943 }
2944}
2945impl ::core::iter::IntoIterator for Byte32Vec {
2946 type Item = Byte32;
2947 type IntoIter = Byte32VecIterator;
2948 fn into_iter(self) -> Self::IntoIter {
2949 let len = self.len();
2950 Byte32VecIterator(self, 0, len)
2951 }
2952}
2953impl<'r> Byte32VecReader<'r> {
2954 pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> {
2955 Byte32VecReaderIterator(&self, 0, self.len())
2956 }
2957}
2958pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize);
2959impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> {
2960 type Item = Byte32Reader<'t>;
2961 fn next(&mut self) -> Option<Self::Item> {
2962 if self.1 >= self.2 {
2963 None
2964 } else {
2965 let ret = self.0.get_unchecked(self.1);
2966 self.1 += 1;
2967 Some(ret)
2968 }
2969 }
2970}
2971impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> {
2972 fn len(&self) -> usize {
2973 self.2 - self.1
2974 }
2975}
2976#[derive(Clone)]
2977pub struct ScriptOpt(molecule::bytes::Bytes);
2978impl ::core::fmt::LowerHex for ScriptOpt {
2979 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2980 use molecule::hex_string;
2981 if f.alternate() {
2982 write!(f, "0x")?;
2983 }
2984 write!(f, "{}", hex_string(self.as_slice()))
2985 }
2986}
2987impl ::core::fmt::Debug for ScriptOpt {
2988 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2989 write!(f, "{}({:#x})", Self::NAME, self)
2990 }
2991}
2992impl ::core::fmt::Display for ScriptOpt {
2993 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2994 if let Some(v) = self.to_opt() {
2995 write!(f, "{}(Some({}))", Self::NAME, v)
2996 } else {
2997 write!(f, "{}(None)", Self::NAME)
2998 }
2999 }
3000}
3001impl ::core::default::Default for ScriptOpt {
3002 fn default() -> Self {
3003 let v: Vec<u8> = vec![];
3004 ScriptOpt::new_unchecked(v.into())
3005 }
3006}
3007impl ScriptOpt {
3008 pub fn is_none(&self) -> bool {
3009 self.0.is_empty()
3010 }
3011 pub fn is_some(&self) -> bool {
3012 !self.0.is_empty()
3013 }
3014 pub fn to_opt(&self) -> Option<Script> {
3015 if self.is_none() {
3016 None
3017 } else {
3018 Some(Script::new_unchecked(self.0.clone()))
3019 }
3020 }
3021 pub fn as_reader<'r>(&'r self) -> ScriptOptReader<'r> {
3022 ScriptOptReader::new_unchecked(self.as_slice())
3023 }
3024}
3025impl molecule::prelude::Entity for ScriptOpt {
3026 type Builder = ScriptOptBuilder;
3027 const NAME: &'static str = "ScriptOpt";
3028 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3029 ScriptOpt(data)
3030 }
3031 fn as_bytes(&self) -> molecule::bytes::Bytes {
3032 self.0.clone()
3033 }
3034 fn as_slice(&self) -> &[u8] {
3035 &self.0[..]
3036 }
3037 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3038 ScriptOptReader::from_slice(slice).map(|reader| reader.to_entity())
3039 }
3040 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3041 ScriptOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3042 }
3043 fn new_builder() -> Self::Builder {
3044 ::core::default::Default::default()
3045 }
3046 fn as_builder(self) -> Self::Builder {
3047 Self::new_builder().set(self.to_opt())
3048 }
3049}
3050#[derive(Clone, Copy)]
3051pub struct ScriptOptReader<'r>(&'r [u8]);
3052impl<'r> ::core::fmt::LowerHex for ScriptOptReader<'r> {
3053 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3054 use molecule::hex_string;
3055 if f.alternate() {
3056 write!(f, "0x")?;
3057 }
3058 write!(f, "{}", hex_string(self.as_slice()))
3059 }
3060}
3061impl<'r> ::core::fmt::Debug for ScriptOptReader<'r> {
3062 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3063 write!(f, "{}({:#x})", Self::NAME, self)
3064 }
3065}
3066impl<'r> ::core::fmt::Display for ScriptOptReader<'r> {
3067 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3068 if let Some(v) = self.to_opt() {
3069 write!(f, "{}(Some({}))", Self::NAME, v)
3070 } else {
3071 write!(f, "{}(None)", Self::NAME)
3072 }
3073 }
3074}
3075impl<'r> ScriptOptReader<'r> {
3076 pub fn is_none(&self) -> bool {
3077 self.0.is_empty()
3078 }
3079 pub fn is_some(&self) -> bool {
3080 !self.0.is_empty()
3081 }
3082 pub fn to_opt(&self) -> Option<ScriptReader<'r>> {
3083 if self.is_none() {
3084 None
3085 } else {
3086 Some(ScriptReader::new_unchecked(self.as_slice()))
3087 }
3088 }
3089}
3090impl<'r> molecule::prelude::Reader<'r> for ScriptOptReader<'r> {
3091 type Entity = ScriptOpt;
3092 const NAME: &'static str = "ScriptOptReader";
3093 fn to_entity(&self) -> Self::Entity {
3094 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3095 }
3096 fn new_unchecked(slice: &'r [u8]) -> Self {
3097 ScriptOptReader(slice)
3098 }
3099 fn as_slice(&self) -> &'r [u8] {
3100 self.0
3101 }
3102 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3103 if !slice.is_empty() {
3104 ScriptReader::verify(&slice[..], compatible)?;
3105 }
3106 Ok(())
3107 }
3108}
3109#[derive(Debug, Default)]
3110pub struct ScriptOptBuilder(pub(crate) Option<Script>);
3111impl ScriptOptBuilder {
3112 pub fn set(mut self, v: Option<Script>) -> Self {
3113 self.0 = v;
3114 self
3115 }
3116}
3117impl molecule::prelude::Builder for ScriptOptBuilder {
3118 type Entity = ScriptOpt;
3119 const NAME: &'static str = "ScriptOptBuilder";
3120 fn expected_length(&self) -> usize {
3121 self.0
3122 .as_ref()
3123 .map(|ref inner| inner.as_slice().len())
3124 .unwrap_or(0)
3125 }
3126 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3127 self.0
3128 .as_ref()
3129 .map(|ref inner| writer.write_all(inner.as_slice()))
3130 .unwrap_or(Ok(()))
3131 }
3132 fn build(&self) -> Self::Entity {
3133 let mut inner = Vec::with_capacity(self.expected_length());
3134 self.write(&mut inner)
3135 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3136 ScriptOpt::new_unchecked(inner.into())
3137 }
3138}
3139#[derive(Clone)]
3140pub struct ProposalShortId(molecule::bytes::Bytes);
3141impl ::core::fmt::LowerHex for ProposalShortId {
3142 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3143 use molecule::hex_string;
3144 if f.alternate() {
3145 write!(f, "0x")?;
3146 }
3147 write!(f, "{}", hex_string(self.as_slice()))
3148 }
3149}
3150impl ::core::fmt::Debug for ProposalShortId {
3151 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3152 write!(f, "{}({:#x})", Self::NAME, self)
3153 }
3154}
3155impl ::core::fmt::Display for ProposalShortId {
3156 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3157 use molecule::hex_string;
3158 let raw_data = hex_string(&self.raw_data());
3159 write!(f, "{}(0x{})", Self::NAME, raw_data)
3160 }
3161}
3162impl ::core::default::Default for ProposalShortId {
3163 fn default() -> Self {
3164 let v: Vec<u8> = vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
3165 ProposalShortId::new_unchecked(v.into())
3166 }
3167}
3168impl ProposalShortId {
3169 pub const TOTAL_SIZE: usize = 10;
3170 pub const ITEM_SIZE: usize = 1;
3171 pub const ITEM_COUNT: usize = 10;
3172 pub fn nth0(&self) -> Byte {
3173 Byte::new_unchecked(self.0.slice(0..1))
3174 }
3175 pub fn nth1(&self) -> Byte {
3176 Byte::new_unchecked(self.0.slice(1..2))
3177 }
3178 pub fn nth2(&self) -> Byte {
3179 Byte::new_unchecked(self.0.slice(2..3))
3180 }
3181 pub fn nth3(&self) -> Byte {
3182 Byte::new_unchecked(self.0.slice(3..4))
3183 }
3184 pub fn nth4(&self) -> Byte {
3185 Byte::new_unchecked(self.0.slice(4..5))
3186 }
3187 pub fn nth5(&self) -> Byte {
3188 Byte::new_unchecked(self.0.slice(5..6))
3189 }
3190 pub fn nth6(&self) -> Byte {
3191 Byte::new_unchecked(self.0.slice(6..7))
3192 }
3193 pub fn nth7(&self) -> Byte {
3194 Byte::new_unchecked(self.0.slice(7..8))
3195 }
3196 pub fn nth8(&self) -> Byte {
3197 Byte::new_unchecked(self.0.slice(8..9))
3198 }
3199 pub fn nth9(&self) -> Byte {
3200 Byte::new_unchecked(self.0.slice(9..10))
3201 }
3202 pub fn raw_data(&self) -> molecule::bytes::Bytes {
3203 self.as_bytes()
3204 }
3205 pub fn as_reader<'r>(&'r self) -> ProposalShortIdReader<'r> {
3206 ProposalShortIdReader::new_unchecked(self.as_slice())
3207 }
3208}
3209impl molecule::prelude::Entity for ProposalShortId {
3210 type Builder = ProposalShortIdBuilder;
3211 const NAME: &'static str = "ProposalShortId";
3212 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3213 ProposalShortId(data)
3214 }
3215 fn as_bytes(&self) -> molecule::bytes::Bytes {
3216 self.0.clone()
3217 }
3218 fn as_slice(&self) -> &[u8] {
3219 &self.0[..]
3220 }
3221 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3222 ProposalShortIdReader::from_slice(slice).map(|reader| reader.to_entity())
3223 }
3224 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3225 ProposalShortIdReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3226 }
3227 fn new_builder() -> Self::Builder {
3228 ::core::default::Default::default()
3229 }
3230 fn as_builder(self) -> Self::Builder {
3231 Self::new_builder().set([
3232 self.nth0(),
3233 self.nth1(),
3234 self.nth2(),
3235 self.nth3(),
3236 self.nth4(),
3237 self.nth5(),
3238 self.nth6(),
3239 self.nth7(),
3240 self.nth8(),
3241 self.nth9(),
3242 ])
3243 }
3244}
3245#[derive(Clone, Copy)]
3246pub struct ProposalShortIdReader<'r>(&'r [u8]);
3247impl<'r> ::core::fmt::LowerHex for ProposalShortIdReader<'r> {
3248 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3249 use molecule::hex_string;
3250 if f.alternate() {
3251 write!(f, "0x")?;
3252 }
3253 write!(f, "{}", hex_string(self.as_slice()))
3254 }
3255}
3256impl<'r> ::core::fmt::Debug for ProposalShortIdReader<'r> {
3257 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3258 write!(f, "{}({:#x})", Self::NAME, self)
3259 }
3260}
3261impl<'r> ::core::fmt::Display for ProposalShortIdReader<'r> {
3262 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3263 use molecule::hex_string;
3264 let raw_data = hex_string(&self.raw_data());
3265 write!(f, "{}(0x{})", Self::NAME, raw_data)
3266 }
3267}
3268impl<'r> ProposalShortIdReader<'r> {
3269 pub const TOTAL_SIZE: usize = 10;
3270 pub const ITEM_SIZE: usize = 1;
3271 pub const ITEM_COUNT: usize = 10;
3272 pub fn nth0(&self) -> ByteReader<'r> {
3273 ByteReader::new_unchecked(&self.as_slice()[0..1])
3274 }
3275 pub fn nth1(&self) -> ByteReader<'r> {
3276 ByteReader::new_unchecked(&self.as_slice()[1..2])
3277 }
3278 pub fn nth2(&self) -> ByteReader<'r> {
3279 ByteReader::new_unchecked(&self.as_slice()[2..3])
3280 }
3281 pub fn nth3(&self) -> ByteReader<'r> {
3282 ByteReader::new_unchecked(&self.as_slice()[3..4])
3283 }
3284 pub fn nth4(&self) -> ByteReader<'r> {
3285 ByteReader::new_unchecked(&self.as_slice()[4..5])
3286 }
3287 pub fn nth5(&self) -> ByteReader<'r> {
3288 ByteReader::new_unchecked(&self.as_slice()[5..6])
3289 }
3290 pub fn nth6(&self) -> ByteReader<'r> {
3291 ByteReader::new_unchecked(&self.as_slice()[6..7])
3292 }
3293 pub fn nth7(&self) -> ByteReader<'r> {
3294 ByteReader::new_unchecked(&self.as_slice()[7..8])
3295 }
3296 pub fn nth8(&self) -> ByteReader<'r> {
3297 ByteReader::new_unchecked(&self.as_slice()[8..9])
3298 }
3299 pub fn nth9(&self) -> ByteReader<'r> {
3300 ByteReader::new_unchecked(&self.as_slice()[9..10])
3301 }
3302 pub fn raw_data(&self) -> &'r [u8] {
3303 self.as_slice()
3304 }
3305}
3306impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdReader<'r> {
3307 type Entity = ProposalShortId;
3308 const NAME: &'static str = "ProposalShortIdReader";
3309 fn to_entity(&self) -> Self::Entity {
3310 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3311 }
3312 fn new_unchecked(slice: &'r [u8]) -> Self {
3313 ProposalShortIdReader(slice)
3314 }
3315 fn as_slice(&self) -> &'r [u8] {
3316 self.0
3317 }
3318 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
3319 use molecule::verification_error as ve;
3320 let slice_len = slice.len();
3321 if slice_len != Self::TOTAL_SIZE {
3322 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
3323 }
3324 Ok(())
3325 }
3326}
3327pub struct ProposalShortIdBuilder(pub(crate) [Byte; 10]);
3328impl ::core::fmt::Debug for ProposalShortIdBuilder {
3329 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3330 write!(f, "{}({:?})", Self::NAME, &self.0[..])
3331 }
3332}
3333impl ::core::default::Default for ProposalShortIdBuilder {
3334 fn default() -> Self {
3335 ProposalShortIdBuilder([
3336 Byte::default(),
3337 Byte::default(),
3338 Byte::default(),
3339 Byte::default(),
3340 Byte::default(),
3341 Byte::default(),
3342 Byte::default(),
3343 Byte::default(),
3344 Byte::default(),
3345 Byte::default(),
3346 ])
3347 }
3348}
3349impl ProposalShortIdBuilder {
3350 pub const TOTAL_SIZE: usize = 10;
3351 pub const ITEM_SIZE: usize = 1;
3352 pub const ITEM_COUNT: usize = 10;
3353 pub fn set(mut self, v: [Byte; 10]) -> Self {
3354 self.0 = v;
3355 self
3356 }
3357 pub fn nth0(mut self, v: Byte) -> Self {
3358 self.0[0] = v;
3359 self
3360 }
3361 pub fn nth1(mut self, v: Byte) -> Self {
3362 self.0[1] = v;
3363 self
3364 }
3365 pub fn nth2(mut self, v: Byte) -> Self {
3366 self.0[2] = v;
3367 self
3368 }
3369 pub fn nth3(mut self, v: Byte) -> Self {
3370 self.0[3] = v;
3371 self
3372 }
3373 pub fn nth4(mut self, v: Byte) -> Self {
3374 self.0[4] = v;
3375 self
3376 }
3377 pub fn nth5(mut self, v: Byte) -> Self {
3378 self.0[5] = v;
3379 self
3380 }
3381 pub fn nth6(mut self, v: Byte) -> Self {
3382 self.0[6] = v;
3383 self
3384 }
3385 pub fn nth7(mut self, v: Byte) -> Self {
3386 self.0[7] = v;
3387 self
3388 }
3389 pub fn nth8(mut self, v: Byte) -> Self {
3390 self.0[8] = v;
3391 self
3392 }
3393 pub fn nth9(mut self, v: Byte) -> Self {
3394 self.0[9] = v;
3395 self
3396 }
3397}
3398impl molecule::prelude::Builder for ProposalShortIdBuilder {
3399 type Entity = ProposalShortId;
3400 const NAME: &'static str = "ProposalShortIdBuilder";
3401 fn expected_length(&self) -> usize {
3402 Self::TOTAL_SIZE
3403 }
3404 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3405 writer.write_all(self.0[0].as_slice())?;
3406 writer.write_all(self.0[1].as_slice())?;
3407 writer.write_all(self.0[2].as_slice())?;
3408 writer.write_all(self.0[3].as_slice())?;
3409 writer.write_all(self.0[4].as_slice())?;
3410 writer.write_all(self.0[5].as_slice())?;
3411 writer.write_all(self.0[6].as_slice())?;
3412 writer.write_all(self.0[7].as_slice())?;
3413 writer.write_all(self.0[8].as_slice())?;
3414 writer.write_all(self.0[9].as_slice())?;
3415 Ok(())
3416 }
3417 fn build(&self) -> Self::Entity {
3418 let mut inner = Vec::with_capacity(self.expected_length());
3419 self.write(&mut inner)
3420 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3421 ProposalShortId::new_unchecked(inner.into())
3422 }
3423}
3424#[derive(Clone)]
3425pub struct UncleBlockVec(molecule::bytes::Bytes);
3426impl ::core::fmt::LowerHex for UncleBlockVec {
3427 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3428 use molecule::hex_string;
3429 if f.alternate() {
3430 write!(f, "0x")?;
3431 }
3432 write!(f, "{}", hex_string(self.as_slice()))
3433 }
3434}
3435impl ::core::fmt::Debug for UncleBlockVec {
3436 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3437 write!(f, "{}({:#x})", Self::NAME, self)
3438 }
3439}
3440impl ::core::fmt::Display for UncleBlockVec {
3441 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3442 write!(f, "{} [", Self::NAME)?;
3443 for i in 0..self.len() {
3444 if i == 0 {
3445 write!(f, "{}", self.get_unchecked(i))?;
3446 } else {
3447 write!(f, ", {}", self.get_unchecked(i))?;
3448 }
3449 }
3450 write!(f, "]")
3451 }
3452}
3453impl ::core::default::Default for UncleBlockVec {
3454 fn default() -> Self {
3455 let v: Vec<u8> = vec![4, 0, 0, 0];
3456 UncleBlockVec::new_unchecked(v.into())
3457 }
3458}
3459impl UncleBlockVec {
3460 pub fn total_size(&self) -> usize {
3461 molecule::unpack_number(self.as_slice()) as usize
3462 }
3463 pub fn item_count(&self) -> usize {
3464 if self.total_size() == molecule::NUMBER_SIZE {
3465 0
3466 } else {
3467 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3468 }
3469 }
3470 pub fn len(&self) -> usize {
3471 self.item_count()
3472 }
3473 pub fn is_empty(&self) -> bool {
3474 self.len() == 0
3475 }
3476 pub fn get(&self, idx: usize) -> Option<UncleBlock> {
3477 if idx >= self.len() {
3478 None
3479 } else {
3480 Some(self.get_unchecked(idx))
3481 }
3482 }
3483 pub fn get_unchecked(&self, idx: usize) -> UncleBlock {
3484 let slice = self.as_slice();
3485 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3486 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3487 if idx == self.len() - 1 {
3488 UncleBlock::new_unchecked(self.0.slice(start..))
3489 } else {
3490 let end_idx = start_idx + molecule::NUMBER_SIZE;
3491 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3492 UncleBlock::new_unchecked(self.0.slice(start..end))
3493 }
3494 }
3495 pub fn as_reader<'r>(&'r self) -> UncleBlockVecReader<'r> {
3496 UncleBlockVecReader::new_unchecked(self.as_slice())
3497 }
3498}
3499impl molecule::prelude::Entity for UncleBlockVec {
3500 type Builder = UncleBlockVecBuilder;
3501 const NAME: &'static str = "UncleBlockVec";
3502 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3503 UncleBlockVec(data)
3504 }
3505 fn as_bytes(&self) -> molecule::bytes::Bytes {
3506 self.0.clone()
3507 }
3508 fn as_slice(&self) -> &[u8] {
3509 &self.0[..]
3510 }
3511 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3512 UncleBlockVecReader::from_slice(slice).map(|reader| reader.to_entity())
3513 }
3514 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3515 UncleBlockVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3516 }
3517 fn new_builder() -> Self::Builder {
3518 ::core::default::Default::default()
3519 }
3520 fn as_builder(self) -> Self::Builder {
3521 Self::new_builder().extend(self.into_iter())
3522 }
3523}
3524#[derive(Clone, Copy)]
3525pub struct UncleBlockVecReader<'r>(&'r [u8]);
3526impl<'r> ::core::fmt::LowerHex for UncleBlockVecReader<'r> {
3527 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3528 use molecule::hex_string;
3529 if f.alternate() {
3530 write!(f, "0x")?;
3531 }
3532 write!(f, "{}", hex_string(self.as_slice()))
3533 }
3534}
3535impl<'r> ::core::fmt::Debug for UncleBlockVecReader<'r> {
3536 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3537 write!(f, "{}({:#x})", Self::NAME, self)
3538 }
3539}
3540impl<'r> ::core::fmt::Display for UncleBlockVecReader<'r> {
3541 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3542 write!(f, "{} [", Self::NAME)?;
3543 for i in 0..self.len() {
3544 if i == 0 {
3545 write!(f, "{}", self.get_unchecked(i))?;
3546 } else {
3547 write!(f, ", {}", self.get_unchecked(i))?;
3548 }
3549 }
3550 write!(f, "]")
3551 }
3552}
3553impl<'r> UncleBlockVecReader<'r> {
3554 pub fn total_size(&self) -> usize {
3555 molecule::unpack_number(self.as_slice()) as usize
3556 }
3557 pub fn item_count(&self) -> usize {
3558 if self.total_size() == molecule::NUMBER_SIZE {
3559 0
3560 } else {
3561 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3562 }
3563 }
3564 pub fn len(&self) -> usize {
3565 self.item_count()
3566 }
3567 pub fn is_empty(&self) -> bool {
3568 self.len() == 0
3569 }
3570 pub fn get(&self, idx: usize) -> Option<UncleBlockReader<'r>> {
3571 if idx >= self.len() {
3572 None
3573 } else {
3574 Some(self.get_unchecked(idx))
3575 }
3576 }
3577 pub fn get_unchecked(&self, idx: usize) -> UncleBlockReader<'r> {
3578 let slice = self.as_slice();
3579 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3580 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3581 if idx == self.len() - 1 {
3582 UncleBlockReader::new_unchecked(&self.as_slice()[start..])
3583 } else {
3584 let end_idx = start_idx + molecule::NUMBER_SIZE;
3585 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3586 UncleBlockReader::new_unchecked(&self.as_slice()[start..end])
3587 }
3588 }
3589}
3590impl<'r> molecule::prelude::Reader<'r> for UncleBlockVecReader<'r> {
3591 type Entity = UncleBlockVec;
3592 const NAME: &'static str = "UncleBlockVecReader";
3593 fn to_entity(&self) -> Self::Entity {
3594 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3595 }
3596 fn new_unchecked(slice: &'r [u8]) -> Self {
3597 UncleBlockVecReader(slice)
3598 }
3599 fn as_slice(&self) -> &'r [u8] {
3600 self.0
3601 }
3602 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3603 use molecule::verification_error as ve;
3604 let slice_len = slice.len();
3605 if slice_len < molecule::NUMBER_SIZE {
3606 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3607 }
3608 let total_size = molecule::unpack_number(slice) as usize;
3609 if slice_len != total_size {
3610 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3611 }
3612 if slice_len == molecule::NUMBER_SIZE {
3613 return Ok(());
3614 }
3615 if slice_len < molecule::NUMBER_SIZE * 2 {
3616 return ve!(
3617 Self,
3618 TotalSizeNotMatch,
3619 molecule::NUMBER_SIZE * 2,
3620 slice_len
3621 );
3622 }
3623 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3624 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3625 return ve!(Self, OffsetsNotMatch);
3626 }
3627 if slice_len < offset_first {
3628 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3629 }
3630 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3631 .chunks_exact(molecule::NUMBER_SIZE)
3632 .map(|x| molecule::unpack_number(x) as usize)
3633 .collect();
3634 offsets.push(total_size);
3635 if offsets.windows(2).any(|i| i[0] > i[1]) {
3636 return ve!(Self, OffsetsNotMatch);
3637 }
3638 for pair in offsets.windows(2) {
3639 let start = pair[0];
3640 let end = pair[1];
3641 UncleBlockReader::verify(&slice[start..end], compatible)?;
3642 }
3643 Ok(())
3644 }
3645}
3646#[derive(Debug, Default)]
3647pub struct UncleBlockVecBuilder(pub(crate) Vec<UncleBlock>);
3648impl UncleBlockVecBuilder {
3649 pub fn set(mut self, v: Vec<UncleBlock>) -> Self {
3650 self.0 = v;
3651 self
3652 }
3653 pub fn push(mut self, v: UncleBlock) -> Self {
3654 self.0.push(v);
3655 self
3656 }
3657 pub fn extend<T: ::core::iter::IntoIterator<Item = UncleBlock>>(mut self, iter: T) -> Self {
3658 for elem in iter {
3659 self.0.push(elem);
3660 }
3661 self
3662 }
3663}
3664impl molecule::prelude::Builder for UncleBlockVecBuilder {
3665 type Entity = UncleBlockVec;
3666 const NAME: &'static str = "UncleBlockVecBuilder";
3667 fn expected_length(&self) -> usize {
3668 molecule::NUMBER_SIZE * (self.0.len() + 1)
3669 + self
3670 .0
3671 .iter()
3672 .map(|inner| inner.as_slice().len())
3673 .sum::<usize>()
3674 }
3675 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3676 let item_count = self.0.len();
3677 if item_count == 0 {
3678 writer.write_all(&molecule::pack_number(
3679 molecule::NUMBER_SIZE as molecule::Number,
3680 ))?;
3681 } else {
3682 let (total_size, offsets) = self.0.iter().fold(
3683 (
3684 molecule::NUMBER_SIZE * (item_count + 1),
3685 Vec::with_capacity(item_count),
3686 ),
3687 |(start, mut offsets), inner| {
3688 offsets.push(start);
3689 (start + inner.as_slice().len(), offsets)
3690 },
3691 );
3692 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3693 for offset in offsets.into_iter() {
3694 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3695 }
3696 for inner in self.0.iter() {
3697 writer.write_all(inner.as_slice())?;
3698 }
3699 }
3700 Ok(())
3701 }
3702 fn build(&self) -> Self::Entity {
3703 let mut inner = Vec::with_capacity(self.expected_length());
3704 self.write(&mut inner)
3705 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3706 UncleBlockVec::new_unchecked(inner.into())
3707 }
3708}
3709pub struct UncleBlockVecIterator(UncleBlockVec, usize, usize);
3710impl ::core::iter::Iterator for UncleBlockVecIterator {
3711 type Item = UncleBlock;
3712 fn next(&mut self) -> Option<Self::Item> {
3713 if self.1 >= self.2 {
3714 None
3715 } else {
3716 let ret = self.0.get_unchecked(self.1);
3717 self.1 += 1;
3718 Some(ret)
3719 }
3720 }
3721}
3722impl ::core::iter::ExactSizeIterator for UncleBlockVecIterator {
3723 fn len(&self) -> usize {
3724 self.2 - self.1
3725 }
3726}
3727impl ::core::iter::IntoIterator for UncleBlockVec {
3728 type Item = UncleBlock;
3729 type IntoIter = UncleBlockVecIterator;
3730 fn into_iter(self) -> Self::IntoIter {
3731 let len = self.len();
3732 UncleBlockVecIterator(self, 0, len)
3733 }
3734}
3735impl<'r> UncleBlockVecReader<'r> {
3736 pub fn iter<'t>(&'t self) -> UncleBlockVecReaderIterator<'t, 'r> {
3737 UncleBlockVecReaderIterator(&self, 0, self.len())
3738 }
3739}
3740pub struct UncleBlockVecReaderIterator<'t, 'r>(&'t UncleBlockVecReader<'r>, usize, usize);
3741impl<'t: 'r, 'r> ::core::iter::Iterator for UncleBlockVecReaderIterator<'t, 'r> {
3742 type Item = UncleBlockReader<'t>;
3743 fn next(&mut self) -> Option<Self::Item> {
3744 if self.1 >= self.2 {
3745 None
3746 } else {
3747 let ret = self.0.get_unchecked(self.1);
3748 self.1 += 1;
3749 Some(ret)
3750 }
3751 }
3752}
3753impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for UncleBlockVecReaderIterator<'t, 'r> {
3754 fn len(&self) -> usize {
3755 self.2 - self.1
3756 }
3757}
3758#[derive(Clone)]
3759pub struct TransactionVec(molecule::bytes::Bytes);
3760impl ::core::fmt::LowerHex for TransactionVec {
3761 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3762 use molecule::hex_string;
3763 if f.alternate() {
3764 write!(f, "0x")?;
3765 }
3766 write!(f, "{}", hex_string(self.as_slice()))
3767 }
3768}
3769impl ::core::fmt::Debug for TransactionVec {
3770 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3771 write!(f, "{}({:#x})", Self::NAME, self)
3772 }
3773}
3774impl ::core::fmt::Display for TransactionVec {
3775 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3776 write!(f, "{} [", Self::NAME)?;
3777 for i in 0..self.len() {
3778 if i == 0 {
3779 write!(f, "{}", self.get_unchecked(i))?;
3780 } else {
3781 write!(f, ", {}", self.get_unchecked(i))?;
3782 }
3783 }
3784 write!(f, "]")
3785 }
3786}
3787impl ::core::default::Default for TransactionVec {
3788 fn default() -> Self {
3789 let v: Vec<u8> = vec![4, 0, 0, 0];
3790 TransactionVec::new_unchecked(v.into())
3791 }
3792}
3793impl TransactionVec {
3794 pub fn total_size(&self) -> usize {
3795 molecule::unpack_number(self.as_slice()) as usize
3796 }
3797 pub fn item_count(&self) -> usize {
3798 if self.total_size() == molecule::NUMBER_SIZE {
3799 0
3800 } else {
3801 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3802 }
3803 }
3804 pub fn len(&self) -> usize {
3805 self.item_count()
3806 }
3807 pub fn is_empty(&self) -> bool {
3808 self.len() == 0
3809 }
3810 pub fn get(&self, idx: usize) -> Option<Transaction> {
3811 if idx >= self.len() {
3812 None
3813 } else {
3814 Some(self.get_unchecked(idx))
3815 }
3816 }
3817 pub fn get_unchecked(&self, idx: usize) -> Transaction {
3818 let slice = self.as_slice();
3819 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3820 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3821 if idx == self.len() - 1 {
3822 Transaction::new_unchecked(self.0.slice(start..))
3823 } else {
3824 let end_idx = start_idx + molecule::NUMBER_SIZE;
3825 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3826 Transaction::new_unchecked(self.0.slice(start..end))
3827 }
3828 }
3829 pub fn as_reader<'r>(&'r self) -> TransactionVecReader<'r> {
3830 TransactionVecReader::new_unchecked(self.as_slice())
3831 }
3832}
3833impl molecule::prelude::Entity for TransactionVec {
3834 type Builder = TransactionVecBuilder;
3835 const NAME: &'static str = "TransactionVec";
3836 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3837 TransactionVec(data)
3838 }
3839 fn as_bytes(&self) -> molecule::bytes::Bytes {
3840 self.0.clone()
3841 }
3842 fn as_slice(&self) -> &[u8] {
3843 &self.0[..]
3844 }
3845 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3846 TransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
3847 }
3848 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3849 TransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3850 }
3851 fn new_builder() -> Self::Builder {
3852 ::core::default::Default::default()
3853 }
3854 fn as_builder(self) -> Self::Builder {
3855 Self::new_builder().extend(self.into_iter())
3856 }
3857}
3858#[derive(Clone, Copy)]
3859pub struct TransactionVecReader<'r>(&'r [u8]);
3860impl<'r> ::core::fmt::LowerHex for TransactionVecReader<'r> {
3861 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3862 use molecule::hex_string;
3863 if f.alternate() {
3864 write!(f, "0x")?;
3865 }
3866 write!(f, "{}", hex_string(self.as_slice()))
3867 }
3868}
3869impl<'r> ::core::fmt::Debug for TransactionVecReader<'r> {
3870 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3871 write!(f, "{}({:#x})", Self::NAME, self)
3872 }
3873}
3874impl<'r> ::core::fmt::Display for TransactionVecReader<'r> {
3875 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3876 write!(f, "{} [", Self::NAME)?;
3877 for i in 0..self.len() {
3878 if i == 0 {
3879 write!(f, "{}", self.get_unchecked(i))?;
3880 } else {
3881 write!(f, ", {}", self.get_unchecked(i))?;
3882 }
3883 }
3884 write!(f, "]")
3885 }
3886}
3887impl<'r> TransactionVecReader<'r> {
3888 pub fn total_size(&self) -> usize {
3889 molecule::unpack_number(self.as_slice()) as usize
3890 }
3891 pub fn item_count(&self) -> usize {
3892 if self.total_size() == molecule::NUMBER_SIZE {
3893 0
3894 } else {
3895 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3896 }
3897 }
3898 pub fn len(&self) -> usize {
3899 self.item_count()
3900 }
3901 pub fn is_empty(&self) -> bool {
3902 self.len() == 0
3903 }
3904 pub fn get(&self, idx: usize) -> Option<TransactionReader<'r>> {
3905 if idx >= self.len() {
3906 None
3907 } else {
3908 Some(self.get_unchecked(idx))
3909 }
3910 }
3911 pub fn get_unchecked(&self, idx: usize) -> TransactionReader<'r> {
3912 let slice = self.as_slice();
3913 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3914 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3915 if idx == self.len() - 1 {
3916 TransactionReader::new_unchecked(&self.as_slice()[start..])
3917 } else {
3918 let end_idx = start_idx + molecule::NUMBER_SIZE;
3919 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3920 TransactionReader::new_unchecked(&self.as_slice()[start..end])
3921 }
3922 }
3923}
3924impl<'r> molecule::prelude::Reader<'r> for TransactionVecReader<'r> {
3925 type Entity = TransactionVec;
3926 const NAME: &'static str = "TransactionVecReader";
3927 fn to_entity(&self) -> Self::Entity {
3928 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3929 }
3930 fn new_unchecked(slice: &'r [u8]) -> Self {
3931 TransactionVecReader(slice)
3932 }
3933 fn as_slice(&self) -> &'r [u8] {
3934 self.0
3935 }
3936 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3937 use molecule::verification_error as ve;
3938 let slice_len = slice.len();
3939 if slice_len < molecule::NUMBER_SIZE {
3940 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3941 }
3942 let total_size = molecule::unpack_number(slice) as usize;
3943 if slice_len != total_size {
3944 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3945 }
3946 if slice_len == molecule::NUMBER_SIZE {
3947 return Ok(());
3948 }
3949 if slice_len < molecule::NUMBER_SIZE * 2 {
3950 return ve!(
3951 Self,
3952 TotalSizeNotMatch,
3953 molecule::NUMBER_SIZE * 2,
3954 slice_len
3955 );
3956 }
3957 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3958 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3959 return ve!(Self, OffsetsNotMatch);
3960 }
3961 if slice_len < offset_first {
3962 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3963 }
3964 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3965 .chunks_exact(molecule::NUMBER_SIZE)
3966 .map(|x| molecule::unpack_number(x) as usize)
3967 .collect();
3968 offsets.push(total_size);
3969 if offsets.windows(2).any(|i| i[0] > i[1]) {
3970 return ve!(Self, OffsetsNotMatch);
3971 }
3972 for pair in offsets.windows(2) {
3973 let start = pair[0];
3974 let end = pair[1];
3975 TransactionReader::verify(&slice[start..end], compatible)?;
3976 }
3977 Ok(())
3978 }
3979}
3980#[derive(Debug, Default)]
3981pub struct TransactionVecBuilder(pub(crate) Vec<Transaction>);
3982impl TransactionVecBuilder {
3983 pub fn set(mut self, v: Vec<Transaction>) -> Self {
3984 self.0 = v;
3985 self
3986 }
3987 pub fn push(mut self, v: Transaction) -> Self {
3988 self.0.push(v);
3989 self
3990 }
3991 pub fn extend<T: ::core::iter::IntoIterator<Item = Transaction>>(mut self, iter: T) -> Self {
3992 for elem in iter {
3993 self.0.push(elem);
3994 }
3995 self
3996 }
3997}
3998impl molecule::prelude::Builder for TransactionVecBuilder {
3999 type Entity = TransactionVec;
4000 const NAME: &'static str = "TransactionVecBuilder";
4001 fn expected_length(&self) -> usize {
4002 molecule::NUMBER_SIZE * (self.0.len() + 1)
4003 + self
4004 .0
4005 .iter()
4006 .map(|inner| inner.as_slice().len())
4007 .sum::<usize>()
4008 }
4009 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4010 let item_count = self.0.len();
4011 if item_count == 0 {
4012 writer.write_all(&molecule::pack_number(
4013 molecule::NUMBER_SIZE as molecule::Number,
4014 ))?;
4015 } else {
4016 let (total_size, offsets) = self.0.iter().fold(
4017 (
4018 molecule::NUMBER_SIZE * (item_count + 1),
4019 Vec::with_capacity(item_count),
4020 ),
4021 |(start, mut offsets), inner| {
4022 offsets.push(start);
4023 (start + inner.as_slice().len(), offsets)
4024 },
4025 );
4026 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4027 for offset in offsets.into_iter() {
4028 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4029 }
4030 for inner in self.0.iter() {
4031 writer.write_all(inner.as_slice())?;
4032 }
4033 }
4034 Ok(())
4035 }
4036 fn build(&self) -> Self::Entity {
4037 let mut inner = Vec::with_capacity(self.expected_length());
4038 self.write(&mut inner)
4039 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4040 TransactionVec::new_unchecked(inner.into())
4041 }
4042}
4043pub struct TransactionVecIterator(TransactionVec, usize, usize);
4044impl ::core::iter::Iterator for TransactionVecIterator {
4045 type Item = Transaction;
4046 fn next(&mut self) -> Option<Self::Item> {
4047 if self.1 >= self.2 {
4048 None
4049 } else {
4050 let ret = self.0.get_unchecked(self.1);
4051 self.1 += 1;
4052 Some(ret)
4053 }
4054 }
4055}
4056impl ::core::iter::ExactSizeIterator for TransactionVecIterator {
4057 fn len(&self) -> usize {
4058 self.2 - self.1
4059 }
4060}
4061impl ::core::iter::IntoIterator for TransactionVec {
4062 type Item = Transaction;
4063 type IntoIter = TransactionVecIterator;
4064 fn into_iter(self) -> Self::IntoIter {
4065 let len = self.len();
4066 TransactionVecIterator(self, 0, len)
4067 }
4068}
4069impl<'r> TransactionVecReader<'r> {
4070 pub fn iter<'t>(&'t self) -> TransactionVecReaderIterator<'t, 'r> {
4071 TransactionVecReaderIterator(&self, 0, self.len())
4072 }
4073}
4074pub struct TransactionVecReaderIterator<'t, 'r>(&'t TransactionVecReader<'r>, usize, usize);
4075impl<'t: 'r, 'r> ::core::iter::Iterator for TransactionVecReaderIterator<'t, 'r> {
4076 type Item = TransactionReader<'t>;
4077 fn next(&mut self) -> Option<Self::Item> {
4078 if self.1 >= self.2 {
4079 None
4080 } else {
4081 let ret = self.0.get_unchecked(self.1);
4082 self.1 += 1;
4083 Some(ret)
4084 }
4085 }
4086}
4087impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for TransactionVecReaderIterator<'t, 'r> {
4088 fn len(&self) -> usize {
4089 self.2 - self.1
4090 }
4091}
4092#[derive(Clone)]
4093pub struct ProposalShortIdVec(molecule::bytes::Bytes);
4094impl ::core::fmt::LowerHex for ProposalShortIdVec {
4095 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4096 use molecule::hex_string;
4097 if f.alternate() {
4098 write!(f, "0x")?;
4099 }
4100 write!(f, "{}", hex_string(self.as_slice()))
4101 }
4102}
4103impl ::core::fmt::Debug for ProposalShortIdVec {
4104 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4105 write!(f, "{}({:#x})", Self::NAME, self)
4106 }
4107}
4108impl ::core::fmt::Display for ProposalShortIdVec {
4109 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4110 write!(f, "{} [", Self::NAME)?;
4111 for i in 0..self.len() {
4112 if i == 0 {
4113 write!(f, "{}", self.get_unchecked(i))?;
4114 } else {
4115 write!(f, ", {}", self.get_unchecked(i))?;
4116 }
4117 }
4118 write!(f, "]")
4119 }
4120}
4121impl ::core::default::Default for ProposalShortIdVec {
4122 fn default() -> Self {
4123 let v: Vec<u8> = vec![0, 0, 0, 0];
4124 ProposalShortIdVec::new_unchecked(v.into())
4125 }
4126}
4127impl ProposalShortIdVec {
4128 pub const ITEM_SIZE: usize = 10;
4129 pub fn total_size(&self) -> usize {
4130 molecule::NUMBER_SIZE * (self.item_count() + 1)
4131 }
4132 pub fn item_count(&self) -> usize {
4133 molecule::unpack_number(self.as_slice()) as usize
4134 }
4135 pub fn len(&self) -> usize {
4136 self.item_count()
4137 }
4138 pub fn is_empty(&self) -> bool {
4139 self.len() == 0
4140 }
4141 pub fn get(&self, idx: usize) -> Option<ProposalShortId> {
4142 if idx >= self.len() {
4143 None
4144 } else {
4145 Some(self.get_unchecked(idx))
4146 }
4147 }
4148 pub fn get_unchecked(&self, idx: usize) -> ProposalShortId {
4149 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4150 let end = start + Self::ITEM_SIZE;
4151 ProposalShortId::new_unchecked(self.0.slice(start..end))
4152 }
4153 pub fn as_reader<'r>(&'r self) -> ProposalShortIdVecReader<'r> {
4154 ProposalShortIdVecReader::new_unchecked(self.as_slice())
4155 }
4156}
4157impl molecule::prelude::Entity for ProposalShortIdVec {
4158 type Builder = ProposalShortIdVecBuilder;
4159 const NAME: &'static str = "ProposalShortIdVec";
4160 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4161 ProposalShortIdVec(data)
4162 }
4163 fn as_bytes(&self) -> molecule::bytes::Bytes {
4164 self.0.clone()
4165 }
4166 fn as_slice(&self) -> &[u8] {
4167 &self.0[..]
4168 }
4169 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4170 ProposalShortIdVecReader::from_slice(slice).map(|reader| reader.to_entity())
4171 }
4172 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4173 ProposalShortIdVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4174 }
4175 fn new_builder() -> Self::Builder {
4176 ::core::default::Default::default()
4177 }
4178 fn as_builder(self) -> Self::Builder {
4179 Self::new_builder().extend(self.into_iter())
4180 }
4181}
4182#[derive(Clone, Copy)]
4183pub struct ProposalShortIdVecReader<'r>(&'r [u8]);
4184impl<'r> ::core::fmt::LowerHex for ProposalShortIdVecReader<'r> {
4185 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4186 use molecule::hex_string;
4187 if f.alternate() {
4188 write!(f, "0x")?;
4189 }
4190 write!(f, "{}", hex_string(self.as_slice()))
4191 }
4192}
4193impl<'r> ::core::fmt::Debug for ProposalShortIdVecReader<'r> {
4194 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4195 write!(f, "{}({:#x})", Self::NAME, self)
4196 }
4197}
4198impl<'r> ::core::fmt::Display for ProposalShortIdVecReader<'r> {
4199 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4200 write!(f, "{} [", Self::NAME)?;
4201 for i in 0..self.len() {
4202 if i == 0 {
4203 write!(f, "{}", self.get_unchecked(i))?;
4204 } else {
4205 write!(f, ", {}", self.get_unchecked(i))?;
4206 }
4207 }
4208 write!(f, "]")
4209 }
4210}
4211impl<'r> ProposalShortIdVecReader<'r> {
4212 pub const ITEM_SIZE: usize = 10;
4213 pub fn total_size(&self) -> usize {
4214 molecule::NUMBER_SIZE * (self.item_count() + 1)
4215 }
4216 pub fn item_count(&self) -> usize {
4217 molecule::unpack_number(self.as_slice()) as usize
4218 }
4219 pub fn len(&self) -> usize {
4220 self.item_count()
4221 }
4222 pub fn is_empty(&self) -> bool {
4223 self.len() == 0
4224 }
4225 pub fn get(&self, idx: usize) -> Option<ProposalShortIdReader<'r>> {
4226 if idx >= self.len() {
4227 None
4228 } else {
4229 Some(self.get_unchecked(idx))
4230 }
4231 }
4232 pub fn get_unchecked(&self, idx: usize) -> ProposalShortIdReader<'r> {
4233 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4234 let end = start + Self::ITEM_SIZE;
4235 ProposalShortIdReader::new_unchecked(&self.as_slice()[start..end])
4236 }
4237}
4238impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdVecReader<'r> {
4239 type Entity = ProposalShortIdVec;
4240 const NAME: &'static str = "ProposalShortIdVecReader";
4241 fn to_entity(&self) -> Self::Entity {
4242 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4243 }
4244 fn new_unchecked(slice: &'r [u8]) -> Self {
4245 ProposalShortIdVecReader(slice)
4246 }
4247 fn as_slice(&self) -> &'r [u8] {
4248 self.0
4249 }
4250 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4251 use molecule::verification_error as ve;
4252 let slice_len = slice.len();
4253 if slice_len < molecule::NUMBER_SIZE {
4254 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4255 }
4256 let item_count = molecule::unpack_number(slice) as usize;
4257 if item_count == 0 {
4258 if slice_len != molecule::NUMBER_SIZE {
4259 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
4260 }
4261 return Ok(());
4262 }
4263 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
4264 if slice_len != total_size {
4265 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4266 }
4267 Ok(())
4268 }
4269}
4270#[derive(Debug, Default)]
4271pub struct ProposalShortIdVecBuilder(pub(crate) Vec<ProposalShortId>);
4272impl ProposalShortIdVecBuilder {
4273 pub const ITEM_SIZE: usize = 10;
4274 pub fn set(mut self, v: Vec<ProposalShortId>) -> Self {
4275 self.0 = v;
4276 self
4277 }
4278 pub fn push(mut self, v: ProposalShortId) -> Self {
4279 self.0.push(v);
4280 self
4281 }
4282 pub fn extend<T: ::core::iter::IntoIterator<Item = ProposalShortId>>(
4283 mut self,
4284 iter: T,
4285 ) -> Self {
4286 for elem in iter {
4287 self.0.push(elem);
4288 }
4289 self
4290 }
4291}
4292impl molecule::prelude::Builder for ProposalShortIdVecBuilder {
4293 type Entity = ProposalShortIdVec;
4294 const NAME: &'static str = "ProposalShortIdVecBuilder";
4295 fn expected_length(&self) -> usize {
4296 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
4297 }
4298 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4299 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
4300 for inner in &self.0[..] {
4301 writer.write_all(inner.as_slice())?;
4302 }
4303 Ok(())
4304 }
4305 fn build(&self) -> Self::Entity {
4306 let mut inner = Vec::with_capacity(self.expected_length());
4307 self.write(&mut inner)
4308 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4309 ProposalShortIdVec::new_unchecked(inner.into())
4310 }
4311}
4312pub struct ProposalShortIdVecIterator(ProposalShortIdVec, usize, usize);
4313impl ::core::iter::Iterator for ProposalShortIdVecIterator {
4314 type Item = ProposalShortId;
4315 fn next(&mut self) -> Option<Self::Item> {
4316 if self.1 >= self.2 {
4317 None
4318 } else {
4319 let ret = self.0.get_unchecked(self.1);
4320 self.1 += 1;
4321 Some(ret)
4322 }
4323 }
4324}
4325impl ::core::iter::ExactSizeIterator for ProposalShortIdVecIterator {
4326 fn len(&self) -> usize {
4327 self.2 - self.1
4328 }
4329}
4330impl ::core::iter::IntoIterator for ProposalShortIdVec {
4331 type Item = ProposalShortId;
4332 type IntoIter = ProposalShortIdVecIterator;
4333 fn into_iter(self) -> Self::IntoIter {
4334 let len = self.len();
4335 ProposalShortIdVecIterator(self, 0, len)
4336 }
4337}
4338impl<'r> ProposalShortIdVecReader<'r> {
4339 pub fn iter<'t>(&'t self) -> ProposalShortIdVecReaderIterator<'t, 'r> {
4340 ProposalShortIdVecReaderIterator(&self, 0, self.len())
4341 }
4342}
4343pub struct ProposalShortIdVecReaderIterator<'t, 'r>(&'t ProposalShortIdVecReader<'r>, usize, usize);
4344impl<'t: 'r, 'r> ::core::iter::Iterator for ProposalShortIdVecReaderIterator<'t, 'r> {
4345 type Item = ProposalShortIdReader<'t>;
4346 fn next(&mut self) -> Option<Self::Item> {
4347 if self.1 >= self.2 {
4348 None
4349 } else {
4350 let ret = self.0.get_unchecked(self.1);
4351 self.1 += 1;
4352 Some(ret)
4353 }
4354 }
4355}
4356impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for ProposalShortIdVecReaderIterator<'t, 'r> {
4357 fn len(&self) -> usize {
4358 self.2 - self.1
4359 }
4360}
4361#[derive(Clone)]
4362pub struct CellDepVec(molecule::bytes::Bytes);
4363impl ::core::fmt::LowerHex for CellDepVec {
4364 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4365 use molecule::hex_string;
4366 if f.alternate() {
4367 write!(f, "0x")?;
4368 }
4369 write!(f, "{}", hex_string(self.as_slice()))
4370 }
4371}
4372impl ::core::fmt::Debug for CellDepVec {
4373 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4374 write!(f, "{}({:#x})", Self::NAME, self)
4375 }
4376}
4377impl ::core::fmt::Display for CellDepVec {
4378 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4379 write!(f, "{} [", Self::NAME)?;
4380 for i in 0..self.len() {
4381 if i == 0 {
4382 write!(f, "{}", self.get_unchecked(i))?;
4383 } else {
4384 write!(f, ", {}", self.get_unchecked(i))?;
4385 }
4386 }
4387 write!(f, "]")
4388 }
4389}
4390impl ::core::default::Default for CellDepVec {
4391 fn default() -> Self {
4392 let v: Vec<u8> = vec![0, 0, 0, 0];
4393 CellDepVec::new_unchecked(v.into())
4394 }
4395}
4396impl CellDepVec {
4397 pub const ITEM_SIZE: usize = 37;
4398 pub fn total_size(&self) -> usize {
4399 molecule::NUMBER_SIZE * (self.item_count() + 1)
4400 }
4401 pub fn item_count(&self) -> usize {
4402 molecule::unpack_number(self.as_slice()) as usize
4403 }
4404 pub fn len(&self) -> usize {
4405 self.item_count()
4406 }
4407 pub fn is_empty(&self) -> bool {
4408 self.len() == 0
4409 }
4410 pub fn get(&self, idx: usize) -> Option<CellDep> {
4411 if idx >= self.len() {
4412 None
4413 } else {
4414 Some(self.get_unchecked(idx))
4415 }
4416 }
4417 pub fn get_unchecked(&self, idx: usize) -> CellDep {
4418 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4419 let end = start + Self::ITEM_SIZE;
4420 CellDep::new_unchecked(self.0.slice(start..end))
4421 }
4422 pub fn as_reader<'r>(&'r self) -> CellDepVecReader<'r> {
4423 CellDepVecReader::new_unchecked(self.as_slice())
4424 }
4425}
4426impl molecule::prelude::Entity for CellDepVec {
4427 type Builder = CellDepVecBuilder;
4428 const NAME: &'static str = "CellDepVec";
4429 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4430 CellDepVec(data)
4431 }
4432 fn as_bytes(&self) -> molecule::bytes::Bytes {
4433 self.0.clone()
4434 }
4435 fn as_slice(&self) -> &[u8] {
4436 &self.0[..]
4437 }
4438 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4439 CellDepVecReader::from_slice(slice).map(|reader| reader.to_entity())
4440 }
4441 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4442 CellDepVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4443 }
4444 fn new_builder() -> Self::Builder {
4445 ::core::default::Default::default()
4446 }
4447 fn as_builder(self) -> Self::Builder {
4448 Self::new_builder().extend(self.into_iter())
4449 }
4450}
4451#[derive(Clone, Copy)]
4452pub struct CellDepVecReader<'r>(&'r [u8]);
4453impl<'r> ::core::fmt::LowerHex for CellDepVecReader<'r> {
4454 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4455 use molecule::hex_string;
4456 if f.alternate() {
4457 write!(f, "0x")?;
4458 }
4459 write!(f, "{}", hex_string(self.as_slice()))
4460 }
4461}
4462impl<'r> ::core::fmt::Debug for CellDepVecReader<'r> {
4463 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4464 write!(f, "{}({:#x})", Self::NAME, self)
4465 }
4466}
4467impl<'r> ::core::fmt::Display for CellDepVecReader<'r> {
4468 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4469 write!(f, "{} [", Self::NAME)?;
4470 for i in 0..self.len() {
4471 if i == 0 {
4472 write!(f, "{}", self.get_unchecked(i))?;
4473 } else {
4474 write!(f, ", {}", self.get_unchecked(i))?;
4475 }
4476 }
4477 write!(f, "]")
4478 }
4479}
4480impl<'r> CellDepVecReader<'r> {
4481 pub const ITEM_SIZE: usize = 37;
4482 pub fn total_size(&self) -> usize {
4483 molecule::NUMBER_SIZE * (self.item_count() + 1)
4484 }
4485 pub fn item_count(&self) -> usize {
4486 molecule::unpack_number(self.as_slice()) as usize
4487 }
4488 pub fn len(&self) -> usize {
4489 self.item_count()
4490 }
4491 pub fn is_empty(&self) -> bool {
4492 self.len() == 0
4493 }
4494 pub fn get(&self, idx: usize) -> Option<CellDepReader<'r>> {
4495 if idx >= self.len() {
4496 None
4497 } else {
4498 Some(self.get_unchecked(idx))
4499 }
4500 }
4501 pub fn get_unchecked(&self, idx: usize) -> CellDepReader<'r> {
4502 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4503 let end = start + Self::ITEM_SIZE;
4504 CellDepReader::new_unchecked(&self.as_slice()[start..end])
4505 }
4506}
4507impl<'r> molecule::prelude::Reader<'r> for CellDepVecReader<'r> {
4508 type Entity = CellDepVec;
4509 const NAME: &'static str = "CellDepVecReader";
4510 fn to_entity(&self) -> Self::Entity {
4511 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4512 }
4513 fn new_unchecked(slice: &'r [u8]) -> Self {
4514 CellDepVecReader(slice)
4515 }
4516 fn as_slice(&self) -> &'r [u8] {
4517 self.0
4518 }
4519 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4520 use molecule::verification_error as ve;
4521 let slice_len = slice.len();
4522 if slice_len < molecule::NUMBER_SIZE {
4523 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4524 }
4525 let item_count = molecule::unpack_number(slice) as usize;
4526 if item_count == 0 {
4527 if slice_len != molecule::NUMBER_SIZE {
4528 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
4529 }
4530 return Ok(());
4531 }
4532 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
4533 if slice_len != total_size {
4534 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4535 }
4536 Ok(())
4537 }
4538}
4539#[derive(Debug, Default)]
4540pub struct CellDepVecBuilder(pub(crate) Vec<CellDep>);
4541impl CellDepVecBuilder {
4542 pub const ITEM_SIZE: usize = 37;
4543 pub fn set(mut self, v: Vec<CellDep>) -> Self {
4544 self.0 = v;
4545 self
4546 }
4547 pub fn push(mut self, v: CellDep) -> Self {
4548 self.0.push(v);
4549 self
4550 }
4551 pub fn extend<T: ::core::iter::IntoIterator<Item = CellDep>>(mut self, iter: T) -> Self {
4552 for elem in iter {
4553 self.0.push(elem);
4554 }
4555 self
4556 }
4557}
4558impl molecule::prelude::Builder for CellDepVecBuilder {
4559 type Entity = CellDepVec;
4560 const NAME: &'static str = "CellDepVecBuilder";
4561 fn expected_length(&self) -> usize {
4562 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
4563 }
4564 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4565 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
4566 for inner in &self.0[..] {
4567 writer.write_all(inner.as_slice())?;
4568 }
4569 Ok(())
4570 }
4571 fn build(&self) -> Self::Entity {
4572 let mut inner = Vec::with_capacity(self.expected_length());
4573 self.write(&mut inner)
4574 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4575 CellDepVec::new_unchecked(inner.into())
4576 }
4577}
4578pub struct CellDepVecIterator(CellDepVec, usize, usize);
4579impl ::core::iter::Iterator for CellDepVecIterator {
4580 type Item = CellDep;
4581 fn next(&mut self) -> Option<Self::Item> {
4582 if self.1 >= self.2 {
4583 None
4584 } else {
4585 let ret = self.0.get_unchecked(self.1);
4586 self.1 += 1;
4587 Some(ret)
4588 }
4589 }
4590}
4591impl ::core::iter::ExactSizeIterator for CellDepVecIterator {
4592 fn len(&self) -> usize {
4593 self.2 - self.1
4594 }
4595}
4596impl ::core::iter::IntoIterator for CellDepVec {
4597 type Item = CellDep;
4598 type IntoIter = CellDepVecIterator;
4599 fn into_iter(self) -> Self::IntoIter {
4600 let len = self.len();
4601 CellDepVecIterator(self, 0, len)
4602 }
4603}
4604impl<'r> CellDepVecReader<'r> {
4605 pub fn iter<'t>(&'t self) -> CellDepVecReaderIterator<'t, 'r> {
4606 CellDepVecReaderIterator(&self, 0, self.len())
4607 }
4608}
4609pub struct CellDepVecReaderIterator<'t, 'r>(&'t CellDepVecReader<'r>, usize, usize);
4610impl<'t: 'r, 'r> ::core::iter::Iterator for CellDepVecReaderIterator<'t, 'r> {
4611 type Item = CellDepReader<'t>;
4612 fn next(&mut self) -> Option<Self::Item> {
4613 if self.1 >= self.2 {
4614 None
4615 } else {
4616 let ret = self.0.get_unchecked(self.1);
4617 self.1 += 1;
4618 Some(ret)
4619 }
4620 }
4621}
4622impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellDepVecReaderIterator<'t, 'r> {
4623 fn len(&self) -> usize {
4624 self.2 - self.1
4625 }
4626}
4627#[derive(Clone)]
4628pub struct CellInputVec(molecule::bytes::Bytes);
4629impl ::core::fmt::LowerHex for CellInputVec {
4630 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4631 use molecule::hex_string;
4632 if f.alternate() {
4633 write!(f, "0x")?;
4634 }
4635 write!(f, "{}", hex_string(self.as_slice()))
4636 }
4637}
4638impl ::core::fmt::Debug for CellInputVec {
4639 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4640 write!(f, "{}({:#x})", Self::NAME, self)
4641 }
4642}
4643impl ::core::fmt::Display for CellInputVec {
4644 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4645 write!(f, "{} [", Self::NAME)?;
4646 for i in 0..self.len() {
4647 if i == 0 {
4648 write!(f, "{}", self.get_unchecked(i))?;
4649 } else {
4650 write!(f, ", {}", self.get_unchecked(i))?;
4651 }
4652 }
4653 write!(f, "]")
4654 }
4655}
4656impl ::core::default::Default for CellInputVec {
4657 fn default() -> Self {
4658 let v: Vec<u8> = vec![0, 0, 0, 0];
4659 CellInputVec::new_unchecked(v.into())
4660 }
4661}
4662impl CellInputVec {
4663 pub const ITEM_SIZE: usize = 44;
4664 pub fn total_size(&self) -> usize {
4665 molecule::NUMBER_SIZE * (self.item_count() + 1)
4666 }
4667 pub fn item_count(&self) -> usize {
4668 molecule::unpack_number(self.as_slice()) as usize
4669 }
4670 pub fn len(&self) -> usize {
4671 self.item_count()
4672 }
4673 pub fn is_empty(&self) -> bool {
4674 self.len() == 0
4675 }
4676 pub fn get(&self, idx: usize) -> Option<CellInput> {
4677 if idx >= self.len() {
4678 None
4679 } else {
4680 Some(self.get_unchecked(idx))
4681 }
4682 }
4683 pub fn get_unchecked(&self, idx: usize) -> CellInput {
4684 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4685 let end = start + Self::ITEM_SIZE;
4686 CellInput::new_unchecked(self.0.slice(start..end))
4687 }
4688 pub fn as_reader<'r>(&'r self) -> CellInputVecReader<'r> {
4689 CellInputVecReader::new_unchecked(self.as_slice())
4690 }
4691}
4692impl molecule::prelude::Entity for CellInputVec {
4693 type Builder = CellInputVecBuilder;
4694 const NAME: &'static str = "CellInputVec";
4695 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4696 CellInputVec(data)
4697 }
4698 fn as_bytes(&self) -> molecule::bytes::Bytes {
4699 self.0.clone()
4700 }
4701 fn as_slice(&self) -> &[u8] {
4702 &self.0[..]
4703 }
4704 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4705 CellInputVecReader::from_slice(slice).map(|reader| reader.to_entity())
4706 }
4707 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4708 CellInputVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4709 }
4710 fn new_builder() -> Self::Builder {
4711 ::core::default::Default::default()
4712 }
4713 fn as_builder(self) -> Self::Builder {
4714 Self::new_builder().extend(self.into_iter())
4715 }
4716}
4717#[derive(Clone, Copy)]
4718pub struct CellInputVecReader<'r>(&'r [u8]);
4719impl<'r> ::core::fmt::LowerHex for CellInputVecReader<'r> {
4720 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4721 use molecule::hex_string;
4722 if f.alternate() {
4723 write!(f, "0x")?;
4724 }
4725 write!(f, "{}", hex_string(self.as_slice()))
4726 }
4727}
4728impl<'r> ::core::fmt::Debug for CellInputVecReader<'r> {
4729 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4730 write!(f, "{}({:#x})", Self::NAME, self)
4731 }
4732}
4733impl<'r> ::core::fmt::Display for CellInputVecReader<'r> {
4734 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4735 write!(f, "{} [", Self::NAME)?;
4736 for i in 0..self.len() {
4737 if i == 0 {
4738 write!(f, "{}", self.get_unchecked(i))?;
4739 } else {
4740 write!(f, ", {}", self.get_unchecked(i))?;
4741 }
4742 }
4743 write!(f, "]")
4744 }
4745}
4746impl<'r> CellInputVecReader<'r> {
4747 pub const ITEM_SIZE: usize = 44;
4748 pub fn total_size(&self) -> usize {
4749 molecule::NUMBER_SIZE * (self.item_count() + 1)
4750 }
4751 pub fn item_count(&self) -> usize {
4752 molecule::unpack_number(self.as_slice()) as usize
4753 }
4754 pub fn len(&self) -> usize {
4755 self.item_count()
4756 }
4757 pub fn is_empty(&self) -> bool {
4758 self.len() == 0
4759 }
4760 pub fn get(&self, idx: usize) -> Option<CellInputReader<'r>> {
4761 if idx >= self.len() {
4762 None
4763 } else {
4764 Some(self.get_unchecked(idx))
4765 }
4766 }
4767 pub fn get_unchecked(&self, idx: usize) -> CellInputReader<'r> {
4768 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
4769 let end = start + Self::ITEM_SIZE;
4770 CellInputReader::new_unchecked(&self.as_slice()[start..end])
4771 }
4772}
4773impl<'r> molecule::prelude::Reader<'r> for CellInputVecReader<'r> {
4774 type Entity = CellInputVec;
4775 const NAME: &'static str = "CellInputVecReader";
4776 fn to_entity(&self) -> Self::Entity {
4777 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4778 }
4779 fn new_unchecked(slice: &'r [u8]) -> Self {
4780 CellInputVecReader(slice)
4781 }
4782 fn as_slice(&self) -> &'r [u8] {
4783 self.0
4784 }
4785 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4786 use molecule::verification_error as ve;
4787 let slice_len = slice.len();
4788 if slice_len < molecule::NUMBER_SIZE {
4789 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4790 }
4791 let item_count = molecule::unpack_number(slice) as usize;
4792 if item_count == 0 {
4793 if slice_len != molecule::NUMBER_SIZE {
4794 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
4795 }
4796 return Ok(());
4797 }
4798 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
4799 if slice_len != total_size {
4800 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4801 }
4802 Ok(())
4803 }
4804}
4805#[derive(Debug, Default)]
4806pub struct CellInputVecBuilder(pub(crate) Vec<CellInput>);
4807impl CellInputVecBuilder {
4808 pub const ITEM_SIZE: usize = 44;
4809 pub fn set(mut self, v: Vec<CellInput>) -> Self {
4810 self.0 = v;
4811 self
4812 }
4813 pub fn push(mut self, v: CellInput) -> Self {
4814 self.0.push(v);
4815 self
4816 }
4817 pub fn extend<T: ::core::iter::IntoIterator<Item = CellInput>>(mut self, iter: T) -> Self {
4818 for elem in iter {
4819 self.0.push(elem);
4820 }
4821 self
4822 }
4823}
4824impl molecule::prelude::Builder for CellInputVecBuilder {
4825 type Entity = CellInputVec;
4826 const NAME: &'static str = "CellInputVecBuilder";
4827 fn expected_length(&self) -> usize {
4828 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
4829 }
4830 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4831 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
4832 for inner in &self.0[..] {
4833 writer.write_all(inner.as_slice())?;
4834 }
4835 Ok(())
4836 }
4837 fn build(&self) -> Self::Entity {
4838 let mut inner = Vec::with_capacity(self.expected_length());
4839 self.write(&mut inner)
4840 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4841 CellInputVec::new_unchecked(inner.into())
4842 }
4843}
4844pub struct CellInputVecIterator(CellInputVec, usize, usize);
4845impl ::core::iter::Iterator for CellInputVecIterator {
4846 type Item = CellInput;
4847 fn next(&mut self) -> Option<Self::Item> {
4848 if self.1 >= self.2 {
4849 None
4850 } else {
4851 let ret = self.0.get_unchecked(self.1);
4852 self.1 += 1;
4853 Some(ret)
4854 }
4855 }
4856}
4857impl ::core::iter::ExactSizeIterator for CellInputVecIterator {
4858 fn len(&self) -> usize {
4859 self.2 - self.1
4860 }
4861}
4862impl ::core::iter::IntoIterator for CellInputVec {
4863 type Item = CellInput;
4864 type IntoIter = CellInputVecIterator;
4865 fn into_iter(self) -> Self::IntoIter {
4866 let len = self.len();
4867 CellInputVecIterator(self, 0, len)
4868 }
4869}
4870impl<'r> CellInputVecReader<'r> {
4871 pub fn iter<'t>(&'t self) -> CellInputVecReaderIterator<'t, 'r> {
4872 CellInputVecReaderIterator(&self, 0, self.len())
4873 }
4874}
4875pub struct CellInputVecReaderIterator<'t, 'r>(&'t CellInputVecReader<'r>, usize, usize);
4876impl<'t: 'r, 'r> ::core::iter::Iterator for CellInputVecReaderIterator<'t, 'r> {
4877 type Item = CellInputReader<'t>;
4878 fn next(&mut self) -> Option<Self::Item> {
4879 if self.1 >= self.2 {
4880 None
4881 } else {
4882 let ret = self.0.get_unchecked(self.1);
4883 self.1 += 1;
4884 Some(ret)
4885 }
4886 }
4887}
4888impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellInputVecReaderIterator<'t, 'r> {
4889 fn len(&self) -> usize {
4890 self.2 - self.1
4891 }
4892}
4893#[derive(Clone)]
4894pub struct CellOutputVec(molecule::bytes::Bytes);
4895impl ::core::fmt::LowerHex for CellOutputVec {
4896 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4897 use molecule::hex_string;
4898 if f.alternate() {
4899 write!(f, "0x")?;
4900 }
4901 write!(f, "{}", hex_string(self.as_slice()))
4902 }
4903}
4904impl ::core::fmt::Debug for CellOutputVec {
4905 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4906 write!(f, "{}({:#x})", Self::NAME, self)
4907 }
4908}
4909impl ::core::fmt::Display for CellOutputVec {
4910 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4911 write!(f, "{} [", Self::NAME)?;
4912 for i in 0..self.len() {
4913 if i == 0 {
4914 write!(f, "{}", self.get_unchecked(i))?;
4915 } else {
4916 write!(f, ", {}", self.get_unchecked(i))?;
4917 }
4918 }
4919 write!(f, "]")
4920 }
4921}
4922impl ::core::default::Default for CellOutputVec {
4923 fn default() -> Self {
4924 let v: Vec<u8> = vec![4, 0, 0, 0];
4925 CellOutputVec::new_unchecked(v.into())
4926 }
4927}
4928impl CellOutputVec {
4929 pub fn total_size(&self) -> usize {
4930 molecule::unpack_number(self.as_slice()) as usize
4931 }
4932 pub fn item_count(&self) -> usize {
4933 if self.total_size() == molecule::NUMBER_SIZE {
4934 0
4935 } else {
4936 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4937 }
4938 }
4939 pub fn len(&self) -> usize {
4940 self.item_count()
4941 }
4942 pub fn is_empty(&self) -> bool {
4943 self.len() == 0
4944 }
4945 pub fn get(&self, idx: usize) -> Option<CellOutput> {
4946 if idx >= self.len() {
4947 None
4948 } else {
4949 Some(self.get_unchecked(idx))
4950 }
4951 }
4952 pub fn get_unchecked(&self, idx: usize) -> CellOutput {
4953 let slice = self.as_slice();
4954 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4955 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4956 if idx == self.len() - 1 {
4957 CellOutput::new_unchecked(self.0.slice(start..))
4958 } else {
4959 let end_idx = start_idx + molecule::NUMBER_SIZE;
4960 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4961 CellOutput::new_unchecked(self.0.slice(start..end))
4962 }
4963 }
4964 pub fn as_reader<'r>(&'r self) -> CellOutputVecReader<'r> {
4965 CellOutputVecReader::new_unchecked(self.as_slice())
4966 }
4967}
4968impl molecule::prelude::Entity for CellOutputVec {
4969 type Builder = CellOutputVecBuilder;
4970 const NAME: &'static str = "CellOutputVec";
4971 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4972 CellOutputVec(data)
4973 }
4974 fn as_bytes(&self) -> molecule::bytes::Bytes {
4975 self.0.clone()
4976 }
4977 fn as_slice(&self) -> &[u8] {
4978 &self.0[..]
4979 }
4980 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4981 CellOutputVecReader::from_slice(slice).map(|reader| reader.to_entity())
4982 }
4983 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4984 CellOutputVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4985 }
4986 fn new_builder() -> Self::Builder {
4987 ::core::default::Default::default()
4988 }
4989 fn as_builder(self) -> Self::Builder {
4990 Self::new_builder().extend(self.into_iter())
4991 }
4992}
4993#[derive(Clone, Copy)]
4994pub struct CellOutputVecReader<'r>(&'r [u8]);
4995impl<'r> ::core::fmt::LowerHex for CellOutputVecReader<'r> {
4996 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4997 use molecule::hex_string;
4998 if f.alternate() {
4999 write!(f, "0x")?;
5000 }
5001 write!(f, "{}", hex_string(self.as_slice()))
5002 }
5003}
5004impl<'r> ::core::fmt::Debug for CellOutputVecReader<'r> {
5005 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5006 write!(f, "{}({:#x})", Self::NAME, self)
5007 }
5008}
5009impl<'r> ::core::fmt::Display for CellOutputVecReader<'r> {
5010 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5011 write!(f, "{} [", Self::NAME)?;
5012 for i in 0..self.len() {
5013 if i == 0 {
5014 write!(f, "{}", self.get_unchecked(i))?;
5015 } else {
5016 write!(f, ", {}", self.get_unchecked(i))?;
5017 }
5018 }
5019 write!(f, "]")
5020 }
5021}
5022impl<'r> CellOutputVecReader<'r> {
5023 pub fn total_size(&self) -> usize {
5024 molecule::unpack_number(self.as_slice()) as usize
5025 }
5026 pub fn item_count(&self) -> usize {
5027 if self.total_size() == molecule::NUMBER_SIZE {
5028 0
5029 } else {
5030 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5031 }
5032 }
5033 pub fn len(&self) -> usize {
5034 self.item_count()
5035 }
5036 pub fn is_empty(&self) -> bool {
5037 self.len() == 0
5038 }
5039 pub fn get(&self, idx: usize) -> Option<CellOutputReader<'r>> {
5040 if idx >= self.len() {
5041 None
5042 } else {
5043 Some(self.get_unchecked(idx))
5044 }
5045 }
5046 pub fn get_unchecked(&self, idx: usize) -> CellOutputReader<'r> {
5047 let slice = self.as_slice();
5048 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
5049 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
5050 if idx == self.len() - 1 {
5051 CellOutputReader::new_unchecked(&self.as_slice()[start..])
5052 } else {
5053 let end_idx = start_idx + molecule::NUMBER_SIZE;
5054 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
5055 CellOutputReader::new_unchecked(&self.as_slice()[start..end])
5056 }
5057 }
5058}
5059impl<'r> molecule::prelude::Reader<'r> for CellOutputVecReader<'r> {
5060 type Entity = CellOutputVec;
5061 const NAME: &'static str = "CellOutputVecReader";
5062 fn to_entity(&self) -> Self::Entity {
5063 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5064 }
5065 fn new_unchecked(slice: &'r [u8]) -> Self {
5066 CellOutputVecReader(slice)
5067 }
5068 fn as_slice(&self) -> &'r [u8] {
5069 self.0
5070 }
5071 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5072 use molecule::verification_error as ve;
5073 let slice_len = slice.len();
5074 if slice_len < molecule::NUMBER_SIZE {
5075 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5076 }
5077 let total_size = molecule::unpack_number(slice) as usize;
5078 if slice_len != total_size {
5079 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5080 }
5081 if slice_len == molecule::NUMBER_SIZE {
5082 return Ok(());
5083 }
5084 if slice_len < molecule::NUMBER_SIZE * 2 {
5085 return ve!(
5086 Self,
5087 TotalSizeNotMatch,
5088 molecule::NUMBER_SIZE * 2,
5089 slice_len
5090 );
5091 }
5092 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5093 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5094 return ve!(Self, OffsetsNotMatch);
5095 }
5096 if slice_len < offset_first {
5097 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5098 }
5099 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5100 .chunks_exact(molecule::NUMBER_SIZE)
5101 .map(|x| molecule::unpack_number(x) as usize)
5102 .collect();
5103 offsets.push(total_size);
5104 if offsets.windows(2).any(|i| i[0] > i[1]) {
5105 return ve!(Self, OffsetsNotMatch);
5106 }
5107 for pair in offsets.windows(2) {
5108 let start = pair[0];
5109 let end = pair[1];
5110 CellOutputReader::verify(&slice[start..end], compatible)?;
5111 }
5112 Ok(())
5113 }
5114}
5115#[derive(Debug, Default)]
5116pub struct CellOutputVecBuilder(pub(crate) Vec<CellOutput>);
5117impl CellOutputVecBuilder {
5118 pub fn set(mut self, v: Vec<CellOutput>) -> Self {
5119 self.0 = v;
5120 self
5121 }
5122 pub fn push(mut self, v: CellOutput) -> Self {
5123 self.0.push(v);
5124 self
5125 }
5126 pub fn extend<T: ::core::iter::IntoIterator<Item = CellOutput>>(mut self, iter: T) -> Self {
5127 for elem in iter {
5128 self.0.push(elem);
5129 }
5130 self
5131 }
5132}
5133impl molecule::prelude::Builder for CellOutputVecBuilder {
5134 type Entity = CellOutputVec;
5135 const NAME: &'static str = "CellOutputVecBuilder";
5136 fn expected_length(&self) -> usize {
5137 molecule::NUMBER_SIZE * (self.0.len() + 1)
5138 + self
5139 .0
5140 .iter()
5141 .map(|inner| inner.as_slice().len())
5142 .sum::<usize>()
5143 }
5144 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5145 let item_count = self.0.len();
5146 if item_count == 0 {
5147 writer.write_all(&molecule::pack_number(
5148 molecule::NUMBER_SIZE as molecule::Number,
5149 ))?;
5150 } else {
5151 let (total_size, offsets) = self.0.iter().fold(
5152 (
5153 molecule::NUMBER_SIZE * (item_count + 1),
5154 Vec::with_capacity(item_count),
5155 ),
5156 |(start, mut offsets), inner| {
5157 offsets.push(start);
5158 (start + inner.as_slice().len(), offsets)
5159 },
5160 );
5161 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5162 for offset in offsets.into_iter() {
5163 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5164 }
5165 for inner in self.0.iter() {
5166 writer.write_all(inner.as_slice())?;
5167 }
5168 }
5169 Ok(())
5170 }
5171 fn build(&self) -> Self::Entity {
5172 let mut inner = Vec::with_capacity(self.expected_length());
5173 self.write(&mut inner)
5174 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5175 CellOutputVec::new_unchecked(inner.into())
5176 }
5177}
5178pub struct CellOutputVecIterator(CellOutputVec, usize, usize);
5179impl ::core::iter::Iterator for CellOutputVecIterator {
5180 type Item = CellOutput;
5181 fn next(&mut self) -> Option<Self::Item> {
5182 if self.1 >= self.2 {
5183 None
5184 } else {
5185 let ret = self.0.get_unchecked(self.1);
5186 self.1 += 1;
5187 Some(ret)
5188 }
5189 }
5190}
5191impl ::core::iter::ExactSizeIterator for CellOutputVecIterator {
5192 fn len(&self) -> usize {
5193 self.2 - self.1
5194 }
5195}
5196impl ::core::iter::IntoIterator for CellOutputVec {
5197 type Item = CellOutput;
5198 type IntoIter = CellOutputVecIterator;
5199 fn into_iter(self) -> Self::IntoIter {
5200 let len = self.len();
5201 CellOutputVecIterator(self, 0, len)
5202 }
5203}
5204impl<'r> CellOutputVecReader<'r> {
5205 pub fn iter<'t>(&'t self) -> CellOutputVecReaderIterator<'t, 'r> {
5206 CellOutputVecReaderIterator(&self, 0, self.len())
5207 }
5208}
5209pub struct CellOutputVecReaderIterator<'t, 'r>(&'t CellOutputVecReader<'r>, usize, usize);
5210impl<'t: 'r, 'r> ::core::iter::Iterator for CellOutputVecReaderIterator<'t, 'r> {
5211 type Item = CellOutputReader<'t>;
5212 fn next(&mut self) -> Option<Self::Item> {
5213 if self.1 >= self.2 {
5214 None
5215 } else {
5216 let ret = self.0.get_unchecked(self.1);
5217 self.1 += 1;
5218 Some(ret)
5219 }
5220 }
5221}
5222impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellOutputVecReaderIterator<'t, 'r> {
5223 fn len(&self) -> usize {
5224 self.2 - self.1
5225 }
5226}
5227#[derive(Clone)]
5228pub struct Script(molecule::bytes::Bytes);
5229impl ::core::fmt::LowerHex for Script {
5230 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5231 use molecule::hex_string;
5232 if f.alternate() {
5233 write!(f, "0x")?;
5234 }
5235 write!(f, "{}", hex_string(self.as_slice()))
5236 }
5237}
5238impl ::core::fmt::Debug for Script {
5239 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5240 write!(f, "{}({:#x})", Self::NAME, self)
5241 }
5242}
5243impl ::core::fmt::Display for Script {
5244 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5245 write!(f, "{} {{ ", Self::NAME)?;
5246 write!(f, "{}: {}", "code_hash", self.code_hash())?;
5247 write!(f, ", {}: {}", "hash_type", self.hash_type())?;
5248 write!(f, ", {}: {}", "args", self.args())?;
5249 let extra_count = self.count_extra_fields();
5250 if extra_count != 0 {
5251 write!(f, ", .. ({} fields)", extra_count)?;
5252 }
5253 write!(f, " }}")
5254 }
5255}
5256impl ::core::default::Default for Script {
5257 fn default() -> Self {
5258 let v: Vec<u8> = vec![
5259 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5261 ];
5262 Script::new_unchecked(v.into())
5263 }
5264}
5265impl Script {
5266 pub const FIELD_COUNT: usize = 3;
5267 pub fn total_size(&self) -> usize {
5268 molecule::unpack_number(self.as_slice()) as usize
5269 }
5270 pub fn field_count(&self) -> usize {
5271 if self.total_size() == molecule::NUMBER_SIZE {
5272 0
5273 } else {
5274 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5275 }
5276 }
5277 pub fn count_extra_fields(&self) -> usize {
5278 self.field_count() - Self::FIELD_COUNT
5279 }
5280 pub fn has_extra_fields(&self) -> bool {
5281 Self::FIELD_COUNT != self.field_count()
5282 }
5283 pub fn code_hash(&self) -> Byte32 {
5284 let slice = self.as_slice();
5285 let start = molecule::unpack_number(&slice[4..]) as usize;
5286 let end = molecule::unpack_number(&slice[8..]) as usize;
5287 Byte32::new_unchecked(self.0.slice(start..end))
5288 }
5289 pub fn hash_type(&self) -> Byte {
5290 let slice = self.as_slice();
5291 let start = molecule::unpack_number(&slice[8..]) as usize;
5292 let end = molecule::unpack_number(&slice[12..]) as usize;
5293 Byte::new_unchecked(self.0.slice(start..end))
5294 }
5295 pub fn args(&self) -> Bytes {
5296 let slice = self.as_slice();
5297 let start = molecule::unpack_number(&slice[12..]) as usize;
5298 if self.has_extra_fields() {
5299 let end = molecule::unpack_number(&slice[16..]) as usize;
5300 Bytes::new_unchecked(self.0.slice(start..end))
5301 } else {
5302 Bytes::new_unchecked(self.0.slice(start..))
5303 }
5304 }
5305 pub fn as_reader<'r>(&'r self) -> ScriptReader<'r> {
5306 ScriptReader::new_unchecked(self.as_slice())
5307 }
5308}
5309impl molecule::prelude::Entity for Script {
5310 type Builder = ScriptBuilder;
5311 const NAME: &'static str = "Script";
5312 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5313 Script(data)
5314 }
5315 fn as_bytes(&self) -> molecule::bytes::Bytes {
5316 self.0.clone()
5317 }
5318 fn as_slice(&self) -> &[u8] {
5319 &self.0[..]
5320 }
5321 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5322 ScriptReader::from_slice(slice).map(|reader| reader.to_entity())
5323 }
5324 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5325 ScriptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5326 }
5327 fn new_builder() -> Self::Builder {
5328 ::core::default::Default::default()
5329 }
5330 fn as_builder(self) -> Self::Builder {
5331 Self::new_builder()
5332 .code_hash(self.code_hash())
5333 .hash_type(self.hash_type())
5334 .args(self.args())
5335 }
5336}
5337#[derive(Clone, Copy)]
5338pub struct ScriptReader<'r>(&'r [u8]);
5339impl<'r> ::core::fmt::LowerHex for ScriptReader<'r> {
5340 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5341 use molecule::hex_string;
5342 if f.alternate() {
5343 write!(f, "0x")?;
5344 }
5345 write!(f, "{}", hex_string(self.as_slice()))
5346 }
5347}
5348impl<'r> ::core::fmt::Debug for ScriptReader<'r> {
5349 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5350 write!(f, "{}({:#x})", Self::NAME, self)
5351 }
5352}
5353impl<'r> ::core::fmt::Display for ScriptReader<'r> {
5354 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5355 write!(f, "{} {{ ", Self::NAME)?;
5356 write!(f, "{}: {}", "code_hash", self.code_hash())?;
5357 write!(f, ", {}: {}", "hash_type", self.hash_type())?;
5358 write!(f, ", {}: {}", "args", self.args())?;
5359 let extra_count = self.count_extra_fields();
5360 if extra_count != 0 {
5361 write!(f, ", .. ({} fields)", extra_count)?;
5362 }
5363 write!(f, " }}")
5364 }
5365}
5366impl<'r> ScriptReader<'r> {
5367 pub const FIELD_COUNT: usize = 3;
5368 pub fn total_size(&self) -> usize {
5369 molecule::unpack_number(self.as_slice()) as usize
5370 }
5371 pub fn field_count(&self) -> usize {
5372 if self.total_size() == molecule::NUMBER_SIZE {
5373 0
5374 } else {
5375 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5376 }
5377 }
5378 pub fn count_extra_fields(&self) -> usize {
5379 self.field_count() - Self::FIELD_COUNT
5380 }
5381 pub fn has_extra_fields(&self) -> bool {
5382 Self::FIELD_COUNT != self.field_count()
5383 }
5384 pub fn code_hash(&self) -> Byte32Reader<'r> {
5385 let slice = self.as_slice();
5386 let start = molecule::unpack_number(&slice[4..]) as usize;
5387 let end = molecule::unpack_number(&slice[8..]) as usize;
5388 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
5389 }
5390 pub fn hash_type(&self) -> ByteReader<'r> {
5391 let slice = self.as_slice();
5392 let start = molecule::unpack_number(&slice[8..]) as usize;
5393 let end = molecule::unpack_number(&slice[12..]) as usize;
5394 ByteReader::new_unchecked(&self.as_slice()[start..end])
5395 }
5396 pub fn args(&self) -> BytesReader<'r> {
5397 let slice = self.as_slice();
5398 let start = molecule::unpack_number(&slice[12..]) as usize;
5399 if self.has_extra_fields() {
5400 let end = molecule::unpack_number(&slice[16..]) as usize;
5401 BytesReader::new_unchecked(&self.as_slice()[start..end])
5402 } else {
5403 BytesReader::new_unchecked(&self.as_slice()[start..])
5404 }
5405 }
5406}
5407impl<'r> molecule::prelude::Reader<'r> for ScriptReader<'r> {
5408 type Entity = Script;
5409 const NAME: &'static str = "ScriptReader";
5410 fn to_entity(&self) -> Self::Entity {
5411 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5412 }
5413 fn new_unchecked(slice: &'r [u8]) -> Self {
5414 ScriptReader(slice)
5415 }
5416 fn as_slice(&self) -> &'r [u8] {
5417 self.0
5418 }
5419 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5420 use molecule::verification_error as ve;
5421 let slice_len = slice.len();
5422 if slice_len < molecule::NUMBER_SIZE {
5423 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5424 }
5425 let total_size = molecule::unpack_number(slice) as usize;
5426 if slice_len != total_size {
5427 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5428 }
5429 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
5430 return Ok(());
5431 }
5432 if slice_len < molecule::NUMBER_SIZE * 2 {
5433 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
5434 }
5435 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5436 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5437 return ve!(Self, OffsetsNotMatch);
5438 }
5439 if slice_len < offset_first {
5440 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5441 }
5442 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
5443 if field_count < Self::FIELD_COUNT {
5444 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5445 } else if !compatible && field_count > Self::FIELD_COUNT {
5446 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
5447 };
5448 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5449 .chunks_exact(molecule::NUMBER_SIZE)
5450 .map(|x| molecule::unpack_number(x) as usize)
5451 .collect();
5452 offsets.push(total_size);
5453 if offsets.windows(2).any(|i| i[0] > i[1]) {
5454 return ve!(Self, OffsetsNotMatch);
5455 }
5456 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
5457 ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
5458 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
5459 Ok(())
5460 }
5461}
5462#[derive(Debug, Default)]
5463pub struct ScriptBuilder {
5464 pub(crate) code_hash: Byte32,
5465 pub(crate) hash_type: Byte,
5466 pub(crate) args: Bytes,
5467}
5468impl ScriptBuilder {
5469 pub const FIELD_COUNT: usize = 3;
5470 pub fn code_hash(mut self, v: Byte32) -> Self {
5471 self.code_hash = v;
5472 self
5473 }
5474 pub fn hash_type(mut self, v: Byte) -> Self {
5475 self.hash_type = v;
5476 self
5477 }
5478 pub fn args(mut self, v: Bytes) -> Self {
5479 self.args = v;
5480 self
5481 }
5482}
5483impl molecule::prelude::Builder for ScriptBuilder {
5484 type Entity = Script;
5485 const NAME: &'static str = "ScriptBuilder";
5486 fn expected_length(&self) -> usize {
5487 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
5488 + self.code_hash.as_slice().len()
5489 + self.hash_type.as_slice().len()
5490 + self.args.as_slice().len()
5491 }
5492 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5493 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
5494 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
5495 offsets.push(total_size);
5496 total_size += self.code_hash.as_slice().len();
5497 offsets.push(total_size);
5498 total_size += self.hash_type.as_slice().len();
5499 offsets.push(total_size);
5500 total_size += self.args.as_slice().len();
5501 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5502 for offset in offsets.into_iter() {
5503 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5504 }
5505 writer.write_all(self.code_hash.as_slice())?;
5506 writer.write_all(self.hash_type.as_slice())?;
5507 writer.write_all(self.args.as_slice())?;
5508 Ok(())
5509 }
5510 fn build(&self) -> Self::Entity {
5511 let mut inner = Vec::with_capacity(self.expected_length());
5512 self.write(&mut inner)
5513 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5514 Script::new_unchecked(inner.into())
5515 }
5516}
5517#[derive(Clone)]
5518pub struct OutPoint(molecule::bytes::Bytes);
5519impl ::core::fmt::LowerHex for OutPoint {
5520 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5521 use molecule::hex_string;
5522 if f.alternate() {
5523 write!(f, "0x")?;
5524 }
5525 write!(f, "{}", hex_string(self.as_slice()))
5526 }
5527}
5528impl ::core::fmt::Debug for OutPoint {
5529 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5530 write!(f, "{}({:#x})", Self::NAME, self)
5531 }
5532}
5533impl ::core::fmt::Display for OutPoint {
5534 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5535 write!(f, "{} {{ ", Self::NAME)?;
5536 write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
5537 write!(f, ", {}: {}", "index", self.index())?;
5538 write!(f, " }}")
5539 }
5540}
5541impl ::core::default::Default for OutPoint {
5542 fn default() -> Self {
5543 let v: Vec<u8> = vec![
5544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5545 0, 0, 0, 0, 0, 0, 0,
5546 ];
5547 OutPoint::new_unchecked(v.into())
5548 }
5549}
5550impl OutPoint {
5551 pub const TOTAL_SIZE: usize = 36;
5552 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5553 pub const FIELD_COUNT: usize = 2;
5554 pub fn tx_hash(&self) -> Byte32 {
5555 Byte32::new_unchecked(self.0.slice(0..32))
5556 }
5557 pub fn index(&self) -> Uint32 {
5558 Uint32::new_unchecked(self.0.slice(32..36))
5559 }
5560 pub fn as_reader<'r>(&'r self) -> OutPointReader<'r> {
5561 OutPointReader::new_unchecked(self.as_slice())
5562 }
5563}
5564impl molecule::prelude::Entity for OutPoint {
5565 type Builder = OutPointBuilder;
5566 const NAME: &'static str = "OutPoint";
5567 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5568 OutPoint(data)
5569 }
5570 fn as_bytes(&self) -> molecule::bytes::Bytes {
5571 self.0.clone()
5572 }
5573 fn as_slice(&self) -> &[u8] {
5574 &self.0[..]
5575 }
5576 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5577 OutPointReader::from_slice(slice).map(|reader| reader.to_entity())
5578 }
5579 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5580 OutPointReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5581 }
5582 fn new_builder() -> Self::Builder {
5583 ::core::default::Default::default()
5584 }
5585 fn as_builder(self) -> Self::Builder {
5586 Self::new_builder()
5587 .tx_hash(self.tx_hash())
5588 .index(self.index())
5589 }
5590}
5591#[derive(Clone, Copy)]
5592pub struct OutPointReader<'r>(&'r [u8]);
5593impl<'r> ::core::fmt::LowerHex for OutPointReader<'r> {
5594 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5595 use molecule::hex_string;
5596 if f.alternate() {
5597 write!(f, "0x")?;
5598 }
5599 write!(f, "{}", hex_string(self.as_slice()))
5600 }
5601}
5602impl<'r> ::core::fmt::Debug for OutPointReader<'r> {
5603 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5604 write!(f, "{}({:#x})", Self::NAME, self)
5605 }
5606}
5607impl<'r> ::core::fmt::Display for OutPointReader<'r> {
5608 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5609 write!(f, "{} {{ ", Self::NAME)?;
5610 write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
5611 write!(f, ", {}: {}", "index", self.index())?;
5612 write!(f, " }}")
5613 }
5614}
5615impl<'r> OutPointReader<'r> {
5616 pub const TOTAL_SIZE: usize = 36;
5617 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5618 pub const FIELD_COUNT: usize = 2;
5619 pub fn tx_hash(&self) -> Byte32Reader<'r> {
5620 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
5621 }
5622 pub fn index(&self) -> Uint32Reader<'r> {
5623 Uint32Reader::new_unchecked(&self.as_slice()[32..36])
5624 }
5625}
5626impl<'r> molecule::prelude::Reader<'r> for OutPointReader<'r> {
5627 type Entity = OutPoint;
5628 const NAME: &'static str = "OutPointReader";
5629 fn to_entity(&self) -> Self::Entity {
5630 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5631 }
5632 fn new_unchecked(slice: &'r [u8]) -> Self {
5633 OutPointReader(slice)
5634 }
5635 fn as_slice(&self) -> &'r [u8] {
5636 self.0
5637 }
5638 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5639 use molecule::verification_error as ve;
5640 let slice_len = slice.len();
5641 if slice_len != Self::TOTAL_SIZE {
5642 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5643 }
5644 Ok(())
5645 }
5646}
5647#[derive(Debug, Default)]
5648pub struct OutPointBuilder {
5649 pub(crate) tx_hash: Byte32,
5650 pub(crate) index: Uint32,
5651}
5652impl OutPointBuilder {
5653 pub const TOTAL_SIZE: usize = 36;
5654 pub const FIELD_SIZES: [usize; 2] = [32, 4];
5655 pub const FIELD_COUNT: usize = 2;
5656 pub fn tx_hash(mut self, v: Byte32) -> Self {
5657 self.tx_hash = v;
5658 self
5659 }
5660 pub fn index(mut self, v: Uint32) -> Self {
5661 self.index = v;
5662 self
5663 }
5664}
5665impl molecule::prelude::Builder for OutPointBuilder {
5666 type Entity = OutPoint;
5667 const NAME: &'static str = "OutPointBuilder";
5668 fn expected_length(&self) -> usize {
5669 Self::TOTAL_SIZE
5670 }
5671 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5672 writer.write_all(self.tx_hash.as_slice())?;
5673 writer.write_all(self.index.as_slice())?;
5674 Ok(())
5675 }
5676 fn build(&self) -> Self::Entity {
5677 let mut inner = Vec::with_capacity(self.expected_length());
5678 self.write(&mut inner)
5679 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5680 OutPoint::new_unchecked(inner.into())
5681 }
5682}
5683#[derive(Clone)]
5684pub struct CellInput(molecule::bytes::Bytes);
5685impl ::core::fmt::LowerHex for CellInput {
5686 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5687 use molecule::hex_string;
5688 if f.alternate() {
5689 write!(f, "0x")?;
5690 }
5691 write!(f, "{}", hex_string(self.as_slice()))
5692 }
5693}
5694impl ::core::fmt::Debug for CellInput {
5695 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5696 write!(f, "{}({:#x})", Self::NAME, self)
5697 }
5698}
5699impl ::core::fmt::Display for CellInput {
5700 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5701 write!(f, "{} {{ ", Self::NAME)?;
5702 write!(f, "{}: {}", "since", self.since())?;
5703 write!(f, ", {}: {}", "previous_output", self.previous_output())?;
5704 write!(f, " }}")
5705 }
5706}
5707impl ::core::default::Default for CellInput {
5708 fn default() -> Self {
5709 let v: Vec<u8> = vec![
5710 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5711 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5712 ];
5713 CellInput::new_unchecked(v.into())
5714 }
5715}
5716impl CellInput {
5717 pub const TOTAL_SIZE: usize = 44;
5718 pub const FIELD_SIZES: [usize; 2] = [8, 36];
5719 pub const FIELD_COUNT: usize = 2;
5720 pub fn since(&self) -> Uint64 {
5721 Uint64::new_unchecked(self.0.slice(0..8))
5722 }
5723 pub fn previous_output(&self) -> OutPoint {
5724 OutPoint::new_unchecked(self.0.slice(8..44))
5725 }
5726 pub fn as_reader<'r>(&'r self) -> CellInputReader<'r> {
5727 CellInputReader::new_unchecked(self.as_slice())
5728 }
5729}
5730impl molecule::prelude::Entity for CellInput {
5731 type Builder = CellInputBuilder;
5732 const NAME: &'static str = "CellInput";
5733 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5734 CellInput(data)
5735 }
5736 fn as_bytes(&self) -> molecule::bytes::Bytes {
5737 self.0.clone()
5738 }
5739 fn as_slice(&self) -> &[u8] {
5740 &self.0[..]
5741 }
5742 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5743 CellInputReader::from_slice(slice).map(|reader| reader.to_entity())
5744 }
5745 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5746 CellInputReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5747 }
5748 fn new_builder() -> Self::Builder {
5749 ::core::default::Default::default()
5750 }
5751 fn as_builder(self) -> Self::Builder {
5752 Self::new_builder()
5753 .since(self.since())
5754 .previous_output(self.previous_output())
5755 }
5756}
5757#[derive(Clone, Copy)]
5758pub struct CellInputReader<'r>(&'r [u8]);
5759impl<'r> ::core::fmt::LowerHex for CellInputReader<'r> {
5760 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5761 use molecule::hex_string;
5762 if f.alternate() {
5763 write!(f, "0x")?;
5764 }
5765 write!(f, "{}", hex_string(self.as_slice()))
5766 }
5767}
5768impl<'r> ::core::fmt::Debug for CellInputReader<'r> {
5769 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5770 write!(f, "{}({:#x})", Self::NAME, self)
5771 }
5772}
5773impl<'r> ::core::fmt::Display for CellInputReader<'r> {
5774 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5775 write!(f, "{} {{ ", Self::NAME)?;
5776 write!(f, "{}: {}", "since", self.since())?;
5777 write!(f, ", {}: {}", "previous_output", self.previous_output())?;
5778 write!(f, " }}")
5779 }
5780}
5781impl<'r> CellInputReader<'r> {
5782 pub const TOTAL_SIZE: usize = 44;
5783 pub const FIELD_SIZES: [usize; 2] = [8, 36];
5784 pub const FIELD_COUNT: usize = 2;
5785 pub fn since(&self) -> Uint64Reader<'r> {
5786 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
5787 }
5788 pub fn previous_output(&self) -> OutPointReader<'r> {
5789 OutPointReader::new_unchecked(&self.as_slice()[8..44])
5790 }
5791}
5792impl<'r> molecule::prelude::Reader<'r> for CellInputReader<'r> {
5793 type Entity = CellInput;
5794 const NAME: &'static str = "CellInputReader";
5795 fn to_entity(&self) -> Self::Entity {
5796 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5797 }
5798 fn new_unchecked(slice: &'r [u8]) -> Self {
5799 CellInputReader(slice)
5800 }
5801 fn as_slice(&self) -> &'r [u8] {
5802 self.0
5803 }
5804 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5805 use molecule::verification_error as ve;
5806 let slice_len = slice.len();
5807 if slice_len != Self::TOTAL_SIZE {
5808 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
5809 }
5810 Ok(())
5811 }
5812}
5813#[derive(Debug, Default)]
5814pub struct CellInputBuilder {
5815 pub(crate) since: Uint64,
5816 pub(crate) previous_output: OutPoint,
5817}
5818impl CellInputBuilder {
5819 pub const TOTAL_SIZE: usize = 44;
5820 pub const FIELD_SIZES: [usize; 2] = [8, 36];
5821 pub const FIELD_COUNT: usize = 2;
5822 pub fn since(mut self, v: Uint64) -> Self {
5823 self.since = v;
5824 self
5825 }
5826 pub fn previous_output(mut self, v: OutPoint) -> Self {
5827 self.previous_output = v;
5828 self
5829 }
5830}
5831impl molecule::prelude::Builder for CellInputBuilder {
5832 type Entity = CellInput;
5833 const NAME: &'static str = "CellInputBuilder";
5834 fn expected_length(&self) -> usize {
5835 Self::TOTAL_SIZE
5836 }
5837 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5838 writer.write_all(self.since.as_slice())?;
5839 writer.write_all(self.previous_output.as_slice())?;
5840 Ok(())
5841 }
5842 fn build(&self) -> Self::Entity {
5843 let mut inner = Vec::with_capacity(self.expected_length());
5844 self.write(&mut inner)
5845 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5846 CellInput::new_unchecked(inner.into())
5847 }
5848}
5849#[derive(Clone)]
5850pub struct CellOutput(molecule::bytes::Bytes);
5851impl ::core::fmt::LowerHex for CellOutput {
5852 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5853 use molecule::hex_string;
5854 if f.alternate() {
5855 write!(f, "0x")?;
5856 }
5857 write!(f, "{}", hex_string(self.as_slice()))
5858 }
5859}
5860impl ::core::fmt::Debug for CellOutput {
5861 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5862 write!(f, "{}({:#x})", Self::NAME, self)
5863 }
5864}
5865impl ::core::fmt::Display for CellOutput {
5866 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5867 write!(f, "{} {{ ", Self::NAME)?;
5868 write!(f, "{}: {}", "capacity", self.capacity())?;
5869 write!(f, ", {}: {}", "lock", self.lock())?;
5870 write!(f, ", {}: {}", "type_", self.type_())?;
5871 let extra_count = self.count_extra_fields();
5872 if extra_count != 0 {
5873 write!(f, ", .. ({} fields)", extra_count)?;
5874 }
5875 write!(f, " }}")
5876 }
5877}
5878impl ::core::default::Default for CellOutput {
5879 fn default() -> Self {
5880 let v: Vec<u8> = vec![
5881 77, 0, 0, 0, 16, 0, 0, 0, 24, 0, 0, 0, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 0, 0,
5882 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5883 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5884 ];
5885 CellOutput::new_unchecked(v.into())
5886 }
5887}
5888impl CellOutput {
5889 pub const FIELD_COUNT: usize = 3;
5890 pub fn total_size(&self) -> usize {
5891 molecule::unpack_number(self.as_slice()) as usize
5892 }
5893 pub fn field_count(&self) -> usize {
5894 if self.total_size() == molecule::NUMBER_SIZE {
5895 0
5896 } else {
5897 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5898 }
5899 }
5900 pub fn count_extra_fields(&self) -> usize {
5901 self.field_count() - Self::FIELD_COUNT
5902 }
5903 pub fn has_extra_fields(&self) -> bool {
5904 Self::FIELD_COUNT != self.field_count()
5905 }
5906 pub fn capacity(&self) -> Uint64 {
5907 let slice = self.as_slice();
5908 let start = molecule::unpack_number(&slice[4..]) as usize;
5909 let end = molecule::unpack_number(&slice[8..]) as usize;
5910 Uint64::new_unchecked(self.0.slice(start..end))
5911 }
5912 pub fn lock(&self) -> Script {
5913 let slice = self.as_slice();
5914 let start = molecule::unpack_number(&slice[8..]) as usize;
5915 let end = molecule::unpack_number(&slice[12..]) as usize;
5916 Script::new_unchecked(self.0.slice(start..end))
5917 }
5918 pub fn type_(&self) -> ScriptOpt {
5919 let slice = self.as_slice();
5920 let start = molecule::unpack_number(&slice[12..]) as usize;
5921 if self.has_extra_fields() {
5922 let end = molecule::unpack_number(&slice[16..]) as usize;
5923 ScriptOpt::new_unchecked(self.0.slice(start..end))
5924 } else {
5925 ScriptOpt::new_unchecked(self.0.slice(start..))
5926 }
5927 }
5928 pub fn as_reader<'r>(&'r self) -> CellOutputReader<'r> {
5929 CellOutputReader::new_unchecked(self.as_slice())
5930 }
5931}
5932impl molecule::prelude::Entity for CellOutput {
5933 type Builder = CellOutputBuilder;
5934 const NAME: &'static str = "CellOutput";
5935 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5936 CellOutput(data)
5937 }
5938 fn as_bytes(&self) -> molecule::bytes::Bytes {
5939 self.0.clone()
5940 }
5941 fn as_slice(&self) -> &[u8] {
5942 &self.0[..]
5943 }
5944 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5945 CellOutputReader::from_slice(slice).map(|reader| reader.to_entity())
5946 }
5947 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5948 CellOutputReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5949 }
5950 fn new_builder() -> Self::Builder {
5951 ::core::default::Default::default()
5952 }
5953 fn as_builder(self) -> Self::Builder {
5954 Self::new_builder()
5955 .capacity(self.capacity())
5956 .lock(self.lock())
5957 .type_(self.type_())
5958 }
5959}
5960#[derive(Clone, Copy)]
5961pub struct CellOutputReader<'r>(&'r [u8]);
5962impl<'r> ::core::fmt::LowerHex for CellOutputReader<'r> {
5963 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5964 use molecule::hex_string;
5965 if f.alternate() {
5966 write!(f, "0x")?;
5967 }
5968 write!(f, "{}", hex_string(self.as_slice()))
5969 }
5970}
5971impl<'r> ::core::fmt::Debug for CellOutputReader<'r> {
5972 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5973 write!(f, "{}({:#x})", Self::NAME, self)
5974 }
5975}
5976impl<'r> ::core::fmt::Display for CellOutputReader<'r> {
5977 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5978 write!(f, "{} {{ ", Self::NAME)?;
5979 write!(f, "{}: {}", "capacity", self.capacity())?;
5980 write!(f, ", {}: {}", "lock", self.lock())?;
5981 write!(f, ", {}: {}", "type_", self.type_())?;
5982 let extra_count = self.count_extra_fields();
5983 if extra_count != 0 {
5984 write!(f, ", .. ({} fields)", extra_count)?;
5985 }
5986 write!(f, " }}")
5987 }
5988}
5989impl<'r> CellOutputReader<'r> {
5990 pub const FIELD_COUNT: usize = 3;
5991 pub fn total_size(&self) -> usize {
5992 molecule::unpack_number(self.as_slice()) as usize
5993 }
5994 pub fn field_count(&self) -> usize {
5995 if self.total_size() == molecule::NUMBER_SIZE {
5996 0
5997 } else {
5998 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5999 }
6000 }
6001 pub fn count_extra_fields(&self) -> usize {
6002 self.field_count() - Self::FIELD_COUNT
6003 }
6004 pub fn has_extra_fields(&self) -> bool {
6005 Self::FIELD_COUNT != self.field_count()
6006 }
6007 pub fn capacity(&self) -> Uint64Reader<'r> {
6008 let slice = self.as_slice();
6009 let start = molecule::unpack_number(&slice[4..]) as usize;
6010 let end = molecule::unpack_number(&slice[8..]) as usize;
6011 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
6012 }
6013 pub fn lock(&self) -> ScriptReader<'r> {
6014 let slice = self.as_slice();
6015 let start = molecule::unpack_number(&slice[8..]) as usize;
6016 let end = molecule::unpack_number(&slice[12..]) as usize;
6017 ScriptReader::new_unchecked(&self.as_slice()[start..end])
6018 }
6019 pub fn type_(&self) -> ScriptOptReader<'r> {
6020 let slice = self.as_slice();
6021 let start = molecule::unpack_number(&slice[12..]) as usize;
6022 if self.has_extra_fields() {
6023 let end = molecule::unpack_number(&slice[16..]) as usize;
6024 ScriptOptReader::new_unchecked(&self.as_slice()[start..end])
6025 } else {
6026 ScriptOptReader::new_unchecked(&self.as_slice()[start..])
6027 }
6028 }
6029}
6030impl<'r> molecule::prelude::Reader<'r> for CellOutputReader<'r> {
6031 type Entity = CellOutput;
6032 const NAME: &'static str = "CellOutputReader";
6033 fn to_entity(&self) -> Self::Entity {
6034 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6035 }
6036 fn new_unchecked(slice: &'r [u8]) -> Self {
6037 CellOutputReader(slice)
6038 }
6039 fn as_slice(&self) -> &'r [u8] {
6040 self.0
6041 }
6042 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6043 use molecule::verification_error as ve;
6044 let slice_len = slice.len();
6045 if slice_len < molecule::NUMBER_SIZE {
6046 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6047 }
6048 let total_size = molecule::unpack_number(slice) as usize;
6049 if slice_len != total_size {
6050 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6051 }
6052 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
6053 return Ok(());
6054 }
6055 if slice_len < molecule::NUMBER_SIZE * 2 {
6056 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6057 }
6058 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6059 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6060 return ve!(Self, OffsetsNotMatch);
6061 }
6062 if slice_len < offset_first {
6063 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6064 }
6065 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6066 if field_count < Self::FIELD_COUNT {
6067 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6068 } else if !compatible && field_count > Self::FIELD_COUNT {
6069 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6070 };
6071 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6072 .chunks_exact(molecule::NUMBER_SIZE)
6073 .map(|x| molecule::unpack_number(x) as usize)
6074 .collect();
6075 offsets.push(total_size);
6076 if offsets.windows(2).any(|i| i[0] > i[1]) {
6077 return ve!(Self, OffsetsNotMatch);
6078 }
6079 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6080 ScriptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6081 ScriptOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6082 Ok(())
6083 }
6084}
6085#[derive(Debug, Default)]
6086pub struct CellOutputBuilder {
6087 pub(crate) capacity: Uint64,
6088 pub(crate) lock: Script,
6089 pub(crate) type_: ScriptOpt,
6090}
6091impl CellOutputBuilder {
6092 pub const FIELD_COUNT: usize = 3;
6093 pub fn capacity(mut self, v: Uint64) -> Self {
6094 self.capacity = v;
6095 self
6096 }
6097 pub fn lock(mut self, v: Script) -> Self {
6098 self.lock = v;
6099 self
6100 }
6101 pub fn type_(mut self, v: ScriptOpt) -> Self {
6102 self.type_ = v;
6103 self
6104 }
6105}
6106impl molecule::prelude::Builder for CellOutputBuilder {
6107 type Entity = CellOutput;
6108 const NAME: &'static str = "CellOutputBuilder";
6109 fn expected_length(&self) -> usize {
6110 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6111 + self.capacity.as_slice().len()
6112 + self.lock.as_slice().len()
6113 + self.type_.as_slice().len()
6114 }
6115 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6116 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6117 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6118 offsets.push(total_size);
6119 total_size += self.capacity.as_slice().len();
6120 offsets.push(total_size);
6121 total_size += self.lock.as_slice().len();
6122 offsets.push(total_size);
6123 total_size += self.type_.as_slice().len();
6124 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6125 for offset in offsets.into_iter() {
6126 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6127 }
6128 writer.write_all(self.capacity.as_slice())?;
6129 writer.write_all(self.lock.as_slice())?;
6130 writer.write_all(self.type_.as_slice())?;
6131 Ok(())
6132 }
6133 fn build(&self) -> Self::Entity {
6134 let mut inner = Vec::with_capacity(self.expected_length());
6135 self.write(&mut inner)
6136 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6137 CellOutput::new_unchecked(inner.into())
6138 }
6139}
6140#[derive(Clone)]
6141pub struct CellDep(molecule::bytes::Bytes);
6142impl ::core::fmt::LowerHex for CellDep {
6143 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6144 use molecule::hex_string;
6145 if f.alternate() {
6146 write!(f, "0x")?;
6147 }
6148 write!(f, "{}", hex_string(self.as_slice()))
6149 }
6150}
6151impl ::core::fmt::Debug for CellDep {
6152 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6153 write!(f, "{}({:#x})", Self::NAME, self)
6154 }
6155}
6156impl ::core::fmt::Display for CellDep {
6157 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6158 write!(f, "{} {{ ", Self::NAME)?;
6159 write!(f, "{}: {}", "out_point", self.out_point())?;
6160 write!(f, ", {}: {}", "dep_type", self.dep_type())?;
6161 write!(f, " }}")
6162 }
6163}
6164impl ::core::default::Default for CellDep {
6165 fn default() -> Self {
6166 let v: Vec<u8> = vec![
6167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6168 0, 0, 0, 0, 0, 0, 0, 0,
6169 ];
6170 CellDep::new_unchecked(v.into())
6171 }
6172}
6173impl CellDep {
6174 pub const TOTAL_SIZE: usize = 37;
6175 pub const FIELD_SIZES: [usize; 2] = [36, 1];
6176 pub const FIELD_COUNT: usize = 2;
6177 pub fn out_point(&self) -> OutPoint {
6178 OutPoint::new_unchecked(self.0.slice(0..36))
6179 }
6180 pub fn dep_type(&self) -> Byte {
6181 Byte::new_unchecked(self.0.slice(36..37))
6182 }
6183 pub fn as_reader<'r>(&'r self) -> CellDepReader<'r> {
6184 CellDepReader::new_unchecked(self.as_slice())
6185 }
6186}
6187impl molecule::prelude::Entity for CellDep {
6188 type Builder = CellDepBuilder;
6189 const NAME: &'static str = "CellDep";
6190 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6191 CellDep(data)
6192 }
6193 fn as_bytes(&self) -> molecule::bytes::Bytes {
6194 self.0.clone()
6195 }
6196 fn as_slice(&self) -> &[u8] {
6197 &self.0[..]
6198 }
6199 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6200 CellDepReader::from_slice(slice).map(|reader| reader.to_entity())
6201 }
6202 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6203 CellDepReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6204 }
6205 fn new_builder() -> Self::Builder {
6206 ::core::default::Default::default()
6207 }
6208 fn as_builder(self) -> Self::Builder {
6209 Self::new_builder()
6210 .out_point(self.out_point())
6211 .dep_type(self.dep_type())
6212 }
6213}
6214#[derive(Clone, Copy)]
6215pub struct CellDepReader<'r>(&'r [u8]);
6216impl<'r> ::core::fmt::LowerHex for CellDepReader<'r> {
6217 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6218 use molecule::hex_string;
6219 if f.alternate() {
6220 write!(f, "0x")?;
6221 }
6222 write!(f, "{}", hex_string(self.as_slice()))
6223 }
6224}
6225impl<'r> ::core::fmt::Debug for CellDepReader<'r> {
6226 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6227 write!(f, "{}({:#x})", Self::NAME, self)
6228 }
6229}
6230impl<'r> ::core::fmt::Display for CellDepReader<'r> {
6231 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6232 write!(f, "{} {{ ", Self::NAME)?;
6233 write!(f, "{}: {}", "out_point", self.out_point())?;
6234 write!(f, ", {}: {}", "dep_type", self.dep_type())?;
6235 write!(f, " }}")
6236 }
6237}
6238impl<'r> CellDepReader<'r> {
6239 pub const TOTAL_SIZE: usize = 37;
6240 pub const FIELD_SIZES: [usize; 2] = [36, 1];
6241 pub const FIELD_COUNT: usize = 2;
6242 pub fn out_point(&self) -> OutPointReader<'r> {
6243 OutPointReader::new_unchecked(&self.as_slice()[0..36])
6244 }
6245 pub fn dep_type(&self) -> ByteReader<'r> {
6246 ByteReader::new_unchecked(&self.as_slice()[36..37])
6247 }
6248}
6249impl<'r> molecule::prelude::Reader<'r> for CellDepReader<'r> {
6250 type Entity = CellDep;
6251 const NAME: &'static str = "CellDepReader";
6252 fn to_entity(&self) -> Self::Entity {
6253 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6254 }
6255 fn new_unchecked(slice: &'r [u8]) -> Self {
6256 CellDepReader(slice)
6257 }
6258 fn as_slice(&self) -> &'r [u8] {
6259 self.0
6260 }
6261 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
6262 use molecule::verification_error as ve;
6263 let slice_len = slice.len();
6264 if slice_len != Self::TOTAL_SIZE {
6265 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
6266 }
6267 Ok(())
6268 }
6269}
6270#[derive(Debug, Default)]
6271pub struct CellDepBuilder {
6272 pub(crate) out_point: OutPoint,
6273 pub(crate) dep_type: Byte,
6274}
6275impl CellDepBuilder {
6276 pub const TOTAL_SIZE: usize = 37;
6277 pub const FIELD_SIZES: [usize; 2] = [36, 1];
6278 pub const FIELD_COUNT: usize = 2;
6279 pub fn out_point(mut self, v: OutPoint) -> Self {
6280 self.out_point = v;
6281 self
6282 }
6283 pub fn dep_type(mut self, v: Byte) -> Self {
6284 self.dep_type = v;
6285 self
6286 }
6287}
6288impl molecule::prelude::Builder for CellDepBuilder {
6289 type Entity = CellDep;
6290 const NAME: &'static str = "CellDepBuilder";
6291 fn expected_length(&self) -> usize {
6292 Self::TOTAL_SIZE
6293 }
6294 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6295 writer.write_all(self.out_point.as_slice())?;
6296 writer.write_all(self.dep_type.as_slice())?;
6297 Ok(())
6298 }
6299 fn build(&self) -> Self::Entity {
6300 let mut inner = Vec::with_capacity(self.expected_length());
6301 self.write(&mut inner)
6302 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6303 CellDep::new_unchecked(inner.into())
6304 }
6305}
6306#[derive(Clone)]
6307pub struct RawTransaction(molecule::bytes::Bytes);
6308impl ::core::fmt::LowerHex for RawTransaction {
6309 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6310 use molecule::hex_string;
6311 if f.alternate() {
6312 write!(f, "0x")?;
6313 }
6314 write!(f, "{}", hex_string(self.as_slice()))
6315 }
6316}
6317impl ::core::fmt::Debug for RawTransaction {
6318 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6319 write!(f, "{}({:#x})", Self::NAME, self)
6320 }
6321}
6322impl ::core::fmt::Display for RawTransaction {
6323 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6324 write!(f, "{} {{ ", Self::NAME)?;
6325 write!(f, "{}: {}", "version", self.version())?;
6326 write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
6327 write!(f, ", {}: {}", "header_deps", self.header_deps())?;
6328 write!(f, ", {}: {}", "inputs", self.inputs())?;
6329 write!(f, ", {}: {}", "outputs", self.outputs())?;
6330 write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
6331 let extra_count = self.count_extra_fields();
6332 if extra_count != 0 {
6333 write!(f, ", .. ({} fields)", extra_count)?;
6334 }
6335 write!(f, " }}")
6336 }
6337}
6338impl ::core::default::Default for RawTransaction {
6339 fn default() -> Self {
6340 let v: Vec<u8> = vec![
6341 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0,
6342 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
6343 ];
6344 RawTransaction::new_unchecked(v.into())
6345 }
6346}
6347impl RawTransaction {
6348 pub const FIELD_COUNT: usize = 6;
6349 pub fn total_size(&self) -> usize {
6350 molecule::unpack_number(self.as_slice()) as usize
6351 }
6352 pub fn field_count(&self) -> usize {
6353 if self.total_size() == molecule::NUMBER_SIZE {
6354 0
6355 } else {
6356 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6357 }
6358 }
6359 pub fn count_extra_fields(&self) -> usize {
6360 self.field_count() - Self::FIELD_COUNT
6361 }
6362 pub fn has_extra_fields(&self) -> bool {
6363 Self::FIELD_COUNT != self.field_count()
6364 }
6365 pub fn version(&self) -> Uint32 {
6366 let slice = self.as_slice();
6367 let start = molecule::unpack_number(&slice[4..]) as usize;
6368 let end = molecule::unpack_number(&slice[8..]) as usize;
6369 Uint32::new_unchecked(self.0.slice(start..end))
6370 }
6371 pub fn cell_deps(&self) -> CellDepVec {
6372 let slice = self.as_slice();
6373 let start = molecule::unpack_number(&slice[8..]) as usize;
6374 let end = molecule::unpack_number(&slice[12..]) as usize;
6375 CellDepVec::new_unchecked(self.0.slice(start..end))
6376 }
6377 pub fn header_deps(&self) -> Byte32Vec {
6378 let slice = self.as_slice();
6379 let start = molecule::unpack_number(&slice[12..]) as usize;
6380 let end = molecule::unpack_number(&slice[16..]) as usize;
6381 Byte32Vec::new_unchecked(self.0.slice(start..end))
6382 }
6383 pub fn inputs(&self) -> CellInputVec {
6384 let slice = self.as_slice();
6385 let start = molecule::unpack_number(&slice[16..]) as usize;
6386 let end = molecule::unpack_number(&slice[20..]) as usize;
6387 CellInputVec::new_unchecked(self.0.slice(start..end))
6388 }
6389 pub fn outputs(&self) -> CellOutputVec {
6390 let slice = self.as_slice();
6391 let start = molecule::unpack_number(&slice[20..]) as usize;
6392 let end = molecule::unpack_number(&slice[24..]) as usize;
6393 CellOutputVec::new_unchecked(self.0.slice(start..end))
6394 }
6395 pub fn outputs_data(&self) -> BytesVec {
6396 let slice = self.as_slice();
6397 let start = molecule::unpack_number(&slice[24..]) as usize;
6398 if self.has_extra_fields() {
6399 let end = molecule::unpack_number(&slice[28..]) as usize;
6400 BytesVec::new_unchecked(self.0.slice(start..end))
6401 } else {
6402 BytesVec::new_unchecked(self.0.slice(start..))
6403 }
6404 }
6405 pub fn as_reader<'r>(&'r self) -> RawTransactionReader<'r> {
6406 RawTransactionReader::new_unchecked(self.as_slice())
6407 }
6408}
6409impl molecule::prelude::Entity for RawTransaction {
6410 type Builder = RawTransactionBuilder;
6411 const NAME: &'static str = "RawTransaction";
6412 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6413 RawTransaction(data)
6414 }
6415 fn as_bytes(&self) -> molecule::bytes::Bytes {
6416 self.0.clone()
6417 }
6418 fn as_slice(&self) -> &[u8] {
6419 &self.0[..]
6420 }
6421 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6422 RawTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
6423 }
6424 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6425 RawTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6426 }
6427 fn new_builder() -> Self::Builder {
6428 ::core::default::Default::default()
6429 }
6430 fn as_builder(self) -> Self::Builder {
6431 Self::new_builder()
6432 .version(self.version())
6433 .cell_deps(self.cell_deps())
6434 .header_deps(self.header_deps())
6435 .inputs(self.inputs())
6436 .outputs(self.outputs())
6437 .outputs_data(self.outputs_data())
6438 }
6439}
6440#[derive(Clone, Copy)]
6441pub struct RawTransactionReader<'r>(&'r [u8]);
6442impl<'r> ::core::fmt::LowerHex for RawTransactionReader<'r> {
6443 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6444 use molecule::hex_string;
6445 if f.alternate() {
6446 write!(f, "0x")?;
6447 }
6448 write!(f, "{}", hex_string(self.as_slice()))
6449 }
6450}
6451impl<'r> ::core::fmt::Debug for RawTransactionReader<'r> {
6452 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6453 write!(f, "{}({:#x})", Self::NAME, self)
6454 }
6455}
6456impl<'r> ::core::fmt::Display for RawTransactionReader<'r> {
6457 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6458 write!(f, "{} {{ ", Self::NAME)?;
6459 write!(f, "{}: {}", "version", self.version())?;
6460 write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
6461 write!(f, ", {}: {}", "header_deps", self.header_deps())?;
6462 write!(f, ", {}: {}", "inputs", self.inputs())?;
6463 write!(f, ", {}: {}", "outputs", self.outputs())?;
6464 write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
6465 let extra_count = self.count_extra_fields();
6466 if extra_count != 0 {
6467 write!(f, ", .. ({} fields)", extra_count)?;
6468 }
6469 write!(f, " }}")
6470 }
6471}
6472impl<'r> RawTransactionReader<'r> {
6473 pub const FIELD_COUNT: usize = 6;
6474 pub fn total_size(&self) -> usize {
6475 molecule::unpack_number(self.as_slice()) as usize
6476 }
6477 pub fn field_count(&self) -> usize {
6478 if self.total_size() == molecule::NUMBER_SIZE {
6479 0
6480 } else {
6481 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6482 }
6483 }
6484 pub fn count_extra_fields(&self) -> usize {
6485 self.field_count() - Self::FIELD_COUNT
6486 }
6487 pub fn has_extra_fields(&self) -> bool {
6488 Self::FIELD_COUNT != self.field_count()
6489 }
6490 pub fn version(&self) -> Uint32Reader<'r> {
6491 let slice = self.as_slice();
6492 let start = molecule::unpack_number(&slice[4..]) as usize;
6493 let end = molecule::unpack_number(&slice[8..]) as usize;
6494 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
6495 }
6496 pub fn cell_deps(&self) -> CellDepVecReader<'r> {
6497 let slice = self.as_slice();
6498 let start = molecule::unpack_number(&slice[8..]) as usize;
6499 let end = molecule::unpack_number(&slice[12..]) as usize;
6500 CellDepVecReader::new_unchecked(&self.as_slice()[start..end])
6501 }
6502 pub fn header_deps(&self) -> Byte32VecReader<'r> {
6503 let slice = self.as_slice();
6504 let start = molecule::unpack_number(&slice[12..]) as usize;
6505 let end = molecule::unpack_number(&slice[16..]) as usize;
6506 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
6507 }
6508 pub fn inputs(&self) -> CellInputVecReader<'r> {
6509 let slice = self.as_slice();
6510 let start = molecule::unpack_number(&slice[16..]) as usize;
6511 let end = molecule::unpack_number(&slice[20..]) as usize;
6512 CellInputVecReader::new_unchecked(&self.as_slice()[start..end])
6513 }
6514 pub fn outputs(&self) -> CellOutputVecReader<'r> {
6515 let slice = self.as_slice();
6516 let start = molecule::unpack_number(&slice[20..]) as usize;
6517 let end = molecule::unpack_number(&slice[24..]) as usize;
6518 CellOutputVecReader::new_unchecked(&self.as_slice()[start..end])
6519 }
6520 pub fn outputs_data(&self) -> BytesVecReader<'r> {
6521 let slice = self.as_slice();
6522 let start = molecule::unpack_number(&slice[24..]) as usize;
6523 if self.has_extra_fields() {
6524 let end = molecule::unpack_number(&slice[28..]) as usize;
6525 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6526 } else {
6527 BytesVecReader::new_unchecked(&self.as_slice()[start..])
6528 }
6529 }
6530}
6531impl<'r> molecule::prelude::Reader<'r> for RawTransactionReader<'r> {
6532 type Entity = RawTransaction;
6533 const NAME: &'static str = "RawTransactionReader";
6534 fn to_entity(&self) -> Self::Entity {
6535 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6536 }
6537 fn new_unchecked(slice: &'r [u8]) -> Self {
6538 RawTransactionReader(slice)
6539 }
6540 fn as_slice(&self) -> &'r [u8] {
6541 self.0
6542 }
6543 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6544 use molecule::verification_error as ve;
6545 let slice_len = slice.len();
6546 if slice_len < molecule::NUMBER_SIZE {
6547 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6548 }
6549 let total_size = molecule::unpack_number(slice) as usize;
6550 if slice_len != total_size {
6551 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6552 }
6553 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
6554 return Ok(());
6555 }
6556 if slice_len < molecule::NUMBER_SIZE * 2 {
6557 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6558 }
6559 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6560 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6561 return ve!(Self, OffsetsNotMatch);
6562 }
6563 if slice_len < offset_first {
6564 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6565 }
6566 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6567 if field_count < Self::FIELD_COUNT {
6568 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6569 } else if !compatible && field_count > Self::FIELD_COUNT {
6570 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6571 };
6572 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6573 .chunks_exact(molecule::NUMBER_SIZE)
6574 .map(|x| molecule::unpack_number(x) as usize)
6575 .collect();
6576 offsets.push(total_size);
6577 if offsets.windows(2).any(|i| i[0] > i[1]) {
6578 return ve!(Self, OffsetsNotMatch);
6579 }
6580 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6581 CellDepVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6582 Byte32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6583 CellInputVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
6584 CellOutputVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
6585 BytesVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
6586 Ok(())
6587 }
6588}
6589#[derive(Debug, Default)]
6590pub struct RawTransactionBuilder {
6591 pub(crate) version: Uint32,
6592 pub(crate) cell_deps: CellDepVec,
6593 pub(crate) header_deps: Byte32Vec,
6594 pub(crate) inputs: CellInputVec,
6595 pub(crate) outputs: CellOutputVec,
6596 pub(crate) outputs_data: BytesVec,
6597}
6598impl RawTransactionBuilder {
6599 pub const FIELD_COUNT: usize = 6;
6600 pub fn version(mut self, v: Uint32) -> Self {
6601 self.version = v;
6602 self
6603 }
6604 pub fn cell_deps(mut self, v: CellDepVec) -> Self {
6605 self.cell_deps = v;
6606 self
6607 }
6608 pub fn header_deps(mut self, v: Byte32Vec) -> Self {
6609 self.header_deps = v;
6610 self
6611 }
6612 pub fn inputs(mut self, v: CellInputVec) -> Self {
6613 self.inputs = v;
6614 self
6615 }
6616 pub fn outputs(mut self, v: CellOutputVec) -> Self {
6617 self.outputs = v;
6618 self
6619 }
6620 pub fn outputs_data(mut self, v: BytesVec) -> Self {
6621 self.outputs_data = v;
6622 self
6623 }
6624}
6625impl molecule::prelude::Builder for RawTransactionBuilder {
6626 type Entity = RawTransaction;
6627 const NAME: &'static str = "RawTransactionBuilder";
6628 fn expected_length(&self) -> usize {
6629 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6630 + self.version.as_slice().len()
6631 + self.cell_deps.as_slice().len()
6632 + self.header_deps.as_slice().len()
6633 + self.inputs.as_slice().len()
6634 + self.outputs.as_slice().len()
6635 + self.outputs_data.as_slice().len()
6636 }
6637 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6638 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6639 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6640 offsets.push(total_size);
6641 total_size += self.version.as_slice().len();
6642 offsets.push(total_size);
6643 total_size += self.cell_deps.as_slice().len();
6644 offsets.push(total_size);
6645 total_size += self.header_deps.as_slice().len();
6646 offsets.push(total_size);
6647 total_size += self.inputs.as_slice().len();
6648 offsets.push(total_size);
6649 total_size += self.outputs.as_slice().len();
6650 offsets.push(total_size);
6651 total_size += self.outputs_data.as_slice().len();
6652 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6653 for offset in offsets.into_iter() {
6654 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6655 }
6656 writer.write_all(self.version.as_slice())?;
6657 writer.write_all(self.cell_deps.as_slice())?;
6658 writer.write_all(self.header_deps.as_slice())?;
6659 writer.write_all(self.inputs.as_slice())?;
6660 writer.write_all(self.outputs.as_slice())?;
6661 writer.write_all(self.outputs_data.as_slice())?;
6662 Ok(())
6663 }
6664 fn build(&self) -> Self::Entity {
6665 let mut inner = Vec::with_capacity(self.expected_length());
6666 self.write(&mut inner)
6667 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6668 RawTransaction::new_unchecked(inner.into())
6669 }
6670}
6671#[derive(Clone)]
6672pub struct Transaction(molecule::bytes::Bytes);
6673impl ::core::fmt::LowerHex for Transaction {
6674 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6675 use molecule::hex_string;
6676 if f.alternate() {
6677 write!(f, "0x")?;
6678 }
6679 write!(f, "{}", hex_string(self.as_slice()))
6680 }
6681}
6682impl ::core::fmt::Debug for Transaction {
6683 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6684 write!(f, "{}({:#x})", Self::NAME, self)
6685 }
6686}
6687impl ::core::fmt::Display for Transaction {
6688 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6689 write!(f, "{} {{ ", Self::NAME)?;
6690 write!(f, "{}: {}", "raw", self.raw())?;
6691 write!(f, ", {}: {}", "witnesses", self.witnesses())?;
6692 let extra_count = self.count_extra_fields();
6693 if extra_count != 0 {
6694 write!(f, ", .. ({} fields)", extra_count)?;
6695 }
6696 write!(f, " }}")
6697 }
6698}
6699impl ::core::default::Default for Transaction {
6700 fn default() -> Self {
6701 let v: Vec<u8> = vec![
6702 68, 0, 0, 0, 12, 0, 0, 0, 64, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0,
6703 0, 40, 0, 0, 0, 44, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6704 0, 4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
6705 ];
6706 Transaction::new_unchecked(v.into())
6707 }
6708}
6709impl Transaction {
6710 pub const FIELD_COUNT: usize = 2;
6711 pub fn total_size(&self) -> usize {
6712 molecule::unpack_number(self.as_slice()) as usize
6713 }
6714 pub fn field_count(&self) -> usize {
6715 if self.total_size() == molecule::NUMBER_SIZE {
6716 0
6717 } else {
6718 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6719 }
6720 }
6721 pub fn count_extra_fields(&self) -> usize {
6722 self.field_count() - Self::FIELD_COUNT
6723 }
6724 pub fn has_extra_fields(&self) -> bool {
6725 Self::FIELD_COUNT != self.field_count()
6726 }
6727 pub fn raw(&self) -> RawTransaction {
6728 let slice = self.as_slice();
6729 let start = molecule::unpack_number(&slice[4..]) as usize;
6730 let end = molecule::unpack_number(&slice[8..]) as usize;
6731 RawTransaction::new_unchecked(self.0.slice(start..end))
6732 }
6733 pub fn witnesses(&self) -> BytesVec {
6734 let slice = self.as_slice();
6735 let start = molecule::unpack_number(&slice[8..]) as usize;
6736 if self.has_extra_fields() {
6737 let end = molecule::unpack_number(&slice[12..]) as usize;
6738 BytesVec::new_unchecked(self.0.slice(start..end))
6739 } else {
6740 BytesVec::new_unchecked(self.0.slice(start..))
6741 }
6742 }
6743 pub fn as_reader<'r>(&'r self) -> TransactionReader<'r> {
6744 TransactionReader::new_unchecked(self.as_slice())
6745 }
6746}
6747impl molecule::prelude::Entity for Transaction {
6748 type Builder = TransactionBuilder;
6749 const NAME: &'static str = "Transaction";
6750 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6751 Transaction(data)
6752 }
6753 fn as_bytes(&self) -> molecule::bytes::Bytes {
6754 self.0.clone()
6755 }
6756 fn as_slice(&self) -> &[u8] {
6757 &self.0[..]
6758 }
6759 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6760 TransactionReader::from_slice(slice).map(|reader| reader.to_entity())
6761 }
6762 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6763 TransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6764 }
6765 fn new_builder() -> Self::Builder {
6766 ::core::default::Default::default()
6767 }
6768 fn as_builder(self) -> Self::Builder {
6769 Self::new_builder()
6770 .raw(self.raw())
6771 .witnesses(self.witnesses())
6772 }
6773}
6774#[derive(Clone, Copy)]
6775pub struct TransactionReader<'r>(&'r [u8]);
6776impl<'r> ::core::fmt::LowerHex for TransactionReader<'r> {
6777 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6778 use molecule::hex_string;
6779 if f.alternate() {
6780 write!(f, "0x")?;
6781 }
6782 write!(f, "{}", hex_string(self.as_slice()))
6783 }
6784}
6785impl<'r> ::core::fmt::Debug for TransactionReader<'r> {
6786 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6787 write!(f, "{}({:#x})", Self::NAME, self)
6788 }
6789}
6790impl<'r> ::core::fmt::Display for TransactionReader<'r> {
6791 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6792 write!(f, "{} {{ ", Self::NAME)?;
6793 write!(f, "{}: {}", "raw", self.raw())?;
6794 write!(f, ", {}: {}", "witnesses", self.witnesses())?;
6795 let extra_count = self.count_extra_fields();
6796 if extra_count != 0 {
6797 write!(f, ", .. ({} fields)", extra_count)?;
6798 }
6799 write!(f, " }}")
6800 }
6801}
6802impl<'r> TransactionReader<'r> {
6803 pub const FIELD_COUNT: usize = 2;
6804 pub fn total_size(&self) -> usize {
6805 molecule::unpack_number(self.as_slice()) as usize
6806 }
6807 pub fn field_count(&self) -> usize {
6808 if self.total_size() == molecule::NUMBER_SIZE {
6809 0
6810 } else {
6811 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6812 }
6813 }
6814 pub fn count_extra_fields(&self) -> usize {
6815 self.field_count() - Self::FIELD_COUNT
6816 }
6817 pub fn has_extra_fields(&self) -> bool {
6818 Self::FIELD_COUNT != self.field_count()
6819 }
6820 pub fn raw(&self) -> RawTransactionReader<'r> {
6821 let slice = self.as_slice();
6822 let start = molecule::unpack_number(&slice[4..]) as usize;
6823 let end = molecule::unpack_number(&slice[8..]) as usize;
6824 RawTransactionReader::new_unchecked(&self.as_slice()[start..end])
6825 }
6826 pub fn witnesses(&self) -> BytesVecReader<'r> {
6827 let slice = self.as_slice();
6828 let start = molecule::unpack_number(&slice[8..]) as usize;
6829 if self.has_extra_fields() {
6830 let end = molecule::unpack_number(&slice[12..]) as usize;
6831 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
6832 } else {
6833 BytesVecReader::new_unchecked(&self.as_slice()[start..])
6834 }
6835 }
6836}
6837impl<'r> molecule::prelude::Reader<'r> for TransactionReader<'r> {
6838 type Entity = Transaction;
6839 const NAME: &'static str = "TransactionReader";
6840 fn to_entity(&self) -> Self::Entity {
6841 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6842 }
6843 fn new_unchecked(slice: &'r [u8]) -> Self {
6844 TransactionReader(slice)
6845 }
6846 fn as_slice(&self) -> &'r [u8] {
6847 self.0
6848 }
6849 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6850 use molecule::verification_error as ve;
6851 let slice_len = slice.len();
6852 if slice_len < molecule::NUMBER_SIZE {
6853 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6854 }
6855 let total_size = molecule::unpack_number(slice) as usize;
6856 if slice_len != total_size {
6857 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6858 }
6859 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
6860 return Ok(());
6861 }
6862 if slice_len < molecule::NUMBER_SIZE * 2 {
6863 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6864 }
6865 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6866 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6867 return ve!(Self, OffsetsNotMatch);
6868 }
6869 if slice_len < offset_first {
6870 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6871 }
6872 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6873 if field_count < Self::FIELD_COUNT {
6874 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6875 } else if !compatible && field_count > Self::FIELD_COUNT {
6876 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6877 };
6878 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6879 .chunks_exact(molecule::NUMBER_SIZE)
6880 .map(|x| molecule::unpack_number(x) as usize)
6881 .collect();
6882 offsets.push(total_size);
6883 if offsets.windows(2).any(|i| i[0] > i[1]) {
6884 return ve!(Self, OffsetsNotMatch);
6885 }
6886 RawTransactionReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6887 BytesVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6888 Ok(())
6889 }
6890}
6891#[derive(Debug, Default)]
6892pub struct TransactionBuilder {
6893 pub(crate) raw: RawTransaction,
6894 pub(crate) witnesses: BytesVec,
6895}
6896impl TransactionBuilder {
6897 pub const FIELD_COUNT: usize = 2;
6898 pub fn raw(mut self, v: RawTransaction) -> Self {
6899 self.raw = v;
6900 self
6901 }
6902 pub fn witnesses(mut self, v: BytesVec) -> Self {
6903 self.witnesses = v;
6904 self
6905 }
6906}
6907impl molecule::prelude::Builder for TransactionBuilder {
6908 type Entity = Transaction;
6909 const NAME: &'static str = "TransactionBuilder";
6910 fn expected_length(&self) -> usize {
6911 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6912 + self.raw.as_slice().len()
6913 + self.witnesses.as_slice().len()
6914 }
6915 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6916 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6917 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6918 offsets.push(total_size);
6919 total_size += self.raw.as_slice().len();
6920 offsets.push(total_size);
6921 total_size += self.witnesses.as_slice().len();
6922 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6923 for offset in offsets.into_iter() {
6924 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6925 }
6926 writer.write_all(self.raw.as_slice())?;
6927 writer.write_all(self.witnesses.as_slice())?;
6928 Ok(())
6929 }
6930 fn build(&self) -> Self::Entity {
6931 let mut inner = Vec::with_capacity(self.expected_length());
6932 self.write(&mut inner)
6933 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6934 Transaction::new_unchecked(inner.into())
6935 }
6936}
6937#[derive(Clone)]
6938pub struct RawHeader(molecule::bytes::Bytes);
6939impl ::core::fmt::LowerHex for RawHeader {
6940 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6941 use molecule::hex_string;
6942 if f.alternate() {
6943 write!(f, "0x")?;
6944 }
6945 write!(f, "{}", hex_string(self.as_slice()))
6946 }
6947}
6948impl ::core::fmt::Debug for RawHeader {
6949 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6950 write!(f, "{}({:#x})", Self::NAME, self)
6951 }
6952}
6953impl ::core::fmt::Display for RawHeader {
6954 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6955 write!(f, "{} {{ ", Self::NAME)?;
6956 write!(f, "{}: {}", "version", self.version())?;
6957 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
6958 write!(f, ", {}: {}", "timestamp", self.timestamp())?;
6959 write!(f, ", {}: {}", "number", self.number())?;
6960 write!(f, ", {}: {}", "epoch", self.epoch())?;
6961 write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
6962 write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
6963 write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
6964 write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
6965 write!(f, ", {}: {}", "dao", self.dao())?;
6966 write!(f, " }}")
6967 }
6968}
6969impl ::core::default::Default for RawHeader {
6970 fn default() -> Self {
6971 let v: Vec<u8> = vec![
6972 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6973 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6974 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6975 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6976 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6977 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6978 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6979 ];
6980 RawHeader::new_unchecked(v.into())
6981 }
6982}
6983impl RawHeader {
6984 pub const TOTAL_SIZE: usize = 192;
6985 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
6986 pub const FIELD_COUNT: usize = 10;
6987 pub fn version(&self) -> Uint32 {
6988 Uint32::new_unchecked(self.0.slice(0..4))
6989 }
6990 pub fn compact_target(&self) -> Uint32 {
6991 Uint32::new_unchecked(self.0.slice(4..8))
6992 }
6993 pub fn timestamp(&self) -> Uint64 {
6994 Uint64::new_unchecked(self.0.slice(8..16))
6995 }
6996 pub fn number(&self) -> Uint64 {
6997 Uint64::new_unchecked(self.0.slice(16..24))
6998 }
6999 pub fn epoch(&self) -> Uint64 {
7000 Uint64::new_unchecked(self.0.slice(24..32))
7001 }
7002 pub fn parent_hash(&self) -> Byte32 {
7003 Byte32::new_unchecked(self.0.slice(32..64))
7004 }
7005 pub fn transactions_root(&self) -> Byte32 {
7006 Byte32::new_unchecked(self.0.slice(64..96))
7007 }
7008 pub fn proposals_hash(&self) -> Byte32 {
7009 Byte32::new_unchecked(self.0.slice(96..128))
7010 }
7011 pub fn extra_hash(&self) -> Byte32 {
7012 Byte32::new_unchecked(self.0.slice(128..160))
7013 }
7014 pub fn dao(&self) -> Byte32 {
7015 Byte32::new_unchecked(self.0.slice(160..192))
7016 }
7017 pub fn as_reader<'r>(&'r self) -> RawHeaderReader<'r> {
7018 RawHeaderReader::new_unchecked(self.as_slice())
7019 }
7020}
7021impl molecule::prelude::Entity for RawHeader {
7022 type Builder = RawHeaderBuilder;
7023 const NAME: &'static str = "RawHeader";
7024 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7025 RawHeader(data)
7026 }
7027 fn as_bytes(&self) -> molecule::bytes::Bytes {
7028 self.0.clone()
7029 }
7030 fn as_slice(&self) -> &[u8] {
7031 &self.0[..]
7032 }
7033 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7034 RawHeaderReader::from_slice(slice).map(|reader| reader.to_entity())
7035 }
7036 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7037 RawHeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7038 }
7039 fn new_builder() -> Self::Builder {
7040 ::core::default::Default::default()
7041 }
7042 fn as_builder(self) -> Self::Builder {
7043 Self::new_builder()
7044 .version(self.version())
7045 .compact_target(self.compact_target())
7046 .timestamp(self.timestamp())
7047 .number(self.number())
7048 .epoch(self.epoch())
7049 .parent_hash(self.parent_hash())
7050 .transactions_root(self.transactions_root())
7051 .proposals_hash(self.proposals_hash())
7052 .extra_hash(self.extra_hash())
7053 .dao(self.dao())
7054 }
7055}
7056#[derive(Clone, Copy)]
7057pub struct RawHeaderReader<'r>(&'r [u8]);
7058impl<'r> ::core::fmt::LowerHex for RawHeaderReader<'r> {
7059 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7060 use molecule::hex_string;
7061 if f.alternate() {
7062 write!(f, "0x")?;
7063 }
7064 write!(f, "{}", hex_string(self.as_slice()))
7065 }
7066}
7067impl<'r> ::core::fmt::Debug for RawHeaderReader<'r> {
7068 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7069 write!(f, "{}({:#x})", Self::NAME, self)
7070 }
7071}
7072impl<'r> ::core::fmt::Display for RawHeaderReader<'r> {
7073 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7074 write!(f, "{} {{ ", Self::NAME)?;
7075 write!(f, "{}: {}", "version", self.version())?;
7076 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
7077 write!(f, ", {}: {}", "timestamp", self.timestamp())?;
7078 write!(f, ", {}: {}", "number", self.number())?;
7079 write!(f, ", {}: {}", "epoch", self.epoch())?;
7080 write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
7081 write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
7082 write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
7083 write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
7084 write!(f, ", {}: {}", "dao", self.dao())?;
7085 write!(f, " }}")
7086 }
7087}
7088impl<'r> RawHeaderReader<'r> {
7089 pub const TOTAL_SIZE: usize = 192;
7090 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
7091 pub const FIELD_COUNT: usize = 10;
7092 pub fn version(&self) -> Uint32Reader<'r> {
7093 Uint32Reader::new_unchecked(&self.as_slice()[0..4])
7094 }
7095 pub fn compact_target(&self) -> Uint32Reader<'r> {
7096 Uint32Reader::new_unchecked(&self.as_slice()[4..8])
7097 }
7098 pub fn timestamp(&self) -> Uint64Reader<'r> {
7099 Uint64Reader::new_unchecked(&self.as_slice()[8..16])
7100 }
7101 pub fn number(&self) -> Uint64Reader<'r> {
7102 Uint64Reader::new_unchecked(&self.as_slice()[16..24])
7103 }
7104 pub fn epoch(&self) -> Uint64Reader<'r> {
7105 Uint64Reader::new_unchecked(&self.as_slice()[24..32])
7106 }
7107 pub fn parent_hash(&self) -> Byte32Reader<'r> {
7108 Byte32Reader::new_unchecked(&self.as_slice()[32..64])
7109 }
7110 pub fn transactions_root(&self) -> Byte32Reader<'r> {
7111 Byte32Reader::new_unchecked(&self.as_slice()[64..96])
7112 }
7113 pub fn proposals_hash(&self) -> Byte32Reader<'r> {
7114 Byte32Reader::new_unchecked(&self.as_slice()[96..128])
7115 }
7116 pub fn extra_hash(&self) -> Byte32Reader<'r> {
7117 Byte32Reader::new_unchecked(&self.as_slice()[128..160])
7118 }
7119 pub fn dao(&self) -> Byte32Reader<'r> {
7120 Byte32Reader::new_unchecked(&self.as_slice()[160..192])
7121 }
7122}
7123impl<'r> molecule::prelude::Reader<'r> for RawHeaderReader<'r> {
7124 type Entity = RawHeader;
7125 const NAME: &'static str = "RawHeaderReader";
7126 fn to_entity(&self) -> Self::Entity {
7127 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7128 }
7129 fn new_unchecked(slice: &'r [u8]) -> Self {
7130 RawHeaderReader(slice)
7131 }
7132 fn as_slice(&self) -> &'r [u8] {
7133 self.0
7134 }
7135 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
7136 use molecule::verification_error as ve;
7137 let slice_len = slice.len();
7138 if slice_len != Self::TOTAL_SIZE {
7139 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
7140 }
7141 Ok(())
7142 }
7143}
7144#[derive(Debug, Default)]
7145pub struct RawHeaderBuilder {
7146 pub(crate) version: Uint32,
7147 pub(crate) compact_target: Uint32,
7148 pub(crate) timestamp: Uint64,
7149 pub(crate) number: Uint64,
7150 pub(crate) epoch: Uint64,
7151 pub(crate) parent_hash: Byte32,
7152 pub(crate) transactions_root: Byte32,
7153 pub(crate) proposals_hash: Byte32,
7154 pub(crate) extra_hash: Byte32,
7155 pub(crate) dao: Byte32,
7156}
7157impl RawHeaderBuilder {
7158 pub const TOTAL_SIZE: usize = 192;
7159 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
7160 pub const FIELD_COUNT: usize = 10;
7161 pub fn version(mut self, v: Uint32) -> Self {
7162 self.version = v;
7163 self
7164 }
7165 pub fn compact_target(mut self, v: Uint32) -> Self {
7166 self.compact_target = v;
7167 self
7168 }
7169 pub fn timestamp(mut self, v: Uint64) -> Self {
7170 self.timestamp = v;
7171 self
7172 }
7173 pub fn number(mut self, v: Uint64) -> Self {
7174 self.number = v;
7175 self
7176 }
7177 pub fn epoch(mut self, v: Uint64) -> Self {
7178 self.epoch = v;
7179 self
7180 }
7181 pub fn parent_hash(mut self, v: Byte32) -> Self {
7182 self.parent_hash = v;
7183 self
7184 }
7185 pub fn transactions_root(mut self, v: Byte32) -> Self {
7186 self.transactions_root = v;
7187 self
7188 }
7189 pub fn proposals_hash(mut self, v: Byte32) -> Self {
7190 self.proposals_hash = v;
7191 self
7192 }
7193 pub fn extra_hash(mut self, v: Byte32) -> Self {
7194 self.extra_hash = v;
7195 self
7196 }
7197 pub fn dao(mut self, v: Byte32) -> Self {
7198 self.dao = v;
7199 self
7200 }
7201}
7202impl molecule::prelude::Builder for RawHeaderBuilder {
7203 type Entity = RawHeader;
7204 const NAME: &'static str = "RawHeaderBuilder";
7205 fn expected_length(&self) -> usize {
7206 Self::TOTAL_SIZE
7207 }
7208 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7209 writer.write_all(self.version.as_slice())?;
7210 writer.write_all(self.compact_target.as_slice())?;
7211 writer.write_all(self.timestamp.as_slice())?;
7212 writer.write_all(self.number.as_slice())?;
7213 writer.write_all(self.epoch.as_slice())?;
7214 writer.write_all(self.parent_hash.as_slice())?;
7215 writer.write_all(self.transactions_root.as_slice())?;
7216 writer.write_all(self.proposals_hash.as_slice())?;
7217 writer.write_all(self.extra_hash.as_slice())?;
7218 writer.write_all(self.dao.as_slice())?;
7219 Ok(())
7220 }
7221 fn build(&self) -> Self::Entity {
7222 let mut inner = Vec::with_capacity(self.expected_length());
7223 self.write(&mut inner)
7224 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7225 RawHeader::new_unchecked(inner.into())
7226 }
7227}
7228#[derive(Clone)]
7229pub struct Header(molecule::bytes::Bytes);
7230impl ::core::fmt::LowerHex for Header {
7231 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7232 use molecule::hex_string;
7233 if f.alternate() {
7234 write!(f, "0x")?;
7235 }
7236 write!(f, "{}", hex_string(self.as_slice()))
7237 }
7238}
7239impl ::core::fmt::Debug for Header {
7240 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7241 write!(f, "{}({:#x})", Self::NAME, self)
7242 }
7243}
7244impl ::core::fmt::Display for Header {
7245 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7246 write!(f, "{} {{ ", Self::NAME)?;
7247 write!(f, "{}: {}", "raw", self.raw())?;
7248 write!(f, ", {}: {}", "nonce", self.nonce())?;
7249 write!(f, " }}")
7250 }
7251}
7252impl ::core::default::Default for Header {
7253 fn default() -> Self {
7254 let v: Vec<u8> = vec![
7255 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7256 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7257 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7258 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7259 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7260 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7261 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7262 0, 0, 0, 0, 0,
7263 ];
7264 Header::new_unchecked(v.into())
7265 }
7266}
7267impl Header {
7268 pub const TOTAL_SIZE: usize = 208;
7269 pub const FIELD_SIZES: [usize; 2] = [192, 16];
7270 pub const FIELD_COUNT: usize = 2;
7271 pub fn raw(&self) -> RawHeader {
7272 RawHeader::new_unchecked(self.0.slice(0..192))
7273 }
7274 pub fn nonce(&self) -> Uint128 {
7275 Uint128::new_unchecked(self.0.slice(192..208))
7276 }
7277 pub fn as_reader<'r>(&'r self) -> HeaderReader<'r> {
7278 HeaderReader::new_unchecked(self.as_slice())
7279 }
7280}
7281impl molecule::prelude::Entity for Header {
7282 type Builder = HeaderBuilder;
7283 const NAME: &'static str = "Header";
7284 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7285 Header(data)
7286 }
7287 fn as_bytes(&self) -> molecule::bytes::Bytes {
7288 self.0.clone()
7289 }
7290 fn as_slice(&self) -> &[u8] {
7291 &self.0[..]
7292 }
7293 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7294 HeaderReader::from_slice(slice).map(|reader| reader.to_entity())
7295 }
7296 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7297 HeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7298 }
7299 fn new_builder() -> Self::Builder {
7300 ::core::default::Default::default()
7301 }
7302 fn as_builder(self) -> Self::Builder {
7303 Self::new_builder().raw(self.raw()).nonce(self.nonce())
7304 }
7305}
7306#[derive(Clone, Copy)]
7307pub struct HeaderReader<'r>(&'r [u8]);
7308impl<'r> ::core::fmt::LowerHex for HeaderReader<'r> {
7309 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7310 use molecule::hex_string;
7311 if f.alternate() {
7312 write!(f, "0x")?;
7313 }
7314 write!(f, "{}", hex_string(self.as_slice()))
7315 }
7316}
7317impl<'r> ::core::fmt::Debug for HeaderReader<'r> {
7318 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7319 write!(f, "{}({:#x})", Self::NAME, self)
7320 }
7321}
7322impl<'r> ::core::fmt::Display for HeaderReader<'r> {
7323 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7324 write!(f, "{} {{ ", Self::NAME)?;
7325 write!(f, "{}: {}", "raw", self.raw())?;
7326 write!(f, ", {}: {}", "nonce", self.nonce())?;
7327 write!(f, " }}")
7328 }
7329}
7330impl<'r> HeaderReader<'r> {
7331 pub const TOTAL_SIZE: usize = 208;
7332 pub const FIELD_SIZES: [usize; 2] = [192, 16];
7333 pub const FIELD_COUNT: usize = 2;
7334 pub fn raw(&self) -> RawHeaderReader<'r> {
7335 RawHeaderReader::new_unchecked(&self.as_slice()[0..192])
7336 }
7337 pub fn nonce(&self) -> Uint128Reader<'r> {
7338 Uint128Reader::new_unchecked(&self.as_slice()[192..208])
7339 }
7340}
7341impl<'r> molecule::prelude::Reader<'r> for HeaderReader<'r> {
7342 type Entity = Header;
7343 const NAME: &'static str = "HeaderReader";
7344 fn to_entity(&self) -> Self::Entity {
7345 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7346 }
7347 fn new_unchecked(slice: &'r [u8]) -> Self {
7348 HeaderReader(slice)
7349 }
7350 fn as_slice(&self) -> &'r [u8] {
7351 self.0
7352 }
7353 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
7354 use molecule::verification_error as ve;
7355 let slice_len = slice.len();
7356 if slice_len != Self::TOTAL_SIZE {
7357 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
7358 }
7359 Ok(())
7360 }
7361}
7362#[derive(Debug, Default)]
7363pub struct HeaderBuilder {
7364 pub(crate) raw: RawHeader,
7365 pub(crate) nonce: Uint128,
7366}
7367impl HeaderBuilder {
7368 pub const TOTAL_SIZE: usize = 208;
7369 pub const FIELD_SIZES: [usize; 2] = [192, 16];
7370 pub const FIELD_COUNT: usize = 2;
7371 pub fn raw(mut self, v: RawHeader) -> Self {
7372 self.raw = v;
7373 self
7374 }
7375 pub fn nonce(mut self, v: Uint128) -> Self {
7376 self.nonce = v;
7377 self
7378 }
7379}
7380impl molecule::prelude::Builder for HeaderBuilder {
7381 type Entity = Header;
7382 const NAME: &'static str = "HeaderBuilder";
7383 fn expected_length(&self) -> usize {
7384 Self::TOTAL_SIZE
7385 }
7386 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7387 writer.write_all(self.raw.as_slice())?;
7388 writer.write_all(self.nonce.as_slice())?;
7389 Ok(())
7390 }
7391 fn build(&self) -> Self::Entity {
7392 let mut inner = Vec::with_capacity(self.expected_length());
7393 self.write(&mut inner)
7394 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7395 Header::new_unchecked(inner.into())
7396 }
7397}
7398#[derive(Clone)]
7399pub struct UncleBlock(molecule::bytes::Bytes);
7400impl ::core::fmt::LowerHex for UncleBlock {
7401 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7402 use molecule::hex_string;
7403 if f.alternate() {
7404 write!(f, "0x")?;
7405 }
7406 write!(f, "{}", hex_string(self.as_slice()))
7407 }
7408}
7409impl ::core::fmt::Debug for UncleBlock {
7410 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7411 write!(f, "{}({:#x})", Self::NAME, self)
7412 }
7413}
7414impl ::core::fmt::Display for UncleBlock {
7415 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7416 write!(f, "{} {{ ", Self::NAME)?;
7417 write!(f, "{}: {}", "header", self.header())?;
7418 write!(f, ", {}: {}", "proposals", self.proposals())?;
7419 let extra_count = self.count_extra_fields();
7420 if extra_count != 0 {
7421 write!(f, ", .. ({} fields)", extra_count)?;
7422 }
7423 write!(f, " }}")
7424 }
7425}
7426impl ::core::default::Default for UncleBlock {
7427 fn default() -> Self {
7428 let v: Vec<u8> = vec![
7429 224, 0, 0, 0, 12, 0, 0, 0, 220, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7430 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7431 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7432 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7433 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7434 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7435 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7436 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7437 ];
7438 UncleBlock::new_unchecked(v.into())
7439 }
7440}
7441impl UncleBlock {
7442 pub const FIELD_COUNT: usize = 2;
7443 pub fn total_size(&self) -> usize {
7444 molecule::unpack_number(self.as_slice()) as usize
7445 }
7446 pub fn field_count(&self) -> usize {
7447 if self.total_size() == molecule::NUMBER_SIZE {
7448 0
7449 } else {
7450 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7451 }
7452 }
7453 pub fn count_extra_fields(&self) -> usize {
7454 self.field_count() - Self::FIELD_COUNT
7455 }
7456 pub fn has_extra_fields(&self) -> bool {
7457 Self::FIELD_COUNT != self.field_count()
7458 }
7459 pub fn header(&self) -> Header {
7460 let slice = self.as_slice();
7461 let start = molecule::unpack_number(&slice[4..]) as usize;
7462 let end = molecule::unpack_number(&slice[8..]) as usize;
7463 Header::new_unchecked(self.0.slice(start..end))
7464 }
7465 pub fn proposals(&self) -> ProposalShortIdVec {
7466 let slice = self.as_slice();
7467 let start = molecule::unpack_number(&slice[8..]) as usize;
7468 if self.has_extra_fields() {
7469 let end = molecule::unpack_number(&slice[12..]) as usize;
7470 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7471 } else {
7472 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
7473 }
7474 }
7475 pub fn as_reader<'r>(&'r self) -> UncleBlockReader<'r> {
7476 UncleBlockReader::new_unchecked(self.as_slice())
7477 }
7478}
7479impl molecule::prelude::Entity for UncleBlock {
7480 type Builder = UncleBlockBuilder;
7481 const NAME: &'static str = "UncleBlock";
7482 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7483 UncleBlock(data)
7484 }
7485 fn as_bytes(&self) -> molecule::bytes::Bytes {
7486 self.0.clone()
7487 }
7488 fn as_slice(&self) -> &[u8] {
7489 &self.0[..]
7490 }
7491 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7492 UncleBlockReader::from_slice(slice).map(|reader| reader.to_entity())
7493 }
7494 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7495 UncleBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7496 }
7497 fn new_builder() -> Self::Builder {
7498 ::core::default::Default::default()
7499 }
7500 fn as_builder(self) -> Self::Builder {
7501 Self::new_builder()
7502 .header(self.header())
7503 .proposals(self.proposals())
7504 }
7505}
7506#[derive(Clone, Copy)]
7507pub struct UncleBlockReader<'r>(&'r [u8]);
7508impl<'r> ::core::fmt::LowerHex for UncleBlockReader<'r> {
7509 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7510 use molecule::hex_string;
7511 if f.alternate() {
7512 write!(f, "0x")?;
7513 }
7514 write!(f, "{}", hex_string(self.as_slice()))
7515 }
7516}
7517impl<'r> ::core::fmt::Debug for UncleBlockReader<'r> {
7518 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7519 write!(f, "{}({:#x})", Self::NAME, self)
7520 }
7521}
7522impl<'r> ::core::fmt::Display for UncleBlockReader<'r> {
7523 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7524 write!(f, "{} {{ ", Self::NAME)?;
7525 write!(f, "{}: {}", "header", self.header())?;
7526 write!(f, ", {}: {}", "proposals", self.proposals())?;
7527 let extra_count = self.count_extra_fields();
7528 if extra_count != 0 {
7529 write!(f, ", .. ({} fields)", extra_count)?;
7530 }
7531 write!(f, " }}")
7532 }
7533}
7534impl<'r> UncleBlockReader<'r> {
7535 pub const FIELD_COUNT: usize = 2;
7536 pub fn total_size(&self) -> usize {
7537 molecule::unpack_number(self.as_slice()) as usize
7538 }
7539 pub fn field_count(&self) -> usize {
7540 if self.total_size() == molecule::NUMBER_SIZE {
7541 0
7542 } else {
7543 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7544 }
7545 }
7546 pub fn count_extra_fields(&self) -> usize {
7547 self.field_count() - Self::FIELD_COUNT
7548 }
7549 pub fn has_extra_fields(&self) -> bool {
7550 Self::FIELD_COUNT != self.field_count()
7551 }
7552 pub fn header(&self) -> HeaderReader<'r> {
7553 let slice = self.as_slice();
7554 let start = molecule::unpack_number(&slice[4..]) as usize;
7555 let end = molecule::unpack_number(&slice[8..]) as usize;
7556 HeaderReader::new_unchecked(&self.as_slice()[start..end])
7557 }
7558 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
7559 let slice = self.as_slice();
7560 let start = molecule::unpack_number(&slice[8..]) as usize;
7561 if self.has_extra_fields() {
7562 let end = molecule::unpack_number(&slice[12..]) as usize;
7563 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7564 } else {
7565 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
7566 }
7567 }
7568}
7569impl<'r> molecule::prelude::Reader<'r> for UncleBlockReader<'r> {
7570 type Entity = UncleBlock;
7571 const NAME: &'static str = "UncleBlockReader";
7572 fn to_entity(&self) -> Self::Entity {
7573 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7574 }
7575 fn new_unchecked(slice: &'r [u8]) -> Self {
7576 UncleBlockReader(slice)
7577 }
7578 fn as_slice(&self) -> &'r [u8] {
7579 self.0
7580 }
7581 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7582 use molecule::verification_error as ve;
7583 let slice_len = slice.len();
7584 if slice_len < molecule::NUMBER_SIZE {
7585 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7586 }
7587 let total_size = molecule::unpack_number(slice) as usize;
7588 if slice_len != total_size {
7589 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7590 }
7591 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
7592 return Ok(());
7593 }
7594 if slice_len < molecule::NUMBER_SIZE * 2 {
7595 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7596 }
7597 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7598 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7599 return ve!(Self, OffsetsNotMatch);
7600 }
7601 if slice_len < offset_first {
7602 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7603 }
7604 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7605 if field_count < Self::FIELD_COUNT {
7606 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7607 } else if !compatible && field_count > Self::FIELD_COUNT {
7608 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7609 };
7610 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7611 .chunks_exact(molecule::NUMBER_SIZE)
7612 .map(|x| molecule::unpack_number(x) as usize)
7613 .collect();
7614 offsets.push(total_size);
7615 if offsets.windows(2).any(|i| i[0] > i[1]) {
7616 return ve!(Self, OffsetsNotMatch);
7617 }
7618 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7619 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7620 Ok(())
7621 }
7622}
7623#[derive(Debug, Default)]
7624pub struct UncleBlockBuilder {
7625 pub(crate) header: Header,
7626 pub(crate) proposals: ProposalShortIdVec,
7627}
7628impl UncleBlockBuilder {
7629 pub const FIELD_COUNT: usize = 2;
7630 pub fn header(mut self, v: Header) -> Self {
7631 self.header = v;
7632 self
7633 }
7634 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
7635 self.proposals = v;
7636 self
7637 }
7638}
7639impl molecule::prelude::Builder for UncleBlockBuilder {
7640 type Entity = UncleBlock;
7641 const NAME: &'static str = "UncleBlockBuilder";
7642 fn expected_length(&self) -> usize {
7643 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7644 + self.header.as_slice().len()
7645 + self.proposals.as_slice().len()
7646 }
7647 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7648 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7649 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7650 offsets.push(total_size);
7651 total_size += self.header.as_slice().len();
7652 offsets.push(total_size);
7653 total_size += self.proposals.as_slice().len();
7654 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7655 for offset in offsets.into_iter() {
7656 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7657 }
7658 writer.write_all(self.header.as_slice())?;
7659 writer.write_all(self.proposals.as_slice())?;
7660 Ok(())
7661 }
7662 fn build(&self) -> Self::Entity {
7663 let mut inner = Vec::with_capacity(self.expected_length());
7664 self.write(&mut inner)
7665 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7666 UncleBlock::new_unchecked(inner.into())
7667 }
7668}
7669#[derive(Clone)]
7670pub struct Block(molecule::bytes::Bytes);
7671impl ::core::fmt::LowerHex for Block {
7672 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7673 use molecule::hex_string;
7674 if f.alternate() {
7675 write!(f, "0x")?;
7676 }
7677 write!(f, "{}", hex_string(self.as_slice()))
7678 }
7679}
7680impl ::core::fmt::Debug for Block {
7681 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7682 write!(f, "{}({:#x})", Self::NAME, self)
7683 }
7684}
7685impl ::core::fmt::Display for Block {
7686 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7687 write!(f, "{} {{ ", Self::NAME)?;
7688 write!(f, "{}: {}", "header", self.header())?;
7689 write!(f, ", {}: {}", "uncles", self.uncles())?;
7690 write!(f, ", {}: {}", "transactions", self.transactions())?;
7691 write!(f, ", {}: {}", "proposals", self.proposals())?;
7692 let extra_count = self.count_extra_fields();
7693 if extra_count != 0 {
7694 write!(f, ", .. ({} fields)", extra_count)?;
7695 }
7696 write!(f, " }}")
7697 }
7698}
7699impl ::core::default::Default for Block {
7700 fn default() -> Self {
7701 let v: Vec<u8> = vec![
7702 240, 0, 0, 0, 20, 0, 0, 0, 228, 0, 0, 0, 232, 0, 0, 0, 236, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7703 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7705 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7706 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7707 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7708 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
7710 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
7711 ];
7712 Block::new_unchecked(v.into())
7713 }
7714}
7715impl Block {
7716 pub const FIELD_COUNT: usize = 4;
7717 pub fn total_size(&self) -> usize {
7718 molecule::unpack_number(self.as_slice()) as usize
7719 }
7720 pub fn field_count(&self) -> usize {
7721 if self.total_size() == molecule::NUMBER_SIZE {
7722 0
7723 } else {
7724 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7725 }
7726 }
7727 pub fn count_extra_fields(&self) -> usize {
7728 self.field_count() - Self::FIELD_COUNT
7729 }
7730 pub fn has_extra_fields(&self) -> bool {
7731 Self::FIELD_COUNT != self.field_count()
7732 }
7733 pub fn header(&self) -> Header {
7734 let slice = self.as_slice();
7735 let start = molecule::unpack_number(&slice[4..]) as usize;
7736 let end = molecule::unpack_number(&slice[8..]) as usize;
7737 Header::new_unchecked(self.0.slice(start..end))
7738 }
7739 pub fn uncles(&self) -> UncleBlockVec {
7740 let slice = self.as_slice();
7741 let start = molecule::unpack_number(&slice[8..]) as usize;
7742 let end = molecule::unpack_number(&slice[12..]) as usize;
7743 UncleBlockVec::new_unchecked(self.0.slice(start..end))
7744 }
7745 pub fn transactions(&self) -> TransactionVec {
7746 let slice = self.as_slice();
7747 let start = molecule::unpack_number(&slice[12..]) as usize;
7748 let end = molecule::unpack_number(&slice[16..]) as usize;
7749 TransactionVec::new_unchecked(self.0.slice(start..end))
7750 }
7751 pub fn proposals(&self) -> ProposalShortIdVec {
7752 let slice = self.as_slice();
7753 let start = molecule::unpack_number(&slice[16..]) as usize;
7754 if self.has_extra_fields() {
7755 let end = molecule::unpack_number(&slice[20..]) as usize;
7756 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
7757 } else {
7758 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
7759 }
7760 }
7761 pub fn as_reader<'r>(&'r self) -> BlockReader<'r> {
7762 BlockReader::new_unchecked(self.as_slice())
7763 }
7764}
7765impl molecule::prelude::Entity for Block {
7766 type Builder = BlockBuilder;
7767 const NAME: &'static str = "Block";
7768 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7769 Block(data)
7770 }
7771 fn as_bytes(&self) -> molecule::bytes::Bytes {
7772 self.0.clone()
7773 }
7774 fn as_slice(&self) -> &[u8] {
7775 &self.0[..]
7776 }
7777 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7778 BlockReader::from_slice(slice).map(|reader| reader.to_entity())
7779 }
7780 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7781 BlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7782 }
7783 fn new_builder() -> Self::Builder {
7784 ::core::default::Default::default()
7785 }
7786 fn as_builder(self) -> Self::Builder {
7787 Self::new_builder()
7788 .header(self.header())
7789 .uncles(self.uncles())
7790 .transactions(self.transactions())
7791 .proposals(self.proposals())
7792 }
7793}
7794#[derive(Clone, Copy)]
7795pub struct BlockReader<'r>(&'r [u8]);
7796impl<'r> ::core::fmt::LowerHex for BlockReader<'r> {
7797 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7798 use molecule::hex_string;
7799 if f.alternate() {
7800 write!(f, "0x")?;
7801 }
7802 write!(f, "{}", hex_string(self.as_slice()))
7803 }
7804}
7805impl<'r> ::core::fmt::Debug for BlockReader<'r> {
7806 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7807 write!(f, "{}({:#x})", Self::NAME, self)
7808 }
7809}
7810impl<'r> ::core::fmt::Display for BlockReader<'r> {
7811 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7812 write!(f, "{} {{ ", Self::NAME)?;
7813 write!(f, "{}: {}", "header", self.header())?;
7814 write!(f, ", {}: {}", "uncles", self.uncles())?;
7815 write!(f, ", {}: {}", "transactions", self.transactions())?;
7816 write!(f, ", {}: {}", "proposals", self.proposals())?;
7817 let extra_count = self.count_extra_fields();
7818 if extra_count != 0 {
7819 write!(f, ", .. ({} fields)", extra_count)?;
7820 }
7821 write!(f, " }}")
7822 }
7823}
7824impl<'r> BlockReader<'r> {
7825 pub const FIELD_COUNT: usize = 4;
7826 pub fn total_size(&self) -> usize {
7827 molecule::unpack_number(self.as_slice()) as usize
7828 }
7829 pub fn field_count(&self) -> usize {
7830 if self.total_size() == molecule::NUMBER_SIZE {
7831 0
7832 } else {
7833 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7834 }
7835 }
7836 pub fn count_extra_fields(&self) -> usize {
7837 self.field_count() - Self::FIELD_COUNT
7838 }
7839 pub fn has_extra_fields(&self) -> bool {
7840 Self::FIELD_COUNT != self.field_count()
7841 }
7842 pub fn header(&self) -> HeaderReader<'r> {
7843 let slice = self.as_slice();
7844 let start = molecule::unpack_number(&slice[4..]) as usize;
7845 let end = molecule::unpack_number(&slice[8..]) as usize;
7846 HeaderReader::new_unchecked(&self.as_slice()[start..end])
7847 }
7848 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
7849 let slice = self.as_slice();
7850 let start = molecule::unpack_number(&slice[8..]) as usize;
7851 let end = molecule::unpack_number(&slice[12..]) as usize;
7852 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
7853 }
7854 pub fn transactions(&self) -> TransactionVecReader<'r> {
7855 let slice = self.as_slice();
7856 let start = molecule::unpack_number(&slice[12..]) as usize;
7857 let end = molecule::unpack_number(&slice[16..]) as usize;
7858 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
7859 }
7860 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
7861 let slice = self.as_slice();
7862 let start = molecule::unpack_number(&slice[16..]) as usize;
7863 if self.has_extra_fields() {
7864 let end = molecule::unpack_number(&slice[20..]) as usize;
7865 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
7866 } else {
7867 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
7868 }
7869 }
7870}
7871impl<'r> molecule::prelude::Reader<'r> for BlockReader<'r> {
7872 type Entity = Block;
7873 const NAME: &'static str = "BlockReader";
7874 fn to_entity(&self) -> Self::Entity {
7875 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7876 }
7877 fn new_unchecked(slice: &'r [u8]) -> Self {
7878 BlockReader(slice)
7879 }
7880 fn as_slice(&self) -> &'r [u8] {
7881 self.0
7882 }
7883 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7884 use molecule::verification_error as ve;
7885 let slice_len = slice.len();
7886 if slice_len < molecule::NUMBER_SIZE {
7887 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7888 }
7889 let total_size = molecule::unpack_number(slice) as usize;
7890 if slice_len != total_size {
7891 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7892 }
7893 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
7894 return Ok(());
7895 }
7896 if slice_len < molecule::NUMBER_SIZE * 2 {
7897 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7898 }
7899 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7900 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7901 return ve!(Self, OffsetsNotMatch);
7902 }
7903 if slice_len < offset_first {
7904 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7905 }
7906 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7907 if field_count < Self::FIELD_COUNT {
7908 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7909 } else if !compatible && field_count > Self::FIELD_COUNT {
7910 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7911 };
7912 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7913 .chunks_exact(molecule::NUMBER_SIZE)
7914 .map(|x| molecule::unpack_number(x) as usize)
7915 .collect();
7916 offsets.push(total_size);
7917 if offsets.windows(2).any(|i| i[0] > i[1]) {
7918 return ve!(Self, OffsetsNotMatch);
7919 }
7920 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7921 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7922 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7923 ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
7924 Ok(())
7925 }
7926}
7927#[derive(Debug, Default)]
7928pub struct BlockBuilder {
7929 pub(crate) header: Header,
7930 pub(crate) uncles: UncleBlockVec,
7931 pub(crate) transactions: TransactionVec,
7932 pub(crate) proposals: ProposalShortIdVec,
7933}
7934impl BlockBuilder {
7935 pub const FIELD_COUNT: usize = 4;
7936 pub fn header(mut self, v: Header) -> Self {
7937 self.header = v;
7938 self
7939 }
7940 pub fn uncles(mut self, v: UncleBlockVec) -> Self {
7941 self.uncles = v;
7942 self
7943 }
7944 pub fn transactions(mut self, v: TransactionVec) -> Self {
7945 self.transactions = v;
7946 self
7947 }
7948 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
7949 self.proposals = v;
7950 self
7951 }
7952}
7953impl molecule::prelude::Builder for BlockBuilder {
7954 type Entity = Block;
7955 const NAME: &'static str = "BlockBuilder";
7956 fn expected_length(&self) -> usize {
7957 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7958 + self.header.as_slice().len()
7959 + self.uncles.as_slice().len()
7960 + self.transactions.as_slice().len()
7961 + self.proposals.as_slice().len()
7962 }
7963 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7964 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7965 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7966 offsets.push(total_size);
7967 total_size += self.header.as_slice().len();
7968 offsets.push(total_size);
7969 total_size += self.uncles.as_slice().len();
7970 offsets.push(total_size);
7971 total_size += self.transactions.as_slice().len();
7972 offsets.push(total_size);
7973 total_size += self.proposals.as_slice().len();
7974 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7975 for offset in offsets.into_iter() {
7976 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7977 }
7978 writer.write_all(self.header.as_slice())?;
7979 writer.write_all(self.uncles.as_slice())?;
7980 writer.write_all(self.transactions.as_slice())?;
7981 writer.write_all(self.proposals.as_slice())?;
7982 Ok(())
7983 }
7984 fn build(&self) -> Self::Entity {
7985 let mut inner = Vec::with_capacity(self.expected_length());
7986 self.write(&mut inner)
7987 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7988 Block::new_unchecked(inner.into())
7989 }
7990}
7991#[derive(Clone)]
7992pub struct BlockV1(molecule::bytes::Bytes);
7993impl ::core::fmt::LowerHex for BlockV1 {
7994 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7995 use molecule::hex_string;
7996 if f.alternate() {
7997 write!(f, "0x")?;
7998 }
7999 write!(f, "{}", hex_string(self.as_slice()))
8000 }
8001}
8002impl ::core::fmt::Debug for BlockV1 {
8003 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8004 write!(f, "{}({:#x})", Self::NAME, self)
8005 }
8006}
8007impl ::core::fmt::Display for BlockV1 {
8008 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8009 write!(f, "{} {{ ", Self::NAME)?;
8010 write!(f, "{}: {}", "header", self.header())?;
8011 write!(f, ", {}: {}", "uncles", self.uncles())?;
8012 write!(f, ", {}: {}", "transactions", self.transactions())?;
8013 write!(f, ", {}: {}", "proposals", self.proposals())?;
8014 write!(f, ", {}: {}", "extension", self.extension())?;
8015 let extra_count = self.count_extra_fields();
8016 if extra_count != 0 {
8017 write!(f, ", .. ({} fields)", extra_count)?;
8018 }
8019 write!(f, " }}")
8020 }
8021}
8022impl ::core::default::Default for BlockV1 {
8023 fn default() -> Self {
8024 let v: Vec<u8> = vec![
8025 248, 0, 0, 0, 24, 0, 0, 0, 232, 0, 0, 0, 236, 0, 0, 0, 240, 0, 0, 0, 244, 0, 0, 0, 0,
8026 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8027 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8028 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8029 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8030 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8031 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8032 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8033 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8034 ];
8035 BlockV1::new_unchecked(v.into())
8036 }
8037}
8038impl BlockV1 {
8039 pub const FIELD_COUNT: usize = 5;
8040 pub fn total_size(&self) -> usize {
8041 molecule::unpack_number(self.as_slice()) as usize
8042 }
8043 pub fn field_count(&self) -> usize {
8044 if self.total_size() == molecule::NUMBER_SIZE {
8045 0
8046 } else {
8047 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8048 }
8049 }
8050 pub fn count_extra_fields(&self) -> usize {
8051 self.field_count() - Self::FIELD_COUNT
8052 }
8053 pub fn has_extra_fields(&self) -> bool {
8054 Self::FIELD_COUNT != self.field_count()
8055 }
8056 pub fn header(&self) -> Header {
8057 let slice = self.as_slice();
8058 let start = molecule::unpack_number(&slice[4..]) as usize;
8059 let end = molecule::unpack_number(&slice[8..]) as usize;
8060 Header::new_unchecked(self.0.slice(start..end))
8061 }
8062 pub fn uncles(&self) -> UncleBlockVec {
8063 let slice = self.as_slice();
8064 let start = molecule::unpack_number(&slice[8..]) as usize;
8065 let end = molecule::unpack_number(&slice[12..]) as usize;
8066 UncleBlockVec::new_unchecked(self.0.slice(start..end))
8067 }
8068 pub fn transactions(&self) -> TransactionVec {
8069 let slice = self.as_slice();
8070 let start = molecule::unpack_number(&slice[12..]) as usize;
8071 let end = molecule::unpack_number(&slice[16..]) as usize;
8072 TransactionVec::new_unchecked(self.0.slice(start..end))
8073 }
8074 pub fn proposals(&self) -> ProposalShortIdVec {
8075 let slice = self.as_slice();
8076 let start = molecule::unpack_number(&slice[16..]) as usize;
8077 let end = molecule::unpack_number(&slice[20..]) as usize;
8078 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
8079 }
8080 pub fn extension(&self) -> Bytes {
8081 let slice = self.as_slice();
8082 let start = molecule::unpack_number(&slice[20..]) as usize;
8083 if self.has_extra_fields() {
8084 let end = molecule::unpack_number(&slice[24..]) as usize;
8085 Bytes::new_unchecked(self.0.slice(start..end))
8086 } else {
8087 Bytes::new_unchecked(self.0.slice(start..))
8088 }
8089 }
8090 pub fn as_reader<'r>(&'r self) -> BlockV1Reader<'r> {
8091 BlockV1Reader::new_unchecked(self.as_slice())
8092 }
8093}
8094impl molecule::prelude::Entity for BlockV1 {
8095 type Builder = BlockV1Builder;
8096 const NAME: &'static str = "BlockV1";
8097 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8098 BlockV1(data)
8099 }
8100 fn as_bytes(&self) -> molecule::bytes::Bytes {
8101 self.0.clone()
8102 }
8103 fn as_slice(&self) -> &[u8] {
8104 &self.0[..]
8105 }
8106 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8107 BlockV1Reader::from_slice(slice).map(|reader| reader.to_entity())
8108 }
8109 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8110 BlockV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8111 }
8112 fn new_builder() -> Self::Builder {
8113 ::core::default::Default::default()
8114 }
8115 fn as_builder(self) -> Self::Builder {
8116 Self::new_builder()
8117 .header(self.header())
8118 .uncles(self.uncles())
8119 .transactions(self.transactions())
8120 .proposals(self.proposals())
8121 .extension(self.extension())
8122 }
8123}
8124#[derive(Clone, Copy)]
8125pub struct BlockV1Reader<'r>(&'r [u8]);
8126impl<'r> ::core::fmt::LowerHex for BlockV1Reader<'r> {
8127 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8128 use molecule::hex_string;
8129 if f.alternate() {
8130 write!(f, "0x")?;
8131 }
8132 write!(f, "{}", hex_string(self.as_slice()))
8133 }
8134}
8135impl<'r> ::core::fmt::Debug for BlockV1Reader<'r> {
8136 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8137 write!(f, "{}({:#x})", Self::NAME, self)
8138 }
8139}
8140impl<'r> ::core::fmt::Display for BlockV1Reader<'r> {
8141 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8142 write!(f, "{} {{ ", Self::NAME)?;
8143 write!(f, "{}: {}", "header", self.header())?;
8144 write!(f, ", {}: {}", "uncles", self.uncles())?;
8145 write!(f, ", {}: {}", "transactions", self.transactions())?;
8146 write!(f, ", {}: {}", "proposals", self.proposals())?;
8147 write!(f, ", {}: {}", "extension", self.extension())?;
8148 let extra_count = self.count_extra_fields();
8149 if extra_count != 0 {
8150 write!(f, ", .. ({} fields)", extra_count)?;
8151 }
8152 write!(f, " }}")
8153 }
8154}
8155impl<'r> BlockV1Reader<'r> {
8156 pub const FIELD_COUNT: usize = 5;
8157 pub fn total_size(&self) -> usize {
8158 molecule::unpack_number(self.as_slice()) as usize
8159 }
8160 pub fn field_count(&self) -> usize {
8161 if self.total_size() == molecule::NUMBER_SIZE {
8162 0
8163 } else {
8164 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8165 }
8166 }
8167 pub fn count_extra_fields(&self) -> usize {
8168 self.field_count() - Self::FIELD_COUNT
8169 }
8170 pub fn has_extra_fields(&self) -> bool {
8171 Self::FIELD_COUNT != self.field_count()
8172 }
8173 pub fn header(&self) -> HeaderReader<'r> {
8174 let slice = self.as_slice();
8175 let start = molecule::unpack_number(&slice[4..]) as usize;
8176 let end = molecule::unpack_number(&slice[8..]) as usize;
8177 HeaderReader::new_unchecked(&self.as_slice()[start..end])
8178 }
8179 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
8180 let slice = self.as_slice();
8181 let start = molecule::unpack_number(&slice[8..]) as usize;
8182 let end = molecule::unpack_number(&slice[12..]) as usize;
8183 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
8184 }
8185 pub fn transactions(&self) -> TransactionVecReader<'r> {
8186 let slice = self.as_slice();
8187 let start = molecule::unpack_number(&slice[12..]) as usize;
8188 let end = molecule::unpack_number(&slice[16..]) as usize;
8189 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
8190 }
8191 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
8192 let slice = self.as_slice();
8193 let start = molecule::unpack_number(&slice[16..]) as usize;
8194 let end = molecule::unpack_number(&slice[20..]) as usize;
8195 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
8196 }
8197 pub fn extension(&self) -> BytesReader<'r> {
8198 let slice = self.as_slice();
8199 let start = molecule::unpack_number(&slice[20..]) as usize;
8200 if self.has_extra_fields() {
8201 let end = molecule::unpack_number(&slice[24..]) as usize;
8202 BytesReader::new_unchecked(&self.as_slice()[start..end])
8203 } else {
8204 BytesReader::new_unchecked(&self.as_slice()[start..])
8205 }
8206 }
8207}
8208impl<'r> molecule::prelude::Reader<'r> for BlockV1Reader<'r> {
8209 type Entity = BlockV1;
8210 const NAME: &'static str = "BlockV1Reader";
8211 fn to_entity(&self) -> Self::Entity {
8212 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8213 }
8214 fn new_unchecked(slice: &'r [u8]) -> Self {
8215 BlockV1Reader(slice)
8216 }
8217 fn as_slice(&self) -> &'r [u8] {
8218 self.0
8219 }
8220 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8221 use molecule::verification_error as ve;
8222 let slice_len = slice.len();
8223 if slice_len < molecule::NUMBER_SIZE {
8224 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8225 }
8226 let total_size = molecule::unpack_number(slice) as usize;
8227 if slice_len != total_size {
8228 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8229 }
8230 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
8231 return Ok(());
8232 }
8233 if slice_len < molecule::NUMBER_SIZE * 2 {
8234 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8235 }
8236 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8237 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8238 return ve!(Self, OffsetsNotMatch);
8239 }
8240 if slice_len < offset_first {
8241 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8242 }
8243 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8244 if field_count < Self::FIELD_COUNT {
8245 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8246 } else if !compatible && field_count > Self::FIELD_COUNT {
8247 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8248 };
8249 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8250 .chunks_exact(molecule::NUMBER_SIZE)
8251 .map(|x| molecule::unpack_number(x) as usize)
8252 .collect();
8253 offsets.push(total_size);
8254 if offsets.windows(2).any(|i| i[0] > i[1]) {
8255 return ve!(Self, OffsetsNotMatch);
8256 }
8257 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8258 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8259 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
8260 ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
8261 BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
8262 Ok(())
8263 }
8264}
8265#[derive(Debug, Default)]
8266pub struct BlockV1Builder {
8267 pub(crate) header: Header,
8268 pub(crate) uncles: UncleBlockVec,
8269 pub(crate) transactions: TransactionVec,
8270 pub(crate) proposals: ProposalShortIdVec,
8271 pub(crate) extension: Bytes,
8272}
8273impl BlockV1Builder {
8274 pub const FIELD_COUNT: usize = 5;
8275 pub fn header(mut self, v: Header) -> Self {
8276 self.header = v;
8277 self
8278 }
8279 pub fn uncles(mut self, v: UncleBlockVec) -> Self {
8280 self.uncles = v;
8281 self
8282 }
8283 pub fn transactions(mut self, v: TransactionVec) -> Self {
8284 self.transactions = v;
8285 self
8286 }
8287 pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
8288 self.proposals = v;
8289 self
8290 }
8291 pub fn extension(mut self, v: Bytes) -> Self {
8292 self.extension = v;
8293 self
8294 }
8295}
8296impl molecule::prelude::Builder for BlockV1Builder {
8297 type Entity = BlockV1;
8298 const NAME: &'static str = "BlockV1Builder";
8299 fn expected_length(&self) -> usize {
8300 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8301 + self.header.as_slice().len()
8302 + self.uncles.as_slice().len()
8303 + self.transactions.as_slice().len()
8304 + self.proposals.as_slice().len()
8305 + self.extension.as_slice().len()
8306 }
8307 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8308 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8309 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8310 offsets.push(total_size);
8311 total_size += self.header.as_slice().len();
8312 offsets.push(total_size);
8313 total_size += self.uncles.as_slice().len();
8314 offsets.push(total_size);
8315 total_size += self.transactions.as_slice().len();
8316 offsets.push(total_size);
8317 total_size += self.proposals.as_slice().len();
8318 offsets.push(total_size);
8319 total_size += self.extension.as_slice().len();
8320 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8321 for offset in offsets.into_iter() {
8322 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8323 }
8324 writer.write_all(self.header.as_slice())?;
8325 writer.write_all(self.uncles.as_slice())?;
8326 writer.write_all(self.transactions.as_slice())?;
8327 writer.write_all(self.proposals.as_slice())?;
8328 writer.write_all(self.extension.as_slice())?;
8329 Ok(())
8330 }
8331 fn build(&self) -> Self::Entity {
8332 let mut inner = Vec::with_capacity(self.expected_length());
8333 self.write(&mut inner)
8334 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8335 BlockV1::new_unchecked(inner.into())
8336 }
8337}
8338#[derive(Clone)]
8339pub struct CellbaseWitness(molecule::bytes::Bytes);
8340impl ::core::fmt::LowerHex for CellbaseWitness {
8341 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8342 use molecule::hex_string;
8343 if f.alternate() {
8344 write!(f, "0x")?;
8345 }
8346 write!(f, "{}", hex_string(self.as_slice()))
8347 }
8348}
8349impl ::core::fmt::Debug for CellbaseWitness {
8350 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8351 write!(f, "{}({:#x})", Self::NAME, self)
8352 }
8353}
8354impl ::core::fmt::Display for CellbaseWitness {
8355 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8356 write!(f, "{} {{ ", Self::NAME)?;
8357 write!(f, "{}: {}", "lock", self.lock())?;
8358 write!(f, ", {}: {}", "message", self.message())?;
8359 let extra_count = self.count_extra_fields();
8360 if extra_count != 0 {
8361 write!(f, ", .. ({} fields)", extra_count)?;
8362 }
8363 write!(f, " }}")
8364 }
8365}
8366impl ::core::default::Default for CellbaseWitness {
8367 fn default() -> Self {
8368 let v: Vec<u8> = vec![
8369 69, 0, 0, 0, 12, 0, 0, 0, 65, 0, 0, 0, 53, 0, 0, 0, 16, 0, 0, 0, 48, 0, 0, 0, 49, 0, 0,
8370 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8371 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8372 ];
8373 CellbaseWitness::new_unchecked(v.into())
8374 }
8375}
8376impl CellbaseWitness {
8377 pub const FIELD_COUNT: usize = 2;
8378 pub fn total_size(&self) -> usize {
8379 molecule::unpack_number(self.as_slice()) as usize
8380 }
8381 pub fn field_count(&self) -> usize {
8382 if self.total_size() == molecule::NUMBER_SIZE {
8383 0
8384 } else {
8385 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8386 }
8387 }
8388 pub fn count_extra_fields(&self) -> usize {
8389 self.field_count() - Self::FIELD_COUNT
8390 }
8391 pub fn has_extra_fields(&self) -> bool {
8392 Self::FIELD_COUNT != self.field_count()
8393 }
8394 pub fn lock(&self) -> Script {
8395 let slice = self.as_slice();
8396 let start = molecule::unpack_number(&slice[4..]) as usize;
8397 let end = molecule::unpack_number(&slice[8..]) as usize;
8398 Script::new_unchecked(self.0.slice(start..end))
8399 }
8400 pub fn message(&self) -> Bytes {
8401 let slice = self.as_slice();
8402 let start = molecule::unpack_number(&slice[8..]) as usize;
8403 if self.has_extra_fields() {
8404 let end = molecule::unpack_number(&slice[12..]) as usize;
8405 Bytes::new_unchecked(self.0.slice(start..end))
8406 } else {
8407 Bytes::new_unchecked(self.0.slice(start..))
8408 }
8409 }
8410 pub fn as_reader<'r>(&'r self) -> CellbaseWitnessReader<'r> {
8411 CellbaseWitnessReader::new_unchecked(self.as_slice())
8412 }
8413}
8414impl molecule::prelude::Entity for CellbaseWitness {
8415 type Builder = CellbaseWitnessBuilder;
8416 const NAME: &'static str = "CellbaseWitness";
8417 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8418 CellbaseWitness(data)
8419 }
8420 fn as_bytes(&self) -> molecule::bytes::Bytes {
8421 self.0.clone()
8422 }
8423 fn as_slice(&self) -> &[u8] {
8424 &self.0[..]
8425 }
8426 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8427 CellbaseWitnessReader::from_slice(slice).map(|reader| reader.to_entity())
8428 }
8429 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8430 CellbaseWitnessReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8431 }
8432 fn new_builder() -> Self::Builder {
8433 ::core::default::Default::default()
8434 }
8435 fn as_builder(self) -> Self::Builder {
8436 Self::new_builder()
8437 .lock(self.lock())
8438 .message(self.message())
8439 }
8440}
8441#[derive(Clone, Copy)]
8442pub struct CellbaseWitnessReader<'r>(&'r [u8]);
8443impl<'r> ::core::fmt::LowerHex for CellbaseWitnessReader<'r> {
8444 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8445 use molecule::hex_string;
8446 if f.alternate() {
8447 write!(f, "0x")?;
8448 }
8449 write!(f, "{}", hex_string(self.as_slice()))
8450 }
8451}
8452impl<'r> ::core::fmt::Debug for CellbaseWitnessReader<'r> {
8453 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8454 write!(f, "{}({:#x})", Self::NAME, self)
8455 }
8456}
8457impl<'r> ::core::fmt::Display for CellbaseWitnessReader<'r> {
8458 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8459 write!(f, "{} {{ ", Self::NAME)?;
8460 write!(f, "{}: {}", "lock", self.lock())?;
8461 write!(f, ", {}: {}", "message", self.message())?;
8462 let extra_count = self.count_extra_fields();
8463 if extra_count != 0 {
8464 write!(f, ", .. ({} fields)", extra_count)?;
8465 }
8466 write!(f, " }}")
8467 }
8468}
8469impl<'r> CellbaseWitnessReader<'r> {
8470 pub const FIELD_COUNT: usize = 2;
8471 pub fn total_size(&self) -> usize {
8472 molecule::unpack_number(self.as_slice()) as usize
8473 }
8474 pub fn field_count(&self) -> usize {
8475 if self.total_size() == molecule::NUMBER_SIZE {
8476 0
8477 } else {
8478 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8479 }
8480 }
8481 pub fn count_extra_fields(&self) -> usize {
8482 self.field_count() - Self::FIELD_COUNT
8483 }
8484 pub fn has_extra_fields(&self) -> bool {
8485 Self::FIELD_COUNT != self.field_count()
8486 }
8487 pub fn lock(&self) -> ScriptReader<'r> {
8488 let slice = self.as_slice();
8489 let start = molecule::unpack_number(&slice[4..]) as usize;
8490 let end = molecule::unpack_number(&slice[8..]) as usize;
8491 ScriptReader::new_unchecked(&self.as_slice()[start..end])
8492 }
8493 pub fn message(&self) -> BytesReader<'r> {
8494 let slice = self.as_slice();
8495 let start = molecule::unpack_number(&slice[8..]) as usize;
8496 if self.has_extra_fields() {
8497 let end = molecule::unpack_number(&slice[12..]) as usize;
8498 BytesReader::new_unchecked(&self.as_slice()[start..end])
8499 } else {
8500 BytesReader::new_unchecked(&self.as_slice()[start..])
8501 }
8502 }
8503}
8504impl<'r> molecule::prelude::Reader<'r> for CellbaseWitnessReader<'r> {
8505 type Entity = CellbaseWitness;
8506 const NAME: &'static str = "CellbaseWitnessReader";
8507 fn to_entity(&self) -> Self::Entity {
8508 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8509 }
8510 fn new_unchecked(slice: &'r [u8]) -> Self {
8511 CellbaseWitnessReader(slice)
8512 }
8513 fn as_slice(&self) -> &'r [u8] {
8514 self.0
8515 }
8516 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8517 use molecule::verification_error as ve;
8518 let slice_len = slice.len();
8519 if slice_len < molecule::NUMBER_SIZE {
8520 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8521 }
8522 let total_size = molecule::unpack_number(slice) as usize;
8523 if slice_len != total_size {
8524 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8525 }
8526 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
8527 return Ok(());
8528 }
8529 if slice_len < molecule::NUMBER_SIZE * 2 {
8530 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8531 }
8532 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8533 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8534 return ve!(Self, OffsetsNotMatch);
8535 }
8536 if slice_len < offset_first {
8537 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8538 }
8539 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8540 if field_count < Self::FIELD_COUNT {
8541 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8542 } else if !compatible && field_count > Self::FIELD_COUNT {
8543 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8544 };
8545 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8546 .chunks_exact(molecule::NUMBER_SIZE)
8547 .map(|x| molecule::unpack_number(x) as usize)
8548 .collect();
8549 offsets.push(total_size);
8550 if offsets.windows(2).any(|i| i[0] > i[1]) {
8551 return ve!(Self, OffsetsNotMatch);
8552 }
8553 ScriptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8554 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8555 Ok(())
8556 }
8557}
8558#[derive(Debug, Default)]
8559pub struct CellbaseWitnessBuilder {
8560 pub(crate) lock: Script,
8561 pub(crate) message: Bytes,
8562}
8563impl CellbaseWitnessBuilder {
8564 pub const FIELD_COUNT: usize = 2;
8565 pub fn lock(mut self, v: Script) -> Self {
8566 self.lock = v;
8567 self
8568 }
8569 pub fn message(mut self, v: Bytes) -> Self {
8570 self.message = v;
8571 self
8572 }
8573}
8574impl molecule::prelude::Builder for CellbaseWitnessBuilder {
8575 type Entity = CellbaseWitness;
8576 const NAME: &'static str = "CellbaseWitnessBuilder";
8577 fn expected_length(&self) -> usize {
8578 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8579 + self.lock.as_slice().len()
8580 + self.message.as_slice().len()
8581 }
8582 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8583 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8584 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8585 offsets.push(total_size);
8586 total_size += self.lock.as_slice().len();
8587 offsets.push(total_size);
8588 total_size += self.message.as_slice().len();
8589 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8590 for offset in offsets.into_iter() {
8591 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8592 }
8593 writer.write_all(self.lock.as_slice())?;
8594 writer.write_all(self.message.as_slice())?;
8595 Ok(())
8596 }
8597 fn build(&self) -> Self::Entity {
8598 let mut inner = Vec::with_capacity(self.expected_length());
8599 self.write(&mut inner)
8600 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8601 CellbaseWitness::new_unchecked(inner.into())
8602 }
8603}
8604#[derive(Clone)]
8605pub struct WitnessArgs(molecule::bytes::Bytes);
8606impl ::core::fmt::LowerHex for WitnessArgs {
8607 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8608 use molecule::hex_string;
8609 if f.alternate() {
8610 write!(f, "0x")?;
8611 }
8612 write!(f, "{}", hex_string(self.as_slice()))
8613 }
8614}
8615impl ::core::fmt::Debug for WitnessArgs {
8616 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8617 write!(f, "{}({:#x})", Self::NAME, self)
8618 }
8619}
8620impl ::core::fmt::Display for WitnessArgs {
8621 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8622 write!(f, "{} {{ ", Self::NAME)?;
8623 write!(f, "{}: {}", "lock", self.lock())?;
8624 write!(f, ", {}: {}", "input_type", self.input_type())?;
8625 write!(f, ", {}: {}", "output_type", self.output_type())?;
8626 let extra_count = self.count_extra_fields();
8627 if extra_count != 0 {
8628 write!(f, ", .. ({} fields)", extra_count)?;
8629 }
8630 write!(f, " }}")
8631 }
8632}
8633impl ::core::default::Default for WitnessArgs {
8634 fn default() -> Self {
8635 let v: Vec<u8> = vec![16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0];
8636 WitnessArgs::new_unchecked(v.into())
8637 }
8638}
8639impl WitnessArgs {
8640 pub const FIELD_COUNT: usize = 3;
8641 pub fn total_size(&self) -> usize {
8642 molecule::unpack_number(self.as_slice()) as usize
8643 }
8644 pub fn field_count(&self) -> usize {
8645 if self.total_size() == molecule::NUMBER_SIZE {
8646 0
8647 } else {
8648 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8649 }
8650 }
8651 pub fn count_extra_fields(&self) -> usize {
8652 self.field_count() - Self::FIELD_COUNT
8653 }
8654 pub fn has_extra_fields(&self) -> bool {
8655 Self::FIELD_COUNT != self.field_count()
8656 }
8657 pub fn lock(&self) -> BytesOpt {
8658 let slice = self.as_slice();
8659 let start = molecule::unpack_number(&slice[4..]) as usize;
8660 let end = molecule::unpack_number(&slice[8..]) as usize;
8661 BytesOpt::new_unchecked(self.0.slice(start..end))
8662 }
8663 pub fn input_type(&self) -> BytesOpt {
8664 let slice = self.as_slice();
8665 let start = molecule::unpack_number(&slice[8..]) as usize;
8666 let end = molecule::unpack_number(&slice[12..]) as usize;
8667 BytesOpt::new_unchecked(self.0.slice(start..end))
8668 }
8669 pub fn output_type(&self) -> BytesOpt {
8670 let slice = self.as_slice();
8671 let start = molecule::unpack_number(&slice[12..]) as usize;
8672 if self.has_extra_fields() {
8673 let end = molecule::unpack_number(&slice[16..]) as usize;
8674 BytesOpt::new_unchecked(self.0.slice(start..end))
8675 } else {
8676 BytesOpt::new_unchecked(self.0.slice(start..))
8677 }
8678 }
8679 pub fn as_reader<'r>(&'r self) -> WitnessArgsReader<'r> {
8680 WitnessArgsReader::new_unchecked(self.as_slice())
8681 }
8682}
8683impl molecule::prelude::Entity for WitnessArgs {
8684 type Builder = WitnessArgsBuilder;
8685 const NAME: &'static str = "WitnessArgs";
8686 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8687 WitnessArgs(data)
8688 }
8689 fn as_bytes(&self) -> molecule::bytes::Bytes {
8690 self.0.clone()
8691 }
8692 fn as_slice(&self) -> &[u8] {
8693 &self.0[..]
8694 }
8695 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8696 WitnessArgsReader::from_slice(slice).map(|reader| reader.to_entity())
8697 }
8698 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8699 WitnessArgsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8700 }
8701 fn new_builder() -> Self::Builder {
8702 ::core::default::Default::default()
8703 }
8704 fn as_builder(self) -> Self::Builder {
8705 Self::new_builder()
8706 .lock(self.lock())
8707 .input_type(self.input_type())
8708 .output_type(self.output_type())
8709 }
8710}
8711#[derive(Clone, Copy)]
8712pub struct WitnessArgsReader<'r>(&'r [u8]);
8713impl<'r> ::core::fmt::LowerHex for WitnessArgsReader<'r> {
8714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8715 use molecule::hex_string;
8716 if f.alternate() {
8717 write!(f, "0x")?;
8718 }
8719 write!(f, "{}", hex_string(self.as_slice()))
8720 }
8721}
8722impl<'r> ::core::fmt::Debug for WitnessArgsReader<'r> {
8723 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8724 write!(f, "{}({:#x})", Self::NAME, self)
8725 }
8726}
8727impl<'r> ::core::fmt::Display for WitnessArgsReader<'r> {
8728 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8729 write!(f, "{} {{ ", Self::NAME)?;
8730 write!(f, "{}: {}", "lock", self.lock())?;
8731 write!(f, ", {}: {}", "input_type", self.input_type())?;
8732 write!(f, ", {}: {}", "output_type", self.output_type())?;
8733 let extra_count = self.count_extra_fields();
8734 if extra_count != 0 {
8735 write!(f, ", .. ({} fields)", extra_count)?;
8736 }
8737 write!(f, " }}")
8738 }
8739}
8740impl<'r> WitnessArgsReader<'r> {
8741 pub const FIELD_COUNT: usize = 3;
8742 pub fn total_size(&self) -> usize {
8743 molecule::unpack_number(self.as_slice()) as usize
8744 }
8745 pub fn field_count(&self) -> usize {
8746 if self.total_size() == molecule::NUMBER_SIZE {
8747 0
8748 } else {
8749 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8750 }
8751 }
8752 pub fn count_extra_fields(&self) -> usize {
8753 self.field_count() - Self::FIELD_COUNT
8754 }
8755 pub fn has_extra_fields(&self) -> bool {
8756 Self::FIELD_COUNT != self.field_count()
8757 }
8758 pub fn lock(&self) -> BytesOptReader<'r> {
8759 let slice = self.as_slice();
8760 let start = molecule::unpack_number(&slice[4..]) as usize;
8761 let end = molecule::unpack_number(&slice[8..]) as usize;
8762 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
8763 }
8764 pub fn input_type(&self) -> BytesOptReader<'r> {
8765 let slice = self.as_slice();
8766 let start = molecule::unpack_number(&slice[8..]) as usize;
8767 let end = molecule::unpack_number(&slice[12..]) as usize;
8768 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
8769 }
8770 pub fn output_type(&self) -> BytesOptReader<'r> {
8771 let slice = self.as_slice();
8772 let start = molecule::unpack_number(&slice[12..]) as usize;
8773 if self.has_extra_fields() {
8774 let end = molecule::unpack_number(&slice[16..]) as usize;
8775 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
8776 } else {
8777 BytesOptReader::new_unchecked(&self.as_slice()[start..])
8778 }
8779 }
8780}
8781impl<'r> molecule::prelude::Reader<'r> for WitnessArgsReader<'r> {
8782 type Entity = WitnessArgs;
8783 const NAME: &'static str = "WitnessArgsReader";
8784 fn to_entity(&self) -> Self::Entity {
8785 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8786 }
8787 fn new_unchecked(slice: &'r [u8]) -> Self {
8788 WitnessArgsReader(slice)
8789 }
8790 fn as_slice(&self) -> &'r [u8] {
8791 self.0
8792 }
8793 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8794 use molecule::verification_error as ve;
8795 let slice_len = slice.len();
8796 if slice_len < molecule::NUMBER_SIZE {
8797 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8798 }
8799 let total_size = molecule::unpack_number(slice) as usize;
8800 if slice_len != total_size {
8801 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8802 }
8803 if slice_len == molecule::NUMBER_SIZE && Self::FIELD_COUNT == 0 {
8804 return Ok(());
8805 }
8806 if slice_len < molecule::NUMBER_SIZE * 2 {
8807 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8808 }
8809 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8810 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8811 return ve!(Self, OffsetsNotMatch);
8812 }
8813 if slice_len < offset_first {
8814 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8815 }
8816 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8817 if field_count < Self::FIELD_COUNT {
8818 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8819 } else if !compatible && field_count > Self::FIELD_COUNT {
8820 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8821 };
8822 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8823 .chunks_exact(molecule::NUMBER_SIZE)
8824 .map(|x| molecule::unpack_number(x) as usize)
8825 .collect();
8826 offsets.push(total_size);
8827 if offsets.windows(2).any(|i| i[0] > i[1]) {
8828 return ve!(Self, OffsetsNotMatch);
8829 }
8830 BytesOptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8831 BytesOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8832 BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
8833 Ok(())
8834 }
8835}
8836#[derive(Debug, Default)]
8837pub struct WitnessArgsBuilder {
8838 pub(crate) lock: BytesOpt,
8839 pub(crate) input_type: BytesOpt,
8840 pub(crate) output_type: BytesOpt,
8841}
8842impl WitnessArgsBuilder {
8843 pub const FIELD_COUNT: usize = 3;
8844 pub fn lock(mut self, v: BytesOpt) -> Self {
8845 self.lock = v;
8846 self
8847 }
8848 pub fn input_type(mut self, v: BytesOpt) -> Self {
8849 self.input_type = v;
8850 self
8851 }
8852 pub fn output_type(mut self, v: BytesOpt) -> Self {
8853 self.output_type = v;
8854 self
8855 }
8856}
8857impl molecule::prelude::Builder for WitnessArgsBuilder {
8858 type Entity = WitnessArgs;
8859 const NAME: &'static str = "WitnessArgsBuilder";
8860 fn expected_length(&self) -> usize {
8861 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8862 + self.lock.as_slice().len()
8863 + self.input_type.as_slice().len()
8864 + self.output_type.as_slice().len()
8865 }
8866 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8867 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8868 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8869 offsets.push(total_size);
8870 total_size += self.lock.as_slice().len();
8871 offsets.push(total_size);
8872 total_size += self.input_type.as_slice().len();
8873 offsets.push(total_size);
8874 total_size += self.output_type.as_slice().len();
8875 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8876 for offset in offsets.into_iter() {
8877 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8878 }
8879 writer.write_all(self.lock.as_slice())?;
8880 writer.write_all(self.input_type.as_slice())?;
8881 writer.write_all(self.output_type.as_slice())?;
8882 Ok(())
8883 }
8884 fn build(&self) -> Self::Entity {
8885 let mut inner = Vec::with_capacity(self.expected_length());
8886 self.write(&mut inner)
8887 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8888 WitnessArgs::new_unchecked(inner.into())
8889 }
8890}