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