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}
146#[derive(Clone)]
147pub struct Uint32Builder(pub(crate) [Byte; 4]);
148impl ::core::fmt::Debug for Uint32Builder {
149 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
150 write!(f, "{}({:?})", Self::NAME, &self.0[..])
151 }
152}
153impl ::core::default::Default for Uint32Builder {
154 fn default() -> Self {
155 Uint32Builder([
156 Byte::default(),
157 Byte::default(),
158 Byte::default(),
159 Byte::default(),
160 ])
161 }
162}
163impl Uint32Builder {
164 pub const TOTAL_SIZE: usize = 4;
165 pub const ITEM_SIZE: usize = 1;
166 pub const ITEM_COUNT: usize = 4;
167 pub fn set<T>(mut self, v: T) -> Self
168 where
169 T: ::core::convert::Into<[Byte; 4]>,
170 {
171 self.0 = v.into();
172 self
173 }
174 pub fn nth0<T>(mut self, v: T) -> Self
175 where
176 T: ::core::convert::Into<Byte>,
177 {
178 self.0[0] = v.into();
179 self
180 }
181 pub fn nth1<T>(mut self, v: T) -> Self
182 where
183 T: ::core::convert::Into<Byte>,
184 {
185 self.0[1] = v.into();
186 self
187 }
188 pub fn nth2<T>(mut self, v: T) -> Self
189 where
190 T: ::core::convert::Into<Byte>,
191 {
192 self.0[2] = v.into();
193 self
194 }
195 pub fn nth3<T>(mut self, v: T) -> Self
196 where
197 T: ::core::convert::Into<Byte>,
198 {
199 self.0[3] = v.into();
200 self
201 }
202}
203impl molecule::prelude::Builder for Uint32Builder {
204 type Entity = Uint32;
205 const NAME: &'static str = "Uint32Builder";
206 fn expected_length(&self) -> usize {
207 Self::TOTAL_SIZE
208 }
209 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
210 writer.write_all(self.0[0].as_slice())?;
211 writer.write_all(self.0[1].as_slice())?;
212 writer.write_all(self.0[2].as_slice())?;
213 writer.write_all(self.0[3].as_slice())?;
214 Ok(())
215 }
216 fn build(&self) -> Self::Entity {
217 let mut inner = Vec::with_capacity(self.expected_length());
218 self.write(&mut inner)
219 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
220 Uint32::new_unchecked(inner.into())
221 }
222}
223impl From<[Byte; 4usize]> for Uint32 {
224 fn from(value: [Byte; 4usize]) -> Self {
225 Self::new_builder().set(value).build()
226 }
227}
228impl ::core::convert::TryFrom<&[Byte]> for Uint32 {
229 type Error = ::core::array::TryFromSliceError;
230 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
231 Ok(Self::new_builder()
232 .set(<&[Byte; 4usize]>::try_from(value)?.clone())
233 .build())
234 }
235}
236impl From<Uint32> for [Byte; 4usize] {
237 #[track_caller]
238 fn from(value: Uint32) -> Self {
239 [value.nth0(), value.nth1(), value.nth2(), value.nth3()]
240 }
241}
242impl From<[u8; 4usize]> for Uint32 {
243 fn from(value: [u8; 4usize]) -> Self {
244 Uint32Reader::new_unchecked(&value).to_entity()
245 }
246}
247impl ::core::convert::TryFrom<&[u8]> for Uint32 {
248 type Error = ::core::array::TryFromSliceError;
249 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
250 Ok(<[u8; 4usize]>::try_from(value)?.into())
251 }
252}
253impl From<Uint32> for [u8; 4usize] {
254 #[track_caller]
255 fn from(value: Uint32) -> Self {
256 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
257 }
258}
259impl<'a> From<Uint32Reader<'a>> for &'a [u8; 4usize] {
260 #[track_caller]
261 fn from(value: Uint32Reader<'a>) -> Self {
262 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
263 }
264}
265impl<'a> From<&'a Uint32Reader<'a>> for &'a [u8; 4usize] {
266 #[track_caller]
267 fn from(value: &'a Uint32Reader<'a>) -> Self {
268 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
269 }
270}
271#[derive(Clone)]
272pub struct Uint64(molecule::bytes::Bytes);
273impl ::core::fmt::LowerHex for Uint64 {
274 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
275 use molecule::hex_string;
276 if f.alternate() {
277 write!(f, "0x")?;
278 }
279 write!(f, "{}", hex_string(self.as_slice()))
280 }
281}
282impl ::core::fmt::Debug for Uint64 {
283 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
284 write!(f, "{}({:#x})", Self::NAME, self)
285 }
286}
287impl ::core::fmt::Display for Uint64 {
288 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
289 use molecule::hex_string;
290 let raw_data = hex_string(&self.raw_data());
291 write!(f, "{}(0x{})", Self::NAME, raw_data)
292 }
293}
294impl ::core::default::Default for Uint64 {
295 fn default() -> Self {
296 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
297 Uint64::new_unchecked(v)
298 }
299}
300impl Uint64 {
301 const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
302 pub const TOTAL_SIZE: usize = 8;
303 pub const ITEM_SIZE: usize = 1;
304 pub const ITEM_COUNT: usize = 8;
305 pub fn nth0(&self) -> Byte {
306 Byte::new_unchecked(self.0.slice(0..1))
307 }
308 pub fn nth1(&self) -> Byte {
309 Byte::new_unchecked(self.0.slice(1..2))
310 }
311 pub fn nth2(&self) -> Byte {
312 Byte::new_unchecked(self.0.slice(2..3))
313 }
314 pub fn nth3(&self) -> Byte {
315 Byte::new_unchecked(self.0.slice(3..4))
316 }
317 pub fn nth4(&self) -> Byte {
318 Byte::new_unchecked(self.0.slice(4..5))
319 }
320 pub fn nth5(&self) -> Byte {
321 Byte::new_unchecked(self.0.slice(5..6))
322 }
323 pub fn nth6(&self) -> Byte {
324 Byte::new_unchecked(self.0.slice(6..7))
325 }
326 pub fn nth7(&self) -> Byte {
327 Byte::new_unchecked(self.0.slice(7..8))
328 }
329 pub fn raw_data(&self) -> molecule::bytes::Bytes {
330 self.as_bytes()
331 }
332 pub fn as_reader<'r>(&'r self) -> Uint64Reader<'r> {
333 Uint64Reader::new_unchecked(self.as_slice())
334 }
335}
336impl molecule::prelude::Entity for Uint64 {
337 type Builder = Uint64Builder;
338 const NAME: &'static str = "Uint64";
339 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
340 Uint64(data)
341 }
342 fn as_bytes(&self) -> molecule::bytes::Bytes {
343 self.0.clone()
344 }
345 fn as_slice(&self) -> &[u8] {
346 &self.0[..]
347 }
348 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
349 Uint64Reader::from_slice(slice).map(|reader| reader.to_entity())
350 }
351 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
352 Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
353 }
354 fn new_builder() -> Self::Builder {
355 ::core::default::Default::default()
356 }
357 fn as_builder(self) -> Self::Builder {
358 Self::new_builder().set([
359 self.nth0(),
360 self.nth1(),
361 self.nth2(),
362 self.nth3(),
363 self.nth4(),
364 self.nth5(),
365 self.nth6(),
366 self.nth7(),
367 ])
368 }
369}
370#[derive(Clone, Copy)]
371pub struct Uint64Reader<'r>(&'r [u8]);
372impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> {
373 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
374 use molecule::hex_string;
375 if f.alternate() {
376 write!(f, "0x")?;
377 }
378 write!(f, "{}", hex_string(self.as_slice()))
379 }
380}
381impl<'r> ::core::fmt::Debug for Uint64Reader<'r> {
382 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
383 write!(f, "{}({:#x})", Self::NAME, self)
384 }
385}
386impl<'r> ::core::fmt::Display for Uint64Reader<'r> {
387 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
388 use molecule::hex_string;
389 let raw_data = hex_string(&self.raw_data());
390 write!(f, "{}(0x{})", Self::NAME, raw_data)
391 }
392}
393impl<'r> Uint64Reader<'r> {
394 pub const TOTAL_SIZE: usize = 8;
395 pub const ITEM_SIZE: usize = 1;
396 pub const ITEM_COUNT: usize = 8;
397 pub fn nth0(&self) -> ByteReader<'r> {
398 ByteReader::new_unchecked(&self.as_slice()[0..1])
399 }
400 pub fn nth1(&self) -> ByteReader<'r> {
401 ByteReader::new_unchecked(&self.as_slice()[1..2])
402 }
403 pub fn nth2(&self) -> ByteReader<'r> {
404 ByteReader::new_unchecked(&self.as_slice()[2..3])
405 }
406 pub fn nth3(&self) -> ByteReader<'r> {
407 ByteReader::new_unchecked(&self.as_slice()[3..4])
408 }
409 pub fn nth4(&self) -> ByteReader<'r> {
410 ByteReader::new_unchecked(&self.as_slice()[4..5])
411 }
412 pub fn nth5(&self) -> ByteReader<'r> {
413 ByteReader::new_unchecked(&self.as_slice()[5..6])
414 }
415 pub fn nth6(&self) -> ByteReader<'r> {
416 ByteReader::new_unchecked(&self.as_slice()[6..7])
417 }
418 pub fn nth7(&self) -> ByteReader<'r> {
419 ByteReader::new_unchecked(&self.as_slice()[7..8])
420 }
421 pub fn raw_data(&self) -> &'r [u8] {
422 self.as_slice()
423 }
424}
425impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> {
426 type Entity = Uint64;
427 const NAME: &'static str = "Uint64Reader";
428 fn to_entity(&self) -> Self::Entity {
429 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
430 }
431 fn new_unchecked(slice: &'r [u8]) -> Self {
432 Uint64Reader(slice)
433 }
434 fn as_slice(&self) -> &'r [u8] {
435 self.0
436 }
437 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
438 use molecule::verification_error as ve;
439 let slice_len = slice.len();
440 if slice_len != Self::TOTAL_SIZE {
441 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
442 }
443 Ok(())
444 }
445}
446#[derive(Clone)]
447pub struct Uint64Builder(pub(crate) [Byte; 8]);
448impl ::core::fmt::Debug for Uint64Builder {
449 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
450 write!(f, "{}({:?})", Self::NAME, &self.0[..])
451 }
452}
453impl ::core::default::Default for Uint64Builder {
454 fn default() -> Self {
455 Uint64Builder([
456 Byte::default(),
457 Byte::default(),
458 Byte::default(),
459 Byte::default(),
460 Byte::default(),
461 Byte::default(),
462 Byte::default(),
463 Byte::default(),
464 ])
465 }
466}
467impl Uint64Builder {
468 pub const TOTAL_SIZE: usize = 8;
469 pub const ITEM_SIZE: usize = 1;
470 pub const ITEM_COUNT: usize = 8;
471 pub fn set<T>(mut self, v: T) -> Self
472 where
473 T: ::core::convert::Into<[Byte; 8]>,
474 {
475 self.0 = v.into();
476 self
477 }
478 pub fn nth0<T>(mut self, v: T) -> Self
479 where
480 T: ::core::convert::Into<Byte>,
481 {
482 self.0[0] = v.into();
483 self
484 }
485 pub fn nth1<T>(mut self, v: T) -> Self
486 where
487 T: ::core::convert::Into<Byte>,
488 {
489 self.0[1] = v.into();
490 self
491 }
492 pub fn nth2<T>(mut self, v: T) -> Self
493 where
494 T: ::core::convert::Into<Byte>,
495 {
496 self.0[2] = v.into();
497 self
498 }
499 pub fn nth3<T>(mut self, v: T) -> Self
500 where
501 T: ::core::convert::Into<Byte>,
502 {
503 self.0[3] = v.into();
504 self
505 }
506 pub fn nth4<T>(mut self, v: T) -> Self
507 where
508 T: ::core::convert::Into<Byte>,
509 {
510 self.0[4] = v.into();
511 self
512 }
513 pub fn nth5<T>(mut self, v: T) -> Self
514 where
515 T: ::core::convert::Into<Byte>,
516 {
517 self.0[5] = v.into();
518 self
519 }
520 pub fn nth6<T>(mut self, v: T) -> Self
521 where
522 T: ::core::convert::Into<Byte>,
523 {
524 self.0[6] = v.into();
525 self
526 }
527 pub fn nth7<T>(mut self, v: T) -> Self
528 where
529 T: ::core::convert::Into<Byte>,
530 {
531 self.0[7] = v.into();
532 self
533 }
534}
535impl molecule::prelude::Builder for Uint64Builder {
536 type Entity = Uint64;
537 const NAME: &'static str = "Uint64Builder";
538 fn expected_length(&self) -> usize {
539 Self::TOTAL_SIZE
540 }
541 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
542 writer.write_all(self.0[0].as_slice())?;
543 writer.write_all(self.0[1].as_slice())?;
544 writer.write_all(self.0[2].as_slice())?;
545 writer.write_all(self.0[3].as_slice())?;
546 writer.write_all(self.0[4].as_slice())?;
547 writer.write_all(self.0[5].as_slice())?;
548 writer.write_all(self.0[6].as_slice())?;
549 writer.write_all(self.0[7].as_slice())?;
550 Ok(())
551 }
552 fn build(&self) -> Self::Entity {
553 let mut inner = Vec::with_capacity(self.expected_length());
554 self.write(&mut inner)
555 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
556 Uint64::new_unchecked(inner.into())
557 }
558}
559impl From<[Byte; 8usize]> for Uint64 {
560 fn from(value: [Byte; 8usize]) -> Self {
561 Self::new_builder().set(value).build()
562 }
563}
564impl ::core::convert::TryFrom<&[Byte]> for Uint64 {
565 type Error = ::core::array::TryFromSliceError;
566 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
567 Ok(Self::new_builder()
568 .set(<&[Byte; 8usize]>::try_from(value)?.clone())
569 .build())
570 }
571}
572impl From<Uint64> for [Byte; 8usize] {
573 #[track_caller]
574 fn from(value: Uint64) -> Self {
575 [
576 value.nth0(),
577 value.nth1(),
578 value.nth2(),
579 value.nth3(),
580 value.nth4(),
581 value.nth5(),
582 value.nth6(),
583 value.nth7(),
584 ]
585 }
586}
587impl From<[u8; 8usize]> for Uint64 {
588 fn from(value: [u8; 8usize]) -> Self {
589 Uint64Reader::new_unchecked(&value).to_entity()
590 }
591}
592impl ::core::convert::TryFrom<&[u8]> for Uint64 {
593 type Error = ::core::array::TryFromSliceError;
594 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
595 Ok(<[u8; 8usize]>::try_from(value)?.into())
596 }
597}
598impl From<Uint64> for [u8; 8usize] {
599 #[track_caller]
600 fn from(value: Uint64) -> Self {
601 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
602 }
603}
604impl<'a> From<Uint64Reader<'a>> for &'a [u8; 8usize] {
605 #[track_caller]
606 fn from(value: Uint64Reader<'a>) -> Self {
607 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
608 }
609}
610impl<'a> From<&'a Uint64Reader<'a>> for &'a [u8; 8usize] {
611 #[track_caller]
612 fn from(value: &'a Uint64Reader<'a>) -> Self {
613 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
614 }
615}
616#[derive(Clone)]
617pub struct Uint128(molecule::bytes::Bytes);
618impl ::core::fmt::LowerHex for Uint128 {
619 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
620 use molecule::hex_string;
621 if f.alternate() {
622 write!(f, "0x")?;
623 }
624 write!(f, "{}", hex_string(self.as_slice()))
625 }
626}
627impl ::core::fmt::Debug for Uint128 {
628 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
629 write!(f, "{}({:#x})", Self::NAME, self)
630 }
631}
632impl ::core::fmt::Display for Uint128 {
633 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
634 use molecule::hex_string;
635 let raw_data = hex_string(&self.raw_data());
636 write!(f, "{}(0x{})", Self::NAME, raw_data)
637 }
638}
639impl ::core::default::Default for Uint128 {
640 fn default() -> Self {
641 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
642 Uint128::new_unchecked(v)
643 }
644}
645impl Uint128 {
646 const DEFAULT_VALUE: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
647 pub const TOTAL_SIZE: usize = 16;
648 pub const ITEM_SIZE: usize = 1;
649 pub const ITEM_COUNT: usize = 16;
650 pub fn nth0(&self) -> Byte {
651 Byte::new_unchecked(self.0.slice(0..1))
652 }
653 pub fn nth1(&self) -> Byte {
654 Byte::new_unchecked(self.0.slice(1..2))
655 }
656 pub fn nth2(&self) -> Byte {
657 Byte::new_unchecked(self.0.slice(2..3))
658 }
659 pub fn nth3(&self) -> Byte {
660 Byte::new_unchecked(self.0.slice(3..4))
661 }
662 pub fn nth4(&self) -> Byte {
663 Byte::new_unchecked(self.0.slice(4..5))
664 }
665 pub fn nth5(&self) -> Byte {
666 Byte::new_unchecked(self.0.slice(5..6))
667 }
668 pub fn nth6(&self) -> Byte {
669 Byte::new_unchecked(self.0.slice(6..7))
670 }
671 pub fn nth7(&self) -> Byte {
672 Byte::new_unchecked(self.0.slice(7..8))
673 }
674 pub fn nth8(&self) -> Byte {
675 Byte::new_unchecked(self.0.slice(8..9))
676 }
677 pub fn nth9(&self) -> Byte {
678 Byte::new_unchecked(self.0.slice(9..10))
679 }
680 pub fn nth10(&self) -> Byte {
681 Byte::new_unchecked(self.0.slice(10..11))
682 }
683 pub fn nth11(&self) -> Byte {
684 Byte::new_unchecked(self.0.slice(11..12))
685 }
686 pub fn nth12(&self) -> Byte {
687 Byte::new_unchecked(self.0.slice(12..13))
688 }
689 pub fn nth13(&self) -> Byte {
690 Byte::new_unchecked(self.0.slice(13..14))
691 }
692 pub fn nth14(&self) -> Byte {
693 Byte::new_unchecked(self.0.slice(14..15))
694 }
695 pub fn nth15(&self) -> Byte {
696 Byte::new_unchecked(self.0.slice(15..16))
697 }
698 pub fn raw_data(&self) -> molecule::bytes::Bytes {
699 self.as_bytes()
700 }
701 pub fn as_reader<'r>(&'r self) -> Uint128Reader<'r> {
702 Uint128Reader::new_unchecked(self.as_slice())
703 }
704}
705impl molecule::prelude::Entity for Uint128 {
706 type Builder = Uint128Builder;
707 const NAME: &'static str = "Uint128";
708 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
709 Uint128(data)
710 }
711 fn as_bytes(&self) -> molecule::bytes::Bytes {
712 self.0.clone()
713 }
714 fn as_slice(&self) -> &[u8] {
715 &self.0[..]
716 }
717 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
718 Uint128Reader::from_slice(slice).map(|reader| reader.to_entity())
719 }
720 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
721 Uint128Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
722 }
723 fn new_builder() -> Self::Builder {
724 ::core::default::Default::default()
725 }
726 fn as_builder(self) -> Self::Builder {
727 Self::new_builder().set([
728 self.nth0(),
729 self.nth1(),
730 self.nth2(),
731 self.nth3(),
732 self.nth4(),
733 self.nth5(),
734 self.nth6(),
735 self.nth7(),
736 self.nth8(),
737 self.nth9(),
738 self.nth10(),
739 self.nth11(),
740 self.nth12(),
741 self.nth13(),
742 self.nth14(),
743 self.nth15(),
744 ])
745 }
746}
747#[derive(Clone, Copy)]
748pub struct Uint128Reader<'r>(&'r [u8]);
749impl<'r> ::core::fmt::LowerHex for Uint128Reader<'r> {
750 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
751 use molecule::hex_string;
752 if f.alternate() {
753 write!(f, "0x")?;
754 }
755 write!(f, "{}", hex_string(self.as_slice()))
756 }
757}
758impl<'r> ::core::fmt::Debug for Uint128Reader<'r> {
759 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
760 write!(f, "{}({:#x})", Self::NAME, self)
761 }
762}
763impl<'r> ::core::fmt::Display for Uint128Reader<'r> {
764 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
765 use molecule::hex_string;
766 let raw_data = hex_string(&self.raw_data());
767 write!(f, "{}(0x{})", Self::NAME, raw_data)
768 }
769}
770impl<'r> Uint128Reader<'r> {
771 pub const TOTAL_SIZE: usize = 16;
772 pub const ITEM_SIZE: usize = 1;
773 pub const ITEM_COUNT: usize = 16;
774 pub fn nth0(&self) -> ByteReader<'r> {
775 ByteReader::new_unchecked(&self.as_slice()[0..1])
776 }
777 pub fn nth1(&self) -> ByteReader<'r> {
778 ByteReader::new_unchecked(&self.as_slice()[1..2])
779 }
780 pub fn nth2(&self) -> ByteReader<'r> {
781 ByteReader::new_unchecked(&self.as_slice()[2..3])
782 }
783 pub fn nth3(&self) -> ByteReader<'r> {
784 ByteReader::new_unchecked(&self.as_slice()[3..4])
785 }
786 pub fn nth4(&self) -> ByteReader<'r> {
787 ByteReader::new_unchecked(&self.as_slice()[4..5])
788 }
789 pub fn nth5(&self) -> ByteReader<'r> {
790 ByteReader::new_unchecked(&self.as_slice()[5..6])
791 }
792 pub fn nth6(&self) -> ByteReader<'r> {
793 ByteReader::new_unchecked(&self.as_slice()[6..7])
794 }
795 pub fn nth7(&self) -> ByteReader<'r> {
796 ByteReader::new_unchecked(&self.as_slice()[7..8])
797 }
798 pub fn nth8(&self) -> ByteReader<'r> {
799 ByteReader::new_unchecked(&self.as_slice()[8..9])
800 }
801 pub fn nth9(&self) -> ByteReader<'r> {
802 ByteReader::new_unchecked(&self.as_slice()[9..10])
803 }
804 pub fn nth10(&self) -> ByteReader<'r> {
805 ByteReader::new_unchecked(&self.as_slice()[10..11])
806 }
807 pub fn nth11(&self) -> ByteReader<'r> {
808 ByteReader::new_unchecked(&self.as_slice()[11..12])
809 }
810 pub fn nth12(&self) -> ByteReader<'r> {
811 ByteReader::new_unchecked(&self.as_slice()[12..13])
812 }
813 pub fn nth13(&self) -> ByteReader<'r> {
814 ByteReader::new_unchecked(&self.as_slice()[13..14])
815 }
816 pub fn nth14(&self) -> ByteReader<'r> {
817 ByteReader::new_unchecked(&self.as_slice()[14..15])
818 }
819 pub fn nth15(&self) -> ByteReader<'r> {
820 ByteReader::new_unchecked(&self.as_slice()[15..16])
821 }
822 pub fn raw_data(&self) -> &'r [u8] {
823 self.as_slice()
824 }
825}
826impl<'r> molecule::prelude::Reader<'r> for Uint128Reader<'r> {
827 type Entity = Uint128;
828 const NAME: &'static str = "Uint128Reader";
829 fn to_entity(&self) -> Self::Entity {
830 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
831 }
832 fn new_unchecked(slice: &'r [u8]) -> Self {
833 Uint128Reader(slice)
834 }
835 fn as_slice(&self) -> &'r [u8] {
836 self.0
837 }
838 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
839 use molecule::verification_error as ve;
840 let slice_len = slice.len();
841 if slice_len != Self::TOTAL_SIZE {
842 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
843 }
844 Ok(())
845 }
846}
847#[derive(Clone)]
848pub struct Uint128Builder(pub(crate) [Byte; 16]);
849impl ::core::fmt::Debug for Uint128Builder {
850 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
851 write!(f, "{}({:?})", Self::NAME, &self.0[..])
852 }
853}
854impl ::core::default::Default for Uint128Builder {
855 fn default() -> Self {
856 Uint128Builder([
857 Byte::default(),
858 Byte::default(),
859 Byte::default(),
860 Byte::default(),
861 Byte::default(),
862 Byte::default(),
863 Byte::default(),
864 Byte::default(),
865 Byte::default(),
866 Byte::default(),
867 Byte::default(),
868 Byte::default(),
869 Byte::default(),
870 Byte::default(),
871 Byte::default(),
872 Byte::default(),
873 ])
874 }
875}
876impl Uint128Builder {
877 pub const TOTAL_SIZE: usize = 16;
878 pub const ITEM_SIZE: usize = 1;
879 pub const ITEM_COUNT: usize = 16;
880 pub fn set<T>(mut self, v: T) -> Self
881 where
882 T: ::core::convert::Into<[Byte; 16]>,
883 {
884 self.0 = v.into();
885 self
886 }
887 pub fn nth0<T>(mut self, v: T) -> Self
888 where
889 T: ::core::convert::Into<Byte>,
890 {
891 self.0[0] = v.into();
892 self
893 }
894 pub fn nth1<T>(mut self, v: T) -> Self
895 where
896 T: ::core::convert::Into<Byte>,
897 {
898 self.0[1] = v.into();
899 self
900 }
901 pub fn nth2<T>(mut self, v: T) -> Self
902 where
903 T: ::core::convert::Into<Byte>,
904 {
905 self.0[2] = v.into();
906 self
907 }
908 pub fn nth3<T>(mut self, v: T) -> Self
909 where
910 T: ::core::convert::Into<Byte>,
911 {
912 self.0[3] = v.into();
913 self
914 }
915 pub fn nth4<T>(mut self, v: T) -> Self
916 where
917 T: ::core::convert::Into<Byte>,
918 {
919 self.0[4] = v.into();
920 self
921 }
922 pub fn nth5<T>(mut self, v: T) -> Self
923 where
924 T: ::core::convert::Into<Byte>,
925 {
926 self.0[5] = v.into();
927 self
928 }
929 pub fn nth6<T>(mut self, v: T) -> Self
930 where
931 T: ::core::convert::Into<Byte>,
932 {
933 self.0[6] = v.into();
934 self
935 }
936 pub fn nth7<T>(mut self, v: T) -> Self
937 where
938 T: ::core::convert::Into<Byte>,
939 {
940 self.0[7] = v.into();
941 self
942 }
943 pub fn nth8<T>(mut self, v: T) -> Self
944 where
945 T: ::core::convert::Into<Byte>,
946 {
947 self.0[8] = v.into();
948 self
949 }
950 pub fn nth9<T>(mut self, v: T) -> Self
951 where
952 T: ::core::convert::Into<Byte>,
953 {
954 self.0[9] = v.into();
955 self
956 }
957 pub fn nth10<T>(mut self, v: T) -> Self
958 where
959 T: ::core::convert::Into<Byte>,
960 {
961 self.0[10] = v.into();
962 self
963 }
964 pub fn nth11<T>(mut self, v: T) -> Self
965 where
966 T: ::core::convert::Into<Byte>,
967 {
968 self.0[11] = v.into();
969 self
970 }
971 pub fn nth12<T>(mut self, v: T) -> Self
972 where
973 T: ::core::convert::Into<Byte>,
974 {
975 self.0[12] = v.into();
976 self
977 }
978 pub fn nth13<T>(mut self, v: T) -> Self
979 where
980 T: ::core::convert::Into<Byte>,
981 {
982 self.0[13] = v.into();
983 self
984 }
985 pub fn nth14<T>(mut self, v: T) -> Self
986 where
987 T: ::core::convert::Into<Byte>,
988 {
989 self.0[14] = v.into();
990 self
991 }
992 pub fn nth15<T>(mut self, v: T) -> Self
993 where
994 T: ::core::convert::Into<Byte>,
995 {
996 self.0[15] = v.into();
997 self
998 }
999}
1000impl molecule::prelude::Builder for Uint128Builder {
1001 type Entity = Uint128;
1002 const NAME: &'static str = "Uint128Builder";
1003 fn expected_length(&self) -> usize {
1004 Self::TOTAL_SIZE
1005 }
1006 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1007 writer.write_all(self.0[0].as_slice())?;
1008 writer.write_all(self.0[1].as_slice())?;
1009 writer.write_all(self.0[2].as_slice())?;
1010 writer.write_all(self.0[3].as_slice())?;
1011 writer.write_all(self.0[4].as_slice())?;
1012 writer.write_all(self.0[5].as_slice())?;
1013 writer.write_all(self.0[6].as_slice())?;
1014 writer.write_all(self.0[7].as_slice())?;
1015 writer.write_all(self.0[8].as_slice())?;
1016 writer.write_all(self.0[9].as_slice())?;
1017 writer.write_all(self.0[10].as_slice())?;
1018 writer.write_all(self.0[11].as_slice())?;
1019 writer.write_all(self.0[12].as_slice())?;
1020 writer.write_all(self.0[13].as_slice())?;
1021 writer.write_all(self.0[14].as_slice())?;
1022 writer.write_all(self.0[15].as_slice())?;
1023 Ok(())
1024 }
1025 fn build(&self) -> Self::Entity {
1026 let mut inner = Vec::with_capacity(self.expected_length());
1027 self.write(&mut inner)
1028 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1029 Uint128::new_unchecked(inner.into())
1030 }
1031}
1032impl From<[Byte; 16usize]> for Uint128 {
1033 fn from(value: [Byte; 16usize]) -> Self {
1034 Self::new_builder().set(value).build()
1035 }
1036}
1037impl ::core::convert::TryFrom<&[Byte]> for Uint128 {
1038 type Error = ::core::array::TryFromSliceError;
1039 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
1040 Ok(Self::new_builder()
1041 .set(<&[Byte; 16usize]>::try_from(value)?.clone())
1042 .build())
1043 }
1044}
1045impl From<Uint128> for [Byte; 16usize] {
1046 #[track_caller]
1047 fn from(value: Uint128) -> Self {
1048 [
1049 value.nth0(),
1050 value.nth1(),
1051 value.nth2(),
1052 value.nth3(),
1053 value.nth4(),
1054 value.nth5(),
1055 value.nth6(),
1056 value.nth7(),
1057 value.nth8(),
1058 value.nth9(),
1059 value.nth10(),
1060 value.nth11(),
1061 value.nth12(),
1062 value.nth13(),
1063 value.nth14(),
1064 value.nth15(),
1065 ]
1066 }
1067}
1068impl From<[u8; 16usize]> for Uint128 {
1069 fn from(value: [u8; 16usize]) -> Self {
1070 Uint128Reader::new_unchecked(&value).to_entity()
1071 }
1072}
1073impl ::core::convert::TryFrom<&[u8]> for Uint128 {
1074 type Error = ::core::array::TryFromSliceError;
1075 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
1076 Ok(<[u8; 16usize]>::try_from(value)?.into())
1077 }
1078}
1079impl From<Uint128> for [u8; 16usize] {
1080 #[track_caller]
1081 fn from(value: Uint128) -> Self {
1082 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1083 }
1084}
1085impl<'a> From<Uint128Reader<'a>> for &'a [u8; 16usize] {
1086 #[track_caller]
1087 fn from(value: Uint128Reader<'a>) -> Self {
1088 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1089 }
1090}
1091impl<'a> From<&'a Uint128Reader<'a>> for &'a [u8; 16usize] {
1092 #[track_caller]
1093 fn from(value: &'a Uint128Reader<'a>) -> Self {
1094 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1095 }
1096}
1097#[derive(Clone)]
1098pub struct Byte32(molecule::bytes::Bytes);
1099impl ::core::fmt::LowerHex for Byte32 {
1100 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1101 use molecule::hex_string;
1102 if f.alternate() {
1103 write!(f, "0x")?;
1104 }
1105 write!(f, "{}", hex_string(self.as_slice()))
1106 }
1107}
1108impl ::core::fmt::Debug for Byte32 {
1109 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1110 write!(f, "{}({:#x})", Self::NAME, self)
1111 }
1112}
1113impl ::core::fmt::Display for Byte32 {
1114 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1115 use molecule::hex_string;
1116 let raw_data = hex_string(&self.raw_data());
1117 write!(f, "{}(0x{})", Self::NAME, raw_data)
1118 }
1119}
1120impl ::core::default::Default for Byte32 {
1121 fn default() -> Self {
1122 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1123 Byte32::new_unchecked(v)
1124 }
1125}
1126impl Byte32 {
1127 const DEFAULT_VALUE: [u8; 32] = [
1128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1129 0, 0,
1130 ];
1131 pub const TOTAL_SIZE: usize = 32;
1132 pub const ITEM_SIZE: usize = 1;
1133 pub const ITEM_COUNT: usize = 32;
1134 pub fn nth0(&self) -> Byte {
1135 Byte::new_unchecked(self.0.slice(0..1))
1136 }
1137 pub fn nth1(&self) -> Byte {
1138 Byte::new_unchecked(self.0.slice(1..2))
1139 }
1140 pub fn nth2(&self) -> Byte {
1141 Byte::new_unchecked(self.0.slice(2..3))
1142 }
1143 pub fn nth3(&self) -> Byte {
1144 Byte::new_unchecked(self.0.slice(3..4))
1145 }
1146 pub fn nth4(&self) -> Byte {
1147 Byte::new_unchecked(self.0.slice(4..5))
1148 }
1149 pub fn nth5(&self) -> Byte {
1150 Byte::new_unchecked(self.0.slice(5..6))
1151 }
1152 pub fn nth6(&self) -> Byte {
1153 Byte::new_unchecked(self.0.slice(6..7))
1154 }
1155 pub fn nth7(&self) -> Byte {
1156 Byte::new_unchecked(self.0.slice(7..8))
1157 }
1158 pub fn nth8(&self) -> Byte {
1159 Byte::new_unchecked(self.0.slice(8..9))
1160 }
1161 pub fn nth9(&self) -> Byte {
1162 Byte::new_unchecked(self.0.slice(9..10))
1163 }
1164 pub fn nth10(&self) -> Byte {
1165 Byte::new_unchecked(self.0.slice(10..11))
1166 }
1167 pub fn nth11(&self) -> Byte {
1168 Byte::new_unchecked(self.0.slice(11..12))
1169 }
1170 pub fn nth12(&self) -> Byte {
1171 Byte::new_unchecked(self.0.slice(12..13))
1172 }
1173 pub fn nth13(&self) -> Byte {
1174 Byte::new_unchecked(self.0.slice(13..14))
1175 }
1176 pub fn nth14(&self) -> Byte {
1177 Byte::new_unchecked(self.0.slice(14..15))
1178 }
1179 pub fn nth15(&self) -> Byte {
1180 Byte::new_unchecked(self.0.slice(15..16))
1181 }
1182 pub fn nth16(&self) -> Byte {
1183 Byte::new_unchecked(self.0.slice(16..17))
1184 }
1185 pub fn nth17(&self) -> Byte {
1186 Byte::new_unchecked(self.0.slice(17..18))
1187 }
1188 pub fn nth18(&self) -> Byte {
1189 Byte::new_unchecked(self.0.slice(18..19))
1190 }
1191 pub fn nth19(&self) -> Byte {
1192 Byte::new_unchecked(self.0.slice(19..20))
1193 }
1194 pub fn nth20(&self) -> Byte {
1195 Byte::new_unchecked(self.0.slice(20..21))
1196 }
1197 pub fn nth21(&self) -> Byte {
1198 Byte::new_unchecked(self.0.slice(21..22))
1199 }
1200 pub fn nth22(&self) -> Byte {
1201 Byte::new_unchecked(self.0.slice(22..23))
1202 }
1203 pub fn nth23(&self) -> Byte {
1204 Byte::new_unchecked(self.0.slice(23..24))
1205 }
1206 pub fn nth24(&self) -> Byte {
1207 Byte::new_unchecked(self.0.slice(24..25))
1208 }
1209 pub fn nth25(&self) -> Byte {
1210 Byte::new_unchecked(self.0.slice(25..26))
1211 }
1212 pub fn nth26(&self) -> Byte {
1213 Byte::new_unchecked(self.0.slice(26..27))
1214 }
1215 pub fn nth27(&self) -> Byte {
1216 Byte::new_unchecked(self.0.slice(27..28))
1217 }
1218 pub fn nth28(&self) -> Byte {
1219 Byte::new_unchecked(self.0.slice(28..29))
1220 }
1221 pub fn nth29(&self) -> Byte {
1222 Byte::new_unchecked(self.0.slice(29..30))
1223 }
1224 pub fn nth30(&self) -> Byte {
1225 Byte::new_unchecked(self.0.slice(30..31))
1226 }
1227 pub fn nth31(&self) -> Byte {
1228 Byte::new_unchecked(self.0.slice(31..32))
1229 }
1230 pub fn raw_data(&self) -> molecule::bytes::Bytes {
1231 self.as_bytes()
1232 }
1233 pub fn as_reader<'r>(&'r self) -> Byte32Reader<'r> {
1234 Byte32Reader::new_unchecked(self.as_slice())
1235 }
1236}
1237impl molecule::prelude::Entity for Byte32 {
1238 type Builder = Byte32Builder;
1239 const NAME: &'static str = "Byte32";
1240 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1241 Byte32(data)
1242 }
1243 fn as_bytes(&self) -> molecule::bytes::Bytes {
1244 self.0.clone()
1245 }
1246 fn as_slice(&self) -> &[u8] {
1247 &self.0[..]
1248 }
1249 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1250 Byte32Reader::from_slice(slice).map(|reader| reader.to_entity())
1251 }
1252 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
1253 Byte32Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
1254 }
1255 fn new_builder() -> Self::Builder {
1256 ::core::default::Default::default()
1257 }
1258 fn as_builder(self) -> Self::Builder {
1259 Self::new_builder().set([
1260 self.nth0(),
1261 self.nth1(),
1262 self.nth2(),
1263 self.nth3(),
1264 self.nth4(),
1265 self.nth5(),
1266 self.nth6(),
1267 self.nth7(),
1268 self.nth8(),
1269 self.nth9(),
1270 self.nth10(),
1271 self.nth11(),
1272 self.nth12(),
1273 self.nth13(),
1274 self.nth14(),
1275 self.nth15(),
1276 self.nth16(),
1277 self.nth17(),
1278 self.nth18(),
1279 self.nth19(),
1280 self.nth20(),
1281 self.nth21(),
1282 self.nth22(),
1283 self.nth23(),
1284 self.nth24(),
1285 self.nth25(),
1286 self.nth26(),
1287 self.nth27(),
1288 self.nth28(),
1289 self.nth29(),
1290 self.nth30(),
1291 self.nth31(),
1292 ])
1293 }
1294}
1295#[derive(Clone, Copy)]
1296pub struct Byte32Reader<'r>(&'r [u8]);
1297impl<'r> ::core::fmt::LowerHex for Byte32Reader<'r> {
1298 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1299 use molecule::hex_string;
1300 if f.alternate() {
1301 write!(f, "0x")?;
1302 }
1303 write!(f, "{}", hex_string(self.as_slice()))
1304 }
1305}
1306impl<'r> ::core::fmt::Debug for Byte32Reader<'r> {
1307 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1308 write!(f, "{}({:#x})", Self::NAME, self)
1309 }
1310}
1311impl<'r> ::core::fmt::Display for Byte32Reader<'r> {
1312 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1313 use molecule::hex_string;
1314 let raw_data = hex_string(&self.raw_data());
1315 write!(f, "{}(0x{})", Self::NAME, raw_data)
1316 }
1317}
1318impl<'r> Byte32Reader<'r> {
1319 pub const TOTAL_SIZE: usize = 32;
1320 pub const ITEM_SIZE: usize = 1;
1321 pub const ITEM_COUNT: usize = 32;
1322 pub fn nth0(&self) -> ByteReader<'r> {
1323 ByteReader::new_unchecked(&self.as_slice()[0..1])
1324 }
1325 pub fn nth1(&self) -> ByteReader<'r> {
1326 ByteReader::new_unchecked(&self.as_slice()[1..2])
1327 }
1328 pub fn nth2(&self) -> ByteReader<'r> {
1329 ByteReader::new_unchecked(&self.as_slice()[2..3])
1330 }
1331 pub fn nth3(&self) -> ByteReader<'r> {
1332 ByteReader::new_unchecked(&self.as_slice()[3..4])
1333 }
1334 pub fn nth4(&self) -> ByteReader<'r> {
1335 ByteReader::new_unchecked(&self.as_slice()[4..5])
1336 }
1337 pub fn nth5(&self) -> ByteReader<'r> {
1338 ByteReader::new_unchecked(&self.as_slice()[5..6])
1339 }
1340 pub fn nth6(&self) -> ByteReader<'r> {
1341 ByteReader::new_unchecked(&self.as_slice()[6..7])
1342 }
1343 pub fn nth7(&self) -> ByteReader<'r> {
1344 ByteReader::new_unchecked(&self.as_slice()[7..8])
1345 }
1346 pub fn nth8(&self) -> ByteReader<'r> {
1347 ByteReader::new_unchecked(&self.as_slice()[8..9])
1348 }
1349 pub fn nth9(&self) -> ByteReader<'r> {
1350 ByteReader::new_unchecked(&self.as_slice()[9..10])
1351 }
1352 pub fn nth10(&self) -> ByteReader<'r> {
1353 ByteReader::new_unchecked(&self.as_slice()[10..11])
1354 }
1355 pub fn nth11(&self) -> ByteReader<'r> {
1356 ByteReader::new_unchecked(&self.as_slice()[11..12])
1357 }
1358 pub fn nth12(&self) -> ByteReader<'r> {
1359 ByteReader::new_unchecked(&self.as_slice()[12..13])
1360 }
1361 pub fn nth13(&self) -> ByteReader<'r> {
1362 ByteReader::new_unchecked(&self.as_slice()[13..14])
1363 }
1364 pub fn nth14(&self) -> ByteReader<'r> {
1365 ByteReader::new_unchecked(&self.as_slice()[14..15])
1366 }
1367 pub fn nth15(&self) -> ByteReader<'r> {
1368 ByteReader::new_unchecked(&self.as_slice()[15..16])
1369 }
1370 pub fn nth16(&self) -> ByteReader<'r> {
1371 ByteReader::new_unchecked(&self.as_slice()[16..17])
1372 }
1373 pub fn nth17(&self) -> ByteReader<'r> {
1374 ByteReader::new_unchecked(&self.as_slice()[17..18])
1375 }
1376 pub fn nth18(&self) -> ByteReader<'r> {
1377 ByteReader::new_unchecked(&self.as_slice()[18..19])
1378 }
1379 pub fn nth19(&self) -> ByteReader<'r> {
1380 ByteReader::new_unchecked(&self.as_slice()[19..20])
1381 }
1382 pub fn nth20(&self) -> ByteReader<'r> {
1383 ByteReader::new_unchecked(&self.as_slice()[20..21])
1384 }
1385 pub fn nth21(&self) -> ByteReader<'r> {
1386 ByteReader::new_unchecked(&self.as_slice()[21..22])
1387 }
1388 pub fn nth22(&self) -> ByteReader<'r> {
1389 ByteReader::new_unchecked(&self.as_slice()[22..23])
1390 }
1391 pub fn nth23(&self) -> ByteReader<'r> {
1392 ByteReader::new_unchecked(&self.as_slice()[23..24])
1393 }
1394 pub fn nth24(&self) -> ByteReader<'r> {
1395 ByteReader::new_unchecked(&self.as_slice()[24..25])
1396 }
1397 pub fn nth25(&self) -> ByteReader<'r> {
1398 ByteReader::new_unchecked(&self.as_slice()[25..26])
1399 }
1400 pub fn nth26(&self) -> ByteReader<'r> {
1401 ByteReader::new_unchecked(&self.as_slice()[26..27])
1402 }
1403 pub fn nth27(&self) -> ByteReader<'r> {
1404 ByteReader::new_unchecked(&self.as_slice()[27..28])
1405 }
1406 pub fn nth28(&self) -> ByteReader<'r> {
1407 ByteReader::new_unchecked(&self.as_slice()[28..29])
1408 }
1409 pub fn nth29(&self) -> ByteReader<'r> {
1410 ByteReader::new_unchecked(&self.as_slice()[29..30])
1411 }
1412 pub fn nth30(&self) -> ByteReader<'r> {
1413 ByteReader::new_unchecked(&self.as_slice()[30..31])
1414 }
1415 pub fn nth31(&self) -> ByteReader<'r> {
1416 ByteReader::new_unchecked(&self.as_slice()[31..32])
1417 }
1418 pub fn raw_data(&self) -> &'r [u8] {
1419 self.as_slice()
1420 }
1421}
1422impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> {
1423 type Entity = Byte32;
1424 const NAME: &'static str = "Byte32Reader";
1425 fn to_entity(&self) -> Self::Entity {
1426 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
1427 }
1428 fn new_unchecked(slice: &'r [u8]) -> Self {
1429 Byte32Reader(slice)
1430 }
1431 fn as_slice(&self) -> &'r [u8] {
1432 self.0
1433 }
1434 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
1435 use molecule::verification_error as ve;
1436 let slice_len = slice.len();
1437 if slice_len != Self::TOTAL_SIZE {
1438 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
1439 }
1440 Ok(())
1441 }
1442}
1443#[derive(Clone)]
1444pub struct Byte32Builder(pub(crate) [Byte; 32]);
1445impl ::core::fmt::Debug for Byte32Builder {
1446 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1447 write!(f, "{}({:?})", Self::NAME, &self.0[..])
1448 }
1449}
1450impl ::core::default::Default for Byte32Builder {
1451 fn default() -> Self {
1452 Byte32Builder([
1453 Byte::default(),
1454 Byte::default(),
1455 Byte::default(),
1456 Byte::default(),
1457 Byte::default(),
1458 Byte::default(),
1459 Byte::default(),
1460 Byte::default(),
1461 Byte::default(),
1462 Byte::default(),
1463 Byte::default(),
1464 Byte::default(),
1465 Byte::default(),
1466 Byte::default(),
1467 Byte::default(),
1468 Byte::default(),
1469 Byte::default(),
1470 Byte::default(),
1471 Byte::default(),
1472 Byte::default(),
1473 Byte::default(),
1474 Byte::default(),
1475 Byte::default(),
1476 Byte::default(),
1477 Byte::default(),
1478 Byte::default(),
1479 Byte::default(),
1480 Byte::default(),
1481 Byte::default(),
1482 Byte::default(),
1483 Byte::default(),
1484 Byte::default(),
1485 ])
1486 }
1487}
1488impl Byte32Builder {
1489 pub const TOTAL_SIZE: usize = 32;
1490 pub const ITEM_SIZE: usize = 1;
1491 pub const ITEM_COUNT: usize = 32;
1492 pub fn set<T>(mut self, v: T) -> Self
1493 where
1494 T: ::core::convert::Into<[Byte; 32]>,
1495 {
1496 self.0 = v.into();
1497 self
1498 }
1499 pub fn nth0<T>(mut self, v: T) -> Self
1500 where
1501 T: ::core::convert::Into<Byte>,
1502 {
1503 self.0[0] = v.into();
1504 self
1505 }
1506 pub fn nth1<T>(mut self, v: T) -> Self
1507 where
1508 T: ::core::convert::Into<Byte>,
1509 {
1510 self.0[1] = v.into();
1511 self
1512 }
1513 pub fn nth2<T>(mut self, v: T) -> Self
1514 where
1515 T: ::core::convert::Into<Byte>,
1516 {
1517 self.0[2] = v.into();
1518 self
1519 }
1520 pub fn nth3<T>(mut self, v: T) -> Self
1521 where
1522 T: ::core::convert::Into<Byte>,
1523 {
1524 self.0[3] = v.into();
1525 self
1526 }
1527 pub fn nth4<T>(mut self, v: T) -> Self
1528 where
1529 T: ::core::convert::Into<Byte>,
1530 {
1531 self.0[4] = v.into();
1532 self
1533 }
1534 pub fn nth5<T>(mut self, v: T) -> Self
1535 where
1536 T: ::core::convert::Into<Byte>,
1537 {
1538 self.0[5] = v.into();
1539 self
1540 }
1541 pub fn nth6<T>(mut self, v: T) -> Self
1542 where
1543 T: ::core::convert::Into<Byte>,
1544 {
1545 self.0[6] = v.into();
1546 self
1547 }
1548 pub fn nth7<T>(mut self, v: T) -> Self
1549 where
1550 T: ::core::convert::Into<Byte>,
1551 {
1552 self.0[7] = v.into();
1553 self
1554 }
1555 pub fn nth8<T>(mut self, v: T) -> Self
1556 where
1557 T: ::core::convert::Into<Byte>,
1558 {
1559 self.0[8] = v.into();
1560 self
1561 }
1562 pub fn nth9<T>(mut self, v: T) -> Self
1563 where
1564 T: ::core::convert::Into<Byte>,
1565 {
1566 self.0[9] = v.into();
1567 self
1568 }
1569 pub fn nth10<T>(mut self, v: T) -> Self
1570 where
1571 T: ::core::convert::Into<Byte>,
1572 {
1573 self.0[10] = v.into();
1574 self
1575 }
1576 pub fn nth11<T>(mut self, v: T) -> Self
1577 where
1578 T: ::core::convert::Into<Byte>,
1579 {
1580 self.0[11] = v.into();
1581 self
1582 }
1583 pub fn nth12<T>(mut self, v: T) -> Self
1584 where
1585 T: ::core::convert::Into<Byte>,
1586 {
1587 self.0[12] = v.into();
1588 self
1589 }
1590 pub fn nth13<T>(mut self, v: T) -> Self
1591 where
1592 T: ::core::convert::Into<Byte>,
1593 {
1594 self.0[13] = v.into();
1595 self
1596 }
1597 pub fn nth14<T>(mut self, v: T) -> Self
1598 where
1599 T: ::core::convert::Into<Byte>,
1600 {
1601 self.0[14] = v.into();
1602 self
1603 }
1604 pub fn nth15<T>(mut self, v: T) -> Self
1605 where
1606 T: ::core::convert::Into<Byte>,
1607 {
1608 self.0[15] = v.into();
1609 self
1610 }
1611 pub fn nth16<T>(mut self, v: T) -> Self
1612 where
1613 T: ::core::convert::Into<Byte>,
1614 {
1615 self.0[16] = v.into();
1616 self
1617 }
1618 pub fn nth17<T>(mut self, v: T) -> Self
1619 where
1620 T: ::core::convert::Into<Byte>,
1621 {
1622 self.0[17] = v.into();
1623 self
1624 }
1625 pub fn nth18<T>(mut self, v: T) -> Self
1626 where
1627 T: ::core::convert::Into<Byte>,
1628 {
1629 self.0[18] = v.into();
1630 self
1631 }
1632 pub fn nth19<T>(mut self, v: T) -> Self
1633 where
1634 T: ::core::convert::Into<Byte>,
1635 {
1636 self.0[19] = v.into();
1637 self
1638 }
1639 pub fn nth20<T>(mut self, v: T) -> Self
1640 where
1641 T: ::core::convert::Into<Byte>,
1642 {
1643 self.0[20] = v.into();
1644 self
1645 }
1646 pub fn nth21<T>(mut self, v: T) -> Self
1647 where
1648 T: ::core::convert::Into<Byte>,
1649 {
1650 self.0[21] = v.into();
1651 self
1652 }
1653 pub fn nth22<T>(mut self, v: T) -> Self
1654 where
1655 T: ::core::convert::Into<Byte>,
1656 {
1657 self.0[22] = v.into();
1658 self
1659 }
1660 pub fn nth23<T>(mut self, v: T) -> Self
1661 where
1662 T: ::core::convert::Into<Byte>,
1663 {
1664 self.0[23] = v.into();
1665 self
1666 }
1667 pub fn nth24<T>(mut self, v: T) -> Self
1668 where
1669 T: ::core::convert::Into<Byte>,
1670 {
1671 self.0[24] = v.into();
1672 self
1673 }
1674 pub fn nth25<T>(mut self, v: T) -> Self
1675 where
1676 T: ::core::convert::Into<Byte>,
1677 {
1678 self.0[25] = v.into();
1679 self
1680 }
1681 pub fn nth26<T>(mut self, v: T) -> Self
1682 where
1683 T: ::core::convert::Into<Byte>,
1684 {
1685 self.0[26] = v.into();
1686 self
1687 }
1688 pub fn nth27<T>(mut self, v: T) -> Self
1689 where
1690 T: ::core::convert::Into<Byte>,
1691 {
1692 self.0[27] = v.into();
1693 self
1694 }
1695 pub fn nth28<T>(mut self, v: T) -> Self
1696 where
1697 T: ::core::convert::Into<Byte>,
1698 {
1699 self.0[28] = v.into();
1700 self
1701 }
1702 pub fn nth29<T>(mut self, v: T) -> Self
1703 where
1704 T: ::core::convert::Into<Byte>,
1705 {
1706 self.0[29] = v.into();
1707 self
1708 }
1709 pub fn nth30<T>(mut self, v: T) -> Self
1710 where
1711 T: ::core::convert::Into<Byte>,
1712 {
1713 self.0[30] = v.into();
1714 self
1715 }
1716 pub fn nth31<T>(mut self, v: T) -> Self
1717 where
1718 T: ::core::convert::Into<Byte>,
1719 {
1720 self.0[31] = v.into();
1721 self
1722 }
1723}
1724impl molecule::prelude::Builder for Byte32Builder {
1725 type Entity = Byte32;
1726 const NAME: &'static str = "Byte32Builder";
1727 fn expected_length(&self) -> usize {
1728 Self::TOTAL_SIZE
1729 }
1730 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
1731 writer.write_all(self.0[0].as_slice())?;
1732 writer.write_all(self.0[1].as_slice())?;
1733 writer.write_all(self.0[2].as_slice())?;
1734 writer.write_all(self.0[3].as_slice())?;
1735 writer.write_all(self.0[4].as_slice())?;
1736 writer.write_all(self.0[5].as_slice())?;
1737 writer.write_all(self.0[6].as_slice())?;
1738 writer.write_all(self.0[7].as_slice())?;
1739 writer.write_all(self.0[8].as_slice())?;
1740 writer.write_all(self.0[9].as_slice())?;
1741 writer.write_all(self.0[10].as_slice())?;
1742 writer.write_all(self.0[11].as_slice())?;
1743 writer.write_all(self.0[12].as_slice())?;
1744 writer.write_all(self.0[13].as_slice())?;
1745 writer.write_all(self.0[14].as_slice())?;
1746 writer.write_all(self.0[15].as_slice())?;
1747 writer.write_all(self.0[16].as_slice())?;
1748 writer.write_all(self.0[17].as_slice())?;
1749 writer.write_all(self.0[18].as_slice())?;
1750 writer.write_all(self.0[19].as_slice())?;
1751 writer.write_all(self.0[20].as_slice())?;
1752 writer.write_all(self.0[21].as_slice())?;
1753 writer.write_all(self.0[22].as_slice())?;
1754 writer.write_all(self.0[23].as_slice())?;
1755 writer.write_all(self.0[24].as_slice())?;
1756 writer.write_all(self.0[25].as_slice())?;
1757 writer.write_all(self.0[26].as_slice())?;
1758 writer.write_all(self.0[27].as_slice())?;
1759 writer.write_all(self.0[28].as_slice())?;
1760 writer.write_all(self.0[29].as_slice())?;
1761 writer.write_all(self.0[30].as_slice())?;
1762 writer.write_all(self.0[31].as_slice())?;
1763 Ok(())
1764 }
1765 fn build(&self) -> Self::Entity {
1766 let mut inner = Vec::with_capacity(self.expected_length());
1767 self.write(&mut inner)
1768 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
1769 Byte32::new_unchecked(inner.into())
1770 }
1771}
1772impl From<[Byte; 32usize]> for Byte32 {
1773 fn from(value: [Byte; 32usize]) -> Self {
1774 Self::new_builder().set(value).build()
1775 }
1776}
1777impl ::core::convert::TryFrom<&[Byte]> for Byte32 {
1778 type Error = ::core::array::TryFromSliceError;
1779 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
1780 Ok(Self::new_builder()
1781 .set(<&[Byte; 32usize]>::try_from(value)?.clone())
1782 .build())
1783 }
1784}
1785impl From<Byte32> for [Byte; 32usize] {
1786 #[track_caller]
1787 fn from(value: Byte32) -> Self {
1788 [
1789 value.nth0(),
1790 value.nth1(),
1791 value.nth2(),
1792 value.nth3(),
1793 value.nth4(),
1794 value.nth5(),
1795 value.nth6(),
1796 value.nth7(),
1797 value.nth8(),
1798 value.nth9(),
1799 value.nth10(),
1800 value.nth11(),
1801 value.nth12(),
1802 value.nth13(),
1803 value.nth14(),
1804 value.nth15(),
1805 value.nth16(),
1806 value.nth17(),
1807 value.nth18(),
1808 value.nth19(),
1809 value.nth20(),
1810 value.nth21(),
1811 value.nth22(),
1812 value.nth23(),
1813 value.nth24(),
1814 value.nth25(),
1815 value.nth26(),
1816 value.nth27(),
1817 value.nth28(),
1818 value.nth29(),
1819 value.nth30(),
1820 value.nth31(),
1821 ]
1822 }
1823}
1824impl From<[u8; 32usize]> for Byte32 {
1825 fn from(value: [u8; 32usize]) -> Self {
1826 Byte32Reader::new_unchecked(&value).to_entity()
1827 }
1828}
1829impl ::core::convert::TryFrom<&[u8]> for Byte32 {
1830 type Error = ::core::array::TryFromSliceError;
1831 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
1832 Ok(<[u8; 32usize]>::try_from(value)?.into())
1833 }
1834}
1835impl From<Byte32> for [u8; 32usize] {
1836 #[track_caller]
1837 fn from(value: Byte32) -> Self {
1838 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1839 }
1840}
1841impl<'a> From<Byte32Reader<'a>> for &'a [u8; 32usize] {
1842 #[track_caller]
1843 fn from(value: Byte32Reader<'a>) -> Self {
1844 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1845 }
1846}
1847impl<'a> From<&'a Byte32Reader<'a>> for &'a [u8; 32usize] {
1848 #[track_caller]
1849 fn from(value: &'a Byte32Reader<'a>) -> Self {
1850 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
1851 }
1852}
1853#[derive(Clone)]
1854pub struct Uint256(molecule::bytes::Bytes);
1855impl ::core::fmt::LowerHex for Uint256 {
1856 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1857 use molecule::hex_string;
1858 if f.alternate() {
1859 write!(f, "0x")?;
1860 }
1861 write!(f, "{}", hex_string(self.as_slice()))
1862 }
1863}
1864impl ::core::fmt::Debug for Uint256 {
1865 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1866 write!(f, "{}({:#x})", Self::NAME, self)
1867 }
1868}
1869impl ::core::fmt::Display for Uint256 {
1870 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
1871 use molecule::hex_string;
1872 let raw_data = hex_string(&self.raw_data());
1873 write!(f, "{}(0x{})", Self::NAME, raw_data)
1874 }
1875}
1876impl ::core::default::Default for Uint256 {
1877 fn default() -> Self {
1878 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
1879 Uint256::new_unchecked(v)
1880 }
1881}
1882impl Uint256 {
1883 const DEFAULT_VALUE: [u8; 32] = [
1884 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1885 0, 0,
1886 ];
1887 pub const TOTAL_SIZE: usize = 32;
1888 pub const ITEM_SIZE: usize = 1;
1889 pub const ITEM_COUNT: usize = 32;
1890 pub fn nth0(&self) -> Byte {
1891 Byte::new_unchecked(self.0.slice(0..1))
1892 }
1893 pub fn nth1(&self) -> Byte {
1894 Byte::new_unchecked(self.0.slice(1..2))
1895 }
1896 pub fn nth2(&self) -> Byte {
1897 Byte::new_unchecked(self.0.slice(2..3))
1898 }
1899 pub fn nth3(&self) -> Byte {
1900 Byte::new_unchecked(self.0.slice(3..4))
1901 }
1902 pub fn nth4(&self) -> Byte {
1903 Byte::new_unchecked(self.0.slice(4..5))
1904 }
1905 pub fn nth5(&self) -> Byte {
1906 Byte::new_unchecked(self.0.slice(5..6))
1907 }
1908 pub fn nth6(&self) -> Byte {
1909 Byte::new_unchecked(self.0.slice(6..7))
1910 }
1911 pub fn nth7(&self) -> Byte {
1912 Byte::new_unchecked(self.0.slice(7..8))
1913 }
1914 pub fn nth8(&self) -> Byte {
1915 Byte::new_unchecked(self.0.slice(8..9))
1916 }
1917 pub fn nth9(&self) -> Byte {
1918 Byte::new_unchecked(self.0.slice(9..10))
1919 }
1920 pub fn nth10(&self) -> Byte {
1921 Byte::new_unchecked(self.0.slice(10..11))
1922 }
1923 pub fn nth11(&self) -> Byte {
1924 Byte::new_unchecked(self.0.slice(11..12))
1925 }
1926 pub fn nth12(&self) -> Byte {
1927 Byte::new_unchecked(self.0.slice(12..13))
1928 }
1929 pub fn nth13(&self) -> Byte {
1930 Byte::new_unchecked(self.0.slice(13..14))
1931 }
1932 pub fn nth14(&self) -> Byte {
1933 Byte::new_unchecked(self.0.slice(14..15))
1934 }
1935 pub fn nth15(&self) -> Byte {
1936 Byte::new_unchecked(self.0.slice(15..16))
1937 }
1938 pub fn nth16(&self) -> Byte {
1939 Byte::new_unchecked(self.0.slice(16..17))
1940 }
1941 pub fn nth17(&self) -> Byte {
1942 Byte::new_unchecked(self.0.slice(17..18))
1943 }
1944 pub fn nth18(&self) -> Byte {
1945 Byte::new_unchecked(self.0.slice(18..19))
1946 }
1947 pub fn nth19(&self) -> Byte {
1948 Byte::new_unchecked(self.0.slice(19..20))
1949 }
1950 pub fn nth20(&self) -> Byte {
1951 Byte::new_unchecked(self.0.slice(20..21))
1952 }
1953 pub fn nth21(&self) -> Byte {
1954 Byte::new_unchecked(self.0.slice(21..22))
1955 }
1956 pub fn nth22(&self) -> Byte {
1957 Byte::new_unchecked(self.0.slice(22..23))
1958 }
1959 pub fn nth23(&self) -> Byte {
1960 Byte::new_unchecked(self.0.slice(23..24))
1961 }
1962 pub fn nth24(&self) -> Byte {
1963 Byte::new_unchecked(self.0.slice(24..25))
1964 }
1965 pub fn nth25(&self) -> Byte {
1966 Byte::new_unchecked(self.0.slice(25..26))
1967 }
1968 pub fn nth26(&self) -> Byte {
1969 Byte::new_unchecked(self.0.slice(26..27))
1970 }
1971 pub fn nth27(&self) -> Byte {
1972 Byte::new_unchecked(self.0.slice(27..28))
1973 }
1974 pub fn nth28(&self) -> Byte {
1975 Byte::new_unchecked(self.0.slice(28..29))
1976 }
1977 pub fn nth29(&self) -> Byte {
1978 Byte::new_unchecked(self.0.slice(29..30))
1979 }
1980 pub fn nth30(&self) -> Byte {
1981 Byte::new_unchecked(self.0.slice(30..31))
1982 }
1983 pub fn nth31(&self) -> Byte {
1984 Byte::new_unchecked(self.0.slice(31..32))
1985 }
1986 pub fn raw_data(&self) -> molecule::bytes::Bytes {
1987 self.as_bytes()
1988 }
1989 pub fn as_reader<'r>(&'r self) -> Uint256Reader<'r> {
1990 Uint256Reader::new_unchecked(self.as_slice())
1991 }
1992}
1993impl molecule::prelude::Entity for Uint256 {
1994 type Builder = Uint256Builder;
1995 const NAME: &'static str = "Uint256";
1996 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
1997 Uint256(data)
1998 }
1999 fn as_bytes(&self) -> molecule::bytes::Bytes {
2000 self.0.clone()
2001 }
2002 fn as_slice(&self) -> &[u8] {
2003 &self.0[..]
2004 }
2005 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2006 Uint256Reader::from_slice(slice).map(|reader| reader.to_entity())
2007 }
2008 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2009 Uint256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2010 }
2011 fn new_builder() -> Self::Builder {
2012 ::core::default::Default::default()
2013 }
2014 fn as_builder(self) -> Self::Builder {
2015 Self::new_builder().set([
2016 self.nth0(),
2017 self.nth1(),
2018 self.nth2(),
2019 self.nth3(),
2020 self.nth4(),
2021 self.nth5(),
2022 self.nth6(),
2023 self.nth7(),
2024 self.nth8(),
2025 self.nth9(),
2026 self.nth10(),
2027 self.nth11(),
2028 self.nth12(),
2029 self.nth13(),
2030 self.nth14(),
2031 self.nth15(),
2032 self.nth16(),
2033 self.nth17(),
2034 self.nth18(),
2035 self.nth19(),
2036 self.nth20(),
2037 self.nth21(),
2038 self.nth22(),
2039 self.nth23(),
2040 self.nth24(),
2041 self.nth25(),
2042 self.nth26(),
2043 self.nth27(),
2044 self.nth28(),
2045 self.nth29(),
2046 self.nth30(),
2047 self.nth31(),
2048 ])
2049 }
2050}
2051#[derive(Clone, Copy)]
2052pub struct Uint256Reader<'r>(&'r [u8]);
2053impl<'r> ::core::fmt::LowerHex for Uint256Reader<'r> {
2054 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2055 use molecule::hex_string;
2056 if f.alternate() {
2057 write!(f, "0x")?;
2058 }
2059 write!(f, "{}", hex_string(self.as_slice()))
2060 }
2061}
2062impl<'r> ::core::fmt::Debug for Uint256Reader<'r> {
2063 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2064 write!(f, "{}({:#x})", Self::NAME, self)
2065 }
2066}
2067impl<'r> ::core::fmt::Display for Uint256Reader<'r> {
2068 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2069 use molecule::hex_string;
2070 let raw_data = hex_string(&self.raw_data());
2071 write!(f, "{}(0x{})", Self::NAME, raw_data)
2072 }
2073}
2074impl<'r> Uint256Reader<'r> {
2075 pub const TOTAL_SIZE: usize = 32;
2076 pub const ITEM_SIZE: usize = 1;
2077 pub const ITEM_COUNT: usize = 32;
2078 pub fn nth0(&self) -> ByteReader<'r> {
2079 ByteReader::new_unchecked(&self.as_slice()[0..1])
2080 }
2081 pub fn nth1(&self) -> ByteReader<'r> {
2082 ByteReader::new_unchecked(&self.as_slice()[1..2])
2083 }
2084 pub fn nth2(&self) -> ByteReader<'r> {
2085 ByteReader::new_unchecked(&self.as_slice()[2..3])
2086 }
2087 pub fn nth3(&self) -> ByteReader<'r> {
2088 ByteReader::new_unchecked(&self.as_slice()[3..4])
2089 }
2090 pub fn nth4(&self) -> ByteReader<'r> {
2091 ByteReader::new_unchecked(&self.as_slice()[4..5])
2092 }
2093 pub fn nth5(&self) -> ByteReader<'r> {
2094 ByteReader::new_unchecked(&self.as_slice()[5..6])
2095 }
2096 pub fn nth6(&self) -> ByteReader<'r> {
2097 ByteReader::new_unchecked(&self.as_slice()[6..7])
2098 }
2099 pub fn nth7(&self) -> ByteReader<'r> {
2100 ByteReader::new_unchecked(&self.as_slice()[7..8])
2101 }
2102 pub fn nth8(&self) -> ByteReader<'r> {
2103 ByteReader::new_unchecked(&self.as_slice()[8..9])
2104 }
2105 pub fn nth9(&self) -> ByteReader<'r> {
2106 ByteReader::new_unchecked(&self.as_slice()[9..10])
2107 }
2108 pub fn nth10(&self) -> ByteReader<'r> {
2109 ByteReader::new_unchecked(&self.as_slice()[10..11])
2110 }
2111 pub fn nth11(&self) -> ByteReader<'r> {
2112 ByteReader::new_unchecked(&self.as_slice()[11..12])
2113 }
2114 pub fn nth12(&self) -> ByteReader<'r> {
2115 ByteReader::new_unchecked(&self.as_slice()[12..13])
2116 }
2117 pub fn nth13(&self) -> ByteReader<'r> {
2118 ByteReader::new_unchecked(&self.as_slice()[13..14])
2119 }
2120 pub fn nth14(&self) -> ByteReader<'r> {
2121 ByteReader::new_unchecked(&self.as_slice()[14..15])
2122 }
2123 pub fn nth15(&self) -> ByteReader<'r> {
2124 ByteReader::new_unchecked(&self.as_slice()[15..16])
2125 }
2126 pub fn nth16(&self) -> ByteReader<'r> {
2127 ByteReader::new_unchecked(&self.as_slice()[16..17])
2128 }
2129 pub fn nth17(&self) -> ByteReader<'r> {
2130 ByteReader::new_unchecked(&self.as_slice()[17..18])
2131 }
2132 pub fn nth18(&self) -> ByteReader<'r> {
2133 ByteReader::new_unchecked(&self.as_slice()[18..19])
2134 }
2135 pub fn nth19(&self) -> ByteReader<'r> {
2136 ByteReader::new_unchecked(&self.as_slice()[19..20])
2137 }
2138 pub fn nth20(&self) -> ByteReader<'r> {
2139 ByteReader::new_unchecked(&self.as_slice()[20..21])
2140 }
2141 pub fn nth21(&self) -> ByteReader<'r> {
2142 ByteReader::new_unchecked(&self.as_slice()[21..22])
2143 }
2144 pub fn nth22(&self) -> ByteReader<'r> {
2145 ByteReader::new_unchecked(&self.as_slice()[22..23])
2146 }
2147 pub fn nth23(&self) -> ByteReader<'r> {
2148 ByteReader::new_unchecked(&self.as_slice()[23..24])
2149 }
2150 pub fn nth24(&self) -> ByteReader<'r> {
2151 ByteReader::new_unchecked(&self.as_slice()[24..25])
2152 }
2153 pub fn nth25(&self) -> ByteReader<'r> {
2154 ByteReader::new_unchecked(&self.as_slice()[25..26])
2155 }
2156 pub fn nth26(&self) -> ByteReader<'r> {
2157 ByteReader::new_unchecked(&self.as_slice()[26..27])
2158 }
2159 pub fn nth27(&self) -> ByteReader<'r> {
2160 ByteReader::new_unchecked(&self.as_slice()[27..28])
2161 }
2162 pub fn nth28(&self) -> ByteReader<'r> {
2163 ByteReader::new_unchecked(&self.as_slice()[28..29])
2164 }
2165 pub fn nth29(&self) -> ByteReader<'r> {
2166 ByteReader::new_unchecked(&self.as_slice()[29..30])
2167 }
2168 pub fn nth30(&self) -> ByteReader<'r> {
2169 ByteReader::new_unchecked(&self.as_slice()[30..31])
2170 }
2171 pub fn nth31(&self) -> ByteReader<'r> {
2172 ByteReader::new_unchecked(&self.as_slice()[31..32])
2173 }
2174 pub fn raw_data(&self) -> &'r [u8] {
2175 self.as_slice()
2176 }
2177}
2178impl<'r> molecule::prelude::Reader<'r> for Uint256Reader<'r> {
2179 type Entity = Uint256;
2180 const NAME: &'static str = "Uint256Reader";
2181 fn to_entity(&self) -> Self::Entity {
2182 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2183 }
2184 fn new_unchecked(slice: &'r [u8]) -> Self {
2185 Uint256Reader(slice)
2186 }
2187 fn as_slice(&self) -> &'r [u8] {
2188 self.0
2189 }
2190 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2191 use molecule::verification_error as ve;
2192 let slice_len = slice.len();
2193 if slice_len != Self::TOTAL_SIZE {
2194 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
2195 }
2196 Ok(())
2197 }
2198}
2199#[derive(Clone)]
2200pub struct Uint256Builder(pub(crate) [Byte; 32]);
2201impl ::core::fmt::Debug for Uint256Builder {
2202 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2203 write!(f, "{}({:?})", Self::NAME, &self.0[..])
2204 }
2205}
2206impl ::core::default::Default for Uint256Builder {
2207 fn default() -> Self {
2208 Uint256Builder([
2209 Byte::default(),
2210 Byte::default(),
2211 Byte::default(),
2212 Byte::default(),
2213 Byte::default(),
2214 Byte::default(),
2215 Byte::default(),
2216 Byte::default(),
2217 Byte::default(),
2218 Byte::default(),
2219 Byte::default(),
2220 Byte::default(),
2221 Byte::default(),
2222 Byte::default(),
2223 Byte::default(),
2224 Byte::default(),
2225 Byte::default(),
2226 Byte::default(),
2227 Byte::default(),
2228 Byte::default(),
2229 Byte::default(),
2230 Byte::default(),
2231 Byte::default(),
2232 Byte::default(),
2233 Byte::default(),
2234 Byte::default(),
2235 Byte::default(),
2236 Byte::default(),
2237 Byte::default(),
2238 Byte::default(),
2239 Byte::default(),
2240 Byte::default(),
2241 ])
2242 }
2243}
2244impl Uint256Builder {
2245 pub const TOTAL_SIZE: usize = 32;
2246 pub const ITEM_SIZE: usize = 1;
2247 pub const ITEM_COUNT: usize = 32;
2248 pub fn set<T>(mut self, v: T) -> Self
2249 where
2250 T: ::core::convert::Into<[Byte; 32]>,
2251 {
2252 self.0 = v.into();
2253 self
2254 }
2255 pub fn nth0<T>(mut self, v: T) -> Self
2256 where
2257 T: ::core::convert::Into<Byte>,
2258 {
2259 self.0[0] = v.into();
2260 self
2261 }
2262 pub fn nth1<T>(mut self, v: T) -> Self
2263 where
2264 T: ::core::convert::Into<Byte>,
2265 {
2266 self.0[1] = v.into();
2267 self
2268 }
2269 pub fn nth2<T>(mut self, v: T) -> Self
2270 where
2271 T: ::core::convert::Into<Byte>,
2272 {
2273 self.0[2] = v.into();
2274 self
2275 }
2276 pub fn nth3<T>(mut self, v: T) -> Self
2277 where
2278 T: ::core::convert::Into<Byte>,
2279 {
2280 self.0[3] = v.into();
2281 self
2282 }
2283 pub fn nth4<T>(mut self, v: T) -> Self
2284 where
2285 T: ::core::convert::Into<Byte>,
2286 {
2287 self.0[4] = v.into();
2288 self
2289 }
2290 pub fn nth5<T>(mut self, v: T) -> Self
2291 where
2292 T: ::core::convert::Into<Byte>,
2293 {
2294 self.0[5] = v.into();
2295 self
2296 }
2297 pub fn nth6<T>(mut self, v: T) -> Self
2298 where
2299 T: ::core::convert::Into<Byte>,
2300 {
2301 self.0[6] = v.into();
2302 self
2303 }
2304 pub fn nth7<T>(mut self, v: T) -> Self
2305 where
2306 T: ::core::convert::Into<Byte>,
2307 {
2308 self.0[7] = v.into();
2309 self
2310 }
2311 pub fn nth8<T>(mut self, v: T) -> Self
2312 where
2313 T: ::core::convert::Into<Byte>,
2314 {
2315 self.0[8] = v.into();
2316 self
2317 }
2318 pub fn nth9<T>(mut self, v: T) -> Self
2319 where
2320 T: ::core::convert::Into<Byte>,
2321 {
2322 self.0[9] = v.into();
2323 self
2324 }
2325 pub fn nth10<T>(mut self, v: T) -> Self
2326 where
2327 T: ::core::convert::Into<Byte>,
2328 {
2329 self.0[10] = v.into();
2330 self
2331 }
2332 pub fn nth11<T>(mut self, v: T) -> Self
2333 where
2334 T: ::core::convert::Into<Byte>,
2335 {
2336 self.0[11] = v.into();
2337 self
2338 }
2339 pub fn nth12<T>(mut self, v: T) -> Self
2340 where
2341 T: ::core::convert::Into<Byte>,
2342 {
2343 self.0[12] = v.into();
2344 self
2345 }
2346 pub fn nth13<T>(mut self, v: T) -> Self
2347 where
2348 T: ::core::convert::Into<Byte>,
2349 {
2350 self.0[13] = v.into();
2351 self
2352 }
2353 pub fn nth14<T>(mut self, v: T) -> Self
2354 where
2355 T: ::core::convert::Into<Byte>,
2356 {
2357 self.0[14] = v.into();
2358 self
2359 }
2360 pub fn nth15<T>(mut self, v: T) -> Self
2361 where
2362 T: ::core::convert::Into<Byte>,
2363 {
2364 self.0[15] = v.into();
2365 self
2366 }
2367 pub fn nth16<T>(mut self, v: T) -> Self
2368 where
2369 T: ::core::convert::Into<Byte>,
2370 {
2371 self.0[16] = v.into();
2372 self
2373 }
2374 pub fn nth17<T>(mut self, v: T) -> Self
2375 where
2376 T: ::core::convert::Into<Byte>,
2377 {
2378 self.0[17] = v.into();
2379 self
2380 }
2381 pub fn nth18<T>(mut self, v: T) -> Self
2382 where
2383 T: ::core::convert::Into<Byte>,
2384 {
2385 self.0[18] = v.into();
2386 self
2387 }
2388 pub fn nth19<T>(mut self, v: T) -> Self
2389 where
2390 T: ::core::convert::Into<Byte>,
2391 {
2392 self.0[19] = v.into();
2393 self
2394 }
2395 pub fn nth20<T>(mut self, v: T) -> Self
2396 where
2397 T: ::core::convert::Into<Byte>,
2398 {
2399 self.0[20] = v.into();
2400 self
2401 }
2402 pub fn nth21<T>(mut self, v: T) -> Self
2403 where
2404 T: ::core::convert::Into<Byte>,
2405 {
2406 self.0[21] = v.into();
2407 self
2408 }
2409 pub fn nth22<T>(mut self, v: T) -> Self
2410 where
2411 T: ::core::convert::Into<Byte>,
2412 {
2413 self.0[22] = v.into();
2414 self
2415 }
2416 pub fn nth23<T>(mut self, v: T) -> Self
2417 where
2418 T: ::core::convert::Into<Byte>,
2419 {
2420 self.0[23] = v.into();
2421 self
2422 }
2423 pub fn nth24<T>(mut self, v: T) -> Self
2424 where
2425 T: ::core::convert::Into<Byte>,
2426 {
2427 self.0[24] = v.into();
2428 self
2429 }
2430 pub fn nth25<T>(mut self, v: T) -> Self
2431 where
2432 T: ::core::convert::Into<Byte>,
2433 {
2434 self.0[25] = v.into();
2435 self
2436 }
2437 pub fn nth26<T>(mut self, v: T) -> Self
2438 where
2439 T: ::core::convert::Into<Byte>,
2440 {
2441 self.0[26] = v.into();
2442 self
2443 }
2444 pub fn nth27<T>(mut self, v: T) -> Self
2445 where
2446 T: ::core::convert::Into<Byte>,
2447 {
2448 self.0[27] = v.into();
2449 self
2450 }
2451 pub fn nth28<T>(mut self, v: T) -> Self
2452 where
2453 T: ::core::convert::Into<Byte>,
2454 {
2455 self.0[28] = v.into();
2456 self
2457 }
2458 pub fn nth29<T>(mut self, v: T) -> Self
2459 where
2460 T: ::core::convert::Into<Byte>,
2461 {
2462 self.0[29] = v.into();
2463 self
2464 }
2465 pub fn nth30<T>(mut self, v: T) -> Self
2466 where
2467 T: ::core::convert::Into<Byte>,
2468 {
2469 self.0[30] = v.into();
2470 self
2471 }
2472 pub fn nth31<T>(mut self, v: T) -> Self
2473 where
2474 T: ::core::convert::Into<Byte>,
2475 {
2476 self.0[31] = v.into();
2477 self
2478 }
2479}
2480impl molecule::prelude::Builder for Uint256Builder {
2481 type Entity = Uint256;
2482 const NAME: &'static str = "Uint256Builder";
2483 fn expected_length(&self) -> usize {
2484 Self::TOTAL_SIZE
2485 }
2486 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2487 writer.write_all(self.0[0].as_slice())?;
2488 writer.write_all(self.0[1].as_slice())?;
2489 writer.write_all(self.0[2].as_slice())?;
2490 writer.write_all(self.0[3].as_slice())?;
2491 writer.write_all(self.0[4].as_slice())?;
2492 writer.write_all(self.0[5].as_slice())?;
2493 writer.write_all(self.0[6].as_slice())?;
2494 writer.write_all(self.0[7].as_slice())?;
2495 writer.write_all(self.0[8].as_slice())?;
2496 writer.write_all(self.0[9].as_slice())?;
2497 writer.write_all(self.0[10].as_slice())?;
2498 writer.write_all(self.0[11].as_slice())?;
2499 writer.write_all(self.0[12].as_slice())?;
2500 writer.write_all(self.0[13].as_slice())?;
2501 writer.write_all(self.0[14].as_slice())?;
2502 writer.write_all(self.0[15].as_slice())?;
2503 writer.write_all(self.0[16].as_slice())?;
2504 writer.write_all(self.0[17].as_slice())?;
2505 writer.write_all(self.0[18].as_slice())?;
2506 writer.write_all(self.0[19].as_slice())?;
2507 writer.write_all(self.0[20].as_slice())?;
2508 writer.write_all(self.0[21].as_slice())?;
2509 writer.write_all(self.0[22].as_slice())?;
2510 writer.write_all(self.0[23].as_slice())?;
2511 writer.write_all(self.0[24].as_slice())?;
2512 writer.write_all(self.0[25].as_slice())?;
2513 writer.write_all(self.0[26].as_slice())?;
2514 writer.write_all(self.0[27].as_slice())?;
2515 writer.write_all(self.0[28].as_slice())?;
2516 writer.write_all(self.0[29].as_slice())?;
2517 writer.write_all(self.0[30].as_slice())?;
2518 writer.write_all(self.0[31].as_slice())?;
2519 Ok(())
2520 }
2521 fn build(&self) -> Self::Entity {
2522 let mut inner = Vec::with_capacity(self.expected_length());
2523 self.write(&mut inner)
2524 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2525 Uint256::new_unchecked(inner.into())
2526 }
2527}
2528impl From<[Byte; 32usize]> for Uint256 {
2529 fn from(value: [Byte; 32usize]) -> Self {
2530 Self::new_builder().set(value).build()
2531 }
2532}
2533impl ::core::convert::TryFrom<&[Byte]> for Uint256 {
2534 type Error = ::core::array::TryFromSliceError;
2535 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
2536 Ok(Self::new_builder()
2537 .set(<&[Byte; 32usize]>::try_from(value)?.clone())
2538 .build())
2539 }
2540}
2541impl From<Uint256> for [Byte; 32usize] {
2542 #[track_caller]
2543 fn from(value: Uint256) -> Self {
2544 [
2545 value.nth0(),
2546 value.nth1(),
2547 value.nth2(),
2548 value.nth3(),
2549 value.nth4(),
2550 value.nth5(),
2551 value.nth6(),
2552 value.nth7(),
2553 value.nth8(),
2554 value.nth9(),
2555 value.nth10(),
2556 value.nth11(),
2557 value.nth12(),
2558 value.nth13(),
2559 value.nth14(),
2560 value.nth15(),
2561 value.nth16(),
2562 value.nth17(),
2563 value.nth18(),
2564 value.nth19(),
2565 value.nth20(),
2566 value.nth21(),
2567 value.nth22(),
2568 value.nth23(),
2569 value.nth24(),
2570 value.nth25(),
2571 value.nth26(),
2572 value.nth27(),
2573 value.nth28(),
2574 value.nth29(),
2575 value.nth30(),
2576 value.nth31(),
2577 ]
2578 }
2579}
2580impl From<[u8; 32usize]> for Uint256 {
2581 fn from(value: [u8; 32usize]) -> Self {
2582 Uint256Reader::new_unchecked(&value).to_entity()
2583 }
2584}
2585impl ::core::convert::TryFrom<&[u8]> for Uint256 {
2586 type Error = ::core::array::TryFromSliceError;
2587 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
2588 Ok(<[u8; 32usize]>::try_from(value)?.into())
2589 }
2590}
2591impl From<Uint256> for [u8; 32usize] {
2592 #[track_caller]
2593 fn from(value: Uint256) -> Self {
2594 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
2595 }
2596}
2597impl<'a> From<Uint256Reader<'a>> for &'a [u8; 32usize] {
2598 #[track_caller]
2599 fn from(value: Uint256Reader<'a>) -> Self {
2600 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
2601 }
2602}
2603impl<'a> From<&'a Uint256Reader<'a>> for &'a [u8; 32usize] {
2604 #[track_caller]
2605 fn from(value: &'a Uint256Reader<'a>) -> Self {
2606 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
2607 }
2608}
2609#[derive(Clone)]
2610pub struct Bytes(molecule::bytes::Bytes);
2611impl ::core::fmt::LowerHex for Bytes {
2612 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2613 use molecule::hex_string;
2614 if f.alternate() {
2615 write!(f, "0x")?;
2616 }
2617 write!(f, "{}", hex_string(self.as_slice()))
2618 }
2619}
2620impl ::core::fmt::Debug for Bytes {
2621 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2622 write!(f, "{}({:#x})", Self::NAME, self)
2623 }
2624}
2625impl ::core::fmt::Display for Bytes {
2626 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2627 use molecule::hex_string;
2628 let raw_data = hex_string(&self.raw_data());
2629 write!(f, "{}(0x{})", Self::NAME, raw_data)
2630 }
2631}
2632impl ::core::default::Default for Bytes {
2633 fn default() -> Self {
2634 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2635 Bytes::new_unchecked(v)
2636 }
2637}
2638impl Bytes {
2639 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
2640 pub const ITEM_SIZE: usize = 1;
2641 pub fn total_size(&self) -> usize {
2642 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2643 }
2644 pub fn item_count(&self) -> usize {
2645 molecule::unpack_number(self.as_slice()) as usize
2646 }
2647 pub fn len(&self) -> usize {
2648 self.item_count()
2649 }
2650 pub fn is_empty(&self) -> bool {
2651 self.len() == 0
2652 }
2653 pub fn get(&self, idx: usize) -> Option<Byte> {
2654 if idx >= self.len() {
2655 None
2656 } else {
2657 Some(self.get_unchecked(idx))
2658 }
2659 }
2660 pub fn get_unchecked(&self, idx: usize) -> Byte {
2661 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2662 let end = start + Self::ITEM_SIZE;
2663 Byte::new_unchecked(self.0.slice(start..end))
2664 }
2665 pub fn raw_data(&self) -> molecule::bytes::Bytes {
2666 self.0.slice(molecule::NUMBER_SIZE..)
2667 }
2668 pub fn as_reader<'r>(&'r self) -> BytesReader<'r> {
2669 BytesReader::new_unchecked(self.as_slice())
2670 }
2671}
2672impl molecule::prelude::Entity for Bytes {
2673 type Builder = BytesBuilder;
2674 const NAME: &'static str = "Bytes";
2675 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2676 Bytes(data)
2677 }
2678 fn as_bytes(&self) -> molecule::bytes::Bytes {
2679 self.0.clone()
2680 }
2681 fn as_slice(&self) -> &[u8] {
2682 &self.0[..]
2683 }
2684 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2685 BytesReader::from_slice(slice).map(|reader| reader.to_entity())
2686 }
2687 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2688 BytesReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2689 }
2690 fn new_builder() -> Self::Builder {
2691 ::core::default::Default::default()
2692 }
2693 fn as_builder(self) -> Self::Builder {
2694 Self::new_builder().extend(self.into_iter())
2695 }
2696}
2697#[derive(Clone, Copy)]
2698pub struct BytesReader<'r>(&'r [u8]);
2699impl<'r> ::core::fmt::LowerHex for BytesReader<'r> {
2700 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2701 use molecule::hex_string;
2702 if f.alternate() {
2703 write!(f, "0x")?;
2704 }
2705 write!(f, "{}", hex_string(self.as_slice()))
2706 }
2707}
2708impl<'r> ::core::fmt::Debug for BytesReader<'r> {
2709 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2710 write!(f, "{}({:#x})", Self::NAME, self)
2711 }
2712}
2713impl<'r> ::core::fmt::Display for BytesReader<'r> {
2714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2715 use molecule::hex_string;
2716 let raw_data = hex_string(&self.raw_data());
2717 write!(f, "{}(0x{})", Self::NAME, raw_data)
2718 }
2719}
2720impl<'r> BytesReader<'r> {
2721 pub const ITEM_SIZE: usize = 1;
2722 pub fn total_size(&self) -> usize {
2723 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
2724 }
2725 pub fn item_count(&self) -> usize {
2726 molecule::unpack_number(self.as_slice()) as usize
2727 }
2728 pub fn len(&self) -> usize {
2729 self.item_count()
2730 }
2731 pub fn is_empty(&self) -> bool {
2732 self.len() == 0
2733 }
2734 pub fn get(&self, idx: usize) -> Option<ByteReader<'r>> {
2735 if idx >= self.len() {
2736 None
2737 } else {
2738 Some(self.get_unchecked(idx))
2739 }
2740 }
2741 pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> {
2742 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
2743 let end = start + Self::ITEM_SIZE;
2744 ByteReader::new_unchecked(&self.as_slice()[start..end])
2745 }
2746 pub fn raw_data(&self) -> &'r [u8] {
2747 &self.as_slice()[molecule::NUMBER_SIZE..]
2748 }
2749}
2750impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> {
2751 type Entity = Bytes;
2752 const NAME: &'static str = "BytesReader";
2753 fn to_entity(&self) -> Self::Entity {
2754 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2755 }
2756 fn new_unchecked(slice: &'r [u8]) -> Self {
2757 BytesReader(slice)
2758 }
2759 fn as_slice(&self) -> &'r [u8] {
2760 self.0
2761 }
2762 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
2763 use molecule::verification_error as ve;
2764 let slice_len = slice.len();
2765 if slice_len < molecule::NUMBER_SIZE {
2766 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
2767 }
2768 let item_count = molecule::unpack_number(slice) as usize;
2769 if item_count == 0 {
2770 if slice_len != molecule::NUMBER_SIZE {
2771 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
2772 }
2773 return Ok(());
2774 }
2775 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
2776 if slice_len != total_size {
2777 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
2778 }
2779 Ok(())
2780 }
2781}
2782#[derive(Clone, Debug, Default)]
2783pub struct BytesBuilder(pub(crate) Vec<Byte>);
2784impl BytesBuilder {
2785 pub const ITEM_SIZE: usize = 1;
2786 pub fn set(mut self, v: Vec<Byte>) -> Self {
2787 self.0 = v;
2788 self
2789 }
2790 pub fn push<T>(mut self, v: T) -> Self
2791 where
2792 T: ::core::convert::Into<Byte>,
2793 {
2794 self.0.push(v.into());
2795 self
2796 }
2797 pub fn extend<T: ::core::iter::IntoIterator<Item = Byte>>(mut self, iter: T) -> Self {
2798 self.0.extend(iter);
2799 self
2800 }
2801 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Byte>
2802 where
2803 T: ::core::convert::Into<Byte>,
2804 {
2805 self.0
2806 .get_mut(index)
2807 .map(|item| ::core::mem::replace(item, v.into()))
2808 }
2809}
2810impl molecule::prelude::Builder for BytesBuilder {
2811 type Entity = Bytes;
2812 const NAME: &'static str = "BytesBuilder";
2813 fn expected_length(&self) -> usize {
2814 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
2815 }
2816 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
2817 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
2818 for inner in &self.0[..] {
2819 writer.write_all(inner.as_slice())?;
2820 }
2821 Ok(())
2822 }
2823 fn build(&self) -> Self::Entity {
2824 let mut inner = Vec::with_capacity(self.expected_length());
2825 self.write(&mut inner)
2826 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
2827 Bytes::new_unchecked(inner.into())
2828 }
2829}
2830pub struct BytesIterator(Bytes, usize, usize);
2831impl ::core::iter::Iterator for BytesIterator {
2832 type Item = Byte;
2833 fn next(&mut self) -> Option<Self::Item> {
2834 if self.1 >= self.2 {
2835 None
2836 } else {
2837 let ret = self.0.get_unchecked(self.1);
2838 self.1 += 1;
2839 Some(ret)
2840 }
2841 }
2842}
2843impl ::core::iter::ExactSizeIterator for BytesIterator {
2844 fn len(&self) -> usize {
2845 self.2 - self.1
2846 }
2847}
2848impl ::core::iter::IntoIterator for Bytes {
2849 type Item = Byte;
2850 type IntoIter = BytesIterator;
2851 fn into_iter(self) -> Self::IntoIter {
2852 let len = self.len();
2853 BytesIterator(self, 0, len)
2854 }
2855}
2856impl<T> ::core::iter::FromIterator<T> for Bytes
2857where
2858 T: Into<Byte>,
2859{
2860 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
2861 Self::new_builder()
2862 .extend(iter.into_iter().map(Into::into))
2863 .build()
2864 }
2865}
2866impl<T> From<Vec<T>> for Bytes
2867where
2868 T: Into<Byte>,
2869{
2870 fn from(v: Vec<T>) -> Self {
2871 v.into_iter().collect()
2872 }
2873}
2874#[derive(Clone)]
2875pub struct BytesOpt(molecule::bytes::Bytes);
2876impl ::core::fmt::LowerHex for BytesOpt {
2877 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2878 use molecule::hex_string;
2879 if f.alternate() {
2880 write!(f, "0x")?;
2881 }
2882 write!(f, "{}", hex_string(self.as_slice()))
2883 }
2884}
2885impl ::core::fmt::Debug for BytesOpt {
2886 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2887 write!(f, "{}({:#x})", Self::NAME, self)
2888 }
2889}
2890impl ::core::fmt::Display for BytesOpt {
2891 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2892 if let Some(v) = self.to_opt() {
2893 write!(f, "{}(Some({}))", Self::NAME, v)
2894 } else {
2895 write!(f, "{}(None)", Self::NAME)
2896 }
2897 }
2898}
2899impl ::core::default::Default for BytesOpt {
2900 fn default() -> Self {
2901 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
2902 BytesOpt::new_unchecked(v)
2903 }
2904}
2905impl BytesOpt {
2906 const DEFAULT_VALUE: [u8; 0] = [];
2907 pub fn is_none(&self) -> bool {
2908 self.0.is_empty()
2909 }
2910 pub fn is_some(&self) -> bool {
2911 !self.0.is_empty()
2912 }
2913 pub fn to_opt(&self) -> Option<Bytes> {
2914 if self.is_none() {
2915 None
2916 } else {
2917 Some(Bytes::new_unchecked(self.0.clone()))
2918 }
2919 }
2920 pub fn as_reader<'r>(&'r self) -> BytesOptReader<'r> {
2921 BytesOptReader::new_unchecked(self.as_slice())
2922 }
2923}
2924impl molecule::prelude::Entity for BytesOpt {
2925 type Builder = BytesOptBuilder;
2926 const NAME: &'static str = "BytesOpt";
2927 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
2928 BytesOpt(data)
2929 }
2930 fn as_bytes(&self) -> molecule::bytes::Bytes {
2931 self.0.clone()
2932 }
2933 fn as_slice(&self) -> &[u8] {
2934 &self.0[..]
2935 }
2936 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2937 BytesOptReader::from_slice(slice).map(|reader| reader.to_entity())
2938 }
2939 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
2940 BytesOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
2941 }
2942 fn new_builder() -> Self::Builder {
2943 ::core::default::Default::default()
2944 }
2945 fn as_builder(self) -> Self::Builder {
2946 Self::new_builder().set(self.to_opt())
2947 }
2948}
2949#[derive(Clone, Copy)]
2950pub struct BytesOptReader<'r>(&'r [u8]);
2951impl<'r> ::core::fmt::LowerHex for BytesOptReader<'r> {
2952 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2953 use molecule::hex_string;
2954 if f.alternate() {
2955 write!(f, "0x")?;
2956 }
2957 write!(f, "{}", hex_string(self.as_slice()))
2958 }
2959}
2960impl<'r> ::core::fmt::Debug for BytesOptReader<'r> {
2961 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2962 write!(f, "{}({:#x})", Self::NAME, self)
2963 }
2964}
2965impl<'r> ::core::fmt::Display for BytesOptReader<'r> {
2966 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2967 if let Some(v) = self.to_opt() {
2968 write!(f, "{}(Some({}))", Self::NAME, v)
2969 } else {
2970 write!(f, "{}(None)", Self::NAME)
2971 }
2972 }
2973}
2974impl<'r> BytesOptReader<'r> {
2975 pub fn is_none(&self) -> bool {
2976 self.0.is_empty()
2977 }
2978 pub fn is_some(&self) -> bool {
2979 !self.0.is_empty()
2980 }
2981 pub fn to_opt(&self) -> Option<BytesReader<'r>> {
2982 if self.is_none() {
2983 None
2984 } else {
2985 Some(BytesReader::new_unchecked(self.as_slice()))
2986 }
2987 }
2988}
2989impl<'r> molecule::prelude::Reader<'r> for BytesOptReader<'r> {
2990 type Entity = BytesOpt;
2991 const NAME: &'static str = "BytesOptReader";
2992 fn to_entity(&self) -> Self::Entity {
2993 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
2994 }
2995 fn new_unchecked(slice: &'r [u8]) -> Self {
2996 BytesOptReader(slice)
2997 }
2998 fn as_slice(&self) -> &'r [u8] {
2999 self.0
3000 }
3001 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3002 if !slice.is_empty() {
3003 BytesReader::verify(&slice[..], compatible)?;
3004 }
3005 Ok(())
3006 }
3007}
3008#[derive(Clone, Debug, Default)]
3009pub struct BytesOptBuilder(pub(crate) Option<Bytes>);
3010impl BytesOptBuilder {
3011 pub fn set<T>(mut self, v: T) -> Self
3012 where
3013 T: ::core::convert::Into<Option<Bytes>>,
3014 {
3015 self.0 = v.into();
3016 self
3017 }
3018}
3019impl molecule::prelude::Builder for BytesOptBuilder {
3020 type Entity = BytesOpt;
3021 const NAME: &'static str = "BytesOptBuilder";
3022 fn expected_length(&self) -> usize {
3023 self.0
3024 .as_ref()
3025 .map(|ref inner| inner.as_slice().len())
3026 .unwrap_or(0)
3027 }
3028 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3029 self.0
3030 .as_ref()
3031 .map(|ref inner| writer.write_all(inner.as_slice()))
3032 .unwrap_or(Ok(()))
3033 }
3034 fn build(&self) -> Self::Entity {
3035 let mut inner = Vec::with_capacity(self.expected_length());
3036 self.write(&mut inner)
3037 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3038 BytesOpt::new_unchecked(inner.into())
3039 }
3040}
3041impl From<Bytes> for BytesOpt {
3042 fn from(value: Bytes) -> Self {
3043 Self::new_builder().set(Some(value)).build()
3044 }
3045}
3046#[derive(Clone)]
3047pub struct BytesOptVec(molecule::bytes::Bytes);
3048impl ::core::fmt::LowerHex for BytesOptVec {
3049 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3050 use molecule::hex_string;
3051 if f.alternate() {
3052 write!(f, "0x")?;
3053 }
3054 write!(f, "{}", hex_string(self.as_slice()))
3055 }
3056}
3057impl ::core::fmt::Debug for BytesOptVec {
3058 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3059 write!(f, "{}({:#x})", Self::NAME, self)
3060 }
3061}
3062impl ::core::fmt::Display for BytesOptVec {
3063 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3064 write!(f, "{} [", Self::NAME)?;
3065 for i in 0..self.len() {
3066 if i == 0 {
3067 write!(f, "{}", self.get_unchecked(i))?;
3068 } else {
3069 write!(f, ", {}", self.get_unchecked(i))?;
3070 }
3071 }
3072 write!(f, "]")
3073 }
3074}
3075impl ::core::default::Default for BytesOptVec {
3076 fn default() -> Self {
3077 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3078 BytesOptVec::new_unchecked(v)
3079 }
3080}
3081impl BytesOptVec {
3082 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
3083 pub fn total_size(&self) -> usize {
3084 molecule::unpack_number(self.as_slice()) as usize
3085 }
3086 pub fn item_count(&self) -> usize {
3087 if self.total_size() == molecule::NUMBER_SIZE {
3088 0
3089 } else {
3090 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3091 }
3092 }
3093 pub fn len(&self) -> usize {
3094 self.item_count()
3095 }
3096 pub fn is_empty(&self) -> bool {
3097 self.len() == 0
3098 }
3099 pub fn get(&self, idx: usize) -> Option<BytesOpt> {
3100 if idx >= self.len() {
3101 None
3102 } else {
3103 Some(self.get_unchecked(idx))
3104 }
3105 }
3106 pub fn get_unchecked(&self, idx: usize) -> BytesOpt {
3107 let slice = self.as_slice();
3108 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3109 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3110 if idx == self.len() - 1 {
3111 BytesOpt::new_unchecked(self.0.slice(start..))
3112 } else {
3113 let end_idx = start_idx + molecule::NUMBER_SIZE;
3114 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3115 BytesOpt::new_unchecked(self.0.slice(start..end))
3116 }
3117 }
3118 pub fn as_reader<'r>(&'r self) -> BytesOptVecReader<'r> {
3119 BytesOptVecReader::new_unchecked(self.as_slice())
3120 }
3121}
3122impl molecule::prelude::Entity for BytesOptVec {
3123 type Builder = BytesOptVecBuilder;
3124 const NAME: &'static str = "BytesOptVec";
3125 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3126 BytesOptVec(data)
3127 }
3128 fn as_bytes(&self) -> molecule::bytes::Bytes {
3129 self.0.clone()
3130 }
3131 fn as_slice(&self) -> &[u8] {
3132 &self.0[..]
3133 }
3134 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3135 BytesOptVecReader::from_slice(slice).map(|reader| reader.to_entity())
3136 }
3137 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3138 BytesOptVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3139 }
3140 fn new_builder() -> Self::Builder {
3141 ::core::default::Default::default()
3142 }
3143 fn as_builder(self) -> Self::Builder {
3144 Self::new_builder().extend(self.into_iter())
3145 }
3146}
3147#[derive(Clone, Copy)]
3148pub struct BytesOptVecReader<'r>(&'r [u8]);
3149impl<'r> ::core::fmt::LowerHex for BytesOptVecReader<'r> {
3150 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3151 use molecule::hex_string;
3152 if f.alternate() {
3153 write!(f, "0x")?;
3154 }
3155 write!(f, "{}", hex_string(self.as_slice()))
3156 }
3157}
3158impl<'r> ::core::fmt::Debug for BytesOptVecReader<'r> {
3159 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3160 write!(f, "{}({:#x})", Self::NAME, self)
3161 }
3162}
3163impl<'r> ::core::fmt::Display for BytesOptVecReader<'r> {
3164 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3165 write!(f, "{} [", Self::NAME)?;
3166 for i in 0..self.len() {
3167 if i == 0 {
3168 write!(f, "{}", self.get_unchecked(i))?;
3169 } else {
3170 write!(f, ", {}", self.get_unchecked(i))?;
3171 }
3172 }
3173 write!(f, "]")
3174 }
3175}
3176impl<'r> BytesOptVecReader<'r> {
3177 pub fn total_size(&self) -> usize {
3178 molecule::unpack_number(self.as_slice()) as usize
3179 }
3180 pub fn item_count(&self) -> usize {
3181 if self.total_size() == molecule::NUMBER_SIZE {
3182 0
3183 } else {
3184 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3185 }
3186 }
3187 pub fn len(&self) -> usize {
3188 self.item_count()
3189 }
3190 pub fn is_empty(&self) -> bool {
3191 self.len() == 0
3192 }
3193 pub fn get(&self, idx: usize) -> Option<BytesOptReader<'r>> {
3194 if idx >= self.len() {
3195 None
3196 } else {
3197 Some(self.get_unchecked(idx))
3198 }
3199 }
3200 pub fn get_unchecked(&self, idx: usize) -> BytesOptReader<'r> {
3201 let slice = self.as_slice();
3202 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3203 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3204 if idx == self.len() - 1 {
3205 BytesOptReader::new_unchecked(&self.as_slice()[start..])
3206 } else {
3207 let end_idx = start_idx + molecule::NUMBER_SIZE;
3208 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3209 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
3210 }
3211 }
3212}
3213impl<'r> molecule::prelude::Reader<'r> for BytesOptVecReader<'r> {
3214 type Entity = BytesOptVec;
3215 const NAME: &'static str = "BytesOptVecReader";
3216 fn to_entity(&self) -> Self::Entity {
3217 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3218 }
3219 fn new_unchecked(slice: &'r [u8]) -> Self {
3220 BytesOptVecReader(slice)
3221 }
3222 fn as_slice(&self) -> &'r [u8] {
3223 self.0
3224 }
3225 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3226 use molecule::verification_error as ve;
3227 let slice_len = slice.len();
3228 if slice_len < molecule::NUMBER_SIZE {
3229 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3230 }
3231 let total_size = molecule::unpack_number(slice) as usize;
3232 if slice_len != total_size {
3233 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3234 }
3235 if slice_len == molecule::NUMBER_SIZE {
3236 return Ok(());
3237 }
3238 if slice_len < molecule::NUMBER_SIZE * 2 {
3239 return ve!(
3240 Self,
3241 TotalSizeNotMatch,
3242 molecule::NUMBER_SIZE * 2,
3243 slice_len
3244 );
3245 }
3246 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3247 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3248 return ve!(Self, OffsetsNotMatch);
3249 }
3250 if slice_len < offset_first {
3251 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3252 }
3253 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3254 .chunks_exact(molecule::NUMBER_SIZE)
3255 .map(|x| molecule::unpack_number(x) as usize)
3256 .collect();
3257 offsets.push(total_size);
3258 if offsets.windows(2).any(|i| i[0] > i[1]) {
3259 return ve!(Self, OffsetsNotMatch);
3260 }
3261 for pair in offsets.windows(2) {
3262 let start = pair[0];
3263 let end = pair[1];
3264 BytesOptReader::verify(&slice[start..end], compatible)?;
3265 }
3266 Ok(())
3267 }
3268}
3269#[derive(Clone, Debug, Default)]
3270pub struct BytesOptVecBuilder(pub(crate) Vec<BytesOpt>);
3271impl BytesOptVecBuilder {
3272 pub fn set(mut self, v: Vec<BytesOpt>) -> Self {
3273 self.0 = v;
3274 self
3275 }
3276 pub fn push<T>(mut self, v: T) -> Self
3277 where
3278 T: ::core::convert::Into<BytesOpt>,
3279 {
3280 self.0.push(v.into());
3281 self
3282 }
3283 pub fn extend<T: ::core::iter::IntoIterator<Item = BytesOpt>>(mut self, iter: T) -> Self {
3284 self.0.extend(iter);
3285 self
3286 }
3287 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<BytesOpt>
3288 where
3289 T: ::core::convert::Into<BytesOpt>,
3290 {
3291 self.0
3292 .get_mut(index)
3293 .map(|item| ::core::mem::replace(item, v.into()))
3294 }
3295}
3296impl molecule::prelude::Builder for BytesOptVecBuilder {
3297 type Entity = BytesOptVec;
3298 const NAME: &'static str = "BytesOptVecBuilder";
3299 fn expected_length(&self) -> usize {
3300 molecule::NUMBER_SIZE * (self.0.len() + 1)
3301 + self
3302 .0
3303 .iter()
3304 .map(|inner| inner.as_slice().len())
3305 .sum::<usize>()
3306 }
3307 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3308 let item_count = self.0.len();
3309 if item_count == 0 {
3310 writer.write_all(&molecule::pack_number(
3311 molecule::NUMBER_SIZE as molecule::Number,
3312 ))?;
3313 } else {
3314 let (total_size, offsets) = self.0.iter().fold(
3315 (
3316 molecule::NUMBER_SIZE * (item_count + 1),
3317 Vec::with_capacity(item_count),
3318 ),
3319 |(start, mut offsets), inner| {
3320 offsets.push(start);
3321 (start + inner.as_slice().len(), offsets)
3322 },
3323 );
3324 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3325 for offset in offsets.into_iter() {
3326 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3327 }
3328 for inner in self.0.iter() {
3329 writer.write_all(inner.as_slice())?;
3330 }
3331 }
3332 Ok(())
3333 }
3334 fn build(&self) -> Self::Entity {
3335 let mut inner = Vec::with_capacity(self.expected_length());
3336 self.write(&mut inner)
3337 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3338 BytesOptVec::new_unchecked(inner.into())
3339 }
3340}
3341pub struct BytesOptVecIterator(BytesOptVec, usize, usize);
3342impl ::core::iter::Iterator for BytesOptVecIterator {
3343 type Item = BytesOpt;
3344 fn next(&mut self) -> Option<Self::Item> {
3345 if self.1 >= self.2 {
3346 None
3347 } else {
3348 let ret = self.0.get_unchecked(self.1);
3349 self.1 += 1;
3350 Some(ret)
3351 }
3352 }
3353}
3354impl ::core::iter::ExactSizeIterator for BytesOptVecIterator {
3355 fn len(&self) -> usize {
3356 self.2 - self.1
3357 }
3358}
3359impl ::core::iter::IntoIterator for BytesOptVec {
3360 type Item = BytesOpt;
3361 type IntoIter = BytesOptVecIterator;
3362 fn into_iter(self) -> Self::IntoIter {
3363 let len = self.len();
3364 BytesOptVecIterator(self, 0, len)
3365 }
3366}
3367impl<'r> BytesOptVecReader<'r> {
3368 pub fn iter<'t>(&'t self) -> BytesOptVecReaderIterator<'t, 'r> {
3369 BytesOptVecReaderIterator(&self, 0, self.len())
3370 }
3371}
3372pub struct BytesOptVecReaderIterator<'t, 'r>(&'t BytesOptVecReader<'r>, usize, usize);
3373impl<'t: 'r, 'r> ::core::iter::Iterator for BytesOptVecReaderIterator<'t, 'r> {
3374 type Item = BytesOptReader<'t>;
3375 fn next(&mut self) -> Option<Self::Item> {
3376 if self.1 >= self.2 {
3377 None
3378 } else {
3379 let ret = self.0.get_unchecked(self.1);
3380 self.1 += 1;
3381 Some(ret)
3382 }
3383 }
3384}
3385impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesOptVecReaderIterator<'t, 'r> {
3386 fn len(&self) -> usize {
3387 self.2 - self.1
3388 }
3389}
3390impl<T> ::core::iter::FromIterator<T> for BytesOptVec
3391where
3392 T: Into<BytesOpt>,
3393{
3394 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3395 Self::new_builder()
3396 .extend(iter.into_iter().map(Into::into))
3397 .build()
3398 }
3399}
3400impl<T> From<Vec<T>> for BytesOptVec
3401where
3402 T: Into<BytesOpt>,
3403{
3404 fn from(v: Vec<T>) -> Self {
3405 v.into_iter().collect()
3406 }
3407}
3408#[derive(Clone)]
3409pub struct BytesVec(molecule::bytes::Bytes);
3410impl ::core::fmt::LowerHex for BytesVec {
3411 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3412 use molecule::hex_string;
3413 if f.alternate() {
3414 write!(f, "0x")?;
3415 }
3416 write!(f, "{}", hex_string(self.as_slice()))
3417 }
3418}
3419impl ::core::fmt::Debug for BytesVec {
3420 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3421 write!(f, "{}({:#x})", Self::NAME, self)
3422 }
3423}
3424impl ::core::fmt::Display for BytesVec {
3425 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3426 write!(f, "{} [", Self::NAME)?;
3427 for i in 0..self.len() {
3428 if i == 0 {
3429 write!(f, "{}", self.get_unchecked(i))?;
3430 } else {
3431 write!(f, ", {}", self.get_unchecked(i))?;
3432 }
3433 }
3434 write!(f, "]")
3435 }
3436}
3437impl ::core::default::Default for BytesVec {
3438 fn default() -> Self {
3439 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3440 BytesVec::new_unchecked(v)
3441 }
3442}
3443impl BytesVec {
3444 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
3445 pub fn total_size(&self) -> usize {
3446 molecule::unpack_number(self.as_slice()) as usize
3447 }
3448 pub fn item_count(&self) -> usize {
3449 if self.total_size() == molecule::NUMBER_SIZE {
3450 0
3451 } else {
3452 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3453 }
3454 }
3455 pub fn len(&self) -> usize {
3456 self.item_count()
3457 }
3458 pub fn is_empty(&self) -> bool {
3459 self.len() == 0
3460 }
3461 pub fn get(&self, idx: usize) -> Option<Bytes> {
3462 if idx >= self.len() {
3463 None
3464 } else {
3465 Some(self.get_unchecked(idx))
3466 }
3467 }
3468 pub fn get_unchecked(&self, idx: usize) -> Bytes {
3469 let slice = self.as_slice();
3470 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3471 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3472 if idx == self.len() - 1 {
3473 Bytes::new_unchecked(self.0.slice(start..))
3474 } else {
3475 let end_idx = start_idx + molecule::NUMBER_SIZE;
3476 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3477 Bytes::new_unchecked(self.0.slice(start..end))
3478 }
3479 }
3480 pub fn as_reader<'r>(&'r self) -> BytesVecReader<'r> {
3481 BytesVecReader::new_unchecked(self.as_slice())
3482 }
3483}
3484impl molecule::prelude::Entity for BytesVec {
3485 type Builder = BytesVecBuilder;
3486 const NAME: &'static str = "BytesVec";
3487 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3488 BytesVec(data)
3489 }
3490 fn as_bytes(&self) -> molecule::bytes::Bytes {
3491 self.0.clone()
3492 }
3493 fn as_slice(&self) -> &[u8] {
3494 &self.0[..]
3495 }
3496 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3497 BytesVecReader::from_slice(slice).map(|reader| reader.to_entity())
3498 }
3499 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3500 BytesVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3501 }
3502 fn new_builder() -> Self::Builder {
3503 ::core::default::Default::default()
3504 }
3505 fn as_builder(self) -> Self::Builder {
3506 Self::new_builder().extend(self.into_iter())
3507 }
3508}
3509#[derive(Clone, Copy)]
3510pub struct BytesVecReader<'r>(&'r [u8]);
3511impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> {
3512 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3513 use molecule::hex_string;
3514 if f.alternate() {
3515 write!(f, "0x")?;
3516 }
3517 write!(f, "{}", hex_string(self.as_slice()))
3518 }
3519}
3520impl<'r> ::core::fmt::Debug for BytesVecReader<'r> {
3521 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3522 write!(f, "{}({:#x})", Self::NAME, self)
3523 }
3524}
3525impl<'r> ::core::fmt::Display for BytesVecReader<'r> {
3526 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3527 write!(f, "{} [", Self::NAME)?;
3528 for i in 0..self.len() {
3529 if i == 0 {
3530 write!(f, "{}", self.get_unchecked(i))?;
3531 } else {
3532 write!(f, ", {}", self.get_unchecked(i))?;
3533 }
3534 }
3535 write!(f, "]")
3536 }
3537}
3538impl<'r> BytesVecReader<'r> {
3539 pub fn total_size(&self) -> usize {
3540 molecule::unpack_number(self.as_slice()) as usize
3541 }
3542 pub fn item_count(&self) -> usize {
3543 if self.total_size() == molecule::NUMBER_SIZE {
3544 0
3545 } else {
3546 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
3547 }
3548 }
3549 pub fn len(&self) -> usize {
3550 self.item_count()
3551 }
3552 pub fn is_empty(&self) -> bool {
3553 self.len() == 0
3554 }
3555 pub fn get(&self, idx: usize) -> Option<BytesReader<'r>> {
3556 if idx >= self.len() {
3557 None
3558 } else {
3559 Some(self.get_unchecked(idx))
3560 }
3561 }
3562 pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> {
3563 let slice = self.as_slice();
3564 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
3565 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
3566 if idx == self.len() - 1 {
3567 BytesReader::new_unchecked(&self.as_slice()[start..])
3568 } else {
3569 let end_idx = start_idx + molecule::NUMBER_SIZE;
3570 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
3571 BytesReader::new_unchecked(&self.as_slice()[start..end])
3572 }
3573 }
3574}
3575impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> {
3576 type Entity = BytesVec;
3577 const NAME: &'static str = "BytesVecReader";
3578 fn to_entity(&self) -> Self::Entity {
3579 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3580 }
3581 fn new_unchecked(slice: &'r [u8]) -> Self {
3582 BytesVecReader(slice)
3583 }
3584 fn as_slice(&self) -> &'r [u8] {
3585 self.0
3586 }
3587 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
3588 use molecule::verification_error as ve;
3589 let slice_len = slice.len();
3590 if slice_len < molecule::NUMBER_SIZE {
3591 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3592 }
3593 let total_size = molecule::unpack_number(slice) as usize;
3594 if slice_len != total_size {
3595 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3596 }
3597 if slice_len == molecule::NUMBER_SIZE {
3598 return Ok(());
3599 }
3600 if slice_len < molecule::NUMBER_SIZE * 2 {
3601 return ve!(
3602 Self,
3603 TotalSizeNotMatch,
3604 molecule::NUMBER_SIZE * 2,
3605 slice_len
3606 );
3607 }
3608 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
3609 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
3610 return ve!(Self, OffsetsNotMatch);
3611 }
3612 if slice_len < offset_first {
3613 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
3614 }
3615 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
3616 .chunks_exact(molecule::NUMBER_SIZE)
3617 .map(|x| molecule::unpack_number(x) as usize)
3618 .collect();
3619 offsets.push(total_size);
3620 if offsets.windows(2).any(|i| i[0] > i[1]) {
3621 return ve!(Self, OffsetsNotMatch);
3622 }
3623 for pair in offsets.windows(2) {
3624 let start = pair[0];
3625 let end = pair[1];
3626 BytesReader::verify(&slice[start..end], compatible)?;
3627 }
3628 Ok(())
3629 }
3630}
3631#[derive(Clone, Debug, Default)]
3632pub struct BytesVecBuilder(pub(crate) Vec<Bytes>);
3633impl BytesVecBuilder {
3634 pub fn set(mut self, v: Vec<Bytes>) -> Self {
3635 self.0 = v;
3636 self
3637 }
3638 pub fn push<T>(mut self, v: T) -> Self
3639 where
3640 T: ::core::convert::Into<Bytes>,
3641 {
3642 self.0.push(v.into());
3643 self
3644 }
3645 pub fn extend<T: ::core::iter::IntoIterator<Item = Bytes>>(mut self, iter: T) -> Self {
3646 self.0.extend(iter);
3647 self
3648 }
3649 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Bytes>
3650 where
3651 T: ::core::convert::Into<Bytes>,
3652 {
3653 self.0
3654 .get_mut(index)
3655 .map(|item| ::core::mem::replace(item, v.into()))
3656 }
3657}
3658impl molecule::prelude::Builder for BytesVecBuilder {
3659 type Entity = BytesVec;
3660 const NAME: &'static str = "BytesVecBuilder";
3661 fn expected_length(&self) -> usize {
3662 molecule::NUMBER_SIZE * (self.0.len() + 1)
3663 + self
3664 .0
3665 .iter()
3666 .map(|inner| inner.as_slice().len())
3667 .sum::<usize>()
3668 }
3669 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3670 let item_count = self.0.len();
3671 if item_count == 0 {
3672 writer.write_all(&molecule::pack_number(
3673 molecule::NUMBER_SIZE as molecule::Number,
3674 ))?;
3675 } else {
3676 let (total_size, offsets) = self.0.iter().fold(
3677 (
3678 molecule::NUMBER_SIZE * (item_count + 1),
3679 Vec::with_capacity(item_count),
3680 ),
3681 |(start, mut offsets), inner| {
3682 offsets.push(start);
3683 (start + inner.as_slice().len(), offsets)
3684 },
3685 );
3686 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
3687 for offset in offsets.into_iter() {
3688 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
3689 }
3690 for inner in self.0.iter() {
3691 writer.write_all(inner.as_slice())?;
3692 }
3693 }
3694 Ok(())
3695 }
3696 fn build(&self) -> Self::Entity {
3697 let mut inner = Vec::with_capacity(self.expected_length());
3698 self.write(&mut inner)
3699 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3700 BytesVec::new_unchecked(inner.into())
3701 }
3702}
3703pub struct BytesVecIterator(BytesVec, usize, usize);
3704impl ::core::iter::Iterator for BytesVecIterator {
3705 type Item = Bytes;
3706 fn next(&mut self) -> Option<Self::Item> {
3707 if self.1 >= self.2 {
3708 None
3709 } else {
3710 let ret = self.0.get_unchecked(self.1);
3711 self.1 += 1;
3712 Some(ret)
3713 }
3714 }
3715}
3716impl ::core::iter::ExactSizeIterator for BytesVecIterator {
3717 fn len(&self) -> usize {
3718 self.2 - self.1
3719 }
3720}
3721impl ::core::iter::IntoIterator for BytesVec {
3722 type Item = Bytes;
3723 type IntoIter = BytesVecIterator;
3724 fn into_iter(self) -> Self::IntoIter {
3725 let len = self.len();
3726 BytesVecIterator(self, 0, len)
3727 }
3728}
3729impl<'r> BytesVecReader<'r> {
3730 pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> {
3731 BytesVecReaderIterator(&self, 0, self.len())
3732 }
3733}
3734pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize);
3735impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> {
3736 type Item = BytesReader<'t>;
3737 fn next(&mut self) -> Option<Self::Item> {
3738 if self.1 >= self.2 {
3739 None
3740 } else {
3741 let ret = self.0.get_unchecked(self.1);
3742 self.1 += 1;
3743 Some(ret)
3744 }
3745 }
3746}
3747impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, 'r> {
3748 fn len(&self) -> usize {
3749 self.2 - self.1
3750 }
3751}
3752impl<T> ::core::iter::FromIterator<T> for BytesVec
3753where
3754 T: Into<Bytes>,
3755{
3756 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
3757 Self::new_builder()
3758 .extend(iter.into_iter().map(Into::into))
3759 .build()
3760 }
3761}
3762impl<T> From<Vec<T>> for BytesVec
3763where
3764 T: Into<Bytes>,
3765{
3766 fn from(v: Vec<T>) -> Self {
3767 v.into_iter().collect()
3768 }
3769}
3770#[derive(Clone)]
3771pub struct Byte32Vec(molecule::bytes::Bytes);
3772impl ::core::fmt::LowerHex for Byte32Vec {
3773 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3774 use molecule::hex_string;
3775 if f.alternate() {
3776 write!(f, "0x")?;
3777 }
3778 write!(f, "{}", hex_string(self.as_slice()))
3779 }
3780}
3781impl ::core::fmt::Debug for Byte32Vec {
3782 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3783 write!(f, "{}({:#x})", Self::NAME, self)
3784 }
3785}
3786impl ::core::fmt::Display for Byte32Vec {
3787 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3788 write!(f, "{} [", Self::NAME)?;
3789 for i in 0..self.len() {
3790 if i == 0 {
3791 write!(f, "{}", self.get_unchecked(i))?;
3792 } else {
3793 write!(f, ", {}", self.get_unchecked(i))?;
3794 }
3795 }
3796 write!(f, "]")
3797 }
3798}
3799impl ::core::default::Default for Byte32Vec {
3800 fn default() -> Self {
3801 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
3802 Byte32Vec::new_unchecked(v)
3803 }
3804}
3805impl Byte32Vec {
3806 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
3807 pub const ITEM_SIZE: usize = 32;
3808 pub fn total_size(&self) -> usize {
3809 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
3810 }
3811 pub fn item_count(&self) -> usize {
3812 molecule::unpack_number(self.as_slice()) as usize
3813 }
3814 pub fn len(&self) -> usize {
3815 self.item_count()
3816 }
3817 pub fn is_empty(&self) -> bool {
3818 self.len() == 0
3819 }
3820 pub fn get(&self, idx: usize) -> Option<Byte32> {
3821 if idx >= self.len() {
3822 None
3823 } else {
3824 Some(self.get_unchecked(idx))
3825 }
3826 }
3827 pub fn get_unchecked(&self, idx: usize) -> Byte32 {
3828 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
3829 let end = start + Self::ITEM_SIZE;
3830 Byte32::new_unchecked(self.0.slice(start..end))
3831 }
3832 pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> {
3833 Byte32VecReader::new_unchecked(self.as_slice())
3834 }
3835}
3836impl molecule::prelude::Entity for Byte32Vec {
3837 type Builder = Byte32VecBuilder;
3838 const NAME: &'static str = "Byte32Vec";
3839 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
3840 Byte32Vec(data)
3841 }
3842 fn as_bytes(&self) -> molecule::bytes::Bytes {
3843 self.0.clone()
3844 }
3845 fn as_slice(&self) -> &[u8] {
3846 &self.0[..]
3847 }
3848 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3849 Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity())
3850 }
3851 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
3852 Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
3853 }
3854 fn new_builder() -> Self::Builder {
3855 ::core::default::Default::default()
3856 }
3857 fn as_builder(self) -> Self::Builder {
3858 Self::new_builder().extend(self.into_iter())
3859 }
3860}
3861#[derive(Clone, Copy)]
3862pub struct Byte32VecReader<'r>(&'r [u8]);
3863impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> {
3864 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3865 use molecule::hex_string;
3866 if f.alternate() {
3867 write!(f, "0x")?;
3868 }
3869 write!(f, "{}", hex_string(self.as_slice()))
3870 }
3871}
3872impl<'r> ::core::fmt::Debug for Byte32VecReader<'r> {
3873 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3874 write!(f, "{}({:#x})", Self::NAME, self)
3875 }
3876}
3877impl<'r> ::core::fmt::Display for Byte32VecReader<'r> {
3878 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3879 write!(f, "{} [", Self::NAME)?;
3880 for i in 0..self.len() {
3881 if i == 0 {
3882 write!(f, "{}", self.get_unchecked(i))?;
3883 } else {
3884 write!(f, ", {}", self.get_unchecked(i))?;
3885 }
3886 }
3887 write!(f, "]")
3888 }
3889}
3890impl<'r> Byte32VecReader<'r> {
3891 pub const ITEM_SIZE: usize = 32;
3892 pub fn total_size(&self) -> usize {
3893 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
3894 }
3895 pub fn item_count(&self) -> usize {
3896 molecule::unpack_number(self.as_slice()) as usize
3897 }
3898 pub fn len(&self) -> usize {
3899 self.item_count()
3900 }
3901 pub fn is_empty(&self) -> bool {
3902 self.len() == 0
3903 }
3904 pub fn get(&self, idx: usize) -> Option<Byte32Reader<'r>> {
3905 if idx >= self.len() {
3906 None
3907 } else {
3908 Some(self.get_unchecked(idx))
3909 }
3910 }
3911 pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> {
3912 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
3913 let end = start + Self::ITEM_SIZE;
3914 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
3915 }
3916}
3917impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> {
3918 type Entity = Byte32Vec;
3919 const NAME: &'static str = "Byte32VecReader";
3920 fn to_entity(&self) -> Self::Entity {
3921 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
3922 }
3923 fn new_unchecked(slice: &'r [u8]) -> Self {
3924 Byte32VecReader(slice)
3925 }
3926 fn as_slice(&self) -> &'r [u8] {
3927 self.0
3928 }
3929 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
3930 use molecule::verification_error as ve;
3931 let slice_len = slice.len();
3932 if slice_len < molecule::NUMBER_SIZE {
3933 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
3934 }
3935 let item_count = molecule::unpack_number(slice) as usize;
3936 if item_count == 0 {
3937 if slice_len != molecule::NUMBER_SIZE {
3938 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
3939 }
3940 return Ok(());
3941 }
3942 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
3943 if slice_len != total_size {
3944 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
3945 }
3946 Ok(())
3947 }
3948}
3949#[derive(Clone, Debug, Default)]
3950pub struct Byte32VecBuilder(pub(crate) Vec<Byte32>);
3951impl Byte32VecBuilder {
3952 pub const ITEM_SIZE: usize = 32;
3953 pub fn set(mut self, v: Vec<Byte32>) -> Self {
3954 self.0 = v;
3955 self
3956 }
3957 pub fn push<T>(mut self, v: T) -> Self
3958 where
3959 T: ::core::convert::Into<Byte32>,
3960 {
3961 self.0.push(v.into());
3962 self
3963 }
3964 pub fn extend<T: ::core::iter::IntoIterator<Item = Byte32>>(mut self, iter: T) -> Self {
3965 self.0.extend(iter);
3966 self
3967 }
3968 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Byte32>
3969 where
3970 T: ::core::convert::Into<Byte32>,
3971 {
3972 self.0
3973 .get_mut(index)
3974 .map(|item| ::core::mem::replace(item, v.into()))
3975 }
3976}
3977impl molecule::prelude::Builder for Byte32VecBuilder {
3978 type Entity = Byte32Vec;
3979 const NAME: &'static str = "Byte32VecBuilder";
3980 fn expected_length(&self) -> usize {
3981 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
3982 }
3983 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
3984 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
3985 for inner in &self.0[..] {
3986 writer.write_all(inner.as_slice())?;
3987 }
3988 Ok(())
3989 }
3990 fn build(&self) -> Self::Entity {
3991 let mut inner = Vec::with_capacity(self.expected_length());
3992 self.write(&mut inner)
3993 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
3994 Byte32Vec::new_unchecked(inner.into())
3995 }
3996}
3997pub struct Byte32VecIterator(Byte32Vec, usize, usize);
3998impl ::core::iter::Iterator for Byte32VecIterator {
3999 type Item = Byte32;
4000 fn next(&mut self) -> Option<Self::Item> {
4001 if self.1 >= self.2 {
4002 None
4003 } else {
4004 let ret = self.0.get_unchecked(self.1);
4005 self.1 += 1;
4006 Some(ret)
4007 }
4008 }
4009}
4010impl ::core::iter::ExactSizeIterator for Byte32VecIterator {
4011 fn len(&self) -> usize {
4012 self.2 - self.1
4013 }
4014}
4015impl ::core::iter::IntoIterator for Byte32Vec {
4016 type Item = Byte32;
4017 type IntoIter = Byte32VecIterator;
4018 fn into_iter(self) -> Self::IntoIter {
4019 let len = self.len();
4020 Byte32VecIterator(self, 0, len)
4021 }
4022}
4023impl<'r> Byte32VecReader<'r> {
4024 pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> {
4025 Byte32VecReaderIterator(&self, 0, self.len())
4026 }
4027}
4028pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize);
4029impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> {
4030 type Item = Byte32Reader<'t>;
4031 fn next(&mut self) -> Option<Self::Item> {
4032 if self.1 >= self.2 {
4033 None
4034 } else {
4035 let ret = self.0.get_unchecked(self.1);
4036 self.1 += 1;
4037 Some(ret)
4038 }
4039 }
4040}
4041impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> {
4042 fn len(&self) -> usize {
4043 self.2 - self.1
4044 }
4045}
4046impl<T> ::core::iter::FromIterator<T> for Byte32Vec
4047where
4048 T: Into<Byte32>,
4049{
4050 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
4051 Self::new_builder()
4052 .extend(iter.into_iter().map(Into::into))
4053 .build()
4054 }
4055}
4056impl<T> From<Vec<T>> for Byte32Vec
4057where
4058 T: Into<Byte32>,
4059{
4060 fn from(v: Vec<T>) -> Self {
4061 v.into_iter().collect()
4062 }
4063}
4064#[derive(Clone)]
4065pub struct ScriptOpt(molecule::bytes::Bytes);
4066impl ::core::fmt::LowerHex for ScriptOpt {
4067 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4068 use molecule::hex_string;
4069 if f.alternate() {
4070 write!(f, "0x")?;
4071 }
4072 write!(f, "{}", hex_string(self.as_slice()))
4073 }
4074}
4075impl ::core::fmt::Debug for ScriptOpt {
4076 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4077 write!(f, "{}({:#x})", Self::NAME, self)
4078 }
4079}
4080impl ::core::fmt::Display for ScriptOpt {
4081 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4082 if let Some(v) = self.to_opt() {
4083 write!(f, "{}(Some({}))", Self::NAME, v)
4084 } else {
4085 write!(f, "{}(None)", Self::NAME)
4086 }
4087 }
4088}
4089impl ::core::default::Default for ScriptOpt {
4090 fn default() -> Self {
4091 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4092 ScriptOpt::new_unchecked(v)
4093 }
4094}
4095impl ScriptOpt {
4096 const DEFAULT_VALUE: [u8; 0] = [];
4097 pub fn is_none(&self) -> bool {
4098 self.0.is_empty()
4099 }
4100 pub fn is_some(&self) -> bool {
4101 !self.0.is_empty()
4102 }
4103 pub fn to_opt(&self) -> Option<Script> {
4104 if self.is_none() {
4105 None
4106 } else {
4107 Some(Script::new_unchecked(self.0.clone()))
4108 }
4109 }
4110 pub fn as_reader<'r>(&'r self) -> ScriptOptReader<'r> {
4111 ScriptOptReader::new_unchecked(self.as_slice())
4112 }
4113}
4114impl molecule::prelude::Entity for ScriptOpt {
4115 type Builder = ScriptOptBuilder;
4116 const NAME: &'static str = "ScriptOpt";
4117 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4118 ScriptOpt(data)
4119 }
4120 fn as_bytes(&self) -> molecule::bytes::Bytes {
4121 self.0.clone()
4122 }
4123 fn as_slice(&self) -> &[u8] {
4124 &self.0[..]
4125 }
4126 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4127 ScriptOptReader::from_slice(slice).map(|reader| reader.to_entity())
4128 }
4129 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4130 ScriptOptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4131 }
4132 fn new_builder() -> Self::Builder {
4133 ::core::default::Default::default()
4134 }
4135 fn as_builder(self) -> Self::Builder {
4136 Self::new_builder().set(self.to_opt())
4137 }
4138}
4139#[derive(Clone, Copy)]
4140pub struct ScriptOptReader<'r>(&'r [u8]);
4141impl<'r> ::core::fmt::LowerHex for ScriptOptReader<'r> {
4142 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4143 use molecule::hex_string;
4144 if f.alternate() {
4145 write!(f, "0x")?;
4146 }
4147 write!(f, "{}", hex_string(self.as_slice()))
4148 }
4149}
4150impl<'r> ::core::fmt::Debug for ScriptOptReader<'r> {
4151 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4152 write!(f, "{}({:#x})", Self::NAME, self)
4153 }
4154}
4155impl<'r> ::core::fmt::Display for ScriptOptReader<'r> {
4156 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4157 if let Some(v) = self.to_opt() {
4158 write!(f, "{}(Some({}))", Self::NAME, v)
4159 } else {
4160 write!(f, "{}(None)", Self::NAME)
4161 }
4162 }
4163}
4164impl<'r> ScriptOptReader<'r> {
4165 pub fn is_none(&self) -> bool {
4166 self.0.is_empty()
4167 }
4168 pub fn is_some(&self) -> bool {
4169 !self.0.is_empty()
4170 }
4171 pub fn to_opt(&self) -> Option<ScriptReader<'r>> {
4172 if self.is_none() {
4173 None
4174 } else {
4175 Some(ScriptReader::new_unchecked(self.as_slice()))
4176 }
4177 }
4178}
4179impl<'r> molecule::prelude::Reader<'r> for ScriptOptReader<'r> {
4180 type Entity = ScriptOpt;
4181 const NAME: &'static str = "ScriptOptReader";
4182 fn to_entity(&self) -> Self::Entity {
4183 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4184 }
4185 fn new_unchecked(slice: &'r [u8]) -> Self {
4186 ScriptOptReader(slice)
4187 }
4188 fn as_slice(&self) -> &'r [u8] {
4189 self.0
4190 }
4191 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4192 if !slice.is_empty() {
4193 ScriptReader::verify(&slice[..], compatible)?;
4194 }
4195 Ok(())
4196 }
4197}
4198#[derive(Clone, Debug, Default)]
4199pub struct ScriptOptBuilder(pub(crate) Option<Script>);
4200impl ScriptOptBuilder {
4201 pub fn set<T>(mut self, v: T) -> Self
4202 where
4203 T: ::core::convert::Into<Option<Script>>,
4204 {
4205 self.0 = v.into();
4206 self
4207 }
4208}
4209impl molecule::prelude::Builder for ScriptOptBuilder {
4210 type Entity = ScriptOpt;
4211 const NAME: &'static str = "ScriptOptBuilder";
4212 fn expected_length(&self) -> usize {
4213 self.0
4214 .as_ref()
4215 .map(|ref inner| inner.as_slice().len())
4216 .unwrap_or(0)
4217 }
4218 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4219 self.0
4220 .as_ref()
4221 .map(|ref inner| writer.write_all(inner.as_slice()))
4222 .unwrap_or(Ok(()))
4223 }
4224 fn build(&self) -> Self::Entity {
4225 let mut inner = Vec::with_capacity(self.expected_length());
4226 self.write(&mut inner)
4227 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4228 ScriptOpt::new_unchecked(inner.into())
4229 }
4230}
4231impl From<Script> for ScriptOpt {
4232 fn from(value: Script) -> Self {
4233 Self::new_builder().set(Some(value)).build()
4234 }
4235}
4236#[derive(Clone)]
4237pub struct ProposalShortId(molecule::bytes::Bytes);
4238impl ::core::fmt::LowerHex for ProposalShortId {
4239 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4240 use molecule::hex_string;
4241 if f.alternate() {
4242 write!(f, "0x")?;
4243 }
4244 write!(f, "{}", hex_string(self.as_slice()))
4245 }
4246}
4247impl ::core::fmt::Debug for ProposalShortId {
4248 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4249 write!(f, "{}({:#x})", Self::NAME, self)
4250 }
4251}
4252impl ::core::fmt::Display for ProposalShortId {
4253 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4254 use molecule::hex_string;
4255 let raw_data = hex_string(&self.raw_data());
4256 write!(f, "{}(0x{})", Self::NAME, raw_data)
4257 }
4258}
4259impl ::core::default::Default for ProposalShortId {
4260 fn default() -> Self {
4261 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4262 ProposalShortId::new_unchecked(v)
4263 }
4264}
4265impl ProposalShortId {
4266 const DEFAULT_VALUE: [u8; 10] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
4267 pub const TOTAL_SIZE: usize = 10;
4268 pub const ITEM_SIZE: usize = 1;
4269 pub const ITEM_COUNT: usize = 10;
4270 pub fn nth0(&self) -> Byte {
4271 Byte::new_unchecked(self.0.slice(0..1))
4272 }
4273 pub fn nth1(&self) -> Byte {
4274 Byte::new_unchecked(self.0.slice(1..2))
4275 }
4276 pub fn nth2(&self) -> Byte {
4277 Byte::new_unchecked(self.0.slice(2..3))
4278 }
4279 pub fn nth3(&self) -> Byte {
4280 Byte::new_unchecked(self.0.slice(3..4))
4281 }
4282 pub fn nth4(&self) -> Byte {
4283 Byte::new_unchecked(self.0.slice(4..5))
4284 }
4285 pub fn nth5(&self) -> Byte {
4286 Byte::new_unchecked(self.0.slice(5..6))
4287 }
4288 pub fn nth6(&self) -> Byte {
4289 Byte::new_unchecked(self.0.slice(6..7))
4290 }
4291 pub fn nth7(&self) -> Byte {
4292 Byte::new_unchecked(self.0.slice(7..8))
4293 }
4294 pub fn nth8(&self) -> Byte {
4295 Byte::new_unchecked(self.0.slice(8..9))
4296 }
4297 pub fn nth9(&self) -> Byte {
4298 Byte::new_unchecked(self.0.slice(9..10))
4299 }
4300 pub fn raw_data(&self) -> molecule::bytes::Bytes {
4301 self.as_bytes()
4302 }
4303 pub fn as_reader<'r>(&'r self) -> ProposalShortIdReader<'r> {
4304 ProposalShortIdReader::new_unchecked(self.as_slice())
4305 }
4306}
4307impl molecule::prelude::Entity for ProposalShortId {
4308 type Builder = ProposalShortIdBuilder;
4309 const NAME: &'static str = "ProposalShortId";
4310 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4311 ProposalShortId(data)
4312 }
4313 fn as_bytes(&self) -> molecule::bytes::Bytes {
4314 self.0.clone()
4315 }
4316 fn as_slice(&self) -> &[u8] {
4317 &self.0[..]
4318 }
4319 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4320 ProposalShortIdReader::from_slice(slice).map(|reader| reader.to_entity())
4321 }
4322 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4323 ProposalShortIdReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4324 }
4325 fn new_builder() -> Self::Builder {
4326 ::core::default::Default::default()
4327 }
4328 fn as_builder(self) -> Self::Builder {
4329 Self::new_builder().set([
4330 self.nth0(),
4331 self.nth1(),
4332 self.nth2(),
4333 self.nth3(),
4334 self.nth4(),
4335 self.nth5(),
4336 self.nth6(),
4337 self.nth7(),
4338 self.nth8(),
4339 self.nth9(),
4340 ])
4341 }
4342}
4343#[derive(Clone, Copy)]
4344pub struct ProposalShortIdReader<'r>(&'r [u8]);
4345impl<'r> ::core::fmt::LowerHex for ProposalShortIdReader<'r> {
4346 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4347 use molecule::hex_string;
4348 if f.alternate() {
4349 write!(f, "0x")?;
4350 }
4351 write!(f, "{}", hex_string(self.as_slice()))
4352 }
4353}
4354impl<'r> ::core::fmt::Debug for ProposalShortIdReader<'r> {
4355 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4356 write!(f, "{}({:#x})", Self::NAME, self)
4357 }
4358}
4359impl<'r> ::core::fmt::Display for ProposalShortIdReader<'r> {
4360 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4361 use molecule::hex_string;
4362 let raw_data = hex_string(&self.raw_data());
4363 write!(f, "{}(0x{})", Self::NAME, raw_data)
4364 }
4365}
4366impl<'r> ProposalShortIdReader<'r> {
4367 pub const TOTAL_SIZE: usize = 10;
4368 pub const ITEM_SIZE: usize = 1;
4369 pub const ITEM_COUNT: usize = 10;
4370 pub fn nth0(&self) -> ByteReader<'r> {
4371 ByteReader::new_unchecked(&self.as_slice()[0..1])
4372 }
4373 pub fn nth1(&self) -> ByteReader<'r> {
4374 ByteReader::new_unchecked(&self.as_slice()[1..2])
4375 }
4376 pub fn nth2(&self) -> ByteReader<'r> {
4377 ByteReader::new_unchecked(&self.as_slice()[2..3])
4378 }
4379 pub fn nth3(&self) -> ByteReader<'r> {
4380 ByteReader::new_unchecked(&self.as_slice()[3..4])
4381 }
4382 pub fn nth4(&self) -> ByteReader<'r> {
4383 ByteReader::new_unchecked(&self.as_slice()[4..5])
4384 }
4385 pub fn nth5(&self) -> ByteReader<'r> {
4386 ByteReader::new_unchecked(&self.as_slice()[5..6])
4387 }
4388 pub fn nth6(&self) -> ByteReader<'r> {
4389 ByteReader::new_unchecked(&self.as_slice()[6..7])
4390 }
4391 pub fn nth7(&self) -> ByteReader<'r> {
4392 ByteReader::new_unchecked(&self.as_slice()[7..8])
4393 }
4394 pub fn nth8(&self) -> ByteReader<'r> {
4395 ByteReader::new_unchecked(&self.as_slice()[8..9])
4396 }
4397 pub fn nth9(&self) -> ByteReader<'r> {
4398 ByteReader::new_unchecked(&self.as_slice()[9..10])
4399 }
4400 pub fn raw_data(&self) -> &'r [u8] {
4401 self.as_slice()
4402 }
4403}
4404impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdReader<'r> {
4405 type Entity = ProposalShortId;
4406 const NAME: &'static str = "ProposalShortIdReader";
4407 fn to_entity(&self) -> Self::Entity {
4408 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4409 }
4410 fn new_unchecked(slice: &'r [u8]) -> Self {
4411 ProposalShortIdReader(slice)
4412 }
4413 fn as_slice(&self) -> &'r [u8] {
4414 self.0
4415 }
4416 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
4417 use molecule::verification_error as ve;
4418 let slice_len = slice.len();
4419 if slice_len != Self::TOTAL_SIZE {
4420 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
4421 }
4422 Ok(())
4423 }
4424}
4425#[derive(Clone)]
4426pub struct ProposalShortIdBuilder(pub(crate) [Byte; 10]);
4427impl ::core::fmt::Debug for ProposalShortIdBuilder {
4428 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4429 write!(f, "{}({:?})", Self::NAME, &self.0[..])
4430 }
4431}
4432impl ::core::default::Default for ProposalShortIdBuilder {
4433 fn default() -> Self {
4434 ProposalShortIdBuilder([
4435 Byte::default(),
4436 Byte::default(),
4437 Byte::default(),
4438 Byte::default(),
4439 Byte::default(),
4440 Byte::default(),
4441 Byte::default(),
4442 Byte::default(),
4443 Byte::default(),
4444 Byte::default(),
4445 ])
4446 }
4447}
4448impl ProposalShortIdBuilder {
4449 pub const TOTAL_SIZE: usize = 10;
4450 pub const ITEM_SIZE: usize = 1;
4451 pub const ITEM_COUNT: usize = 10;
4452 pub fn set<T>(mut self, v: T) -> Self
4453 where
4454 T: ::core::convert::Into<[Byte; 10]>,
4455 {
4456 self.0 = v.into();
4457 self
4458 }
4459 pub fn nth0<T>(mut self, v: T) -> Self
4460 where
4461 T: ::core::convert::Into<Byte>,
4462 {
4463 self.0[0] = v.into();
4464 self
4465 }
4466 pub fn nth1<T>(mut self, v: T) -> Self
4467 where
4468 T: ::core::convert::Into<Byte>,
4469 {
4470 self.0[1] = v.into();
4471 self
4472 }
4473 pub fn nth2<T>(mut self, v: T) -> Self
4474 where
4475 T: ::core::convert::Into<Byte>,
4476 {
4477 self.0[2] = v.into();
4478 self
4479 }
4480 pub fn nth3<T>(mut self, v: T) -> Self
4481 where
4482 T: ::core::convert::Into<Byte>,
4483 {
4484 self.0[3] = v.into();
4485 self
4486 }
4487 pub fn nth4<T>(mut self, v: T) -> Self
4488 where
4489 T: ::core::convert::Into<Byte>,
4490 {
4491 self.0[4] = v.into();
4492 self
4493 }
4494 pub fn nth5<T>(mut self, v: T) -> Self
4495 where
4496 T: ::core::convert::Into<Byte>,
4497 {
4498 self.0[5] = v.into();
4499 self
4500 }
4501 pub fn nth6<T>(mut self, v: T) -> Self
4502 where
4503 T: ::core::convert::Into<Byte>,
4504 {
4505 self.0[6] = v.into();
4506 self
4507 }
4508 pub fn nth7<T>(mut self, v: T) -> Self
4509 where
4510 T: ::core::convert::Into<Byte>,
4511 {
4512 self.0[7] = v.into();
4513 self
4514 }
4515 pub fn nth8<T>(mut self, v: T) -> Self
4516 where
4517 T: ::core::convert::Into<Byte>,
4518 {
4519 self.0[8] = v.into();
4520 self
4521 }
4522 pub fn nth9<T>(mut self, v: T) -> Self
4523 where
4524 T: ::core::convert::Into<Byte>,
4525 {
4526 self.0[9] = v.into();
4527 self
4528 }
4529}
4530impl molecule::prelude::Builder for ProposalShortIdBuilder {
4531 type Entity = ProposalShortId;
4532 const NAME: &'static str = "ProposalShortIdBuilder";
4533 fn expected_length(&self) -> usize {
4534 Self::TOTAL_SIZE
4535 }
4536 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4537 writer.write_all(self.0[0].as_slice())?;
4538 writer.write_all(self.0[1].as_slice())?;
4539 writer.write_all(self.0[2].as_slice())?;
4540 writer.write_all(self.0[3].as_slice())?;
4541 writer.write_all(self.0[4].as_slice())?;
4542 writer.write_all(self.0[5].as_slice())?;
4543 writer.write_all(self.0[6].as_slice())?;
4544 writer.write_all(self.0[7].as_slice())?;
4545 writer.write_all(self.0[8].as_slice())?;
4546 writer.write_all(self.0[9].as_slice())?;
4547 Ok(())
4548 }
4549 fn build(&self) -> Self::Entity {
4550 let mut inner = Vec::with_capacity(self.expected_length());
4551 self.write(&mut inner)
4552 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4553 ProposalShortId::new_unchecked(inner.into())
4554 }
4555}
4556impl From<[Byte; 10usize]> for ProposalShortId {
4557 fn from(value: [Byte; 10usize]) -> Self {
4558 Self::new_builder().set(value).build()
4559 }
4560}
4561impl ::core::convert::TryFrom<&[Byte]> for ProposalShortId {
4562 type Error = ::core::array::TryFromSliceError;
4563 fn try_from(value: &[Byte]) -> Result<Self, ::core::array::TryFromSliceError> {
4564 Ok(Self::new_builder()
4565 .set(<&[Byte; 10usize]>::try_from(value)?.clone())
4566 .build())
4567 }
4568}
4569impl From<ProposalShortId> for [Byte; 10usize] {
4570 #[track_caller]
4571 fn from(value: ProposalShortId) -> Self {
4572 [
4573 value.nth0(),
4574 value.nth1(),
4575 value.nth2(),
4576 value.nth3(),
4577 value.nth4(),
4578 value.nth5(),
4579 value.nth6(),
4580 value.nth7(),
4581 value.nth8(),
4582 value.nth9(),
4583 ]
4584 }
4585}
4586impl From<[u8; 10usize]> for ProposalShortId {
4587 fn from(value: [u8; 10usize]) -> Self {
4588 ProposalShortIdReader::new_unchecked(&value).to_entity()
4589 }
4590}
4591impl ::core::convert::TryFrom<&[u8]> for ProposalShortId {
4592 type Error = ::core::array::TryFromSliceError;
4593 fn try_from(value: &[u8]) -> Result<Self, ::core::array::TryFromSliceError> {
4594 Ok(<[u8; 10usize]>::try_from(value)?.into())
4595 }
4596}
4597impl From<ProposalShortId> for [u8; 10usize] {
4598 #[track_caller]
4599 fn from(value: ProposalShortId) -> Self {
4600 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
4601 }
4602}
4603impl<'a> From<ProposalShortIdReader<'a>> for &'a [u8; 10usize] {
4604 #[track_caller]
4605 fn from(value: ProposalShortIdReader<'a>) -> Self {
4606 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
4607 }
4608}
4609impl<'a> From<&'a ProposalShortIdReader<'a>> for &'a [u8; 10usize] {
4610 #[track_caller]
4611 fn from(value: &'a ProposalShortIdReader<'a>) -> Self {
4612 ::core::convert::TryFrom::try_from(value.as_slice()).unwrap()
4613 }
4614}
4615#[derive(Clone)]
4616pub struct UncleBlockVec(molecule::bytes::Bytes);
4617impl ::core::fmt::LowerHex for UncleBlockVec {
4618 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4619 use molecule::hex_string;
4620 if f.alternate() {
4621 write!(f, "0x")?;
4622 }
4623 write!(f, "{}", hex_string(self.as_slice()))
4624 }
4625}
4626impl ::core::fmt::Debug for UncleBlockVec {
4627 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4628 write!(f, "{}({:#x})", Self::NAME, self)
4629 }
4630}
4631impl ::core::fmt::Display for UncleBlockVec {
4632 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4633 write!(f, "{} [", Self::NAME)?;
4634 for i in 0..self.len() {
4635 if i == 0 {
4636 write!(f, "{}", self.get_unchecked(i))?;
4637 } else {
4638 write!(f, ", {}", self.get_unchecked(i))?;
4639 }
4640 }
4641 write!(f, "]")
4642 }
4643}
4644impl ::core::default::Default for UncleBlockVec {
4645 fn default() -> Self {
4646 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
4647 UncleBlockVec::new_unchecked(v)
4648 }
4649}
4650impl UncleBlockVec {
4651 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
4652 pub fn total_size(&self) -> usize {
4653 molecule::unpack_number(self.as_slice()) as usize
4654 }
4655 pub fn item_count(&self) -> usize {
4656 if self.total_size() == molecule::NUMBER_SIZE {
4657 0
4658 } else {
4659 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4660 }
4661 }
4662 pub fn len(&self) -> usize {
4663 self.item_count()
4664 }
4665 pub fn is_empty(&self) -> bool {
4666 self.len() == 0
4667 }
4668 pub fn get(&self, idx: usize) -> Option<UncleBlock> {
4669 if idx >= self.len() {
4670 None
4671 } else {
4672 Some(self.get_unchecked(idx))
4673 }
4674 }
4675 pub fn get_unchecked(&self, idx: usize) -> UncleBlock {
4676 let slice = self.as_slice();
4677 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4678 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4679 if idx == self.len() - 1 {
4680 UncleBlock::new_unchecked(self.0.slice(start..))
4681 } else {
4682 let end_idx = start_idx + molecule::NUMBER_SIZE;
4683 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4684 UncleBlock::new_unchecked(self.0.slice(start..end))
4685 }
4686 }
4687 pub fn as_reader<'r>(&'r self) -> UncleBlockVecReader<'r> {
4688 UncleBlockVecReader::new_unchecked(self.as_slice())
4689 }
4690}
4691impl molecule::prelude::Entity for UncleBlockVec {
4692 type Builder = UncleBlockVecBuilder;
4693 const NAME: &'static str = "UncleBlockVec";
4694 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
4695 UncleBlockVec(data)
4696 }
4697 fn as_bytes(&self) -> molecule::bytes::Bytes {
4698 self.0.clone()
4699 }
4700 fn as_slice(&self) -> &[u8] {
4701 &self.0[..]
4702 }
4703 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4704 UncleBlockVecReader::from_slice(slice).map(|reader| reader.to_entity())
4705 }
4706 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
4707 UncleBlockVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
4708 }
4709 fn new_builder() -> Self::Builder {
4710 ::core::default::Default::default()
4711 }
4712 fn as_builder(self) -> Self::Builder {
4713 Self::new_builder().extend(self.into_iter())
4714 }
4715}
4716#[derive(Clone, Copy)]
4717pub struct UncleBlockVecReader<'r>(&'r [u8]);
4718impl<'r> ::core::fmt::LowerHex for UncleBlockVecReader<'r> {
4719 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4720 use molecule::hex_string;
4721 if f.alternate() {
4722 write!(f, "0x")?;
4723 }
4724 write!(f, "{}", hex_string(self.as_slice()))
4725 }
4726}
4727impl<'r> ::core::fmt::Debug for UncleBlockVecReader<'r> {
4728 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4729 write!(f, "{}({:#x})", Self::NAME, self)
4730 }
4731}
4732impl<'r> ::core::fmt::Display for UncleBlockVecReader<'r> {
4733 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4734 write!(f, "{} [", Self::NAME)?;
4735 for i in 0..self.len() {
4736 if i == 0 {
4737 write!(f, "{}", self.get_unchecked(i))?;
4738 } else {
4739 write!(f, ", {}", self.get_unchecked(i))?;
4740 }
4741 }
4742 write!(f, "]")
4743 }
4744}
4745impl<'r> UncleBlockVecReader<'r> {
4746 pub fn total_size(&self) -> usize {
4747 molecule::unpack_number(self.as_slice()) as usize
4748 }
4749 pub fn item_count(&self) -> usize {
4750 if self.total_size() == molecule::NUMBER_SIZE {
4751 0
4752 } else {
4753 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
4754 }
4755 }
4756 pub fn len(&self) -> usize {
4757 self.item_count()
4758 }
4759 pub fn is_empty(&self) -> bool {
4760 self.len() == 0
4761 }
4762 pub fn get(&self, idx: usize) -> Option<UncleBlockReader<'r>> {
4763 if idx >= self.len() {
4764 None
4765 } else {
4766 Some(self.get_unchecked(idx))
4767 }
4768 }
4769 pub fn get_unchecked(&self, idx: usize) -> UncleBlockReader<'r> {
4770 let slice = self.as_slice();
4771 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
4772 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
4773 if idx == self.len() - 1 {
4774 UncleBlockReader::new_unchecked(&self.as_slice()[start..])
4775 } else {
4776 let end_idx = start_idx + molecule::NUMBER_SIZE;
4777 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
4778 UncleBlockReader::new_unchecked(&self.as_slice()[start..end])
4779 }
4780 }
4781}
4782impl<'r> molecule::prelude::Reader<'r> for UncleBlockVecReader<'r> {
4783 type Entity = UncleBlockVec;
4784 const NAME: &'static str = "UncleBlockVecReader";
4785 fn to_entity(&self) -> Self::Entity {
4786 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
4787 }
4788 fn new_unchecked(slice: &'r [u8]) -> Self {
4789 UncleBlockVecReader(slice)
4790 }
4791 fn as_slice(&self) -> &'r [u8] {
4792 self.0
4793 }
4794 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
4795 use molecule::verification_error as ve;
4796 let slice_len = slice.len();
4797 if slice_len < molecule::NUMBER_SIZE {
4798 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
4799 }
4800 let total_size = molecule::unpack_number(slice) as usize;
4801 if slice_len != total_size {
4802 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
4803 }
4804 if slice_len == molecule::NUMBER_SIZE {
4805 return Ok(());
4806 }
4807 if slice_len < molecule::NUMBER_SIZE * 2 {
4808 return ve!(
4809 Self,
4810 TotalSizeNotMatch,
4811 molecule::NUMBER_SIZE * 2,
4812 slice_len
4813 );
4814 }
4815 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
4816 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
4817 return ve!(Self, OffsetsNotMatch);
4818 }
4819 if slice_len < offset_first {
4820 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
4821 }
4822 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
4823 .chunks_exact(molecule::NUMBER_SIZE)
4824 .map(|x| molecule::unpack_number(x) as usize)
4825 .collect();
4826 offsets.push(total_size);
4827 if offsets.windows(2).any(|i| i[0] > i[1]) {
4828 return ve!(Self, OffsetsNotMatch);
4829 }
4830 for pair in offsets.windows(2) {
4831 let start = pair[0];
4832 let end = pair[1];
4833 UncleBlockReader::verify(&slice[start..end], compatible)?;
4834 }
4835 Ok(())
4836 }
4837}
4838#[derive(Clone, Debug, Default)]
4839pub struct UncleBlockVecBuilder(pub(crate) Vec<UncleBlock>);
4840impl UncleBlockVecBuilder {
4841 pub fn set(mut self, v: Vec<UncleBlock>) -> Self {
4842 self.0 = v;
4843 self
4844 }
4845 pub fn push<T>(mut self, v: T) -> Self
4846 where
4847 T: ::core::convert::Into<UncleBlock>,
4848 {
4849 self.0.push(v.into());
4850 self
4851 }
4852 pub fn extend<T: ::core::iter::IntoIterator<Item = UncleBlock>>(mut self, iter: T) -> Self {
4853 self.0.extend(iter);
4854 self
4855 }
4856 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<UncleBlock>
4857 where
4858 T: ::core::convert::Into<UncleBlock>,
4859 {
4860 self.0
4861 .get_mut(index)
4862 .map(|item| ::core::mem::replace(item, v.into()))
4863 }
4864}
4865impl molecule::prelude::Builder for UncleBlockVecBuilder {
4866 type Entity = UncleBlockVec;
4867 const NAME: &'static str = "UncleBlockVecBuilder";
4868 fn expected_length(&self) -> usize {
4869 molecule::NUMBER_SIZE * (self.0.len() + 1)
4870 + self
4871 .0
4872 .iter()
4873 .map(|inner| inner.as_slice().len())
4874 .sum::<usize>()
4875 }
4876 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
4877 let item_count = self.0.len();
4878 if item_count == 0 {
4879 writer.write_all(&molecule::pack_number(
4880 molecule::NUMBER_SIZE as molecule::Number,
4881 ))?;
4882 } else {
4883 let (total_size, offsets) = self.0.iter().fold(
4884 (
4885 molecule::NUMBER_SIZE * (item_count + 1),
4886 Vec::with_capacity(item_count),
4887 ),
4888 |(start, mut offsets), inner| {
4889 offsets.push(start);
4890 (start + inner.as_slice().len(), offsets)
4891 },
4892 );
4893 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
4894 for offset in offsets.into_iter() {
4895 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
4896 }
4897 for inner in self.0.iter() {
4898 writer.write_all(inner.as_slice())?;
4899 }
4900 }
4901 Ok(())
4902 }
4903 fn build(&self) -> Self::Entity {
4904 let mut inner = Vec::with_capacity(self.expected_length());
4905 self.write(&mut inner)
4906 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
4907 UncleBlockVec::new_unchecked(inner.into())
4908 }
4909}
4910pub struct UncleBlockVecIterator(UncleBlockVec, usize, usize);
4911impl ::core::iter::Iterator for UncleBlockVecIterator {
4912 type Item = UncleBlock;
4913 fn next(&mut self) -> Option<Self::Item> {
4914 if self.1 >= self.2 {
4915 None
4916 } else {
4917 let ret = self.0.get_unchecked(self.1);
4918 self.1 += 1;
4919 Some(ret)
4920 }
4921 }
4922}
4923impl ::core::iter::ExactSizeIterator for UncleBlockVecIterator {
4924 fn len(&self) -> usize {
4925 self.2 - self.1
4926 }
4927}
4928impl ::core::iter::IntoIterator for UncleBlockVec {
4929 type Item = UncleBlock;
4930 type IntoIter = UncleBlockVecIterator;
4931 fn into_iter(self) -> Self::IntoIter {
4932 let len = self.len();
4933 UncleBlockVecIterator(self, 0, len)
4934 }
4935}
4936impl<'r> UncleBlockVecReader<'r> {
4937 pub fn iter<'t>(&'t self) -> UncleBlockVecReaderIterator<'t, 'r> {
4938 UncleBlockVecReaderIterator(&self, 0, self.len())
4939 }
4940}
4941pub struct UncleBlockVecReaderIterator<'t, 'r>(&'t UncleBlockVecReader<'r>, usize, usize);
4942impl<'t: 'r, 'r> ::core::iter::Iterator for UncleBlockVecReaderIterator<'t, 'r> {
4943 type Item = UncleBlockReader<'t>;
4944 fn next(&mut self) -> Option<Self::Item> {
4945 if self.1 >= self.2 {
4946 None
4947 } else {
4948 let ret = self.0.get_unchecked(self.1);
4949 self.1 += 1;
4950 Some(ret)
4951 }
4952 }
4953}
4954impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for UncleBlockVecReaderIterator<'t, 'r> {
4955 fn len(&self) -> usize {
4956 self.2 - self.1
4957 }
4958}
4959impl<T> ::core::iter::FromIterator<T> for UncleBlockVec
4960where
4961 T: Into<UncleBlock>,
4962{
4963 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
4964 Self::new_builder()
4965 .extend(iter.into_iter().map(Into::into))
4966 .build()
4967 }
4968}
4969impl<T> From<Vec<T>> for UncleBlockVec
4970where
4971 T: Into<UncleBlock>,
4972{
4973 fn from(v: Vec<T>) -> Self {
4974 v.into_iter().collect()
4975 }
4976}
4977#[derive(Clone)]
4978pub struct TransactionVec(molecule::bytes::Bytes);
4979impl ::core::fmt::LowerHex for TransactionVec {
4980 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4981 use molecule::hex_string;
4982 if f.alternate() {
4983 write!(f, "0x")?;
4984 }
4985 write!(f, "{}", hex_string(self.as_slice()))
4986 }
4987}
4988impl ::core::fmt::Debug for TransactionVec {
4989 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4990 write!(f, "{}({:#x})", Self::NAME, self)
4991 }
4992}
4993impl ::core::fmt::Display for TransactionVec {
4994 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4995 write!(f, "{} [", Self::NAME)?;
4996 for i in 0..self.len() {
4997 if i == 0 {
4998 write!(f, "{}", self.get_unchecked(i))?;
4999 } else {
5000 write!(f, ", {}", self.get_unchecked(i))?;
5001 }
5002 }
5003 write!(f, "]")
5004 }
5005}
5006impl ::core::default::Default for TransactionVec {
5007 fn default() -> Self {
5008 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5009 TransactionVec::new_unchecked(v)
5010 }
5011}
5012impl TransactionVec {
5013 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
5014 pub fn total_size(&self) -> usize {
5015 molecule::unpack_number(self.as_slice()) as usize
5016 }
5017 pub fn item_count(&self) -> usize {
5018 if self.total_size() == molecule::NUMBER_SIZE {
5019 0
5020 } else {
5021 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5022 }
5023 }
5024 pub fn len(&self) -> usize {
5025 self.item_count()
5026 }
5027 pub fn is_empty(&self) -> bool {
5028 self.len() == 0
5029 }
5030 pub fn get(&self, idx: usize) -> Option<Transaction> {
5031 if idx >= self.len() {
5032 None
5033 } else {
5034 Some(self.get_unchecked(idx))
5035 }
5036 }
5037 pub fn get_unchecked(&self, idx: usize) -> Transaction {
5038 let slice = self.as_slice();
5039 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
5040 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
5041 if idx == self.len() - 1 {
5042 Transaction::new_unchecked(self.0.slice(start..))
5043 } else {
5044 let end_idx = start_idx + molecule::NUMBER_SIZE;
5045 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
5046 Transaction::new_unchecked(self.0.slice(start..end))
5047 }
5048 }
5049 pub fn as_reader<'r>(&'r self) -> TransactionVecReader<'r> {
5050 TransactionVecReader::new_unchecked(self.as_slice())
5051 }
5052}
5053impl molecule::prelude::Entity for TransactionVec {
5054 type Builder = TransactionVecBuilder;
5055 const NAME: &'static str = "TransactionVec";
5056 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5057 TransactionVec(data)
5058 }
5059 fn as_bytes(&self) -> molecule::bytes::Bytes {
5060 self.0.clone()
5061 }
5062 fn as_slice(&self) -> &[u8] {
5063 &self.0[..]
5064 }
5065 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5066 TransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
5067 }
5068 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5069 TransactionVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5070 }
5071 fn new_builder() -> Self::Builder {
5072 ::core::default::Default::default()
5073 }
5074 fn as_builder(self) -> Self::Builder {
5075 Self::new_builder().extend(self.into_iter())
5076 }
5077}
5078#[derive(Clone, Copy)]
5079pub struct TransactionVecReader<'r>(&'r [u8]);
5080impl<'r> ::core::fmt::LowerHex for TransactionVecReader<'r> {
5081 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5082 use molecule::hex_string;
5083 if f.alternate() {
5084 write!(f, "0x")?;
5085 }
5086 write!(f, "{}", hex_string(self.as_slice()))
5087 }
5088}
5089impl<'r> ::core::fmt::Debug for TransactionVecReader<'r> {
5090 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5091 write!(f, "{}({:#x})", Self::NAME, self)
5092 }
5093}
5094impl<'r> ::core::fmt::Display for TransactionVecReader<'r> {
5095 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5096 write!(f, "{} [", Self::NAME)?;
5097 for i in 0..self.len() {
5098 if i == 0 {
5099 write!(f, "{}", self.get_unchecked(i))?;
5100 } else {
5101 write!(f, ", {}", self.get_unchecked(i))?;
5102 }
5103 }
5104 write!(f, "]")
5105 }
5106}
5107impl<'r> TransactionVecReader<'r> {
5108 pub fn total_size(&self) -> usize {
5109 molecule::unpack_number(self.as_slice()) as usize
5110 }
5111 pub fn item_count(&self) -> usize {
5112 if self.total_size() == molecule::NUMBER_SIZE {
5113 0
5114 } else {
5115 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
5116 }
5117 }
5118 pub fn len(&self) -> usize {
5119 self.item_count()
5120 }
5121 pub fn is_empty(&self) -> bool {
5122 self.len() == 0
5123 }
5124 pub fn get(&self, idx: usize) -> Option<TransactionReader<'r>> {
5125 if idx >= self.len() {
5126 None
5127 } else {
5128 Some(self.get_unchecked(idx))
5129 }
5130 }
5131 pub fn get_unchecked(&self, idx: usize) -> TransactionReader<'r> {
5132 let slice = self.as_slice();
5133 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
5134 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
5135 if idx == self.len() - 1 {
5136 TransactionReader::new_unchecked(&self.as_slice()[start..])
5137 } else {
5138 let end_idx = start_idx + molecule::NUMBER_SIZE;
5139 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
5140 TransactionReader::new_unchecked(&self.as_slice()[start..end])
5141 }
5142 }
5143}
5144impl<'r> molecule::prelude::Reader<'r> for TransactionVecReader<'r> {
5145 type Entity = TransactionVec;
5146 const NAME: &'static str = "TransactionVecReader";
5147 fn to_entity(&self) -> Self::Entity {
5148 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5149 }
5150 fn new_unchecked(slice: &'r [u8]) -> Self {
5151 TransactionVecReader(slice)
5152 }
5153 fn as_slice(&self) -> &'r [u8] {
5154 self.0
5155 }
5156 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
5157 use molecule::verification_error as ve;
5158 let slice_len = slice.len();
5159 if slice_len < molecule::NUMBER_SIZE {
5160 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5161 }
5162 let total_size = molecule::unpack_number(slice) as usize;
5163 if slice_len != total_size {
5164 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5165 }
5166 if slice_len == molecule::NUMBER_SIZE {
5167 return Ok(());
5168 }
5169 if slice_len < molecule::NUMBER_SIZE * 2 {
5170 return ve!(
5171 Self,
5172 TotalSizeNotMatch,
5173 molecule::NUMBER_SIZE * 2,
5174 slice_len
5175 );
5176 }
5177 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
5178 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
5179 return ve!(Self, OffsetsNotMatch);
5180 }
5181 if slice_len < offset_first {
5182 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
5183 }
5184 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
5185 .chunks_exact(molecule::NUMBER_SIZE)
5186 .map(|x| molecule::unpack_number(x) as usize)
5187 .collect();
5188 offsets.push(total_size);
5189 if offsets.windows(2).any(|i| i[0] > i[1]) {
5190 return ve!(Self, OffsetsNotMatch);
5191 }
5192 for pair in offsets.windows(2) {
5193 let start = pair[0];
5194 let end = pair[1];
5195 TransactionReader::verify(&slice[start..end], compatible)?;
5196 }
5197 Ok(())
5198 }
5199}
5200#[derive(Clone, Debug, Default)]
5201pub struct TransactionVecBuilder(pub(crate) Vec<Transaction>);
5202impl TransactionVecBuilder {
5203 pub fn set(mut self, v: Vec<Transaction>) -> Self {
5204 self.0 = v;
5205 self
5206 }
5207 pub fn push<T>(mut self, v: T) -> Self
5208 where
5209 T: ::core::convert::Into<Transaction>,
5210 {
5211 self.0.push(v.into());
5212 self
5213 }
5214 pub fn extend<T: ::core::iter::IntoIterator<Item = Transaction>>(mut self, iter: T) -> Self {
5215 self.0.extend(iter);
5216 self
5217 }
5218 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<Transaction>
5219 where
5220 T: ::core::convert::Into<Transaction>,
5221 {
5222 self.0
5223 .get_mut(index)
5224 .map(|item| ::core::mem::replace(item, v.into()))
5225 }
5226}
5227impl molecule::prelude::Builder for TransactionVecBuilder {
5228 type Entity = TransactionVec;
5229 const NAME: &'static str = "TransactionVecBuilder";
5230 fn expected_length(&self) -> usize {
5231 molecule::NUMBER_SIZE * (self.0.len() + 1)
5232 + self
5233 .0
5234 .iter()
5235 .map(|inner| inner.as_slice().len())
5236 .sum::<usize>()
5237 }
5238 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5239 let item_count = self.0.len();
5240 if item_count == 0 {
5241 writer.write_all(&molecule::pack_number(
5242 molecule::NUMBER_SIZE as molecule::Number,
5243 ))?;
5244 } else {
5245 let (total_size, offsets) = self.0.iter().fold(
5246 (
5247 molecule::NUMBER_SIZE * (item_count + 1),
5248 Vec::with_capacity(item_count),
5249 ),
5250 |(start, mut offsets), inner| {
5251 offsets.push(start);
5252 (start + inner.as_slice().len(), offsets)
5253 },
5254 );
5255 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
5256 for offset in offsets.into_iter() {
5257 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
5258 }
5259 for inner in self.0.iter() {
5260 writer.write_all(inner.as_slice())?;
5261 }
5262 }
5263 Ok(())
5264 }
5265 fn build(&self) -> Self::Entity {
5266 let mut inner = Vec::with_capacity(self.expected_length());
5267 self.write(&mut inner)
5268 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5269 TransactionVec::new_unchecked(inner.into())
5270 }
5271}
5272pub struct TransactionVecIterator(TransactionVec, usize, usize);
5273impl ::core::iter::Iterator for TransactionVecIterator {
5274 type Item = Transaction;
5275 fn next(&mut self) -> Option<Self::Item> {
5276 if self.1 >= self.2 {
5277 None
5278 } else {
5279 let ret = self.0.get_unchecked(self.1);
5280 self.1 += 1;
5281 Some(ret)
5282 }
5283 }
5284}
5285impl ::core::iter::ExactSizeIterator for TransactionVecIterator {
5286 fn len(&self) -> usize {
5287 self.2 - self.1
5288 }
5289}
5290impl ::core::iter::IntoIterator for TransactionVec {
5291 type Item = Transaction;
5292 type IntoIter = TransactionVecIterator;
5293 fn into_iter(self) -> Self::IntoIter {
5294 let len = self.len();
5295 TransactionVecIterator(self, 0, len)
5296 }
5297}
5298impl<'r> TransactionVecReader<'r> {
5299 pub fn iter<'t>(&'t self) -> TransactionVecReaderIterator<'t, 'r> {
5300 TransactionVecReaderIterator(&self, 0, self.len())
5301 }
5302}
5303pub struct TransactionVecReaderIterator<'t, 'r>(&'t TransactionVecReader<'r>, usize, usize);
5304impl<'t: 'r, 'r> ::core::iter::Iterator for TransactionVecReaderIterator<'t, 'r> {
5305 type Item = TransactionReader<'t>;
5306 fn next(&mut self) -> Option<Self::Item> {
5307 if self.1 >= self.2 {
5308 None
5309 } else {
5310 let ret = self.0.get_unchecked(self.1);
5311 self.1 += 1;
5312 Some(ret)
5313 }
5314 }
5315}
5316impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for TransactionVecReaderIterator<'t, 'r> {
5317 fn len(&self) -> usize {
5318 self.2 - self.1
5319 }
5320}
5321impl<T> ::core::iter::FromIterator<T> for TransactionVec
5322where
5323 T: Into<Transaction>,
5324{
5325 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
5326 Self::new_builder()
5327 .extend(iter.into_iter().map(Into::into))
5328 .build()
5329 }
5330}
5331impl<T> From<Vec<T>> for TransactionVec
5332where
5333 T: Into<Transaction>,
5334{
5335 fn from(v: Vec<T>) -> Self {
5336 v.into_iter().collect()
5337 }
5338}
5339#[derive(Clone)]
5340pub struct ProposalShortIdVec(molecule::bytes::Bytes);
5341impl ::core::fmt::LowerHex for ProposalShortIdVec {
5342 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5343 use molecule::hex_string;
5344 if f.alternate() {
5345 write!(f, "0x")?;
5346 }
5347 write!(f, "{}", hex_string(self.as_slice()))
5348 }
5349}
5350impl ::core::fmt::Debug for ProposalShortIdVec {
5351 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5352 write!(f, "{}({:#x})", Self::NAME, self)
5353 }
5354}
5355impl ::core::fmt::Display for ProposalShortIdVec {
5356 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5357 write!(f, "{} [", Self::NAME)?;
5358 for i in 0..self.len() {
5359 if i == 0 {
5360 write!(f, "{}", self.get_unchecked(i))?;
5361 } else {
5362 write!(f, ", {}", self.get_unchecked(i))?;
5363 }
5364 }
5365 write!(f, "]")
5366 }
5367}
5368impl ::core::default::Default for ProposalShortIdVec {
5369 fn default() -> Self {
5370 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5371 ProposalShortIdVec::new_unchecked(v)
5372 }
5373}
5374impl ProposalShortIdVec {
5375 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
5376 pub const ITEM_SIZE: usize = 10;
5377 pub fn total_size(&self) -> usize {
5378 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
5379 }
5380 pub fn item_count(&self) -> usize {
5381 molecule::unpack_number(self.as_slice()) as usize
5382 }
5383 pub fn len(&self) -> usize {
5384 self.item_count()
5385 }
5386 pub fn is_empty(&self) -> bool {
5387 self.len() == 0
5388 }
5389 pub fn get(&self, idx: usize) -> Option<ProposalShortId> {
5390 if idx >= self.len() {
5391 None
5392 } else {
5393 Some(self.get_unchecked(idx))
5394 }
5395 }
5396 pub fn get_unchecked(&self, idx: usize) -> ProposalShortId {
5397 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
5398 let end = start + Self::ITEM_SIZE;
5399 ProposalShortId::new_unchecked(self.0.slice(start..end))
5400 }
5401 pub fn as_reader<'r>(&'r self) -> ProposalShortIdVecReader<'r> {
5402 ProposalShortIdVecReader::new_unchecked(self.as_slice())
5403 }
5404}
5405impl molecule::prelude::Entity for ProposalShortIdVec {
5406 type Builder = ProposalShortIdVecBuilder;
5407 const NAME: &'static str = "ProposalShortIdVec";
5408 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5409 ProposalShortIdVec(data)
5410 }
5411 fn as_bytes(&self) -> molecule::bytes::Bytes {
5412 self.0.clone()
5413 }
5414 fn as_slice(&self) -> &[u8] {
5415 &self.0[..]
5416 }
5417 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5418 ProposalShortIdVecReader::from_slice(slice).map(|reader| reader.to_entity())
5419 }
5420 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5421 ProposalShortIdVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5422 }
5423 fn new_builder() -> Self::Builder {
5424 ::core::default::Default::default()
5425 }
5426 fn as_builder(self) -> Self::Builder {
5427 Self::new_builder().extend(self.into_iter())
5428 }
5429}
5430#[derive(Clone, Copy)]
5431pub struct ProposalShortIdVecReader<'r>(&'r [u8]);
5432impl<'r> ::core::fmt::LowerHex for ProposalShortIdVecReader<'r> {
5433 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5434 use molecule::hex_string;
5435 if f.alternate() {
5436 write!(f, "0x")?;
5437 }
5438 write!(f, "{}", hex_string(self.as_slice()))
5439 }
5440}
5441impl<'r> ::core::fmt::Debug for ProposalShortIdVecReader<'r> {
5442 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5443 write!(f, "{}({:#x})", Self::NAME, self)
5444 }
5445}
5446impl<'r> ::core::fmt::Display for ProposalShortIdVecReader<'r> {
5447 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5448 write!(f, "{} [", Self::NAME)?;
5449 for i in 0..self.len() {
5450 if i == 0 {
5451 write!(f, "{}", self.get_unchecked(i))?;
5452 } else {
5453 write!(f, ", {}", self.get_unchecked(i))?;
5454 }
5455 }
5456 write!(f, "]")
5457 }
5458}
5459impl<'r> ProposalShortIdVecReader<'r> {
5460 pub const ITEM_SIZE: usize = 10;
5461 pub fn total_size(&self) -> usize {
5462 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
5463 }
5464 pub fn item_count(&self) -> usize {
5465 molecule::unpack_number(self.as_slice()) as usize
5466 }
5467 pub fn len(&self) -> usize {
5468 self.item_count()
5469 }
5470 pub fn is_empty(&self) -> bool {
5471 self.len() == 0
5472 }
5473 pub fn get(&self, idx: usize) -> Option<ProposalShortIdReader<'r>> {
5474 if idx >= self.len() {
5475 None
5476 } else {
5477 Some(self.get_unchecked(idx))
5478 }
5479 }
5480 pub fn get_unchecked(&self, idx: usize) -> ProposalShortIdReader<'r> {
5481 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
5482 let end = start + Self::ITEM_SIZE;
5483 ProposalShortIdReader::new_unchecked(&self.as_slice()[start..end])
5484 }
5485}
5486impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdVecReader<'r> {
5487 type Entity = ProposalShortIdVec;
5488 const NAME: &'static str = "ProposalShortIdVecReader";
5489 fn to_entity(&self) -> Self::Entity {
5490 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5491 }
5492 fn new_unchecked(slice: &'r [u8]) -> Self {
5493 ProposalShortIdVecReader(slice)
5494 }
5495 fn as_slice(&self) -> &'r [u8] {
5496 self.0
5497 }
5498 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5499 use molecule::verification_error as ve;
5500 let slice_len = slice.len();
5501 if slice_len < molecule::NUMBER_SIZE {
5502 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5503 }
5504 let item_count = molecule::unpack_number(slice) as usize;
5505 if item_count == 0 {
5506 if slice_len != molecule::NUMBER_SIZE {
5507 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
5508 }
5509 return Ok(());
5510 }
5511 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
5512 if slice_len != total_size {
5513 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5514 }
5515 Ok(())
5516 }
5517}
5518#[derive(Clone, Debug, Default)]
5519pub struct ProposalShortIdVecBuilder(pub(crate) Vec<ProposalShortId>);
5520impl ProposalShortIdVecBuilder {
5521 pub const ITEM_SIZE: usize = 10;
5522 pub fn set(mut self, v: Vec<ProposalShortId>) -> Self {
5523 self.0 = v;
5524 self
5525 }
5526 pub fn push<T>(mut self, v: T) -> Self
5527 where
5528 T: ::core::convert::Into<ProposalShortId>,
5529 {
5530 self.0.push(v.into());
5531 self
5532 }
5533 pub fn extend<T: ::core::iter::IntoIterator<Item = ProposalShortId>>(
5534 mut self,
5535 iter: T,
5536 ) -> Self {
5537 self.0.extend(iter);
5538 self
5539 }
5540 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<ProposalShortId>
5541 where
5542 T: ::core::convert::Into<ProposalShortId>,
5543 {
5544 self.0
5545 .get_mut(index)
5546 .map(|item| ::core::mem::replace(item, v.into()))
5547 }
5548}
5549impl molecule::prelude::Builder for ProposalShortIdVecBuilder {
5550 type Entity = ProposalShortIdVec;
5551 const NAME: &'static str = "ProposalShortIdVecBuilder";
5552 fn expected_length(&self) -> usize {
5553 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
5554 }
5555 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5556 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
5557 for inner in &self.0[..] {
5558 writer.write_all(inner.as_slice())?;
5559 }
5560 Ok(())
5561 }
5562 fn build(&self) -> Self::Entity {
5563 let mut inner = Vec::with_capacity(self.expected_length());
5564 self.write(&mut inner)
5565 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5566 ProposalShortIdVec::new_unchecked(inner.into())
5567 }
5568}
5569pub struct ProposalShortIdVecIterator(ProposalShortIdVec, usize, usize);
5570impl ::core::iter::Iterator for ProposalShortIdVecIterator {
5571 type Item = ProposalShortId;
5572 fn next(&mut self) -> Option<Self::Item> {
5573 if self.1 >= self.2 {
5574 None
5575 } else {
5576 let ret = self.0.get_unchecked(self.1);
5577 self.1 += 1;
5578 Some(ret)
5579 }
5580 }
5581}
5582impl ::core::iter::ExactSizeIterator for ProposalShortIdVecIterator {
5583 fn len(&self) -> usize {
5584 self.2 - self.1
5585 }
5586}
5587impl ::core::iter::IntoIterator for ProposalShortIdVec {
5588 type Item = ProposalShortId;
5589 type IntoIter = ProposalShortIdVecIterator;
5590 fn into_iter(self) -> Self::IntoIter {
5591 let len = self.len();
5592 ProposalShortIdVecIterator(self, 0, len)
5593 }
5594}
5595impl<'r> ProposalShortIdVecReader<'r> {
5596 pub fn iter<'t>(&'t self) -> ProposalShortIdVecReaderIterator<'t, 'r> {
5597 ProposalShortIdVecReaderIterator(&self, 0, self.len())
5598 }
5599}
5600pub struct ProposalShortIdVecReaderIterator<'t, 'r>(&'t ProposalShortIdVecReader<'r>, usize, usize);
5601impl<'t: 'r, 'r> ::core::iter::Iterator for ProposalShortIdVecReaderIterator<'t, 'r> {
5602 type Item = ProposalShortIdReader<'t>;
5603 fn next(&mut self) -> Option<Self::Item> {
5604 if self.1 >= self.2 {
5605 None
5606 } else {
5607 let ret = self.0.get_unchecked(self.1);
5608 self.1 += 1;
5609 Some(ret)
5610 }
5611 }
5612}
5613impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for ProposalShortIdVecReaderIterator<'t, 'r> {
5614 fn len(&self) -> usize {
5615 self.2 - self.1
5616 }
5617}
5618impl<T> ::core::iter::FromIterator<T> for ProposalShortIdVec
5619where
5620 T: Into<ProposalShortId>,
5621{
5622 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
5623 Self::new_builder()
5624 .extend(iter.into_iter().map(Into::into))
5625 .build()
5626 }
5627}
5628impl<T> From<Vec<T>> for ProposalShortIdVec
5629where
5630 T: Into<ProposalShortId>,
5631{
5632 fn from(v: Vec<T>) -> Self {
5633 v.into_iter().collect()
5634 }
5635}
5636#[derive(Clone)]
5637pub struct CellDepVec(molecule::bytes::Bytes);
5638impl ::core::fmt::LowerHex for CellDepVec {
5639 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5640 use molecule::hex_string;
5641 if f.alternate() {
5642 write!(f, "0x")?;
5643 }
5644 write!(f, "{}", hex_string(self.as_slice()))
5645 }
5646}
5647impl ::core::fmt::Debug for CellDepVec {
5648 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5649 write!(f, "{}({:#x})", Self::NAME, self)
5650 }
5651}
5652impl ::core::fmt::Display for CellDepVec {
5653 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5654 write!(f, "{} [", Self::NAME)?;
5655 for i in 0..self.len() {
5656 if i == 0 {
5657 write!(f, "{}", self.get_unchecked(i))?;
5658 } else {
5659 write!(f, ", {}", self.get_unchecked(i))?;
5660 }
5661 }
5662 write!(f, "]")
5663 }
5664}
5665impl ::core::default::Default for CellDepVec {
5666 fn default() -> Self {
5667 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5668 CellDepVec::new_unchecked(v)
5669 }
5670}
5671impl CellDepVec {
5672 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
5673 pub const ITEM_SIZE: usize = 37;
5674 pub fn total_size(&self) -> usize {
5675 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
5676 }
5677 pub fn item_count(&self) -> usize {
5678 molecule::unpack_number(self.as_slice()) as usize
5679 }
5680 pub fn len(&self) -> usize {
5681 self.item_count()
5682 }
5683 pub fn is_empty(&self) -> bool {
5684 self.len() == 0
5685 }
5686 pub fn get(&self, idx: usize) -> Option<CellDep> {
5687 if idx >= self.len() {
5688 None
5689 } else {
5690 Some(self.get_unchecked(idx))
5691 }
5692 }
5693 pub fn get_unchecked(&self, idx: usize) -> CellDep {
5694 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
5695 let end = start + Self::ITEM_SIZE;
5696 CellDep::new_unchecked(self.0.slice(start..end))
5697 }
5698 pub fn as_reader<'r>(&'r self) -> CellDepVecReader<'r> {
5699 CellDepVecReader::new_unchecked(self.as_slice())
5700 }
5701}
5702impl molecule::prelude::Entity for CellDepVec {
5703 type Builder = CellDepVecBuilder;
5704 const NAME: &'static str = "CellDepVec";
5705 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
5706 CellDepVec(data)
5707 }
5708 fn as_bytes(&self) -> molecule::bytes::Bytes {
5709 self.0.clone()
5710 }
5711 fn as_slice(&self) -> &[u8] {
5712 &self.0[..]
5713 }
5714 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5715 CellDepVecReader::from_slice(slice).map(|reader| reader.to_entity())
5716 }
5717 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
5718 CellDepVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
5719 }
5720 fn new_builder() -> Self::Builder {
5721 ::core::default::Default::default()
5722 }
5723 fn as_builder(self) -> Self::Builder {
5724 Self::new_builder().extend(self.into_iter())
5725 }
5726}
5727#[derive(Clone, Copy)]
5728pub struct CellDepVecReader<'r>(&'r [u8]);
5729impl<'r> ::core::fmt::LowerHex for CellDepVecReader<'r> {
5730 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5731 use molecule::hex_string;
5732 if f.alternate() {
5733 write!(f, "0x")?;
5734 }
5735 write!(f, "{}", hex_string(self.as_slice()))
5736 }
5737}
5738impl<'r> ::core::fmt::Debug for CellDepVecReader<'r> {
5739 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5740 write!(f, "{}({:#x})", Self::NAME, self)
5741 }
5742}
5743impl<'r> ::core::fmt::Display for CellDepVecReader<'r> {
5744 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5745 write!(f, "{} [", Self::NAME)?;
5746 for i in 0..self.len() {
5747 if i == 0 {
5748 write!(f, "{}", self.get_unchecked(i))?;
5749 } else {
5750 write!(f, ", {}", self.get_unchecked(i))?;
5751 }
5752 }
5753 write!(f, "]")
5754 }
5755}
5756impl<'r> CellDepVecReader<'r> {
5757 pub const ITEM_SIZE: usize = 37;
5758 pub fn total_size(&self) -> usize {
5759 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
5760 }
5761 pub fn item_count(&self) -> usize {
5762 molecule::unpack_number(self.as_slice()) as usize
5763 }
5764 pub fn len(&self) -> usize {
5765 self.item_count()
5766 }
5767 pub fn is_empty(&self) -> bool {
5768 self.len() == 0
5769 }
5770 pub fn get(&self, idx: usize) -> Option<CellDepReader<'r>> {
5771 if idx >= self.len() {
5772 None
5773 } else {
5774 Some(self.get_unchecked(idx))
5775 }
5776 }
5777 pub fn get_unchecked(&self, idx: usize) -> CellDepReader<'r> {
5778 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
5779 let end = start + Self::ITEM_SIZE;
5780 CellDepReader::new_unchecked(&self.as_slice()[start..end])
5781 }
5782}
5783impl<'r> molecule::prelude::Reader<'r> for CellDepVecReader<'r> {
5784 type Entity = CellDepVec;
5785 const NAME: &'static str = "CellDepVecReader";
5786 fn to_entity(&self) -> Self::Entity {
5787 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
5788 }
5789 fn new_unchecked(slice: &'r [u8]) -> Self {
5790 CellDepVecReader(slice)
5791 }
5792 fn as_slice(&self) -> &'r [u8] {
5793 self.0
5794 }
5795 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
5796 use molecule::verification_error as ve;
5797 let slice_len = slice.len();
5798 if slice_len < molecule::NUMBER_SIZE {
5799 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
5800 }
5801 let item_count = molecule::unpack_number(slice) as usize;
5802 if item_count == 0 {
5803 if slice_len != molecule::NUMBER_SIZE {
5804 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
5805 }
5806 return Ok(());
5807 }
5808 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
5809 if slice_len != total_size {
5810 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
5811 }
5812 Ok(())
5813 }
5814}
5815#[derive(Clone, Debug, Default)]
5816pub struct CellDepVecBuilder(pub(crate) Vec<CellDep>);
5817impl CellDepVecBuilder {
5818 pub const ITEM_SIZE: usize = 37;
5819 pub fn set(mut self, v: Vec<CellDep>) -> Self {
5820 self.0 = v;
5821 self
5822 }
5823 pub fn push<T>(mut self, v: T) -> Self
5824 where
5825 T: ::core::convert::Into<CellDep>,
5826 {
5827 self.0.push(v.into());
5828 self
5829 }
5830 pub fn extend<T: ::core::iter::IntoIterator<Item = CellDep>>(mut self, iter: T) -> Self {
5831 self.0.extend(iter);
5832 self
5833 }
5834 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<CellDep>
5835 where
5836 T: ::core::convert::Into<CellDep>,
5837 {
5838 self.0
5839 .get_mut(index)
5840 .map(|item| ::core::mem::replace(item, v.into()))
5841 }
5842}
5843impl molecule::prelude::Builder for CellDepVecBuilder {
5844 type Entity = CellDepVec;
5845 const NAME: &'static str = "CellDepVecBuilder";
5846 fn expected_length(&self) -> usize {
5847 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
5848 }
5849 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
5850 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
5851 for inner in &self.0[..] {
5852 writer.write_all(inner.as_slice())?;
5853 }
5854 Ok(())
5855 }
5856 fn build(&self) -> Self::Entity {
5857 let mut inner = Vec::with_capacity(self.expected_length());
5858 self.write(&mut inner)
5859 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
5860 CellDepVec::new_unchecked(inner.into())
5861 }
5862}
5863pub struct CellDepVecIterator(CellDepVec, usize, usize);
5864impl ::core::iter::Iterator for CellDepVecIterator {
5865 type Item = CellDep;
5866 fn next(&mut self) -> Option<Self::Item> {
5867 if self.1 >= self.2 {
5868 None
5869 } else {
5870 let ret = self.0.get_unchecked(self.1);
5871 self.1 += 1;
5872 Some(ret)
5873 }
5874 }
5875}
5876impl ::core::iter::ExactSizeIterator for CellDepVecIterator {
5877 fn len(&self) -> usize {
5878 self.2 - self.1
5879 }
5880}
5881impl ::core::iter::IntoIterator for CellDepVec {
5882 type Item = CellDep;
5883 type IntoIter = CellDepVecIterator;
5884 fn into_iter(self) -> Self::IntoIter {
5885 let len = self.len();
5886 CellDepVecIterator(self, 0, len)
5887 }
5888}
5889impl<'r> CellDepVecReader<'r> {
5890 pub fn iter<'t>(&'t self) -> CellDepVecReaderIterator<'t, 'r> {
5891 CellDepVecReaderIterator(&self, 0, self.len())
5892 }
5893}
5894pub struct CellDepVecReaderIterator<'t, 'r>(&'t CellDepVecReader<'r>, usize, usize);
5895impl<'t: 'r, 'r> ::core::iter::Iterator for CellDepVecReaderIterator<'t, 'r> {
5896 type Item = CellDepReader<'t>;
5897 fn next(&mut self) -> Option<Self::Item> {
5898 if self.1 >= self.2 {
5899 None
5900 } else {
5901 let ret = self.0.get_unchecked(self.1);
5902 self.1 += 1;
5903 Some(ret)
5904 }
5905 }
5906}
5907impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellDepVecReaderIterator<'t, 'r> {
5908 fn len(&self) -> usize {
5909 self.2 - self.1
5910 }
5911}
5912impl<T> ::core::iter::FromIterator<T> for CellDepVec
5913where
5914 T: Into<CellDep>,
5915{
5916 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
5917 Self::new_builder()
5918 .extend(iter.into_iter().map(Into::into))
5919 .build()
5920 }
5921}
5922impl<T> From<Vec<T>> for CellDepVec
5923where
5924 T: Into<CellDep>,
5925{
5926 fn from(v: Vec<T>) -> Self {
5927 v.into_iter().collect()
5928 }
5929}
5930#[derive(Clone)]
5931pub struct CellInputVec(molecule::bytes::Bytes);
5932impl ::core::fmt::LowerHex for CellInputVec {
5933 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5934 use molecule::hex_string;
5935 if f.alternate() {
5936 write!(f, "0x")?;
5937 }
5938 write!(f, "{}", hex_string(self.as_slice()))
5939 }
5940}
5941impl ::core::fmt::Debug for CellInputVec {
5942 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5943 write!(f, "{}({:#x})", Self::NAME, self)
5944 }
5945}
5946impl ::core::fmt::Display for CellInputVec {
5947 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
5948 write!(f, "{} [", Self::NAME)?;
5949 for i in 0..self.len() {
5950 if i == 0 {
5951 write!(f, "{}", self.get_unchecked(i))?;
5952 } else {
5953 write!(f, ", {}", self.get_unchecked(i))?;
5954 }
5955 }
5956 write!(f, "]")
5957 }
5958}
5959impl ::core::default::Default for CellInputVec {
5960 fn default() -> Self {
5961 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
5962 CellInputVec::new_unchecked(v)
5963 }
5964}
5965impl CellInputVec {
5966 const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
5967 pub const ITEM_SIZE: usize = 44;
5968 pub fn total_size(&self) -> usize {
5969 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
5970 }
5971 pub fn item_count(&self) -> usize {
5972 molecule::unpack_number(self.as_slice()) as usize
5973 }
5974 pub fn len(&self) -> usize {
5975 self.item_count()
5976 }
5977 pub fn is_empty(&self) -> bool {
5978 self.len() == 0
5979 }
5980 pub fn get(&self, idx: usize) -> Option<CellInput> {
5981 if idx >= self.len() {
5982 None
5983 } else {
5984 Some(self.get_unchecked(idx))
5985 }
5986 }
5987 pub fn get_unchecked(&self, idx: usize) -> CellInput {
5988 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
5989 let end = start + Self::ITEM_SIZE;
5990 CellInput::new_unchecked(self.0.slice(start..end))
5991 }
5992 pub fn as_reader<'r>(&'r self) -> CellInputVecReader<'r> {
5993 CellInputVecReader::new_unchecked(self.as_slice())
5994 }
5995}
5996impl molecule::prelude::Entity for CellInputVec {
5997 type Builder = CellInputVecBuilder;
5998 const NAME: &'static str = "CellInputVec";
5999 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6000 CellInputVec(data)
6001 }
6002 fn as_bytes(&self) -> molecule::bytes::Bytes {
6003 self.0.clone()
6004 }
6005 fn as_slice(&self) -> &[u8] {
6006 &self.0[..]
6007 }
6008 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6009 CellInputVecReader::from_slice(slice).map(|reader| reader.to_entity())
6010 }
6011 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6012 CellInputVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6013 }
6014 fn new_builder() -> Self::Builder {
6015 ::core::default::Default::default()
6016 }
6017 fn as_builder(self) -> Self::Builder {
6018 Self::new_builder().extend(self.into_iter())
6019 }
6020}
6021#[derive(Clone, Copy)]
6022pub struct CellInputVecReader<'r>(&'r [u8]);
6023impl<'r> ::core::fmt::LowerHex for CellInputVecReader<'r> {
6024 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6025 use molecule::hex_string;
6026 if f.alternate() {
6027 write!(f, "0x")?;
6028 }
6029 write!(f, "{}", hex_string(self.as_slice()))
6030 }
6031}
6032impl<'r> ::core::fmt::Debug for CellInputVecReader<'r> {
6033 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6034 write!(f, "{}({:#x})", Self::NAME, self)
6035 }
6036}
6037impl<'r> ::core::fmt::Display for CellInputVecReader<'r> {
6038 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6039 write!(f, "{} [", Self::NAME)?;
6040 for i in 0..self.len() {
6041 if i == 0 {
6042 write!(f, "{}", self.get_unchecked(i))?;
6043 } else {
6044 write!(f, ", {}", self.get_unchecked(i))?;
6045 }
6046 }
6047 write!(f, "]")
6048 }
6049}
6050impl<'r> CellInputVecReader<'r> {
6051 pub const ITEM_SIZE: usize = 44;
6052 pub fn total_size(&self) -> usize {
6053 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
6054 }
6055 pub fn item_count(&self) -> usize {
6056 molecule::unpack_number(self.as_slice()) as usize
6057 }
6058 pub fn len(&self) -> usize {
6059 self.item_count()
6060 }
6061 pub fn is_empty(&self) -> bool {
6062 self.len() == 0
6063 }
6064 pub fn get(&self, idx: usize) -> Option<CellInputReader<'r>> {
6065 if idx >= self.len() {
6066 None
6067 } else {
6068 Some(self.get_unchecked(idx))
6069 }
6070 }
6071 pub fn get_unchecked(&self, idx: usize) -> CellInputReader<'r> {
6072 let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
6073 let end = start + Self::ITEM_SIZE;
6074 CellInputReader::new_unchecked(&self.as_slice()[start..end])
6075 }
6076}
6077impl<'r> molecule::prelude::Reader<'r> for CellInputVecReader<'r> {
6078 type Entity = CellInputVec;
6079 const NAME: &'static str = "CellInputVecReader";
6080 fn to_entity(&self) -> Self::Entity {
6081 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6082 }
6083 fn new_unchecked(slice: &'r [u8]) -> Self {
6084 CellInputVecReader(slice)
6085 }
6086 fn as_slice(&self) -> &'r [u8] {
6087 self.0
6088 }
6089 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
6090 use molecule::verification_error as ve;
6091 let slice_len = slice.len();
6092 if slice_len < molecule::NUMBER_SIZE {
6093 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6094 }
6095 let item_count = molecule::unpack_number(slice) as usize;
6096 if item_count == 0 {
6097 if slice_len != molecule::NUMBER_SIZE {
6098 return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len);
6099 }
6100 return Ok(());
6101 }
6102 let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count;
6103 if slice_len != total_size {
6104 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6105 }
6106 Ok(())
6107 }
6108}
6109#[derive(Clone, Debug, Default)]
6110pub struct CellInputVecBuilder(pub(crate) Vec<CellInput>);
6111impl CellInputVecBuilder {
6112 pub const ITEM_SIZE: usize = 44;
6113 pub fn set(mut self, v: Vec<CellInput>) -> Self {
6114 self.0 = v;
6115 self
6116 }
6117 pub fn push<T>(mut self, v: T) -> Self
6118 where
6119 T: ::core::convert::Into<CellInput>,
6120 {
6121 self.0.push(v.into());
6122 self
6123 }
6124 pub fn extend<T: ::core::iter::IntoIterator<Item = CellInput>>(mut self, iter: T) -> Self {
6125 self.0.extend(iter);
6126 self
6127 }
6128 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<CellInput>
6129 where
6130 T: ::core::convert::Into<CellInput>,
6131 {
6132 self.0
6133 .get_mut(index)
6134 .map(|item| ::core::mem::replace(item, v.into()))
6135 }
6136}
6137impl molecule::prelude::Builder for CellInputVecBuilder {
6138 type Entity = CellInputVec;
6139 const NAME: &'static str = "CellInputVecBuilder";
6140 fn expected_length(&self) -> usize {
6141 molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
6142 }
6143 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6144 writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?;
6145 for inner in &self.0[..] {
6146 writer.write_all(inner.as_slice())?;
6147 }
6148 Ok(())
6149 }
6150 fn build(&self) -> Self::Entity {
6151 let mut inner = Vec::with_capacity(self.expected_length());
6152 self.write(&mut inner)
6153 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6154 CellInputVec::new_unchecked(inner.into())
6155 }
6156}
6157pub struct CellInputVecIterator(CellInputVec, usize, usize);
6158impl ::core::iter::Iterator for CellInputVecIterator {
6159 type Item = CellInput;
6160 fn next(&mut self) -> Option<Self::Item> {
6161 if self.1 >= self.2 {
6162 None
6163 } else {
6164 let ret = self.0.get_unchecked(self.1);
6165 self.1 += 1;
6166 Some(ret)
6167 }
6168 }
6169}
6170impl ::core::iter::ExactSizeIterator for CellInputVecIterator {
6171 fn len(&self) -> usize {
6172 self.2 - self.1
6173 }
6174}
6175impl ::core::iter::IntoIterator for CellInputVec {
6176 type Item = CellInput;
6177 type IntoIter = CellInputVecIterator;
6178 fn into_iter(self) -> Self::IntoIter {
6179 let len = self.len();
6180 CellInputVecIterator(self, 0, len)
6181 }
6182}
6183impl<'r> CellInputVecReader<'r> {
6184 pub fn iter<'t>(&'t self) -> CellInputVecReaderIterator<'t, 'r> {
6185 CellInputVecReaderIterator(&self, 0, self.len())
6186 }
6187}
6188pub struct CellInputVecReaderIterator<'t, 'r>(&'t CellInputVecReader<'r>, usize, usize);
6189impl<'t: 'r, 'r> ::core::iter::Iterator for CellInputVecReaderIterator<'t, 'r> {
6190 type Item = CellInputReader<'t>;
6191 fn next(&mut self) -> Option<Self::Item> {
6192 if self.1 >= self.2 {
6193 None
6194 } else {
6195 let ret = self.0.get_unchecked(self.1);
6196 self.1 += 1;
6197 Some(ret)
6198 }
6199 }
6200}
6201impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellInputVecReaderIterator<'t, 'r> {
6202 fn len(&self) -> usize {
6203 self.2 - self.1
6204 }
6205}
6206impl<T> ::core::iter::FromIterator<T> for CellInputVec
6207where
6208 T: Into<CellInput>,
6209{
6210 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
6211 Self::new_builder()
6212 .extend(iter.into_iter().map(Into::into))
6213 .build()
6214 }
6215}
6216impl<T> From<Vec<T>> for CellInputVec
6217where
6218 T: Into<CellInput>,
6219{
6220 fn from(v: Vec<T>) -> Self {
6221 v.into_iter().collect()
6222 }
6223}
6224#[derive(Clone)]
6225pub struct CellOutputVec(molecule::bytes::Bytes);
6226impl ::core::fmt::LowerHex for CellOutputVec {
6227 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6228 use molecule::hex_string;
6229 if f.alternate() {
6230 write!(f, "0x")?;
6231 }
6232 write!(f, "{}", hex_string(self.as_slice()))
6233 }
6234}
6235impl ::core::fmt::Debug for CellOutputVec {
6236 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6237 write!(f, "{}({:#x})", Self::NAME, self)
6238 }
6239}
6240impl ::core::fmt::Display for CellOutputVec {
6241 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6242 write!(f, "{} [", Self::NAME)?;
6243 for i in 0..self.len() {
6244 if i == 0 {
6245 write!(f, "{}", self.get_unchecked(i))?;
6246 } else {
6247 write!(f, ", {}", self.get_unchecked(i))?;
6248 }
6249 }
6250 write!(f, "]")
6251 }
6252}
6253impl ::core::default::Default for CellOutputVec {
6254 fn default() -> Self {
6255 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6256 CellOutputVec::new_unchecked(v)
6257 }
6258}
6259impl CellOutputVec {
6260 const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
6261 pub fn total_size(&self) -> usize {
6262 molecule::unpack_number(self.as_slice()) as usize
6263 }
6264 pub fn item_count(&self) -> usize {
6265 if self.total_size() == molecule::NUMBER_SIZE {
6266 0
6267 } else {
6268 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6269 }
6270 }
6271 pub fn len(&self) -> usize {
6272 self.item_count()
6273 }
6274 pub fn is_empty(&self) -> bool {
6275 self.len() == 0
6276 }
6277 pub fn get(&self, idx: usize) -> Option<CellOutput> {
6278 if idx >= self.len() {
6279 None
6280 } else {
6281 Some(self.get_unchecked(idx))
6282 }
6283 }
6284 pub fn get_unchecked(&self, idx: usize) -> CellOutput {
6285 let slice = self.as_slice();
6286 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
6287 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
6288 if idx == self.len() - 1 {
6289 CellOutput::new_unchecked(self.0.slice(start..))
6290 } else {
6291 let end_idx = start_idx + molecule::NUMBER_SIZE;
6292 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
6293 CellOutput::new_unchecked(self.0.slice(start..end))
6294 }
6295 }
6296 pub fn as_reader<'r>(&'r self) -> CellOutputVecReader<'r> {
6297 CellOutputVecReader::new_unchecked(self.as_slice())
6298 }
6299}
6300impl molecule::prelude::Entity for CellOutputVec {
6301 type Builder = CellOutputVecBuilder;
6302 const NAME: &'static str = "CellOutputVec";
6303 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6304 CellOutputVec(data)
6305 }
6306 fn as_bytes(&self) -> molecule::bytes::Bytes {
6307 self.0.clone()
6308 }
6309 fn as_slice(&self) -> &[u8] {
6310 &self.0[..]
6311 }
6312 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6313 CellOutputVecReader::from_slice(slice).map(|reader| reader.to_entity())
6314 }
6315 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6316 CellOutputVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6317 }
6318 fn new_builder() -> Self::Builder {
6319 ::core::default::Default::default()
6320 }
6321 fn as_builder(self) -> Self::Builder {
6322 Self::new_builder().extend(self.into_iter())
6323 }
6324}
6325#[derive(Clone, Copy)]
6326pub struct CellOutputVecReader<'r>(&'r [u8]);
6327impl<'r> ::core::fmt::LowerHex for CellOutputVecReader<'r> {
6328 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6329 use molecule::hex_string;
6330 if f.alternate() {
6331 write!(f, "0x")?;
6332 }
6333 write!(f, "{}", hex_string(self.as_slice()))
6334 }
6335}
6336impl<'r> ::core::fmt::Debug for CellOutputVecReader<'r> {
6337 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6338 write!(f, "{}({:#x})", Self::NAME, self)
6339 }
6340}
6341impl<'r> ::core::fmt::Display for CellOutputVecReader<'r> {
6342 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6343 write!(f, "{} [", Self::NAME)?;
6344 for i in 0..self.len() {
6345 if i == 0 {
6346 write!(f, "{}", self.get_unchecked(i))?;
6347 } else {
6348 write!(f, ", {}", self.get_unchecked(i))?;
6349 }
6350 }
6351 write!(f, "]")
6352 }
6353}
6354impl<'r> CellOutputVecReader<'r> {
6355 pub fn total_size(&self) -> usize {
6356 molecule::unpack_number(self.as_slice()) as usize
6357 }
6358 pub fn item_count(&self) -> usize {
6359 if self.total_size() == molecule::NUMBER_SIZE {
6360 0
6361 } else {
6362 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6363 }
6364 }
6365 pub fn len(&self) -> usize {
6366 self.item_count()
6367 }
6368 pub fn is_empty(&self) -> bool {
6369 self.len() == 0
6370 }
6371 pub fn get(&self, idx: usize) -> Option<CellOutputReader<'r>> {
6372 if idx >= self.len() {
6373 None
6374 } else {
6375 Some(self.get_unchecked(idx))
6376 }
6377 }
6378 pub fn get_unchecked(&self, idx: usize) -> CellOutputReader<'r> {
6379 let slice = self.as_slice();
6380 let start_idx = molecule::NUMBER_SIZE * (1 + idx);
6381 let start = molecule::unpack_number(&slice[start_idx..]) as usize;
6382 if idx == self.len() - 1 {
6383 CellOutputReader::new_unchecked(&self.as_slice()[start..])
6384 } else {
6385 let end_idx = start_idx + molecule::NUMBER_SIZE;
6386 let end = molecule::unpack_number(&slice[end_idx..]) as usize;
6387 CellOutputReader::new_unchecked(&self.as_slice()[start..end])
6388 }
6389 }
6390}
6391impl<'r> molecule::prelude::Reader<'r> for CellOutputVecReader<'r> {
6392 type Entity = CellOutputVec;
6393 const NAME: &'static str = "CellOutputVecReader";
6394 fn to_entity(&self) -> Self::Entity {
6395 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6396 }
6397 fn new_unchecked(slice: &'r [u8]) -> Self {
6398 CellOutputVecReader(slice)
6399 }
6400 fn as_slice(&self) -> &'r [u8] {
6401 self.0
6402 }
6403 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6404 use molecule::verification_error as ve;
6405 let slice_len = slice.len();
6406 if slice_len < molecule::NUMBER_SIZE {
6407 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6408 }
6409 let total_size = molecule::unpack_number(slice) as usize;
6410 if slice_len != total_size {
6411 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6412 }
6413 if slice_len == molecule::NUMBER_SIZE {
6414 return Ok(());
6415 }
6416 if slice_len < molecule::NUMBER_SIZE * 2 {
6417 return ve!(
6418 Self,
6419 TotalSizeNotMatch,
6420 molecule::NUMBER_SIZE * 2,
6421 slice_len
6422 );
6423 }
6424 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6425 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6426 return ve!(Self, OffsetsNotMatch);
6427 }
6428 if slice_len < offset_first {
6429 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6430 }
6431 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6432 .chunks_exact(molecule::NUMBER_SIZE)
6433 .map(|x| molecule::unpack_number(x) as usize)
6434 .collect();
6435 offsets.push(total_size);
6436 if offsets.windows(2).any(|i| i[0] > i[1]) {
6437 return ve!(Self, OffsetsNotMatch);
6438 }
6439 for pair in offsets.windows(2) {
6440 let start = pair[0];
6441 let end = pair[1];
6442 CellOutputReader::verify(&slice[start..end], compatible)?;
6443 }
6444 Ok(())
6445 }
6446}
6447#[derive(Clone, Debug, Default)]
6448pub struct CellOutputVecBuilder(pub(crate) Vec<CellOutput>);
6449impl CellOutputVecBuilder {
6450 pub fn set(mut self, v: Vec<CellOutput>) -> Self {
6451 self.0 = v;
6452 self
6453 }
6454 pub fn push<T>(mut self, v: T) -> Self
6455 where
6456 T: ::core::convert::Into<CellOutput>,
6457 {
6458 self.0.push(v.into());
6459 self
6460 }
6461 pub fn extend<T: ::core::iter::IntoIterator<Item = CellOutput>>(mut self, iter: T) -> Self {
6462 self.0.extend(iter);
6463 self
6464 }
6465 pub fn replace<T>(&mut self, index: usize, v: T) -> Option<CellOutput>
6466 where
6467 T: ::core::convert::Into<CellOutput>,
6468 {
6469 self.0
6470 .get_mut(index)
6471 .map(|item| ::core::mem::replace(item, v.into()))
6472 }
6473}
6474impl molecule::prelude::Builder for CellOutputVecBuilder {
6475 type Entity = CellOutputVec;
6476 const NAME: &'static str = "CellOutputVecBuilder";
6477 fn expected_length(&self) -> usize {
6478 molecule::NUMBER_SIZE * (self.0.len() + 1)
6479 + self
6480 .0
6481 .iter()
6482 .map(|inner| inner.as_slice().len())
6483 .sum::<usize>()
6484 }
6485 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6486 let item_count = self.0.len();
6487 if item_count == 0 {
6488 writer.write_all(&molecule::pack_number(
6489 molecule::NUMBER_SIZE as molecule::Number,
6490 ))?;
6491 } else {
6492 let (total_size, offsets) = self.0.iter().fold(
6493 (
6494 molecule::NUMBER_SIZE * (item_count + 1),
6495 Vec::with_capacity(item_count),
6496 ),
6497 |(start, mut offsets), inner| {
6498 offsets.push(start);
6499 (start + inner.as_slice().len(), offsets)
6500 },
6501 );
6502 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6503 for offset in offsets.into_iter() {
6504 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6505 }
6506 for inner in self.0.iter() {
6507 writer.write_all(inner.as_slice())?;
6508 }
6509 }
6510 Ok(())
6511 }
6512 fn build(&self) -> Self::Entity {
6513 let mut inner = Vec::with_capacity(self.expected_length());
6514 self.write(&mut inner)
6515 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6516 CellOutputVec::new_unchecked(inner.into())
6517 }
6518}
6519pub struct CellOutputVecIterator(CellOutputVec, usize, usize);
6520impl ::core::iter::Iterator for CellOutputVecIterator {
6521 type Item = CellOutput;
6522 fn next(&mut self) -> Option<Self::Item> {
6523 if self.1 >= self.2 {
6524 None
6525 } else {
6526 let ret = self.0.get_unchecked(self.1);
6527 self.1 += 1;
6528 Some(ret)
6529 }
6530 }
6531}
6532impl ::core::iter::ExactSizeIterator for CellOutputVecIterator {
6533 fn len(&self) -> usize {
6534 self.2 - self.1
6535 }
6536}
6537impl ::core::iter::IntoIterator for CellOutputVec {
6538 type Item = CellOutput;
6539 type IntoIter = CellOutputVecIterator;
6540 fn into_iter(self) -> Self::IntoIter {
6541 let len = self.len();
6542 CellOutputVecIterator(self, 0, len)
6543 }
6544}
6545impl<'r> CellOutputVecReader<'r> {
6546 pub fn iter<'t>(&'t self) -> CellOutputVecReaderIterator<'t, 'r> {
6547 CellOutputVecReaderIterator(&self, 0, self.len())
6548 }
6549}
6550pub struct CellOutputVecReaderIterator<'t, 'r>(&'t CellOutputVecReader<'r>, usize, usize);
6551impl<'t: 'r, 'r> ::core::iter::Iterator for CellOutputVecReaderIterator<'t, 'r> {
6552 type Item = CellOutputReader<'t>;
6553 fn next(&mut self) -> Option<Self::Item> {
6554 if self.1 >= self.2 {
6555 None
6556 } else {
6557 let ret = self.0.get_unchecked(self.1);
6558 self.1 += 1;
6559 Some(ret)
6560 }
6561 }
6562}
6563impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for CellOutputVecReaderIterator<'t, 'r> {
6564 fn len(&self) -> usize {
6565 self.2 - self.1
6566 }
6567}
6568impl<T> ::core::iter::FromIterator<T> for CellOutputVec
6569where
6570 T: Into<CellOutput>,
6571{
6572 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
6573 Self::new_builder()
6574 .extend(iter.into_iter().map(Into::into))
6575 .build()
6576 }
6577}
6578impl<T> From<Vec<T>> for CellOutputVec
6579where
6580 T: Into<CellOutput>,
6581{
6582 fn from(v: Vec<T>) -> Self {
6583 v.into_iter().collect()
6584 }
6585}
6586#[derive(Clone)]
6587pub struct Script(molecule::bytes::Bytes);
6588impl ::core::fmt::LowerHex for Script {
6589 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6590 use molecule::hex_string;
6591 if f.alternate() {
6592 write!(f, "0x")?;
6593 }
6594 write!(f, "{}", hex_string(self.as_slice()))
6595 }
6596}
6597impl ::core::fmt::Debug for Script {
6598 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6599 write!(f, "{}({:#x})", Self::NAME, self)
6600 }
6601}
6602impl ::core::fmt::Display for Script {
6603 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6604 write!(f, "{} {{ ", Self::NAME)?;
6605 write!(f, "{}: {}", "code_hash", self.code_hash())?;
6606 write!(f, ", {}: {}", "hash_type", self.hash_type())?;
6607 write!(f, ", {}: {}", "args", self.args())?;
6608 let extra_count = self.count_extra_fields();
6609 if extra_count != 0 {
6610 write!(f, ", .. ({} fields)", extra_count)?;
6611 }
6612 write!(f, " }}")
6613 }
6614}
6615impl ::core::default::Default for Script {
6616 fn default() -> Self {
6617 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6618 Script::new_unchecked(v)
6619 }
6620}
6621impl Script {
6622 const DEFAULT_VALUE: [u8; 53] = [
6623 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,
6624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6625 ];
6626 pub const FIELD_COUNT: usize = 3;
6627 pub fn total_size(&self) -> usize {
6628 molecule::unpack_number(self.as_slice()) as usize
6629 }
6630 pub fn field_count(&self) -> usize {
6631 if self.total_size() == molecule::NUMBER_SIZE {
6632 0
6633 } else {
6634 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6635 }
6636 }
6637 pub fn count_extra_fields(&self) -> usize {
6638 self.field_count() - Self::FIELD_COUNT
6639 }
6640 pub fn has_extra_fields(&self) -> bool {
6641 Self::FIELD_COUNT != self.field_count()
6642 }
6643 pub fn code_hash(&self) -> Byte32 {
6644 let slice = self.as_slice();
6645 let start = molecule::unpack_number(&slice[4..]) as usize;
6646 let end = molecule::unpack_number(&slice[8..]) as usize;
6647 Byte32::new_unchecked(self.0.slice(start..end))
6648 }
6649 pub fn hash_type(&self) -> Byte {
6650 let slice = self.as_slice();
6651 let start = molecule::unpack_number(&slice[8..]) as usize;
6652 let end = molecule::unpack_number(&slice[12..]) as usize;
6653 Byte::new_unchecked(self.0.slice(start..end))
6654 }
6655 pub fn args(&self) -> Bytes {
6656 let slice = self.as_slice();
6657 let start = molecule::unpack_number(&slice[12..]) as usize;
6658 if self.has_extra_fields() {
6659 let end = molecule::unpack_number(&slice[16..]) as usize;
6660 Bytes::new_unchecked(self.0.slice(start..end))
6661 } else {
6662 Bytes::new_unchecked(self.0.slice(start..))
6663 }
6664 }
6665 pub fn as_reader<'r>(&'r self) -> ScriptReader<'r> {
6666 ScriptReader::new_unchecked(self.as_slice())
6667 }
6668}
6669impl molecule::prelude::Entity for Script {
6670 type Builder = ScriptBuilder;
6671 const NAME: &'static str = "Script";
6672 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6673 Script(data)
6674 }
6675 fn as_bytes(&self) -> molecule::bytes::Bytes {
6676 self.0.clone()
6677 }
6678 fn as_slice(&self) -> &[u8] {
6679 &self.0[..]
6680 }
6681 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6682 ScriptReader::from_slice(slice).map(|reader| reader.to_entity())
6683 }
6684 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6685 ScriptReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6686 }
6687 fn new_builder() -> Self::Builder {
6688 ::core::default::Default::default()
6689 }
6690 fn as_builder(self) -> Self::Builder {
6691 Self::new_builder()
6692 .code_hash(self.code_hash())
6693 .hash_type(self.hash_type())
6694 .args(self.args())
6695 }
6696}
6697#[derive(Clone, Copy)]
6698pub struct ScriptReader<'r>(&'r [u8]);
6699impl<'r> ::core::fmt::LowerHex for ScriptReader<'r> {
6700 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6701 use molecule::hex_string;
6702 if f.alternate() {
6703 write!(f, "0x")?;
6704 }
6705 write!(f, "{}", hex_string(self.as_slice()))
6706 }
6707}
6708impl<'r> ::core::fmt::Debug for ScriptReader<'r> {
6709 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6710 write!(f, "{}({:#x})", Self::NAME, self)
6711 }
6712}
6713impl<'r> ::core::fmt::Display for ScriptReader<'r> {
6714 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6715 write!(f, "{} {{ ", Self::NAME)?;
6716 write!(f, "{}: {}", "code_hash", self.code_hash())?;
6717 write!(f, ", {}: {}", "hash_type", self.hash_type())?;
6718 write!(f, ", {}: {}", "args", self.args())?;
6719 let extra_count = self.count_extra_fields();
6720 if extra_count != 0 {
6721 write!(f, ", .. ({} fields)", extra_count)?;
6722 }
6723 write!(f, " }}")
6724 }
6725}
6726impl<'r> ScriptReader<'r> {
6727 pub const FIELD_COUNT: usize = 3;
6728 pub fn total_size(&self) -> usize {
6729 molecule::unpack_number(self.as_slice()) as usize
6730 }
6731 pub fn field_count(&self) -> usize {
6732 if self.total_size() == molecule::NUMBER_SIZE {
6733 0
6734 } else {
6735 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
6736 }
6737 }
6738 pub fn count_extra_fields(&self) -> usize {
6739 self.field_count() - Self::FIELD_COUNT
6740 }
6741 pub fn has_extra_fields(&self) -> bool {
6742 Self::FIELD_COUNT != self.field_count()
6743 }
6744 pub fn code_hash(&self) -> Byte32Reader<'r> {
6745 let slice = self.as_slice();
6746 let start = molecule::unpack_number(&slice[4..]) as usize;
6747 let end = molecule::unpack_number(&slice[8..]) as usize;
6748 Byte32Reader::new_unchecked(&self.as_slice()[start..end])
6749 }
6750 pub fn hash_type(&self) -> ByteReader<'r> {
6751 let slice = self.as_slice();
6752 let start = molecule::unpack_number(&slice[8..]) as usize;
6753 let end = molecule::unpack_number(&slice[12..]) as usize;
6754 ByteReader::new_unchecked(&self.as_slice()[start..end])
6755 }
6756 pub fn args(&self) -> BytesReader<'r> {
6757 let slice = self.as_slice();
6758 let start = molecule::unpack_number(&slice[12..]) as usize;
6759 if self.has_extra_fields() {
6760 let end = molecule::unpack_number(&slice[16..]) as usize;
6761 BytesReader::new_unchecked(&self.as_slice()[start..end])
6762 } else {
6763 BytesReader::new_unchecked(&self.as_slice()[start..])
6764 }
6765 }
6766}
6767impl<'r> molecule::prelude::Reader<'r> for ScriptReader<'r> {
6768 type Entity = Script;
6769 const NAME: &'static str = "ScriptReader";
6770 fn to_entity(&self) -> Self::Entity {
6771 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6772 }
6773 fn new_unchecked(slice: &'r [u8]) -> Self {
6774 ScriptReader(slice)
6775 }
6776 fn as_slice(&self) -> &'r [u8] {
6777 self.0
6778 }
6779 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
6780 use molecule::verification_error as ve;
6781 let slice_len = slice.len();
6782 if slice_len < molecule::NUMBER_SIZE {
6783 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
6784 }
6785 let total_size = molecule::unpack_number(slice) as usize;
6786 if slice_len != total_size {
6787 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
6788 }
6789 if slice_len < molecule::NUMBER_SIZE * 2 {
6790 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
6791 }
6792 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
6793 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
6794 return ve!(Self, OffsetsNotMatch);
6795 }
6796 if slice_len < offset_first {
6797 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
6798 }
6799 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
6800 if field_count < Self::FIELD_COUNT {
6801 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6802 } else if !compatible && field_count > Self::FIELD_COUNT {
6803 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
6804 };
6805 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
6806 .chunks_exact(molecule::NUMBER_SIZE)
6807 .map(|x| molecule::unpack_number(x) as usize)
6808 .collect();
6809 offsets.push(total_size);
6810 if offsets.windows(2).any(|i| i[0] > i[1]) {
6811 return ve!(Self, OffsetsNotMatch);
6812 }
6813 Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
6814 ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
6815 BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
6816 Ok(())
6817 }
6818}
6819#[derive(Clone, Debug, Default)]
6820pub struct ScriptBuilder {
6821 pub(crate) code_hash: Byte32,
6822 pub(crate) hash_type: Byte,
6823 pub(crate) args: Bytes,
6824}
6825impl ScriptBuilder {
6826 pub const FIELD_COUNT: usize = 3;
6827 pub fn code_hash<T>(mut self, v: T) -> Self
6828 where
6829 T: ::core::convert::Into<Byte32>,
6830 {
6831 self.code_hash = v.into();
6832 self
6833 }
6834 pub fn hash_type<T>(mut self, v: T) -> Self
6835 where
6836 T: ::core::convert::Into<Byte>,
6837 {
6838 self.hash_type = v.into();
6839 self
6840 }
6841 pub fn args<T>(mut self, v: T) -> Self
6842 where
6843 T: ::core::convert::Into<Bytes>,
6844 {
6845 self.args = v.into();
6846 self
6847 }
6848}
6849impl molecule::prelude::Builder for ScriptBuilder {
6850 type Entity = Script;
6851 const NAME: &'static str = "ScriptBuilder";
6852 fn expected_length(&self) -> usize {
6853 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
6854 + self.code_hash.as_slice().len()
6855 + self.hash_type.as_slice().len()
6856 + self.args.as_slice().len()
6857 }
6858 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
6859 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
6860 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
6861 offsets.push(total_size);
6862 total_size += self.code_hash.as_slice().len();
6863 offsets.push(total_size);
6864 total_size += self.hash_type.as_slice().len();
6865 offsets.push(total_size);
6866 total_size += self.args.as_slice().len();
6867 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
6868 for offset in offsets.into_iter() {
6869 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
6870 }
6871 writer.write_all(self.code_hash.as_slice())?;
6872 writer.write_all(self.hash_type.as_slice())?;
6873 writer.write_all(self.args.as_slice())?;
6874 Ok(())
6875 }
6876 fn build(&self) -> Self::Entity {
6877 let mut inner = Vec::with_capacity(self.expected_length());
6878 self.write(&mut inner)
6879 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
6880 Script::new_unchecked(inner.into())
6881 }
6882}
6883#[derive(Clone)]
6884pub struct OutPoint(molecule::bytes::Bytes);
6885impl ::core::fmt::LowerHex for OutPoint {
6886 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6887 use molecule::hex_string;
6888 if f.alternate() {
6889 write!(f, "0x")?;
6890 }
6891 write!(f, "{}", hex_string(self.as_slice()))
6892 }
6893}
6894impl ::core::fmt::Debug for OutPoint {
6895 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6896 write!(f, "{}({:#x})", Self::NAME, self)
6897 }
6898}
6899impl ::core::fmt::Display for OutPoint {
6900 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6901 write!(f, "{} {{ ", Self::NAME)?;
6902 write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
6903 write!(f, ", {}: {}", "index", self.index())?;
6904 write!(f, " }}")
6905 }
6906}
6907impl ::core::default::Default for OutPoint {
6908 fn default() -> Self {
6909 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
6910 OutPoint::new_unchecked(v)
6911 }
6912}
6913impl OutPoint {
6914 const DEFAULT_VALUE: [u8; 36] = [
6915 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
6916 0, 0, 0, 0, 0, 0,
6917 ];
6918 pub const TOTAL_SIZE: usize = 36;
6919 pub const FIELD_SIZES: [usize; 2] = [32, 4];
6920 pub const FIELD_COUNT: usize = 2;
6921 pub fn tx_hash(&self) -> Byte32 {
6922 Byte32::new_unchecked(self.0.slice(0..32))
6923 }
6924 pub fn index(&self) -> Uint32 {
6925 Uint32::new_unchecked(self.0.slice(32..36))
6926 }
6927 pub fn as_reader<'r>(&'r self) -> OutPointReader<'r> {
6928 OutPointReader::new_unchecked(self.as_slice())
6929 }
6930}
6931impl molecule::prelude::Entity for OutPoint {
6932 type Builder = OutPointBuilder;
6933 const NAME: &'static str = "OutPoint";
6934 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
6935 OutPoint(data)
6936 }
6937 fn as_bytes(&self) -> molecule::bytes::Bytes {
6938 self.0.clone()
6939 }
6940 fn as_slice(&self) -> &[u8] {
6941 &self.0[..]
6942 }
6943 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6944 OutPointReader::from_slice(slice).map(|reader| reader.to_entity())
6945 }
6946 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
6947 OutPointReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
6948 }
6949 fn new_builder() -> Self::Builder {
6950 ::core::default::Default::default()
6951 }
6952 fn as_builder(self) -> Self::Builder {
6953 Self::new_builder()
6954 .tx_hash(self.tx_hash())
6955 .index(self.index())
6956 }
6957}
6958#[derive(Clone, Copy)]
6959pub struct OutPointReader<'r>(&'r [u8]);
6960impl<'r> ::core::fmt::LowerHex for OutPointReader<'r> {
6961 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6962 use molecule::hex_string;
6963 if f.alternate() {
6964 write!(f, "0x")?;
6965 }
6966 write!(f, "{}", hex_string(self.as_slice()))
6967 }
6968}
6969impl<'r> ::core::fmt::Debug for OutPointReader<'r> {
6970 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6971 write!(f, "{}({:#x})", Self::NAME, self)
6972 }
6973}
6974impl<'r> ::core::fmt::Display for OutPointReader<'r> {
6975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6976 write!(f, "{} {{ ", Self::NAME)?;
6977 write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
6978 write!(f, ", {}: {}", "index", self.index())?;
6979 write!(f, " }}")
6980 }
6981}
6982impl<'r> OutPointReader<'r> {
6983 pub const TOTAL_SIZE: usize = 36;
6984 pub const FIELD_SIZES: [usize; 2] = [32, 4];
6985 pub const FIELD_COUNT: usize = 2;
6986 pub fn tx_hash(&self) -> Byte32Reader<'r> {
6987 Byte32Reader::new_unchecked(&self.as_slice()[0..32])
6988 }
6989 pub fn index(&self) -> Uint32Reader<'r> {
6990 Uint32Reader::new_unchecked(&self.as_slice()[32..36])
6991 }
6992}
6993impl<'r> molecule::prelude::Reader<'r> for OutPointReader<'r> {
6994 type Entity = OutPoint;
6995 const NAME: &'static str = "OutPointReader";
6996 fn to_entity(&self) -> Self::Entity {
6997 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
6998 }
6999 fn new_unchecked(slice: &'r [u8]) -> Self {
7000 OutPointReader(slice)
7001 }
7002 fn as_slice(&self) -> &'r [u8] {
7003 self.0
7004 }
7005 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
7006 use molecule::verification_error as ve;
7007 let slice_len = slice.len();
7008 if slice_len != Self::TOTAL_SIZE {
7009 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
7010 }
7011 Ok(())
7012 }
7013}
7014#[derive(Clone, Debug, Default)]
7015pub struct OutPointBuilder {
7016 pub(crate) tx_hash: Byte32,
7017 pub(crate) index: Uint32,
7018}
7019impl OutPointBuilder {
7020 pub const TOTAL_SIZE: usize = 36;
7021 pub const FIELD_SIZES: [usize; 2] = [32, 4];
7022 pub const FIELD_COUNT: usize = 2;
7023 pub fn tx_hash<T>(mut self, v: T) -> Self
7024 where
7025 T: ::core::convert::Into<Byte32>,
7026 {
7027 self.tx_hash = v.into();
7028 self
7029 }
7030 pub fn index<T>(mut self, v: T) -> Self
7031 where
7032 T: ::core::convert::Into<Uint32>,
7033 {
7034 self.index = v.into();
7035 self
7036 }
7037}
7038impl molecule::prelude::Builder for OutPointBuilder {
7039 type Entity = OutPoint;
7040 const NAME: &'static str = "OutPointBuilder";
7041 fn expected_length(&self) -> usize {
7042 Self::TOTAL_SIZE
7043 }
7044 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7045 writer.write_all(self.tx_hash.as_slice())?;
7046 writer.write_all(self.index.as_slice())?;
7047 Ok(())
7048 }
7049 fn build(&self) -> Self::Entity {
7050 let mut inner = Vec::with_capacity(self.expected_length());
7051 self.write(&mut inner)
7052 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7053 OutPoint::new_unchecked(inner.into())
7054 }
7055}
7056#[derive(Clone)]
7057pub struct CellInput(molecule::bytes::Bytes);
7058impl ::core::fmt::LowerHex for CellInput {
7059 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7060 use molecule::hex_string;
7061 if f.alternate() {
7062 write!(f, "0x")?;
7063 }
7064 write!(f, "{}", hex_string(self.as_slice()))
7065 }
7066}
7067impl ::core::fmt::Debug for CellInput {
7068 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7069 write!(f, "{}({:#x})", Self::NAME, self)
7070 }
7071}
7072impl ::core::fmt::Display for CellInput {
7073 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7074 write!(f, "{} {{ ", Self::NAME)?;
7075 write!(f, "{}: {}", "since", self.since())?;
7076 write!(f, ", {}: {}", "previous_output", self.previous_output())?;
7077 write!(f, " }}")
7078 }
7079}
7080impl ::core::default::Default for CellInput {
7081 fn default() -> Self {
7082 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7083 CellInput::new_unchecked(v)
7084 }
7085}
7086impl CellInput {
7087 const DEFAULT_VALUE: [u8; 44] = [
7088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7090 ];
7091 pub const TOTAL_SIZE: usize = 44;
7092 pub const FIELD_SIZES: [usize; 2] = [8, 36];
7093 pub const FIELD_COUNT: usize = 2;
7094 pub fn since(&self) -> Uint64 {
7095 Uint64::new_unchecked(self.0.slice(0..8))
7096 }
7097 pub fn previous_output(&self) -> OutPoint {
7098 OutPoint::new_unchecked(self.0.slice(8..44))
7099 }
7100 pub fn as_reader<'r>(&'r self) -> CellInputReader<'r> {
7101 CellInputReader::new_unchecked(self.as_slice())
7102 }
7103}
7104impl molecule::prelude::Entity for CellInput {
7105 type Builder = CellInputBuilder;
7106 const NAME: &'static str = "CellInput";
7107 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7108 CellInput(data)
7109 }
7110 fn as_bytes(&self) -> molecule::bytes::Bytes {
7111 self.0.clone()
7112 }
7113 fn as_slice(&self) -> &[u8] {
7114 &self.0[..]
7115 }
7116 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7117 CellInputReader::from_slice(slice).map(|reader| reader.to_entity())
7118 }
7119 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7120 CellInputReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7121 }
7122 fn new_builder() -> Self::Builder {
7123 ::core::default::Default::default()
7124 }
7125 fn as_builder(self) -> Self::Builder {
7126 Self::new_builder()
7127 .since(self.since())
7128 .previous_output(self.previous_output())
7129 }
7130}
7131#[derive(Clone, Copy)]
7132pub struct CellInputReader<'r>(&'r [u8]);
7133impl<'r> ::core::fmt::LowerHex for CellInputReader<'r> {
7134 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7135 use molecule::hex_string;
7136 if f.alternate() {
7137 write!(f, "0x")?;
7138 }
7139 write!(f, "{}", hex_string(self.as_slice()))
7140 }
7141}
7142impl<'r> ::core::fmt::Debug for CellInputReader<'r> {
7143 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7144 write!(f, "{}({:#x})", Self::NAME, self)
7145 }
7146}
7147impl<'r> ::core::fmt::Display for CellInputReader<'r> {
7148 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7149 write!(f, "{} {{ ", Self::NAME)?;
7150 write!(f, "{}: {}", "since", self.since())?;
7151 write!(f, ", {}: {}", "previous_output", self.previous_output())?;
7152 write!(f, " }}")
7153 }
7154}
7155impl<'r> CellInputReader<'r> {
7156 pub const TOTAL_SIZE: usize = 44;
7157 pub const FIELD_SIZES: [usize; 2] = [8, 36];
7158 pub const FIELD_COUNT: usize = 2;
7159 pub fn since(&self) -> Uint64Reader<'r> {
7160 Uint64Reader::new_unchecked(&self.as_slice()[0..8])
7161 }
7162 pub fn previous_output(&self) -> OutPointReader<'r> {
7163 OutPointReader::new_unchecked(&self.as_slice()[8..44])
7164 }
7165}
7166impl<'r> molecule::prelude::Reader<'r> for CellInputReader<'r> {
7167 type Entity = CellInput;
7168 const NAME: &'static str = "CellInputReader";
7169 fn to_entity(&self) -> Self::Entity {
7170 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7171 }
7172 fn new_unchecked(slice: &'r [u8]) -> Self {
7173 CellInputReader(slice)
7174 }
7175 fn as_slice(&self) -> &'r [u8] {
7176 self.0
7177 }
7178 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
7179 use molecule::verification_error as ve;
7180 let slice_len = slice.len();
7181 if slice_len != Self::TOTAL_SIZE {
7182 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
7183 }
7184 Ok(())
7185 }
7186}
7187#[derive(Clone, Debug, Default)]
7188pub struct CellInputBuilder {
7189 pub(crate) since: Uint64,
7190 pub(crate) previous_output: OutPoint,
7191}
7192impl CellInputBuilder {
7193 pub const TOTAL_SIZE: usize = 44;
7194 pub const FIELD_SIZES: [usize; 2] = [8, 36];
7195 pub const FIELD_COUNT: usize = 2;
7196 pub fn since<T>(mut self, v: T) -> Self
7197 where
7198 T: ::core::convert::Into<Uint64>,
7199 {
7200 self.since = v.into();
7201 self
7202 }
7203 pub fn previous_output<T>(mut self, v: T) -> Self
7204 where
7205 T: ::core::convert::Into<OutPoint>,
7206 {
7207 self.previous_output = v.into();
7208 self
7209 }
7210}
7211impl molecule::prelude::Builder for CellInputBuilder {
7212 type Entity = CellInput;
7213 const NAME: &'static str = "CellInputBuilder";
7214 fn expected_length(&self) -> usize {
7215 Self::TOTAL_SIZE
7216 }
7217 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7218 writer.write_all(self.since.as_slice())?;
7219 writer.write_all(self.previous_output.as_slice())?;
7220 Ok(())
7221 }
7222 fn build(&self) -> Self::Entity {
7223 let mut inner = Vec::with_capacity(self.expected_length());
7224 self.write(&mut inner)
7225 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7226 CellInput::new_unchecked(inner.into())
7227 }
7228}
7229#[derive(Clone)]
7230pub struct CellOutput(molecule::bytes::Bytes);
7231impl ::core::fmt::LowerHex for CellOutput {
7232 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7233 use molecule::hex_string;
7234 if f.alternate() {
7235 write!(f, "0x")?;
7236 }
7237 write!(f, "{}", hex_string(self.as_slice()))
7238 }
7239}
7240impl ::core::fmt::Debug for CellOutput {
7241 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7242 write!(f, "{}({:#x})", Self::NAME, self)
7243 }
7244}
7245impl ::core::fmt::Display for CellOutput {
7246 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7247 write!(f, "{} {{ ", Self::NAME)?;
7248 write!(f, "{}: {}", "capacity", self.capacity())?;
7249 write!(f, ", {}: {}", "lock", self.lock())?;
7250 write!(f, ", {}: {}", "type_", self.type_())?;
7251 let extra_count = self.count_extra_fields();
7252 if extra_count != 0 {
7253 write!(f, ", .. ({} fields)", extra_count)?;
7254 }
7255 write!(f, " }}")
7256 }
7257}
7258impl ::core::default::Default for CellOutput {
7259 fn default() -> Self {
7260 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7261 CellOutput::new_unchecked(v)
7262 }
7263}
7264impl CellOutput {
7265 const DEFAULT_VALUE: [u8; 77] = [
7266 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,
7267 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,
7268 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7269 ];
7270 pub const FIELD_COUNT: usize = 3;
7271 pub fn total_size(&self) -> usize {
7272 molecule::unpack_number(self.as_slice()) as usize
7273 }
7274 pub fn field_count(&self) -> usize {
7275 if self.total_size() == molecule::NUMBER_SIZE {
7276 0
7277 } else {
7278 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7279 }
7280 }
7281 pub fn count_extra_fields(&self) -> usize {
7282 self.field_count() - Self::FIELD_COUNT
7283 }
7284 pub fn has_extra_fields(&self) -> bool {
7285 Self::FIELD_COUNT != self.field_count()
7286 }
7287 pub fn capacity(&self) -> Uint64 {
7288 let slice = self.as_slice();
7289 let start = molecule::unpack_number(&slice[4..]) as usize;
7290 let end = molecule::unpack_number(&slice[8..]) as usize;
7291 Uint64::new_unchecked(self.0.slice(start..end))
7292 }
7293 pub fn lock(&self) -> Script {
7294 let slice = self.as_slice();
7295 let start = molecule::unpack_number(&slice[8..]) as usize;
7296 let end = molecule::unpack_number(&slice[12..]) as usize;
7297 Script::new_unchecked(self.0.slice(start..end))
7298 }
7299 pub fn type_(&self) -> ScriptOpt {
7300 let slice = self.as_slice();
7301 let start = molecule::unpack_number(&slice[12..]) as usize;
7302 if self.has_extra_fields() {
7303 let end = molecule::unpack_number(&slice[16..]) as usize;
7304 ScriptOpt::new_unchecked(self.0.slice(start..end))
7305 } else {
7306 ScriptOpt::new_unchecked(self.0.slice(start..))
7307 }
7308 }
7309 pub fn as_reader<'r>(&'r self) -> CellOutputReader<'r> {
7310 CellOutputReader::new_unchecked(self.as_slice())
7311 }
7312}
7313impl molecule::prelude::Entity for CellOutput {
7314 type Builder = CellOutputBuilder;
7315 const NAME: &'static str = "CellOutput";
7316 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7317 CellOutput(data)
7318 }
7319 fn as_bytes(&self) -> molecule::bytes::Bytes {
7320 self.0.clone()
7321 }
7322 fn as_slice(&self) -> &[u8] {
7323 &self.0[..]
7324 }
7325 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7326 CellOutputReader::from_slice(slice).map(|reader| reader.to_entity())
7327 }
7328 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7329 CellOutputReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7330 }
7331 fn new_builder() -> Self::Builder {
7332 ::core::default::Default::default()
7333 }
7334 fn as_builder(self) -> Self::Builder {
7335 Self::new_builder()
7336 .capacity(self.capacity())
7337 .lock(self.lock())
7338 .type_(self.type_())
7339 }
7340}
7341#[derive(Clone, Copy)]
7342pub struct CellOutputReader<'r>(&'r [u8]);
7343impl<'r> ::core::fmt::LowerHex for CellOutputReader<'r> {
7344 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7345 use molecule::hex_string;
7346 if f.alternate() {
7347 write!(f, "0x")?;
7348 }
7349 write!(f, "{}", hex_string(self.as_slice()))
7350 }
7351}
7352impl<'r> ::core::fmt::Debug for CellOutputReader<'r> {
7353 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7354 write!(f, "{}({:#x})", Self::NAME, self)
7355 }
7356}
7357impl<'r> ::core::fmt::Display for CellOutputReader<'r> {
7358 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7359 write!(f, "{} {{ ", Self::NAME)?;
7360 write!(f, "{}: {}", "capacity", self.capacity())?;
7361 write!(f, ", {}: {}", "lock", self.lock())?;
7362 write!(f, ", {}: {}", "type_", self.type_())?;
7363 let extra_count = self.count_extra_fields();
7364 if extra_count != 0 {
7365 write!(f, ", .. ({} fields)", extra_count)?;
7366 }
7367 write!(f, " }}")
7368 }
7369}
7370impl<'r> CellOutputReader<'r> {
7371 pub const FIELD_COUNT: usize = 3;
7372 pub fn total_size(&self) -> usize {
7373 molecule::unpack_number(self.as_slice()) as usize
7374 }
7375 pub fn field_count(&self) -> usize {
7376 if self.total_size() == molecule::NUMBER_SIZE {
7377 0
7378 } else {
7379 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7380 }
7381 }
7382 pub fn count_extra_fields(&self) -> usize {
7383 self.field_count() - Self::FIELD_COUNT
7384 }
7385 pub fn has_extra_fields(&self) -> bool {
7386 Self::FIELD_COUNT != self.field_count()
7387 }
7388 pub fn capacity(&self) -> Uint64Reader<'r> {
7389 let slice = self.as_slice();
7390 let start = molecule::unpack_number(&slice[4..]) as usize;
7391 let end = molecule::unpack_number(&slice[8..]) as usize;
7392 Uint64Reader::new_unchecked(&self.as_slice()[start..end])
7393 }
7394 pub fn lock(&self) -> ScriptReader<'r> {
7395 let slice = self.as_slice();
7396 let start = molecule::unpack_number(&slice[8..]) as usize;
7397 let end = molecule::unpack_number(&slice[12..]) as usize;
7398 ScriptReader::new_unchecked(&self.as_slice()[start..end])
7399 }
7400 pub fn type_(&self) -> ScriptOptReader<'r> {
7401 let slice = self.as_slice();
7402 let start = molecule::unpack_number(&slice[12..]) as usize;
7403 if self.has_extra_fields() {
7404 let end = molecule::unpack_number(&slice[16..]) as usize;
7405 ScriptOptReader::new_unchecked(&self.as_slice()[start..end])
7406 } else {
7407 ScriptOptReader::new_unchecked(&self.as_slice()[start..])
7408 }
7409 }
7410}
7411impl<'r> molecule::prelude::Reader<'r> for CellOutputReader<'r> {
7412 type Entity = CellOutput;
7413 const NAME: &'static str = "CellOutputReader";
7414 fn to_entity(&self) -> Self::Entity {
7415 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7416 }
7417 fn new_unchecked(slice: &'r [u8]) -> Self {
7418 CellOutputReader(slice)
7419 }
7420 fn as_slice(&self) -> &'r [u8] {
7421 self.0
7422 }
7423 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7424 use molecule::verification_error as ve;
7425 let slice_len = slice.len();
7426 if slice_len < molecule::NUMBER_SIZE {
7427 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7428 }
7429 let total_size = molecule::unpack_number(slice) as usize;
7430 if slice_len != total_size {
7431 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7432 }
7433 if slice_len < molecule::NUMBER_SIZE * 2 {
7434 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7435 }
7436 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7437 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7438 return ve!(Self, OffsetsNotMatch);
7439 }
7440 if slice_len < offset_first {
7441 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7442 }
7443 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7444 if field_count < Self::FIELD_COUNT {
7445 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7446 } else if !compatible && field_count > Self::FIELD_COUNT {
7447 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7448 };
7449 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7450 .chunks_exact(molecule::NUMBER_SIZE)
7451 .map(|x| molecule::unpack_number(x) as usize)
7452 .collect();
7453 offsets.push(total_size);
7454 if offsets.windows(2).any(|i| i[0] > i[1]) {
7455 return ve!(Self, OffsetsNotMatch);
7456 }
7457 Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7458 ScriptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7459 ScriptOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7460 Ok(())
7461 }
7462}
7463#[derive(Clone, Debug, Default)]
7464pub struct CellOutputBuilder {
7465 pub(crate) capacity: Uint64,
7466 pub(crate) lock: Script,
7467 pub(crate) type_: ScriptOpt,
7468}
7469impl CellOutputBuilder {
7470 pub const FIELD_COUNT: usize = 3;
7471 pub fn capacity<T>(mut self, v: T) -> Self
7472 where
7473 T: ::core::convert::Into<Uint64>,
7474 {
7475 self.capacity = v.into();
7476 self
7477 }
7478 pub fn lock<T>(mut self, v: T) -> Self
7479 where
7480 T: ::core::convert::Into<Script>,
7481 {
7482 self.lock = v.into();
7483 self
7484 }
7485 pub fn type_<T>(mut self, v: T) -> Self
7486 where
7487 T: ::core::convert::Into<ScriptOpt>,
7488 {
7489 self.type_ = v.into();
7490 self
7491 }
7492}
7493impl molecule::prelude::Builder for CellOutputBuilder {
7494 type Entity = CellOutput;
7495 const NAME: &'static str = "CellOutputBuilder";
7496 fn expected_length(&self) -> usize {
7497 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
7498 + self.capacity.as_slice().len()
7499 + self.lock.as_slice().len()
7500 + self.type_.as_slice().len()
7501 }
7502 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7503 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
7504 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
7505 offsets.push(total_size);
7506 total_size += self.capacity.as_slice().len();
7507 offsets.push(total_size);
7508 total_size += self.lock.as_slice().len();
7509 offsets.push(total_size);
7510 total_size += self.type_.as_slice().len();
7511 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
7512 for offset in offsets.into_iter() {
7513 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
7514 }
7515 writer.write_all(self.capacity.as_slice())?;
7516 writer.write_all(self.lock.as_slice())?;
7517 writer.write_all(self.type_.as_slice())?;
7518 Ok(())
7519 }
7520 fn build(&self) -> Self::Entity {
7521 let mut inner = Vec::with_capacity(self.expected_length());
7522 self.write(&mut inner)
7523 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7524 CellOutput::new_unchecked(inner.into())
7525 }
7526}
7527#[derive(Clone)]
7528pub struct CellDep(molecule::bytes::Bytes);
7529impl ::core::fmt::LowerHex for CellDep {
7530 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7531 use molecule::hex_string;
7532 if f.alternate() {
7533 write!(f, "0x")?;
7534 }
7535 write!(f, "{}", hex_string(self.as_slice()))
7536 }
7537}
7538impl ::core::fmt::Debug for CellDep {
7539 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7540 write!(f, "{}({:#x})", Self::NAME, self)
7541 }
7542}
7543impl ::core::fmt::Display for CellDep {
7544 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7545 write!(f, "{} {{ ", Self::NAME)?;
7546 write!(f, "{}: {}", "out_point", self.out_point())?;
7547 write!(f, ", {}: {}", "dep_type", self.dep_type())?;
7548 write!(f, " }}")
7549 }
7550}
7551impl ::core::default::Default for CellDep {
7552 fn default() -> Self {
7553 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7554 CellDep::new_unchecked(v)
7555 }
7556}
7557impl CellDep {
7558 const DEFAULT_VALUE: [u8; 37] = [
7559 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7560 0, 0, 0, 0, 0, 0, 0,
7561 ];
7562 pub const TOTAL_SIZE: usize = 37;
7563 pub const FIELD_SIZES: [usize; 2] = [36, 1];
7564 pub const FIELD_COUNT: usize = 2;
7565 pub fn out_point(&self) -> OutPoint {
7566 OutPoint::new_unchecked(self.0.slice(0..36))
7567 }
7568 pub fn dep_type(&self) -> Byte {
7569 Byte::new_unchecked(self.0.slice(36..37))
7570 }
7571 pub fn as_reader<'r>(&'r self) -> CellDepReader<'r> {
7572 CellDepReader::new_unchecked(self.as_slice())
7573 }
7574}
7575impl molecule::prelude::Entity for CellDep {
7576 type Builder = CellDepBuilder;
7577 const NAME: &'static str = "CellDep";
7578 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7579 CellDep(data)
7580 }
7581 fn as_bytes(&self) -> molecule::bytes::Bytes {
7582 self.0.clone()
7583 }
7584 fn as_slice(&self) -> &[u8] {
7585 &self.0[..]
7586 }
7587 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7588 CellDepReader::from_slice(slice).map(|reader| reader.to_entity())
7589 }
7590 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7591 CellDepReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7592 }
7593 fn new_builder() -> Self::Builder {
7594 ::core::default::Default::default()
7595 }
7596 fn as_builder(self) -> Self::Builder {
7597 Self::new_builder()
7598 .out_point(self.out_point())
7599 .dep_type(self.dep_type())
7600 }
7601}
7602#[derive(Clone, Copy)]
7603pub struct CellDepReader<'r>(&'r [u8]);
7604impl<'r> ::core::fmt::LowerHex for CellDepReader<'r> {
7605 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7606 use molecule::hex_string;
7607 if f.alternate() {
7608 write!(f, "0x")?;
7609 }
7610 write!(f, "{}", hex_string(self.as_slice()))
7611 }
7612}
7613impl<'r> ::core::fmt::Debug for CellDepReader<'r> {
7614 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7615 write!(f, "{}({:#x})", Self::NAME, self)
7616 }
7617}
7618impl<'r> ::core::fmt::Display for CellDepReader<'r> {
7619 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7620 write!(f, "{} {{ ", Self::NAME)?;
7621 write!(f, "{}: {}", "out_point", self.out_point())?;
7622 write!(f, ", {}: {}", "dep_type", self.dep_type())?;
7623 write!(f, " }}")
7624 }
7625}
7626impl<'r> CellDepReader<'r> {
7627 pub const TOTAL_SIZE: usize = 37;
7628 pub const FIELD_SIZES: [usize; 2] = [36, 1];
7629 pub const FIELD_COUNT: usize = 2;
7630 pub fn out_point(&self) -> OutPointReader<'r> {
7631 OutPointReader::new_unchecked(&self.as_slice()[0..36])
7632 }
7633 pub fn dep_type(&self) -> ByteReader<'r> {
7634 ByteReader::new_unchecked(&self.as_slice()[36..37])
7635 }
7636}
7637impl<'r> molecule::prelude::Reader<'r> for CellDepReader<'r> {
7638 type Entity = CellDep;
7639 const NAME: &'static str = "CellDepReader";
7640 fn to_entity(&self) -> Self::Entity {
7641 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7642 }
7643 fn new_unchecked(slice: &'r [u8]) -> Self {
7644 CellDepReader(slice)
7645 }
7646 fn as_slice(&self) -> &'r [u8] {
7647 self.0
7648 }
7649 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
7650 use molecule::verification_error as ve;
7651 let slice_len = slice.len();
7652 if slice_len != Self::TOTAL_SIZE {
7653 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
7654 }
7655 Ok(())
7656 }
7657}
7658#[derive(Clone, Debug, Default)]
7659pub struct CellDepBuilder {
7660 pub(crate) out_point: OutPoint,
7661 pub(crate) dep_type: Byte,
7662}
7663impl CellDepBuilder {
7664 pub const TOTAL_SIZE: usize = 37;
7665 pub const FIELD_SIZES: [usize; 2] = [36, 1];
7666 pub const FIELD_COUNT: usize = 2;
7667 pub fn out_point<T>(mut self, v: T) -> Self
7668 where
7669 T: ::core::convert::Into<OutPoint>,
7670 {
7671 self.out_point = v.into();
7672 self
7673 }
7674 pub fn dep_type<T>(mut self, v: T) -> Self
7675 where
7676 T: ::core::convert::Into<Byte>,
7677 {
7678 self.dep_type = v.into();
7679 self
7680 }
7681}
7682impl molecule::prelude::Builder for CellDepBuilder {
7683 type Entity = CellDep;
7684 const NAME: &'static str = "CellDepBuilder";
7685 fn expected_length(&self) -> usize {
7686 Self::TOTAL_SIZE
7687 }
7688 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
7689 writer.write_all(self.out_point.as_slice())?;
7690 writer.write_all(self.dep_type.as_slice())?;
7691 Ok(())
7692 }
7693 fn build(&self) -> Self::Entity {
7694 let mut inner = Vec::with_capacity(self.expected_length());
7695 self.write(&mut inner)
7696 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
7697 CellDep::new_unchecked(inner.into())
7698 }
7699}
7700#[derive(Clone)]
7701pub struct RawTransaction(molecule::bytes::Bytes);
7702impl ::core::fmt::LowerHex for RawTransaction {
7703 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7704 use molecule::hex_string;
7705 if f.alternate() {
7706 write!(f, "0x")?;
7707 }
7708 write!(f, "{}", hex_string(self.as_slice()))
7709 }
7710}
7711impl ::core::fmt::Debug for RawTransaction {
7712 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7713 write!(f, "{}({:#x})", Self::NAME, self)
7714 }
7715}
7716impl ::core::fmt::Display for RawTransaction {
7717 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7718 write!(f, "{} {{ ", Self::NAME)?;
7719 write!(f, "{}: {}", "version", self.version())?;
7720 write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
7721 write!(f, ", {}: {}", "header_deps", self.header_deps())?;
7722 write!(f, ", {}: {}", "inputs", self.inputs())?;
7723 write!(f, ", {}: {}", "outputs", self.outputs())?;
7724 write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
7725 let extra_count = self.count_extra_fields();
7726 if extra_count != 0 {
7727 write!(f, ", .. ({} fields)", extra_count)?;
7728 }
7729 write!(f, " }}")
7730 }
7731}
7732impl ::core::default::Default for RawTransaction {
7733 fn default() -> Self {
7734 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
7735 RawTransaction::new_unchecked(v)
7736 }
7737}
7738impl RawTransaction {
7739 const DEFAULT_VALUE: [u8; 52] = [
7740 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,
7741 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
7742 ];
7743 pub const FIELD_COUNT: usize = 6;
7744 pub fn total_size(&self) -> usize {
7745 molecule::unpack_number(self.as_slice()) as usize
7746 }
7747 pub fn field_count(&self) -> usize {
7748 if self.total_size() == molecule::NUMBER_SIZE {
7749 0
7750 } else {
7751 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7752 }
7753 }
7754 pub fn count_extra_fields(&self) -> usize {
7755 self.field_count() - Self::FIELD_COUNT
7756 }
7757 pub fn has_extra_fields(&self) -> bool {
7758 Self::FIELD_COUNT != self.field_count()
7759 }
7760 pub fn version(&self) -> Uint32 {
7761 let slice = self.as_slice();
7762 let start = molecule::unpack_number(&slice[4..]) as usize;
7763 let end = molecule::unpack_number(&slice[8..]) as usize;
7764 Uint32::new_unchecked(self.0.slice(start..end))
7765 }
7766 pub fn cell_deps(&self) -> CellDepVec {
7767 let slice = self.as_slice();
7768 let start = molecule::unpack_number(&slice[8..]) as usize;
7769 let end = molecule::unpack_number(&slice[12..]) as usize;
7770 CellDepVec::new_unchecked(self.0.slice(start..end))
7771 }
7772 pub fn header_deps(&self) -> Byte32Vec {
7773 let slice = self.as_slice();
7774 let start = molecule::unpack_number(&slice[12..]) as usize;
7775 let end = molecule::unpack_number(&slice[16..]) as usize;
7776 Byte32Vec::new_unchecked(self.0.slice(start..end))
7777 }
7778 pub fn inputs(&self) -> CellInputVec {
7779 let slice = self.as_slice();
7780 let start = molecule::unpack_number(&slice[16..]) as usize;
7781 let end = molecule::unpack_number(&slice[20..]) as usize;
7782 CellInputVec::new_unchecked(self.0.slice(start..end))
7783 }
7784 pub fn outputs(&self) -> CellOutputVec {
7785 let slice = self.as_slice();
7786 let start = molecule::unpack_number(&slice[20..]) as usize;
7787 let end = molecule::unpack_number(&slice[24..]) as usize;
7788 CellOutputVec::new_unchecked(self.0.slice(start..end))
7789 }
7790 pub fn outputs_data(&self) -> BytesVec {
7791 let slice = self.as_slice();
7792 let start = molecule::unpack_number(&slice[24..]) as usize;
7793 if self.has_extra_fields() {
7794 let end = molecule::unpack_number(&slice[28..]) as usize;
7795 BytesVec::new_unchecked(self.0.slice(start..end))
7796 } else {
7797 BytesVec::new_unchecked(self.0.slice(start..))
7798 }
7799 }
7800 pub fn as_reader<'r>(&'r self) -> RawTransactionReader<'r> {
7801 RawTransactionReader::new_unchecked(self.as_slice())
7802 }
7803}
7804impl molecule::prelude::Entity for RawTransaction {
7805 type Builder = RawTransactionBuilder;
7806 const NAME: &'static str = "RawTransaction";
7807 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
7808 RawTransaction(data)
7809 }
7810 fn as_bytes(&self) -> molecule::bytes::Bytes {
7811 self.0.clone()
7812 }
7813 fn as_slice(&self) -> &[u8] {
7814 &self.0[..]
7815 }
7816 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7817 RawTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
7818 }
7819 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
7820 RawTransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
7821 }
7822 fn new_builder() -> Self::Builder {
7823 ::core::default::Default::default()
7824 }
7825 fn as_builder(self) -> Self::Builder {
7826 Self::new_builder()
7827 .version(self.version())
7828 .cell_deps(self.cell_deps())
7829 .header_deps(self.header_deps())
7830 .inputs(self.inputs())
7831 .outputs(self.outputs())
7832 .outputs_data(self.outputs_data())
7833 }
7834}
7835#[derive(Clone, Copy)]
7836pub struct RawTransactionReader<'r>(&'r [u8]);
7837impl<'r> ::core::fmt::LowerHex for RawTransactionReader<'r> {
7838 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7839 use molecule::hex_string;
7840 if f.alternate() {
7841 write!(f, "0x")?;
7842 }
7843 write!(f, "{}", hex_string(self.as_slice()))
7844 }
7845}
7846impl<'r> ::core::fmt::Debug for RawTransactionReader<'r> {
7847 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7848 write!(f, "{}({:#x})", Self::NAME, self)
7849 }
7850}
7851impl<'r> ::core::fmt::Display for RawTransactionReader<'r> {
7852 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
7853 write!(f, "{} {{ ", Self::NAME)?;
7854 write!(f, "{}: {}", "version", self.version())?;
7855 write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
7856 write!(f, ", {}: {}", "header_deps", self.header_deps())?;
7857 write!(f, ", {}: {}", "inputs", self.inputs())?;
7858 write!(f, ", {}: {}", "outputs", self.outputs())?;
7859 write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
7860 let extra_count = self.count_extra_fields();
7861 if extra_count != 0 {
7862 write!(f, ", .. ({} fields)", extra_count)?;
7863 }
7864 write!(f, " }}")
7865 }
7866}
7867impl<'r> RawTransactionReader<'r> {
7868 pub const FIELD_COUNT: usize = 6;
7869 pub fn total_size(&self) -> usize {
7870 molecule::unpack_number(self.as_slice()) as usize
7871 }
7872 pub fn field_count(&self) -> usize {
7873 if self.total_size() == molecule::NUMBER_SIZE {
7874 0
7875 } else {
7876 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
7877 }
7878 }
7879 pub fn count_extra_fields(&self) -> usize {
7880 self.field_count() - Self::FIELD_COUNT
7881 }
7882 pub fn has_extra_fields(&self) -> bool {
7883 Self::FIELD_COUNT != self.field_count()
7884 }
7885 pub fn version(&self) -> Uint32Reader<'r> {
7886 let slice = self.as_slice();
7887 let start = molecule::unpack_number(&slice[4..]) as usize;
7888 let end = molecule::unpack_number(&slice[8..]) as usize;
7889 Uint32Reader::new_unchecked(&self.as_slice()[start..end])
7890 }
7891 pub fn cell_deps(&self) -> CellDepVecReader<'r> {
7892 let slice = self.as_slice();
7893 let start = molecule::unpack_number(&slice[8..]) as usize;
7894 let end = molecule::unpack_number(&slice[12..]) as usize;
7895 CellDepVecReader::new_unchecked(&self.as_slice()[start..end])
7896 }
7897 pub fn header_deps(&self) -> Byte32VecReader<'r> {
7898 let slice = self.as_slice();
7899 let start = molecule::unpack_number(&slice[12..]) as usize;
7900 let end = molecule::unpack_number(&slice[16..]) as usize;
7901 Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
7902 }
7903 pub fn inputs(&self) -> CellInputVecReader<'r> {
7904 let slice = self.as_slice();
7905 let start = molecule::unpack_number(&slice[16..]) as usize;
7906 let end = molecule::unpack_number(&slice[20..]) as usize;
7907 CellInputVecReader::new_unchecked(&self.as_slice()[start..end])
7908 }
7909 pub fn outputs(&self) -> CellOutputVecReader<'r> {
7910 let slice = self.as_slice();
7911 let start = molecule::unpack_number(&slice[20..]) as usize;
7912 let end = molecule::unpack_number(&slice[24..]) as usize;
7913 CellOutputVecReader::new_unchecked(&self.as_slice()[start..end])
7914 }
7915 pub fn outputs_data(&self) -> BytesVecReader<'r> {
7916 let slice = self.as_slice();
7917 let start = molecule::unpack_number(&slice[24..]) as usize;
7918 if self.has_extra_fields() {
7919 let end = molecule::unpack_number(&slice[28..]) as usize;
7920 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
7921 } else {
7922 BytesVecReader::new_unchecked(&self.as_slice()[start..])
7923 }
7924 }
7925}
7926impl<'r> molecule::prelude::Reader<'r> for RawTransactionReader<'r> {
7927 type Entity = RawTransaction;
7928 const NAME: &'static str = "RawTransactionReader";
7929 fn to_entity(&self) -> Self::Entity {
7930 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
7931 }
7932 fn new_unchecked(slice: &'r [u8]) -> Self {
7933 RawTransactionReader(slice)
7934 }
7935 fn as_slice(&self) -> &'r [u8] {
7936 self.0
7937 }
7938 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
7939 use molecule::verification_error as ve;
7940 let slice_len = slice.len();
7941 if slice_len < molecule::NUMBER_SIZE {
7942 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
7943 }
7944 let total_size = molecule::unpack_number(slice) as usize;
7945 if slice_len != total_size {
7946 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
7947 }
7948 if slice_len < molecule::NUMBER_SIZE * 2 {
7949 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
7950 }
7951 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
7952 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
7953 return ve!(Self, OffsetsNotMatch);
7954 }
7955 if slice_len < offset_first {
7956 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
7957 }
7958 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
7959 if field_count < Self::FIELD_COUNT {
7960 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7961 } else if !compatible && field_count > Self::FIELD_COUNT {
7962 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
7963 };
7964 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
7965 .chunks_exact(molecule::NUMBER_SIZE)
7966 .map(|x| molecule::unpack_number(x) as usize)
7967 .collect();
7968 offsets.push(total_size);
7969 if offsets.windows(2).any(|i| i[0] > i[1]) {
7970 return ve!(Self, OffsetsNotMatch);
7971 }
7972 Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
7973 CellDepVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
7974 Byte32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
7975 CellInputVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
7976 CellOutputVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
7977 BytesVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
7978 Ok(())
7979 }
7980}
7981#[derive(Clone, Debug, Default)]
7982pub struct RawTransactionBuilder {
7983 pub(crate) version: Uint32,
7984 pub(crate) cell_deps: CellDepVec,
7985 pub(crate) header_deps: Byte32Vec,
7986 pub(crate) inputs: CellInputVec,
7987 pub(crate) outputs: CellOutputVec,
7988 pub(crate) outputs_data: BytesVec,
7989}
7990impl RawTransactionBuilder {
7991 pub const FIELD_COUNT: usize = 6;
7992 pub fn version<T>(mut self, v: T) -> Self
7993 where
7994 T: ::core::convert::Into<Uint32>,
7995 {
7996 self.version = v.into();
7997 self
7998 }
7999 pub fn cell_deps<T>(mut self, v: T) -> Self
8000 where
8001 T: ::core::convert::Into<CellDepVec>,
8002 {
8003 self.cell_deps = v.into();
8004 self
8005 }
8006 pub fn header_deps<T>(mut self, v: T) -> Self
8007 where
8008 T: ::core::convert::Into<Byte32Vec>,
8009 {
8010 self.header_deps = v.into();
8011 self
8012 }
8013 pub fn inputs<T>(mut self, v: T) -> Self
8014 where
8015 T: ::core::convert::Into<CellInputVec>,
8016 {
8017 self.inputs = v.into();
8018 self
8019 }
8020 pub fn outputs<T>(mut self, v: T) -> Self
8021 where
8022 T: ::core::convert::Into<CellOutputVec>,
8023 {
8024 self.outputs = v.into();
8025 self
8026 }
8027 pub fn outputs_data<T>(mut self, v: T) -> Self
8028 where
8029 T: ::core::convert::Into<BytesVec>,
8030 {
8031 self.outputs_data = v.into();
8032 self
8033 }
8034}
8035impl molecule::prelude::Builder for RawTransactionBuilder {
8036 type Entity = RawTransaction;
8037 const NAME: &'static str = "RawTransactionBuilder";
8038 fn expected_length(&self) -> usize {
8039 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8040 + self.version.as_slice().len()
8041 + self.cell_deps.as_slice().len()
8042 + self.header_deps.as_slice().len()
8043 + self.inputs.as_slice().len()
8044 + self.outputs.as_slice().len()
8045 + self.outputs_data.as_slice().len()
8046 }
8047 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8048 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8049 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8050 offsets.push(total_size);
8051 total_size += self.version.as_slice().len();
8052 offsets.push(total_size);
8053 total_size += self.cell_deps.as_slice().len();
8054 offsets.push(total_size);
8055 total_size += self.header_deps.as_slice().len();
8056 offsets.push(total_size);
8057 total_size += self.inputs.as_slice().len();
8058 offsets.push(total_size);
8059 total_size += self.outputs.as_slice().len();
8060 offsets.push(total_size);
8061 total_size += self.outputs_data.as_slice().len();
8062 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8063 for offset in offsets.into_iter() {
8064 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8065 }
8066 writer.write_all(self.version.as_slice())?;
8067 writer.write_all(self.cell_deps.as_slice())?;
8068 writer.write_all(self.header_deps.as_slice())?;
8069 writer.write_all(self.inputs.as_slice())?;
8070 writer.write_all(self.outputs.as_slice())?;
8071 writer.write_all(self.outputs_data.as_slice())?;
8072 Ok(())
8073 }
8074 fn build(&self) -> Self::Entity {
8075 let mut inner = Vec::with_capacity(self.expected_length());
8076 self.write(&mut inner)
8077 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8078 RawTransaction::new_unchecked(inner.into())
8079 }
8080}
8081#[derive(Clone)]
8082pub struct Transaction(molecule::bytes::Bytes);
8083impl ::core::fmt::LowerHex for Transaction {
8084 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8085 use molecule::hex_string;
8086 if f.alternate() {
8087 write!(f, "0x")?;
8088 }
8089 write!(f, "{}", hex_string(self.as_slice()))
8090 }
8091}
8092impl ::core::fmt::Debug for Transaction {
8093 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8094 write!(f, "{}({:#x})", Self::NAME, self)
8095 }
8096}
8097impl ::core::fmt::Display for Transaction {
8098 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8099 write!(f, "{} {{ ", Self::NAME)?;
8100 write!(f, "{}: {}", "raw", self.raw())?;
8101 write!(f, ", {}: {}", "witnesses", self.witnesses())?;
8102 let extra_count = self.count_extra_fields();
8103 if extra_count != 0 {
8104 write!(f, ", .. ({} fields)", extra_count)?;
8105 }
8106 write!(f, " }}")
8107 }
8108}
8109impl ::core::default::Default for Transaction {
8110 fn default() -> Self {
8111 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8112 Transaction::new_unchecked(v)
8113 }
8114}
8115impl Transaction {
8116 const DEFAULT_VALUE: [u8; 68] = [
8117 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,
8118 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,
8119 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
8120 ];
8121 pub const FIELD_COUNT: usize = 2;
8122 pub fn total_size(&self) -> usize {
8123 molecule::unpack_number(self.as_slice()) as usize
8124 }
8125 pub fn field_count(&self) -> usize {
8126 if self.total_size() == molecule::NUMBER_SIZE {
8127 0
8128 } else {
8129 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8130 }
8131 }
8132 pub fn count_extra_fields(&self) -> usize {
8133 self.field_count() - Self::FIELD_COUNT
8134 }
8135 pub fn has_extra_fields(&self) -> bool {
8136 Self::FIELD_COUNT != self.field_count()
8137 }
8138 pub fn raw(&self) -> RawTransaction {
8139 let slice = self.as_slice();
8140 let start = molecule::unpack_number(&slice[4..]) as usize;
8141 let end = molecule::unpack_number(&slice[8..]) as usize;
8142 RawTransaction::new_unchecked(self.0.slice(start..end))
8143 }
8144 pub fn witnesses(&self) -> BytesVec {
8145 let slice = self.as_slice();
8146 let start = molecule::unpack_number(&slice[8..]) as usize;
8147 if self.has_extra_fields() {
8148 let end = molecule::unpack_number(&slice[12..]) as usize;
8149 BytesVec::new_unchecked(self.0.slice(start..end))
8150 } else {
8151 BytesVec::new_unchecked(self.0.slice(start..))
8152 }
8153 }
8154 pub fn as_reader<'r>(&'r self) -> TransactionReader<'r> {
8155 TransactionReader::new_unchecked(self.as_slice())
8156 }
8157}
8158impl molecule::prelude::Entity for Transaction {
8159 type Builder = TransactionBuilder;
8160 const NAME: &'static str = "Transaction";
8161 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8162 Transaction(data)
8163 }
8164 fn as_bytes(&self) -> molecule::bytes::Bytes {
8165 self.0.clone()
8166 }
8167 fn as_slice(&self) -> &[u8] {
8168 &self.0[..]
8169 }
8170 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8171 TransactionReader::from_slice(slice).map(|reader| reader.to_entity())
8172 }
8173 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8174 TransactionReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8175 }
8176 fn new_builder() -> Self::Builder {
8177 ::core::default::Default::default()
8178 }
8179 fn as_builder(self) -> Self::Builder {
8180 Self::new_builder()
8181 .raw(self.raw())
8182 .witnesses(self.witnesses())
8183 }
8184}
8185#[derive(Clone, Copy)]
8186pub struct TransactionReader<'r>(&'r [u8]);
8187impl<'r> ::core::fmt::LowerHex for TransactionReader<'r> {
8188 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8189 use molecule::hex_string;
8190 if f.alternate() {
8191 write!(f, "0x")?;
8192 }
8193 write!(f, "{}", hex_string(self.as_slice()))
8194 }
8195}
8196impl<'r> ::core::fmt::Debug for TransactionReader<'r> {
8197 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8198 write!(f, "{}({:#x})", Self::NAME, self)
8199 }
8200}
8201impl<'r> ::core::fmt::Display for TransactionReader<'r> {
8202 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8203 write!(f, "{} {{ ", Self::NAME)?;
8204 write!(f, "{}: {}", "raw", self.raw())?;
8205 write!(f, ", {}: {}", "witnesses", self.witnesses())?;
8206 let extra_count = self.count_extra_fields();
8207 if extra_count != 0 {
8208 write!(f, ", .. ({} fields)", extra_count)?;
8209 }
8210 write!(f, " }}")
8211 }
8212}
8213impl<'r> TransactionReader<'r> {
8214 pub const FIELD_COUNT: usize = 2;
8215 pub fn total_size(&self) -> usize {
8216 molecule::unpack_number(self.as_slice()) as usize
8217 }
8218 pub fn field_count(&self) -> usize {
8219 if self.total_size() == molecule::NUMBER_SIZE {
8220 0
8221 } else {
8222 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8223 }
8224 }
8225 pub fn count_extra_fields(&self) -> usize {
8226 self.field_count() - Self::FIELD_COUNT
8227 }
8228 pub fn has_extra_fields(&self) -> bool {
8229 Self::FIELD_COUNT != self.field_count()
8230 }
8231 pub fn raw(&self) -> RawTransactionReader<'r> {
8232 let slice = self.as_slice();
8233 let start = molecule::unpack_number(&slice[4..]) as usize;
8234 let end = molecule::unpack_number(&slice[8..]) as usize;
8235 RawTransactionReader::new_unchecked(&self.as_slice()[start..end])
8236 }
8237 pub fn witnesses(&self) -> BytesVecReader<'r> {
8238 let slice = self.as_slice();
8239 let start = molecule::unpack_number(&slice[8..]) as usize;
8240 if self.has_extra_fields() {
8241 let end = molecule::unpack_number(&slice[12..]) as usize;
8242 BytesVecReader::new_unchecked(&self.as_slice()[start..end])
8243 } else {
8244 BytesVecReader::new_unchecked(&self.as_slice()[start..])
8245 }
8246 }
8247}
8248impl<'r> molecule::prelude::Reader<'r> for TransactionReader<'r> {
8249 type Entity = Transaction;
8250 const NAME: &'static str = "TransactionReader";
8251 fn to_entity(&self) -> Self::Entity {
8252 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8253 }
8254 fn new_unchecked(slice: &'r [u8]) -> Self {
8255 TransactionReader(slice)
8256 }
8257 fn as_slice(&self) -> &'r [u8] {
8258 self.0
8259 }
8260 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
8261 use molecule::verification_error as ve;
8262 let slice_len = slice.len();
8263 if slice_len < molecule::NUMBER_SIZE {
8264 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
8265 }
8266 let total_size = molecule::unpack_number(slice) as usize;
8267 if slice_len != total_size {
8268 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
8269 }
8270 if slice_len < molecule::NUMBER_SIZE * 2 {
8271 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
8272 }
8273 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
8274 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
8275 return ve!(Self, OffsetsNotMatch);
8276 }
8277 if slice_len < offset_first {
8278 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
8279 }
8280 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
8281 if field_count < Self::FIELD_COUNT {
8282 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8283 } else if !compatible && field_count > Self::FIELD_COUNT {
8284 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
8285 };
8286 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
8287 .chunks_exact(molecule::NUMBER_SIZE)
8288 .map(|x| molecule::unpack_number(x) as usize)
8289 .collect();
8290 offsets.push(total_size);
8291 if offsets.windows(2).any(|i| i[0] > i[1]) {
8292 return ve!(Self, OffsetsNotMatch);
8293 }
8294 RawTransactionReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
8295 BytesVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
8296 Ok(())
8297 }
8298}
8299#[derive(Clone, Debug, Default)]
8300pub struct TransactionBuilder {
8301 pub(crate) raw: RawTransaction,
8302 pub(crate) witnesses: BytesVec,
8303}
8304impl TransactionBuilder {
8305 pub const FIELD_COUNT: usize = 2;
8306 pub fn raw<T>(mut self, v: T) -> Self
8307 where
8308 T: ::core::convert::Into<RawTransaction>,
8309 {
8310 self.raw = v.into();
8311 self
8312 }
8313 pub fn witnesses<T>(mut self, v: T) -> Self
8314 where
8315 T: ::core::convert::Into<BytesVec>,
8316 {
8317 self.witnesses = v.into();
8318 self
8319 }
8320}
8321impl molecule::prelude::Builder for TransactionBuilder {
8322 type Entity = Transaction;
8323 const NAME: &'static str = "TransactionBuilder";
8324 fn expected_length(&self) -> usize {
8325 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
8326 + self.raw.as_slice().len()
8327 + self.witnesses.as_slice().len()
8328 }
8329 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8330 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
8331 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
8332 offsets.push(total_size);
8333 total_size += self.raw.as_slice().len();
8334 offsets.push(total_size);
8335 total_size += self.witnesses.as_slice().len();
8336 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
8337 for offset in offsets.into_iter() {
8338 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
8339 }
8340 writer.write_all(self.raw.as_slice())?;
8341 writer.write_all(self.witnesses.as_slice())?;
8342 Ok(())
8343 }
8344 fn build(&self) -> Self::Entity {
8345 let mut inner = Vec::with_capacity(self.expected_length());
8346 self.write(&mut inner)
8347 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8348 Transaction::new_unchecked(inner.into())
8349 }
8350}
8351#[derive(Clone)]
8352pub struct RawHeader(molecule::bytes::Bytes);
8353impl ::core::fmt::LowerHex for RawHeader {
8354 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8355 use molecule::hex_string;
8356 if f.alternate() {
8357 write!(f, "0x")?;
8358 }
8359 write!(f, "{}", hex_string(self.as_slice()))
8360 }
8361}
8362impl ::core::fmt::Debug for RawHeader {
8363 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8364 write!(f, "{}({:#x})", Self::NAME, self)
8365 }
8366}
8367impl ::core::fmt::Display for RawHeader {
8368 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8369 write!(f, "{} {{ ", Self::NAME)?;
8370 write!(f, "{}: {}", "version", self.version())?;
8371 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
8372 write!(f, ", {}: {}", "timestamp", self.timestamp())?;
8373 write!(f, ", {}: {}", "number", self.number())?;
8374 write!(f, ", {}: {}", "epoch", self.epoch())?;
8375 write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
8376 write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
8377 write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
8378 write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
8379 write!(f, ", {}: {}", "dao", self.dao())?;
8380 write!(f, " }}")
8381 }
8382}
8383impl ::core::default::Default for RawHeader {
8384 fn default() -> Self {
8385 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8386 RawHeader::new_unchecked(v)
8387 }
8388}
8389impl RawHeader {
8390 const DEFAULT_VALUE: [u8; 192] = [
8391 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8392 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8393 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8394 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8395 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8396 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8397 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8398 ];
8399 pub const TOTAL_SIZE: usize = 192;
8400 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
8401 pub const FIELD_COUNT: usize = 10;
8402 pub fn version(&self) -> Uint32 {
8403 Uint32::new_unchecked(self.0.slice(0..4))
8404 }
8405 pub fn compact_target(&self) -> Uint32 {
8406 Uint32::new_unchecked(self.0.slice(4..8))
8407 }
8408 pub fn timestamp(&self) -> Uint64 {
8409 Uint64::new_unchecked(self.0.slice(8..16))
8410 }
8411 pub fn number(&self) -> Uint64 {
8412 Uint64::new_unchecked(self.0.slice(16..24))
8413 }
8414 pub fn epoch(&self) -> Uint64 {
8415 Uint64::new_unchecked(self.0.slice(24..32))
8416 }
8417 pub fn parent_hash(&self) -> Byte32 {
8418 Byte32::new_unchecked(self.0.slice(32..64))
8419 }
8420 pub fn transactions_root(&self) -> Byte32 {
8421 Byte32::new_unchecked(self.0.slice(64..96))
8422 }
8423 pub fn proposals_hash(&self) -> Byte32 {
8424 Byte32::new_unchecked(self.0.slice(96..128))
8425 }
8426 pub fn extra_hash(&self) -> Byte32 {
8427 Byte32::new_unchecked(self.0.slice(128..160))
8428 }
8429 pub fn dao(&self) -> Byte32 {
8430 Byte32::new_unchecked(self.0.slice(160..192))
8431 }
8432 pub fn as_reader<'r>(&'r self) -> RawHeaderReader<'r> {
8433 RawHeaderReader::new_unchecked(self.as_slice())
8434 }
8435}
8436impl molecule::prelude::Entity for RawHeader {
8437 type Builder = RawHeaderBuilder;
8438 const NAME: &'static str = "RawHeader";
8439 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8440 RawHeader(data)
8441 }
8442 fn as_bytes(&self) -> molecule::bytes::Bytes {
8443 self.0.clone()
8444 }
8445 fn as_slice(&self) -> &[u8] {
8446 &self.0[..]
8447 }
8448 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8449 RawHeaderReader::from_slice(slice).map(|reader| reader.to_entity())
8450 }
8451 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8452 RawHeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8453 }
8454 fn new_builder() -> Self::Builder {
8455 ::core::default::Default::default()
8456 }
8457 fn as_builder(self) -> Self::Builder {
8458 Self::new_builder()
8459 .version(self.version())
8460 .compact_target(self.compact_target())
8461 .timestamp(self.timestamp())
8462 .number(self.number())
8463 .epoch(self.epoch())
8464 .parent_hash(self.parent_hash())
8465 .transactions_root(self.transactions_root())
8466 .proposals_hash(self.proposals_hash())
8467 .extra_hash(self.extra_hash())
8468 .dao(self.dao())
8469 }
8470}
8471#[derive(Clone, Copy)]
8472pub struct RawHeaderReader<'r>(&'r [u8]);
8473impl<'r> ::core::fmt::LowerHex for RawHeaderReader<'r> {
8474 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8475 use molecule::hex_string;
8476 if f.alternate() {
8477 write!(f, "0x")?;
8478 }
8479 write!(f, "{}", hex_string(self.as_slice()))
8480 }
8481}
8482impl<'r> ::core::fmt::Debug for RawHeaderReader<'r> {
8483 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8484 write!(f, "{}({:#x})", Self::NAME, self)
8485 }
8486}
8487impl<'r> ::core::fmt::Display for RawHeaderReader<'r> {
8488 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8489 write!(f, "{} {{ ", Self::NAME)?;
8490 write!(f, "{}: {}", "version", self.version())?;
8491 write!(f, ", {}: {}", "compact_target", self.compact_target())?;
8492 write!(f, ", {}: {}", "timestamp", self.timestamp())?;
8493 write!(f, ", {}: {}", "number", self.number())?;
8494 write!(f, ", {}: {}", "epoch", self.epoch())?;
8495 write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
8496 write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
8497 write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
8498 write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
8499 write!(f, ", {}: {}", "dao", self.dao())?;
8500 write!(f, " }}")
8501 }
8502}
8503impl<'r> RawHeaderReader<'r> {
8504 pub const TOTAL_SIZE: usize = 192;
8505 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
8506 pub const FIELD_COUNT: usize = 10;
8507 pub fn version(&self) -> Uint32Reader<'r> {
8508 Uint32Reader::new_unchecked(&self.as_slice()[0..4])
8509 }
8510 pub fn compact_target(&self) -> Uint32Reader<'r> {
8511 Uint32Reader::new_unchecked(&self.as_slice()[4..8])
8512 }
8513 pub fn timestamp(&self) -> Uint64Reader<'r> {
8514 Uint64Reader::new_unchecked(&self.as_slice()[8..16])
8515 }
8516 pub fn number(&self) -> Uint64Reader<'r> {
8517 Uint64Reader::new_unchecked(&self.as_slice()[16..24])
8518 }
8519 pub fn epoch(&self) -> Uint64Reader<'r> {
8520 Uint64Reader::new_unchecked(&self.as_slice()[24..32])
8521 }
8522 pub fn parent_hash(&self) -> Byte32Reader<'r> {
8523 Byte32Reader::new_unchecked(&self.as_slice()[32..64])
8524 }
8525 pub fn transactions_root(&self) -> Byte32Reader<'r> {
8526 Byte32Reader::new_unchecked(&self.as_slice()[64..96])
8527 }
8528 pub fn proposals_hash(&self) -> Byte32Reader<'r> {
8529 Byte32Reader::new_unchecked(&self.as_slice()[96..128])
8530 }
8531 pub fn extra_hash(&self) -> Byte32Reader<'r> {
8532 Byte32Reader::new_unchecked(&self.as_slice()[128..160])
8533 }
8534 pub fn dao(&self) -> Byte32Reader<'r> {
8535 Byte32Reader::new_unchecked(&self.as_slice()[160..192])
8536 }
8537}
8538impl<'r> molecule::prelude::Reader<'r> for RawHeaderReader<'r> {
8539 type Entity = RawHeader;
8540 const NAME: &'static str = "RawHeaderReader";
8541 fn to_entity(&self) -> Self::Entity {
8542 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8543 }
8544 fn new_unchecked(slice: &'r [u8]) -> Self {
8545 RawHeaderReader(slice)
8546 }
8547 fn as_slice(&self) -> &'r [u8] {
8548 self.0
8549 }
8550 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
8551 use molecule::verification_error as ve;
8552 let slice_len = slice.len();
8553 if slice_len != Self::TOTAL_SIZE {
8554 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
8555 }
8556 Ok(())
8557 }
8558}
8559#[derive(Clone, Debug, Default)]
8560pub struct RawHeaderBuilder {
8561 pub(crate) version: Uint32,
8562 pub(crate) compact_target: Uint32,
8563 pub(crate) timestamp: Uint64,
8564 pub(crate) number: Uint64,
8565 pub(crate) epoch: Uint64,
8566 pub(crate) parent_hash: Byte32,
8567 pub(crate) transactions_root: Byte32,
8568 pub(crate) proposals_hash: Byte32,
8569 pub(crate) extra_hash: Byte32,
8570 pub(crate) dao: Byte32,
8571}
8572impl RawHeaderBuilder {
8573 pub const TOTAL_SIZE: usize = 192;
8574 pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
8575 pub const FIELD_COUNT: usize = 10;
8576 pub fn version<T>(mut self, v: T) -> Self
8577 where
8578 T: ::core::convert::Into<Uint32>,
8579 {
8580 self.version = v.into();
8581 self
8582 }
8583 pub fn compact_target<T>(mut self, v: T) -> Self
8584 where
8585 T: ::core::convert::Into<Uint32>,
8586 {
8587 self.compact_target = v.into();
8588 self
8589 }
8590 pub fn timestamp<T>(mut self, v: T) -> Self
8591 where
8592 T: ::core::convert::Into<Uint64>,
8593 {
8594 self.timestamp = v.into();
8595 self
8596 }
8597 pub fn number<T>(mut self, v: T) -> Self
8598 where
8599 T: ::core::convert::Into<Uint64>,
8600 {
8601 self.number = v.into();
8602 self
8603 }
8604 pub fn epoch<T>(mut self, v: T) -> Self
8605 where
8606 T: ::core::convert::Into<Uint64>,
8607 {
8608 self.epoch = v.into();
8609 self
8610 }
8611 pub fn parent_hash<T>(mut self, v: T) -> Self
8612 where
8613 T: ::core::convert::Into<Byte32>,
8614 {
8615 self.parent_hash = v.into();
8616 self
8617 }
8618 pub fn transactions_root<T>(mut self, v: T) -> Self
8619 where
8620 T: ::core::convert::Into<Byte32>,
8621 {
8622 self.transactions_root = v.into();
8623 self
8624 }
8625 pub fn proposals_hash<T>(mut self, v: T) -> Self
8626 where
8627 T: ::core::convert::Into<Byte32>,
8628 {
8629 self.proposals_hash = v.into();
8630 self
8631 }
8632 pub fn extra_hash<T>(mut self, v: T) -> Self
8633 where
8634 T: ::core::convert::Into<Byte32>,
8635 {
8636 self.extra_hash = v.into();
8637 self
8638 }
8639 pub fn dao<T>(mut self, v: T) -> Self
8640 where
8641 T: ::core::convert::Into<Byte32>,
8642 {
8643 self.dao = v.into();
8644 self
8645 }
8646}
8647impl molecule::prelude::Builder for RawHeaderBuilder {
8648 type Entity = RawHeader;
8649 const NAME: &'static str = "RawHeaderBuilder";
8650 fn expected_length(&self) -> usize {
8651 Self::TOTAL_SIZE
8652 }
8653 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8654 writer.write_all(self.version.as_slice())?;
8655 writer.write_all(self.compact_target.as_slice())?;
8656 writer.write_all(self.timestamp.as_slice())?;
8657 writer.write_all(self.number.as_slice())?;
8658 writer.write_all(self.epoch.as_slice())?;
8659 writer.write_all(self.parent_hash.as_slice())?;
8660 writer.write_all(self.transactions_root.as_slice())?;
8661 writer.write_all(self.proposals_hash.as_slice())?;
8662 writer.write_all(self.extra_hash.as_slice())?;
8663 writer.write_all(self.dao.as_slice())?;
8664 Ok(())
8665 }
8666 fn build(&self) -> Self::Entity {
8667 let mut inner = Vec::with_capacity(self.expected_length());
8668 self.write(&mut inner)
8669 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8670 RawHeader::new_unchecked(inner.into())
8671 }
8672}
8673#[derive(Clone)]
8674pub struct Header(molecule::bytes::Bytes);
8675impl ::core::fmt::LowerHex for Header {
8676 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8677 use molecule::hex_string;
8678 if f.alternate() {
8679 write!(f, "0x")?;
8680 }
8681 write!(f, "{}", hex_string(self.as_slice()))
8682 }
8683}
8684impl ::core::fmt::Debug for Header {
8685 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8686 write!(f, "{}({:#x})", Self::NAME, self)
8687 }
8688}
8689impl ::core::fmt::Display for Header {
8690 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8691 write!(f, "{} {{ ", Self::NAME)?;
8692 write!(f, "{}: {}", "raw", self.raw())?;
8693 write!(f, ", {}: {}", "nonce", self.nonce())?;
8694 write!(f, " }}")
8695 }
8696}
8697impl ::core::default::Default for Header {
8698 fn default() -> Self {
8699 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8700 Header::new_unchecked(v)
8701 }
8702}
8703impl Header {
8704 const DEFAULT_VALUE: [u8; 208] = [
8705 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8706 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8707 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8708 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8710 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8711 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8712 ];
8713 pub const TOTAL_SIZE: usize = 208;
8714 pub const FIELD_SIZES: [usize; 2] = [192, 16];
8715 pub const FIELD_COUNT: usize = 2;
8716 pub fn raw(&self) -> RawHeader {
8717 RawHeader::new_unchecked(self.0.slice(0..192))
8718 }
8719 pub fn nonce(&self) -> Uint128 {
8720 Uint128::new_unchecked(self.0.slice(192..208))
8721 }
8722 pub fn as_reader<'r>(&'r self) -> HeaderReader<'r> {
8723 HeaderReader::new_unchecked(self.as_slice())
8724 }
8725}
8726impl molecule::prelude::Entity for Header {
8727 type Builder = HeaderBuilder;
8728 const NAME: &'static str = "Header";
8729 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8730 Header(data)
8731 }
8732 fn as_bytes(&self) -> molecule::bytes::Bytes {
8733 self.0.clone()
8734 }
8735 fn as_slice(&self) -> &[u8] {
8736 &self.0[..]
8737 }
8738 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8739 HeaderReader::from_slice(slice).map(|reader| reader.to_entity())
8740 }
8741 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8742 HeaderReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8743 }
8744 fn new_builder() -> Self::Builder {
8745 ::core::default::Default::default()
8746 }
8747 fn as_builder(self) -> Self::Builder {
8748 Self::new_builder().raw(self.raw()).nonce(self.nonce())
8749 }
8750}
8751#[derive(Clone, Copy)]
8752pub struct HeaderReader<'r>(&'r [u8]);
8753impl<'r> ::core::fmt::LowerHex for HeaderReader<'r> {
8754 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8755 use molecule::hex_string;
8756 if f.alternate() {
8757 write!(f, "0x")?;
8758 }
8759 write!(f, "{}", hex_string(self.as_slice()))
8760 }
8761}
8762impl<'r> ::core::fmt::Debug for HeaderReader<'r> {
8763 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8764 write!(f, "{}({:#x})", Self::NAME, self)
8765 }
8766}
8767impl<'r> ::core::fmt::Display for HeaderReader<'r> {
8768 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8769 write!(f, "{} {{ ", Self::NAME)?;
8770 write!(f, "{}: {}", "raw", self.raw())?;
8771 write!(f, ", {}: {}", "nonce", self.nonce())?;
8772 write!(f, " }}")
8773 }
8774}
8775impl<'r> HeaderReader<'r> {
8776 pub const TOTAL_SIZE: usize = 208;
8777 pub const FIELD_SIZES: [usize; 2] = [192, 16];
8778 pub const FIELD_COUNT: usize = 2;
8779 pub fn raw(&self) -> RawHeaderReader<'r> {
8780 RawHeaderReader::new_unchecked(&self.as_slice()[0..192])
8781 }
8782 pub fn nonce(&self) -> Uint128Reader<'r> {
8783 Uint128Reader::new_unchecked(&self.as_slice()[192..208])
8784 }
8785}
8786impl<'r> molecule::prelude::Reader<'r> for HeaderReader<'r> {
8787 type Entity = Header;
8788 const NAME: &'static str = "HeaderReader";
8789 fn to_entity(&self) -> Self::Entity {
8790 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
8791 }
8792 fn new_unchecked(slice: &'r [u8]) -> Self {
8793 HeaderReader(slice)
8794 }
8795 fn as_slice(&self) -> &'r [u8] {
8796 self.0
8797 }
8798 fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> {
8799 use molecule::verification_error as ve;
8800 let slice_len = slice.len();
8801 if slice_len != Self::TOTAL_SIZE {
8802 return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len);
8803 }
8804 Ok(())
8805 }
8806}
8807#[derive(Clone, Debug, Default)]
8808pub struct HeaderBuilder {
8809 pub(crate) raw: RawHeader,
8810 pub(crate) nonce: Uint128,
8811}
8812impl HeaderBuilder {
8813 pub const TOTAL_SIZE: usize = 208;
8814 pub const FIELD_SIZES: [usize; 2] = [192, 16];
8815 pub const FIELD_COUNT: usize = 2;
8816 pub fn raw<T>(mut self, v: T) -> Self
8817 where
8818 T: ::core::convert::Into<RawHeader>,
8819 {
8820 self.raw = v.into();
8821 self
8822 }
8823 pub fn nonce<T>(mut self, v: T) -> Self
8824 where
8825 T: ::core::convert::Into<Uint128>,
8826 {
8827 self.nonce = v.into();
8828 self
8829 }
8830}
8831impl molecule::prelude::Builder for HeaderBuilder {
8832 type Entity = Header;
8833 const NAME: &'static str = "HeaderBuilder";
8834 fn expected_length(&self) -> usize {
8835 Self::TOTAL_SIZE
8836 }
8837 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
8838 writer.write_all(self.raw.as_slice())?;
8839 writer.write_all(self.nonce.as_slice())?;
8840 Ok(())
8841 }
8842 fn build(&self) -> Self::Entity {
8843 let mut inner = Vec::with_capacity(self.expected_length());
8844 self.write(&mut inner)
8845 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
8846 Header::new_unchecked(inner.into())
8847 }
8848}
8849#[derive(Clone)]
8850pub struct UncleBlock(molecule::bytes::Bytes);
8851impl ::core::fmt::LowerHex for UncleBlock {
8852 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8853 use molecule::hex_string;
8854 if f.alternate() {
8855 write!(f, "0x")?;
8856 }
8857 write!(f, "{}", hex_string(self.as_slice()))
8858 }
8859}
8860impl ::core::fmt::Debug for UncleBlock {
8861 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8862 write!(f, "{}({:#x})", Self::NAME, self)
8863 }
8864}
8865impl ::core::fmt::Display for UncleBlock {
8866 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8867 write!(f, "{} {{ ", Self::NAME)?;
8868 write!(f, "{}: {}", "header", self.header())?;
8869 write!(f, ", {}: {}", "proposals", self.proposals())?;
8870 let extra_count = self.count_extra_fields();
8871 if extra_count != 0 {
8872 write!(f, ", .. ({} fields)", extra_count)?;
8873 }
8874 write!(f, " }}")
8875 }
8876}
8877impl ::core::default::Default for UncleBlock {
8878 fn default() -> Self {
8879 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
8880 UncleBlock::new_unchecked(v)
8881 }
8882}
8883impl UncleBlock {
8884 const DEFAULT_VALUE: [u8; 224] = [
8885 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,
8886 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8887 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8888 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8889 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8890 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8891 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8892 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8893 ];
8894 pub const FIELD_COUNT: usize = 2;
8895 pub fn total_size(&self) -> usize {
8896 molecule::unpack_number(self.as_slice()) as usize
8897 }
8898 pub fn field_count(&self) -> usize {
8899 if self.total_size() == molecule::NUMBER_SIZE {
8900 0
8901 } else {
8902 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8903 }
8904 }
8905 pub fn count_extra_fields(&self) -> usize {
8906 self.field_count() - Self::FIELD_COUNT
8907 }
8908 pub fn has_extra_fields(&self) -> bool {
8909 Self::FIELD_COUNT != self.field_count()
8910 }
8911 pub fn header(&self) -> Header {
8912 let slice = self.as_slice();
8913 let start = molecule::unpack_number(&slice[4..]) as usize;
8914 let end = molecule::unpack_number(&slice[8..]) as usize;
8915 Header::new_unchecked(self.0.slice(start..end))
8916 }
8917 pub fn proposals(&self) -> ProposalShortIdVec {
8918 let slice = self.as_slice();
8919 let start = molecule::unpack_number(&slice[8..]) as usize;
8920 if self.has_extra_fields() {
8921 let end = molecule::unpack_number(&slice[12..]) as usize;
8922 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
8923 } else {
8924 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
8925 }
8926 }
8927 pub fn as_reader<'r>(&'r self) -> UncleBlockReader<'r> {
8928 UncleBlockReader::new_unchecked(self.as_slice())
8929 }
8930}
8931impl molecule::prelude::Entity for UncleBlock {
8932 type Builder = UncleBlockBuilder;
8933 const NAME: &'static str = "UncleBlock";
8934 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
8935 UncleBlock(data)
8936 }
8937 fn as_bytes(&self) -> molecule::bytes::Bytes {
8938 self.0.clone()
8939 }
8940 fn as_slice(&self) -> &[u8] {
8941 &self.0[..]
8942 }
8943 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8944 UncleBlockReader::from_slice(slice).map(|reader| reader.to_entity())
8945 }
8946 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
8947 UncleBlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
8948 }
8949 fn new_builder() -> Self::Builder {
8950 ::core::default::Default::default()
8951 }
8952 fn as_builder(self) -> Self::Builder {
8953 Self::new_builder()
8954 .header(self.header())
8955 .proposals(self.proposals())
8956 }
8957}
8958#[derive(Clone, Copy)]
8959pub struct UncleBlockReader<'r>(&'r [u8]);
8960impl<'r> ::core::fmt::LowerHex for UncleBlockReader<'r> {
8961 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8962 use molecule::hex_string;
8963 if f.alternate() {
8964 write!(f, "0x")?;
8965 }
8966 write!(f, "{}", hex_string(self.as_slice()))
8967 }
8968}
8969impl<'r> ::core::fmt::Debug for UncleBlockReader<'r> {
8970 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8971 write!(f, "{}({:#x})", Self::NAME, self)
8972 }
8973}
8974impl<'r> ::core::fmt::Display for UncleBlockReader<'r> {
8975 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
8976 write!(f, "{} {{ ", Self::NAME)?;
8977 write!(f, "{}: {}", "header", self.header())?;
8978 write!(f, ", {}: {}", "proposals", self.proposals())?;
8979 let extra_count = self.count_extra_fields();
8980 if extra_count != 0 {
8981 write!(f, ", .. ({} fields)", extra_count)?;
8982 }
8983 write!(f, " }}")
8984 }
8985}
8986impl<'r> UncleBlockReader<'r> {
8987 pub const FIELD_COUNT: usize = 2;
8988 pub fn total_size(&self) -> usize {
8989 molecule::unpack_number(self.as_slice()) as usize
8990 }
8991 pub fn field_count(&self) -> usize {
8992 if self.total_size() == molecule::NUMBER_SIZE {
8993 0
8994 } else {
8995 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
8996 }
8997 }
8998 pub fn count_extra_fields(&self) -> usize {
8999 self.field_count() - Self::FIELD_COUNT
9000 }
9001 pub fn has_extra_fields(&self) -> bool {
9002 Self::FIELD_COUNT != self.field_count()
9003 }
9004 pub fn header(&self) -> HeaderReader<'r> {
9005 let slice = self.as_slice();
9006 let start = molecule::unpack_number(&slice[4..]) as usize;
9007 let end = molecule::unpack_number(&slice[8..]) as usize;
9008 HeaderReader::new_unchecked(&self.as_slice()[start..end])
9009 }
9010 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
9011 let slice = self.as_slice();
9012 let start = molecule::unpack_number(&slice[8..]) as usize;
9013 if self.has_extra_fields() {
9014 let end = molecule::unpack_number(&slice[12..]) as usize;
9015 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
9016 } else {
9017 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
9018 }
9019 }
9020}
9021impl<'r> molecule::prelude::Reader<'r> for UncleBlockReader<'r> {
9022 type Entity = UncleBlock;
9023 const NAME: &'static str = "UncleBlockReader";
9024 fn to_entity(&self) -> Self::Entity {
9025 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9026 }
9027 fn new_unchecked(slice: &'r [u8]) -> Self {
9028 UncleBlockReader(slice)
9029 }
9030 fn as_slice(&self) -> &'r [u8] {
9031 self.0
9032 }
9033 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9034 use molecule::verification_error as ve;
9035 let slice_len = slice.len();
9036 if slice_len < molecule::NUMBER_SIZE {
9037 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9038 }
9039 let total_size = molecule::unpack_number(slice) as usize;
9040 if slice_len != total_size {
9041 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9042 }
9043 if slice_len < molecule::NUMBER_SIZE * 2 {
9044 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9045 }
9046 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9047 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9048 return ve!(Self, OffsetsNotMatch);
9049 }
9050 if slice_len < offset_first {
9051 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9052 }
9053 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9054 if field_count < Self::FIELD_COUNT {
9055 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9056 } else if !compatible && field_count > Self::FIELD_COUNT {
9057 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9058 };
9059 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9060 .chunks_exact(molecule::NUMBER_SIZE)
9061 .map(|x| molecule::unpack_number(x) as usize)
9062 .collect();
9063 offsets.push(total_size);
9064 if offsets.windows(2).any(|i| i[0] > i[1]) {
9065 return ve!(Self, OffsetsNotMatch);
9066 }
9067 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9068 ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9069 Ok(())
9070 }
9071}
9072#[derive(Clone, Debug, Default)]
9073pub struct UncleBlockBuilder {
9074 pub(crate) header: Header,
9075 pub(crate) proposals: ProposalShortIdVec,
9076}
9077impl UncleBlockBuilder {
9078 pub const FIELD_COUNT: usize = 2;
9079 pub fn header<T>(mut self, v: T) -> Self
9080 where
9081 T: ::core::convert::Into<Header>,
9082 {
9083 self.header = v.into();
9084 self
9085 }
9086 pub fn proposals<T>(mut self, v: T) -> Self
9087 where
9088 T: ::core::convert::Into<ProposalShortIdVec>,
9089 {
9090 self.proposals = v.into();
9091 self
9092 }
9093}
9094impl molecule::prelude::Builder for UncleBlockBuilder {
9095 type Entity = UncleBlock;
9096 const NAME: &'static str = "UncleBlockBuilder";
9097 fn expected_length(&self) -> usize {
9098 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9099 + self.header.as_slice().len()
9100 + self.proposals.as_slice().len()
9101 }
9102 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9103 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9104 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9105 offsets.push(total_size);
9106 total_size += self.header.as_slice().len();
9107 offsets.push(total_size);
9108 total_size += self.proposals.as_slice().len();
9109 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9110 for offset in offsets.into_iter() {
9111 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9112 }
9113 writer.write_all(self.header.as_slice())?;
9114 writer.write_all(self.proposals.as_slice())?;
9115 Ok(())
9116 }
9117 fn build(&self) -> Self::Entity {
9118 let mut inner = Vec::with_capacity(self.expected_length());
9119 self.write(&mut inner)
9120 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9121 UncleBlock::new_unchecked(inner.into())
9122 }
9123}
9124#[derive(Clone)]
9125pub struct Block(molecule::bytes::Bytes);
9126impl ::core::fmt::LowerHex for Block {
9127 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9128 use molecule::hex_string;
9129 if f.alternate() {
9130 write!(f, "0x")?;
9131 }
9132 write!(f, "{}", hex_string(self.as_slice()))
9133 }
9134}
9135impl ::core::fmt::Debug for Block {
9136 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9137 write!(f, "{}({:#x})", Self::NAME, self)
9138 }
9139}
9140impl ::core::fmt::Display for Block {
9141 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9142 write!(f, "{} {{ ", Self::NAME)?;
9143 write!(f, "{}: {}", "header", self.header())?;
9144 write!(f, ", {}: {}", "uncles", self.uncles())?;
9145 write!(f, ", {}: {}", "transactions", self.transactions())?;
9146 write!(f, ", {}: {}", "proposals", self.proposals())?;
9147 let extra_count = self.count_extra_fields();
9148 if extra_count != 0 {
9149 write!(f, ", .. ({} fields)", extra_count)?;
9150 }
9151 write!(f, " }}")
9152 }
9153}
9154impl ::core::default::Default for Block {
9155 fn default() -> Self {
9156 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9157 Block::new_unchecked(v)
9158 }
9159}
9160impl Block {
9161 const DEFAULT_VALUE: [u8; 240] = [
9162 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,
9163 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9166 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9167 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9168 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9169 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,
9170 0, 0, 0,
9171 ];
9172 pub const FIELD_COUNT: usize = 4;
9173 pub fn total_size(&self) -> usize {
9174 molecule::unpack_number(self.as_slice()) as usize
9175 }
9176 pub fn field_count(&self) -> usize {
9177 if self.total_size() == molecule::NUMBER_SIZE {
9178 0
9179 } else {
9180 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9181 }
9182 }
9183 pub fn count_extra_fields(&self) -> usize {
9184 self.field_count() - Self::FIELD_COUNT
9185 }
9186 pub fn has_extra_fields(&self) -> bool {
9187 Self::FIELD_COUNT != self.field_count()
9188 }
9189 pub fn header(&self) -> Header {
9190 let slice = self.as_slice();
9191 let start = molecule::unpack_number(&slice[4..]) as usize;
9192 let end = molecule::unpack_number(&slice[8..]) as usize;
9193 Header::new_unchecked(self.0.slice(start..end))
9194 }
9195 pub fn uncles(&self) -> UncleBlockVec {
9196 let slice = self.as_slice();
9197 let start = molecule::unpack_number(&slice[8..]) as usize;
9198 let end = molecule::unpack_number(&slice[12..]) as usize;
9199 UncleBlockVec::new_unchecked(self.0.slice(start..end))
9200 }
9201 pub fn transactions(&self) -> TransactionVec {
9202 let slice = self.as_slice();
9203 let start = molecule::unpack_number(&slice[12..]) as usize;
9204 let end = molecule::unpack_number(&slice[16..]) as usize;
9205 TransactionVec::new_unchecked(self.0.slice(start..end))
9206 }
9207 pub fn proposals(&self) -> ProposalShortIdVec {
9208 let slice = self.as_slice();
9209 let start = molecule::unpack_number(&slice[16..]) as usize;
9210 if self.has_extra_fields() {
9211 let end = molecule::unpack_number(&slice[20..]) as usize;
9212 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
9213 } else {
9214 ProposalShortIdVec::new_unchecked(self.0.slice(start..))
9215 }
9216 }
9217 pub fn as_reader<'r>(&'r self) -> BlockReader<'r> {
9218 BlockReader::new_unchecked(self.as_slice())
9219 }
9220}
9221impl molecule::prelude::Entity for Block {
9222 type Builder = BlockBuilder;
9223 const NAME: &'static str = "Block";
9224 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9225 Block(data)
9226 }
9227 fn as_bytes(&self) -> molecule::bytes::Bytes {
9228 self.0.clone()
9229 }
9230 fn as_slice(&self) -> &[u8] {
9231 &self.0[..]
9232 }
9233 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9234 BlockReader::from_slice(slice).map(|reader| reader.to_entity())
9235 }
9236 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9237 BlockReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9238 }
9239 fn new_builder() -> Self::Builder {
9240 ::core::default::Default::default()
9241 }
9242 fn as_builder(self) -> Self::Builder {
9243 Self::new_builder()
9244 .header(self.header())
9245 .uncles(self.uncles())
9246 .transactions(self.transactions())
9247 .proposals(self.proposals())
9248 }
9249}
9250#[derive(Clone, Copy)]
9251pub struct BlockReader<'r>(&'r [u8]);
9252impl<'r> ::core::fmt::LowerHex for BlockReader<'r> {
9253 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9254 use molecule::hex_string;
9255 if f.alternate() {
9256 write!(f, "0x")?;
9257 }
9258 write!(f, "{}", hex_string(self.as_slice()))
9259 }
9260}
9261impl<'r> ::core::fmt::Debug for BlockReader<'r> {
9262 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9263 write!(f, "{}({:#x})", Self::NAME, self)
9264 }
9265}
9266impl<'r> ::core::fmt::Display for BlockReader<'r> {
9267 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9268 write!(f, "{} {{ ", Self::NAME)?;
9269 write!(f, "{}: {}", "header", self.header())?;
9270 write!(f, ", {}: {}", "uncles", self.uncles())?;
9271 write!(f, ", {}: {}", "transactions", self.transactions())?;
9272 write!(f, ", {}: {}", "proposals", self.proposals())?;
9273 let extra_count = self.count_extra_fields();
9274 if extra_count != 0 {
9275 write!(f, ", .. ({} fields)", extra_count)?;
9276 }
9277 write!(f, " }}")
9278 }
9279}
9280impl<'r> BlockReader<'r> {
9281 pub const FIELD_COUNT: usize = 4;
9282 pub fn total_size(&self) -> usize {
9283 molecule::unpack_number(self.as_slice()) as usize
9284 }
9285 pub fn field_count(&self) -> usize {
9286 if self.total_size() == molecule::NUMBER_SIZE {
9287 0
9288 } else {
9289 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9290 }
9291 }
9292 pub fn count_extra_fields(&self) -> usize {
9293 self.field_count() - Self::FIELD_COUNT
9294 }
9295 pub fn has_extra_fields(&self) -> bool {
9296 Self::FIELD_COUNT != self.field_count()
9297 }
9298 pub fn header(&self) -> HeaderReader<'r> {
9299 let slice = self.as_slice();
9300 let start = molecule::unpack_number(&slice[4..]) as usize;
9301 let end = molecule::unpack_number(&slice[8..]) as usize;
9302 HeaderReader::new_unchecked(&self.as_slice()[start..end])
9303 }
9304 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
9305 let slice = self.as_slice();
9306 let start = molecule::unpack_number(&slice[8..]) as usize;
9307 let end = molecule::unpack_number(&slice[12..]) as usize;
9308 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
9309 }
9310 pub fn transactions(&self) -> TransactionVecReader<'r> {
9311 let slice = self.as_slice();
9312 let start = molecule::unpack_number(&slice[12..]) as usize;
9313 let end = molecule::unpack_number(&slice[16..]) as usize;
9314 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
9315 }
9316 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
9317 let slice = self.as_slice();
9318 let start = molecule::unpack_number(&slice[16..]) as usize;
9319 if self.has_extra_fields() {
9320 let end = molecule::unpack_number(&slice[20..]) as usize;
9321 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
9322 } else {
9323 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
9324 }
9325 }
9326}
9327impl<'r> molecule::prelude::Reader<'r> for BlockReader<'r> {
9328 type Entity = Block;
9329 const NAME: &'static str = "BlockReader";
9330 fn to_entity(&self) -> Self::Entity {
9331 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9332 }
9333 fn new_unchecked(slice: &'r [u8]) -> Self {
9334 BlockReader(slice)
9335 }
9336 fn as_slice(&self) -> &'r [u8] {
9337 self.0
9338 }
9339 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9340 use molecule::verification_error as ve;
9341 let slice_len = slice.len();
9342 if slice_len < molecule::NUMBER_SIZE {
9343 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9344 }
9345 let total_size = molecule::unpack_number(slice) as usize;
9346 if slice_len != total_size {
9347 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9348 }
9349 if slice_len < molecule::NUMBER_SIZE * 2 {
9350 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9351 }
9352 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9353 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9354 return ve!(Self, OffsetsNotMatch);
9355 }
9356 if slice_len < offset_first {
9357 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9358 }
9359 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9360 if field_count < Self::FIELD_COUNT {
9361 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9362 } else if !compatible && field_count > Self::FIELD_COUNT {
9363 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9364 };
9365 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9366 .chunks_exact(molecule::NUMBER_SIZE)
9367 .map(|x| molecule::unpack_number(x) as usize)
9368 .collect();
9369 offsets.push(total_size);
9370 if offsets.windows(2).any(|i| i[0] > i[1]) {
9371 return ve!(Self, OffsetsNotMatch);
9372 }
9373 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9374 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9375 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
9376 ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
9377 Ok(())
9378 }
9379}
9380#[derive(Clone, Debug, Default)]
9381pub struct BlockBuilder {
9382 pub(crate) header: Header,
9383 pub(crate) uncles: UncleBlockVec,
9384 pub(crate) transactions: TransactionVec,
9385 pub(crate) proposals: ProposalShortIdVec,
9386}
9387impl BlockBuilder {
9388 pub const FIELD_COUNT: usize = 4;
9389 pub fn header<T>(mut self, v: T) -> Self
9390 where
9391 T: ::core::convert::Into<Header>,
9392 {
9393 self.header = v.into();
9394 self
9395 }
9396 pub fn uncles<T>(mut self, v: T) -> Self
9397 where
9398 T: ::core::convert::Into<UncleBlockVec>,
9399 {
9400 self.uncles = v.into();
9401 self
9402 }
9403 pub fn transactions<T>(mut self, v: T) -> Self
9404 where
9405 T: ::core::convert::Into<TransactionVec>,
9406 {
9407 self.transactions = v.into();
9408 self
9409 }
9410 pub fn proposals<T>(mut self, v: T) -> Self
9411 where
9412 T: ::core::convert::Into<ProposalShortIdVec>,
9413 {
9414 self.proposals = v.into();
9415 self
9416 }
9417}
9418impl molecule::prelude::Builder for BlockBuilder {
9419 type Entity = Block;
9420 const NAME: &'static str = "BlockBuilder";
9421 fn expected_length(&self) -> usize {
9422 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9423 + self.header.as_slice().len()
9424 + self.uncles.as_slice().len()
9425 + self.transactions.as_slice().len()
9426 + self.proposals.as_slice().len()
9427 }
9428 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9429 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9430 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9431 offsets.push(total_size);
9432 total_size += self.header.as_slice().len();
9433 offsets.push(total_size);
9434 total_size += self.uncles.as_slice().len();
9435 offsets.push(total_size);
9436 total_size += self.transactions.as_slice().len();
9437 offsets.push(total_size);
9438 total_size += self.proposals.as_slice().len();
9439 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9440 for offset in offsets.into_iter() {
9441 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9442 }
9443 writer.write_all(self.header.as_slice())?;
9444 writer.write_all(self.uncles.as_slice())?;
9445 writer.write_all(self.transactions.as_slice())?;
9446 writer.write_all(self.proposals.as_slice())?;
9447 Ok(())
9448 }
9449 fn build(&self) -> Self::Entity {
9450 let mut inner = Vec::with_capacity(self.expected_length());
9451 self.write(&mut inner)
9452 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9453 Block::new_unchecked(inner.into())
9454 }
9455}
9456#[derive(Clone)]
9457pub struct BlockV1(molecule::bytes::Bytes);
9458impl ::core::fmt::LowerHex for BlockV1 {
9459 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9460 use molecule::hex_string;
9461 if f.alternate() {
9462 write!(f, "0x")?;
9463 }
9464 write!(f, "{}", hex_string(self.as_slice()))
9465 }
9466}
9467impl ::core::fmt::Debug for BlockV1 {
9468 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9469 write!(f, "{}({:#x})", Self::NAME, self)
9470 }
9471}
9472impl ::core::fmt::Display for BlockV1 {
9473 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9474 write!(f, "{} {{ ", Self::NAME)?;
9475 write!(f, "{}: {}", "header", self.header())?;
9476 write!(f, ", {}: {}", "uncles", self.uncles())?;
9477 write!(f, ", {}: {}", "transactions", self.transactions())?;
9478 write!(f, ", {}: {}", "proposals", self.proposals())?;
9479 write!(f, ", {}: {}", "extension", self.extension())?;
9480 let extra_count = self.count_extra_fields();
9481 if extra_count != 0 {
9482 write!(f, ", .. ({} fields)", extra_count)?;
9483 }
9484 write!(f, " }}")
9485 }
9486}
9487impl ::core::default::Default for BlockV1 {
9488 fn default() -> Self {
9489 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9490 BlockV1::new_unchecked(v)
9491 }
9492}
9493impl BlockV1 {
9494 const DEFAULT_VALUE: [u8; 248] = [
9495 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,
9496 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9497 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9498 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9499 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9500 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9501 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9502 0, 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,
9503 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9504 ];
9505 pub const FIELD_COUNT: usize = 5;
9506 pub fn total_size(&self) -> usize {
9507 molecule::unpack_number(self.as_slice()) as usize
9508 }
9509 pub fn field_count(&self) -> usize {
9510 if self.total_size() == molecule::NUMBER_SIZE {
9511 0
9512 } else {
9513 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9514 }
9515 }
9516 pub fn count_extra_fields(&self) -> usize {
9517 self.field_count() - Self::FIELD_COUNT
9518 }
9519 pub fn has_extra_fields(&self) -> bool {
9520 Self::FIELD_COUNT != self.field_count()
9521 }
9522 pub fn header(&self) -> Header {
9523 let slice = self.as_slice();
9524 let start = molecule::unpack_number(&slice[4..]) as usize;
9525 let end = molecule::unpack_number(&slice[8..]) as usize;
9526 Header::new_unchecked(self.0.slice(start..end))
9527 }
9528 pub fn uncles(&self) -> UncleBlockVec {
9529 let slice = self.as_slice();
9530 let start = molecule::unpack_number(&slice[8..]) as usize;
9531 let end = molecule::unpack_number(&slice[12..]) as usize;
9532 UncleBlockVec::new_unchecked(self.0.slice(start..end))
9533 }
9534 pub fn transactions(&self) -> TransactionVec {
9535 let slice = self.as_slice();
9536 let start = molecule::unpack_number(&slice[12..]) as usize;
9537 let end = molecule::unpack_number(&slice[16..]) as usize;
9538 TransactionVec::new_unchecked(self.0.slice(start..end))
9539 }
9540 pub fn proposals(&self) -> ProposalShortIdVec {
9541 let slice = self.as_slice();
9542 let start = molecule::unpack_number(&slice[16..]) as usize;
9543 let end = molecule::unpack_number(&slice[20..]) as usize;
9544 ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
9545 }
9546 pub fn extension(&self) -> Bytes {
9547 let slice = self.as_slice();
9548 let start = molecule::unpack_number(&slice[20..]) as usize;
9549 if self.has_extra_fields() {
9550 let end = molecule::unpack_number(&slice[24..]) as usize;
9551 Bytes::new_unchecked(self.0.slice(start..end))
9552 } else {
9553 Bytes::new_unchecked(self.0.slice(start..))
9554 }
9555 }
9556 pub fn as_reader<'r>(&'r self) -> BlockV1Reader<'r> {
9557 BlockV1Reader::new_unchecked(self.as_slice())
9558 }
9559}
9560impl molecule::prelude::Entity for BlockV1 {
9561 type Builder = BlockV1Builder;
9562 const NAME: &'static str = "BlockV1";
9563 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9564 BlockV1(data)
9565 }
9566 fn as_bytes(&self) -> molecule::bytes::Bytes {
9567 self.0.clone()
9568 }
9569 fn as_slice(&self) -> &[u8] {
9570 &self.0[..]
9571 }
9572 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9573 BlockV1Reader::from_slice(slice).map(|reader| reader.to_entity())
9574 }
9575 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9576 BlockV1Reader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9577 }
9578 fn new_builder() -> Self::Builder {
9579 ::core::default::Default::default()
9580 }
9581 fn as_builder(self) -> Self::Builder {
9582 Self::new_builder()
9583 .header(self.header())
9584 .uncles(self.uncles())
9585 .transactions(self.transactions())
9586 .proposals(self.proposals())
9587 .extension(self.extension())
9588 }
9589}
9590#[derive(Clone, Copy)]
9591pub struct BlockV1Reader<'r>(&'r [u8]);
9592impl<'r> ::core::fmt::LowerHex for BlockV1Reader<'r> {
9593 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9594 use molecule::hex_string;
9595 if f.alternate() {
9596 write!(f, "0x")?;
9597 }
9598 write!(f, "{}", hex_string(self.as_slice()))
9599 }
9600}
9601impl<'r> ::core::fmt::Debug for BlockV1Reader<'r> {
9602 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9603 write!(f, "{}({:#x})", Self::NAME, self)
9604 }
9605}
9606impl<'r> ::core::fmt::Display for BlockV1Reader<'r> {
9607 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9608 write!(f, "{} {{ ", Self::NAME)?;
9609 write!(f, "{}: {}", "header", self.header())?;
9610 write!(f, ", {}: {}", "uncles", self.uncles())?;
9611 write!(f, ", {}: {}", "transactions", self.transactions())?;
9612 write!(f, ", {}: {}", "proposals", self.proposals())?;
9613 write!(f, ", {}: {}", "extension", self.extension())?;
9614 let extra_count = self.count_extra_fields();
9615 if extra_count != 0 {
9616 write!(f, ", .. ({} fields)", extra_count)?;
9617 }
9618 write!(f, " }}")
9619 }
9620}
9621impl<'r> BlockV1Reader<'r> {
9622 pub const FIELD_COUNT: usize = 5;
9623 pub fn total_size(&self) -> usize {
9624 molecule::unpack_number(self.as_slice()) as usize
9625 }
9626 pub fn field_count(&self) -> usize {
9627 if self.total_size() == molecule::NUMBER_SIZE {
9628 0
9629 } else {
9630 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9631 }
9632 }
9633 pub fn count_extra_fields(&self) -> usize {
9634 self.field_count() - Self::FIELD_COUNT
9635 }
9636 pub fn has_extra_fields(&self) -> bool {
9637 Self::FIELD_COUNT != self.field_count()
9638 }
9639 pub fn header(&self) -> HeaderReader<'r> {
9640 let slice = self.as_slice();
9641 let start = molecule::unpack_number(&slice[4..]) as usize;
9642 let end = molecule::unpack_number(&slice[8..]) as usize;
9643 HeaderReader::new_unchecked(&self.as_slice()[start..end])
9644 }
9645 pub fn uncles(&self) -> UncleBlockVecReader<'r> {
9646 let slice = self.as_slice();
9647 let start = molecule::unpack_number(&slice[8..]) as usize;
9648 let end = molecule::unpack_number(&slice[12..]) as usize;
9649 UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
9650 }
9651 pub fn transactions(&self) -> TransactionVecReader<'r> {
9652 let slice = self.as_slice();
9653 let start = molecule::unpack_number(&slice[12..]) as usize;
9654 let end = molecule::unpack_number(&slice[16..]) as usize;
9655 TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
9656 }
9657 pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
9658 let slice = self.as_slice();
9659 let start = molecule::unpack_number(&slice[16..]) as usize;
9660 let end = molecule::unpack_number(&slice[20..]) as usize;
9661 ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
9662 }
9663 pub fn extension(&self) -> BytesReader<'r> {
9664 let slice = self.as_slice();
9665 let start = molecule::unpack_number(&slice[20..]) as usize;
9666 if self.has_extra_fields() {
9667 let end = molecule::unpack_number(&slice[24..]) as usize;
9668 BytesReader::new_unchecked(&self.as_slice()[start..end])
9669 } else {
9670 BytesReader::new_unchecked(&self.as_slice()[start..])
9671 }
9672 }
9673}
9674impl<'r> molecule::prelude::Reader<'r> for BlockV1Reader<'r> {
9675 type Entity = BlockV1;
9676 const NAME: &'static str = "BlockV1Reader";
9677 fn to_entity(&self) -> Self::Entity {
9678 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9679 }
9680 fn new_unchecked(slice: &'r [u8]) -> Self {
9681 BlockV1Reader(slice)
9682 }
9683 fn as_slice(&self) -> &'r [u8] {
9684 self.0
9685 }
9686 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9687 use molecule::verification_error as ve;
9688 let slice_len = slice.len();
9689 if slice_len < molecule::NUMBER_SIZE {
9690 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
9691 }
9692 let total_size = molecule::unpack_number(slice) as usize;
9693 if slice_len != total_size {
9694 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
9695 }
9696 if slice_len < molecule::NUMBER_SIZE * 2 {
9697 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
9698 }
9699 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
9700 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
9701 return ve!(Self, OffsetsNotMatch);
9702 }
9703 if slice_len < offset_first {
9704 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
9705 }
9706 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
9707 if field_count < Self::FIELD_COUNT {
9708 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9709 } else if !compatible && field_count > Self::FIELD_COUNT {
9710 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
9711 };
9712 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
9713 .chunks_exact(molecule::NUMBER_SIZE)
9714 .map(|x| molecule::unpack_number(x) as usize)
9715 .collect();
9716 offsets.push(total_size);
9717 if offsets.windows(2).any(|i| i[0] > i[1]) {
9718 return ve!(Self, OffsetsNotMatch);
9719 }
9720 HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
9721 UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
9722 TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
9723 ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
9724 BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
9725 Ok(())
9726 }
9727}
9728#[derive(Clone, Debug, Default)]
9729pub struct BlockV1Builder {
9730 pub(crate) header: Header,
9731 pub(crate) uncles: UncleBlockVec,
9732 pub(crate) transactions: TransactionVec,
9733 pub(crate) proposals: ProposalShortIdVec,
9734 pub(crate) extension: Bytes,
9735}
9736impl BlockV1Builder {
9737 pub const FIELD_COUNT: usize = 5;
9738 pub fn header<T>(mut self, v: T) -> Self
9739 where
9740 T: ::core::convert::Into<Header>,
9741 {
9742 self.header = v.into();
9743 self
9744 }
9745 pub fn uncles<T>(mut self, v: T) -> Self
9746 where
9747 T: ::core::convert::Into<UncleBlockVec>,
9748 {
9749 self.uncles = v.into();
9750 self
9751 }
9752 pub fn transactions<T>(mut self, v: T) -> Self
9753 where
9754 T: ::core::convert::Into<TransactionVec>,
9755 {
9756 self.transactions = v.into();
9757 self
9758 }
9759 pub fn proposals<T>(mut self, v: T) -> Self
9760 where
9761 T: ::core::convert::Into<ProposalShortIdVec>,
9762 {
9763 self.proposals = v.into();
9764 self
9765 }
9766 pub fn extension<T>(mut self, v: T) -> Self
9767 where
9768 T: ::core::convert::Into<Bytes>,
9769 {
9770 self.extension = v.into();
9771 self
9772 }
9773}
9774impl molecule::prelude::Builder for BlockV1Builder {
9775 type Entity = BlockV1;
9776 const NAME: &'static str = "BlockV1Builder";
9777 fn expected_length(&self) -> usize {
9778 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
9779 + self.header.as_slice().len()
9780 + self.uncles.as_slice().len()
9781 + self.transactions.as_slice().len()
9782 + self.proposals.as_slice().len()
9783 + self.extension.as_slice().len()
9784 }
9785 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
9786 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
9787 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
9788 offsets.push(total_size);
9789 total_size += self.header.as_slice().len();
9790 offsets.push(total_size);
9791 total_size += self.uncles.as_slice().len();
9792 offsets.push(total_size);
9793 total_size += self.transactions.as_slice().len();
9794 offsets.push(total_size);
9795 total_size += self.proposals.as_slice().len();
9796 offsets.push(total_size);
9797 total_size += self.extension.as_slice().len();
9798 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
9799 for offset in offsets.into_iter() {
9800 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
9801 }
9802 writer.write_all(self.header.as_slice())?;
9803 writer.write_all(self.uncles.as_slice())?;
9804 writer.write_all(self.transactions.as_slice())?;
9805 writer.write_all(self.proposals.as_slice())?;
9806 writer.write_all(self.extension.as_slice())?;
9807 Ok(())
9808 }
9809 fn build(&self) -> Self::Entity {
9810 let mut inner = Vec::with_capacity(self.expected_length());
9811 self.write(&mut inner)
9812 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
9813 BlockV1::new_unchecked(inner.into())
9814 }
9815}
9816#[derive(Clone)]
9817pub struct CellbaseWitness(molecule::bytes::Bytes);
9818impl ::core::fmt::LowerHex for CellbaseWitness {
9819 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9820 use molecule::hex_string;
9821 if f.alternate() {
9822 write!(f, "0x")?;
9823 }
9824 write!(f, "{}", hex_string(self.as_slice()))
9825 }
9826}
9827impl ::core::fmt::Debug for CellbaseWitness {
9828 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9829 write!(f, "{}({:#x})", Self::NAME, self)
9830 }
9831}
9832impl ::core::fmt::Display for CellbaseWitness {
9833 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9834 write!(f, "{} {{ ", Self::NAME)?;
9835 write!(f, "{}: {}", "lock", self.lock())?;
9836 write!(f, ", {}: {}", "message", self.message())?;
9837 let extra_count = self.count_extra_fields();
9838 if extra_count != 0 {
9839 write!(f, ", .. ({} fields)", extra_count)?;
9840 }
9841 write!(f, " }}")
9842 }
9843}
9844impl ::core::default::Default for CellbaseWitness {
9845 fn default() -> Self {
9846 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
9847 CellbaseWitness::new_unchecked(v)
9848 }
9849}
9850impl CellbaseWitness {
9851 const DEFAULT_VALUE: [u8; 69] = [
9852 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,
9853 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9854 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9855 ];
9856 pub const FIELD_COUNT: usize = 2;
9857 pub fn total_size(&self) -> usize {
9858 molecule::unpack_number(self.as_slice()) as usize
9859 }
9860 pub fn field_count(&self) -> usize {
9861 if self.total_size() == molecule::NUMBER_SIZE {
9862 0
9863 } else {
9864 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9865 }
9866 }
9867 pub fn count_extra_fields(&self) -> usize {
9868 self.field_count() - Self::FIELD_COUNT
9869 }
9870 pub fn has_extra_fields(&self) -> bool {
9871 Self::FIELD_COUNT != self.field_count()
9872 }
9873 pub fn lock(&self) -> Script {
9874 let slice = self.as_slice();
9875 let start = molecule::unpack_number(&slice[4..]) as usize;
9876 let end = molecule::unpack_number(&slice[8..]) as usize;
9877 Script::new_unchecked(self.0.slice(start..end))
9878 }
9879 pub fn message(&self) -> Bytes {
9880 let slice = self.as_slice();
9881 let start = molecule::unpack_number(&slice[8..]) as usize;
9882 if self.has_extra_fields() {
9883 let end = molecule::unpack_number(&slice[12..]) as usize;
9884 Bytes::new_unchecked(self.0.slice(start..end))
9885 } else {
9886 Bytes::new_unchecked(self.0.slice(start..))
9887 }
9888 }
9889 pub fn as_reader<'r>(&'r self) -> CellbaseWitnessReader<'r> {
9890 CellbaseWitnessReader::new_unchecked(self.as_slice())
9891 }
9892}
9893impl molecule::prelude::Entity for CellbaseWitness {
9894 type Builder = CellbaseWitnessBuilder;
9895 const NAME: &'static str = "CellbaseWitness";
9896 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
9897 CellbaseWitness(data)
9898 }
9899 fn as_bytes(&self) -> molecule::bytes::Bytes {
9900 self.0.clone()
9901 }
9902 fn as_slice(&self) -> &[u8] {
9903 &self.0[..]
9904 }
9905 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9906 CellbaseWitnessReader::from_slice(slice).map(|reader| reader.to_entity())
9907 }
9908 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
9909 CellbaseWitnessReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
9910 }
9911 fn new_builder() -> Self::Builder {
9912 ::core::default::Default::default()
9913 }
9914 fn as_builder(self) -> Self::Builder {
9915 Self::new_builder()
9916 .lock(self.lock())
9917 .message(self.message())
9918 }
9919}
9920#[derive(Clone, Copy)]
9921pub struct CellbaseWitnessReader<'r>(&'r [u8]);
9922impl<'r> ::core::fmt::LowerHex for CellbaseWitnessReader<'r> {
9923 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9924 use molecule::hex_string;
9925 if f.alternate() {
9926 write!(f, "0x")?;
9927 }
9928 write!(f, "{}", hex_string(self.as_slice()))
9929 }
9930}
9931impl<'r> ::core::fmt::Debug for CellbaseWitnessReader<'r> {
9932 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9933 write!(f, "{}({:#x})", Self::NAME, self)
9934 }
9935}
9936impl<'r> ::core::fmt::Display for CellbaseWitnessReader<'r> {
9937 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
9938 write!(f, "{} {{ ", Self::NAME)?;
9939 write!(f, "{}: {}", "lock", self.lock())?;
9940 write!(f, ", {}: {}", "message", self.message())?;
9941 let extra_count = self.count_extra_fields();
9942 if extra_count != 0 {
9943 write!(f, ", .. ({} fields)", extra_count)?;
9944 }
9945 write!(f, " }}")
9946 }
9947}
9948impl<'r> CellbaseWitnessReader<'r> {
9949 pub const FIELD_COUNT: usize = 2;
9950 pub fn total_size(&self) -> usize {
9951 molecule::unpack_number(self.as_slice()) as usize
9952 }
9953 pub fn field_count(&self) -> usize {
9954 if self.total_size() == molecule::NUMBER_SIZE {
9955 0
9956 } else {
9957 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
9958 }
9959 }
9960 pub fn count_extra_fields(&self) -> usize {
9961 self.field_count() - Self::FIELD_COUNT
9962 }
9963 pub fn has_extra_fields(&self) -> bool {
9964 Self::FIELD_COUNT != self.field_count()
9965 }
9966 pub fn lock(&self) -> ScriptReader<'r> {
9967 let slice = self.as_slice();
9968 let start = molecule::unpack_number(&slice[4..]) as usize;
9969 let end = molecule::unpack_number(&slice[8..]) as usize;
9970 ScriptReader::new_unchecked(&self.as_slice()[start..end])
9971 }
9972 pub fn message(&self) -> BytesReader<'r> {
9973 let slice = self.as_slice();
9974 let start = molecule::unpack_number(&slice[8..]) as usize;
9975 if self.has_extra_fields() {
9976 let end = molecule::unpack_number(&slice[12..]) as usize;
9977 BytesReader::new_unchecked(&self.as_slice()[start..end])
9978 } else {
9979 BytesReader::new_unchecked(&self.as_slice()[start..])
9980 }
9981 }
9982}
9983impl<'r> molecule::prelude::Reader<'r> for CellbaseWitnessReader<'r> {
9984 type Entity = CellbaseWitness;
9985 const NAME: &'static str = "CellbaseWitnessReader";
9986 fn to_entity(&self) -> Self::Entity {
9987 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
9988 }
9989 fn new_unchecked(slice: &'r [u8]) -> Self {
9990 CellbaseWitnessReader(slice)
9991 }
9992 fn as_slice(&self) -> &'r [u8] {
9993 self.0
9994 }
9995 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
9996 use molecule::verification_error as ve;
9997 let slice_len = slice.len();
9998 if slice_len < molecule::NUMBER_SIZE {
9999 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10000 }
10001 let total_size = molecule::unpack_number(slice) as usize;
10002 if slice_len != total_size {
10003 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10004 }
10005 if slice_len < molecule::NUMBER_SIZE * 2 {
10006 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
10007 }
10008 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10009 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10010 return ve!(Self, OffsetsNotMatch);
10011 }
10012 if slice_len < offset_first {
10013 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10014 }
10015 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
10016 if field_count < Self::FIELD_COUNT {
10017 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10018 } else if !compatible && field_count > Self::FIELD_COUNT {
10019 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10020 };
10021 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10022 .chunks_exact(molecule::NUMBER_SIZE)
10023 .map(|x| molecule::unpack_number(x) as usize)
10024 .collect();
10025 offsets.push(total_size);
10026 if offsets.windows(2).any(|i| i[0] > i[1]) {
10027 return ve!(Self, OffsetsNotMatch);
10028 }
10029 ScriptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
10030 BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
10031 Ok(())
10032 }
10033}
10034#[derive(Clone, Debug, Default)]
10035pub struct CellbaseWitnessBuilder {
10036 pub(crate) lock: Script,
10037 pub(crate) message: Bytes,
10038}
10039impl CellbaseWitnessBuilder {
10040 pub const FIELD_COUNT: usize = 2;
10041 pub fn lock<T>(mut self, v: T) -> Self
10042 where
10043 T: ::core::convert::Into<Script>,
10044 {
10045 self.lock = v.into();
10046 self
10047 }
10048 pub fn message<T>(mut self, v: T) -> Self
10049 where
10050 T: ::core::convert::Into<Bytes>,
10051 {
10052 self.message = v.into();
10053 self
10054 }
10055}
10056impl molecule::prelude::Builder for CellbaseWitnessBuilder {
10057 type Entity = CellbaseWitness;
10058 const NAME: &'static str = "CellbaseWitnessBuilder";
10059 fn expected_length(&self) -> usize {
10060 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
10061 + self.lock.as_slice().len()
10062 + self.message.as_slice().len()
10063 }
10064 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10065 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
10066 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
10067 offsets.push(total_size);
10068 total_size += self.lock.as_slice().len();
10069 offsets.push(total_size);
10070 total_size += self.message.as_slice().len();
10071 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10072 for offset in offsets.into_iter() {
10073 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10074 }
10075 writer.write_all(self.lock.as_slice())?;
10076 writer.write_all(self.message.as_slice())?;
10077 Ok(())
10078 }
10079 fn build(&self) -> Self::Entity {
10080 let mut inner = Vec::with_capacity(self.expected_length());
10081 self.write(&mut inner)
10082 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10083 CellbaseWitness::new_unchecked(inner.into())
10084 }
10085}
10086#[derive(Clone)]
10087pub struct WitnessArgs(molecule::bytes::Bytes);
10088impl ::core::fmt::LowerHex for WitnessArgs {
10089 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10090 use molecule::hex_string;
10091 if f.alternate() {
10092 write!(f, "0x")?;
10093 }
10094 write!(f, "{}", hex_string(self.as_slice()))
10095 }
10096}
10097impl ::core::fmt::Debug for WitnessArgs {
10098 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10099 write!(f, "{}({:#x})", Self::NAME, self)
10100 }
10101}
10102impl ::core::fmt::Display for WitnessArgs {
10103 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10104 write!(f, "{} {{ ", Self::NAME)?;
10105 write!(f, "{}: {}", "lock", self.lock())?;
10106 write!(f, ", {}: {}", "input_type", self.input_type())?;
10107 write!(f, ", {}: {}", "output_type", self.output_type())?;
10108 let extra_count = self.count_extra_fields();
10109 if extra_count != 0 {
10110 write!(f, ", .. ({} fields)", extra_count)?;
10111 }
10112 write!(f, " }}")
10113 }
10114}
10115impl ::core::default::Default for WitnessArgs {
10116 fn default() -> Self {
10117 let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
10118 WitnessArgs::new_unchecked(v)
10119 }
10120}
10121impl WitnessArgs {
10122 const DEFAULT_VALUE: [u8; 16] = [16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0];
10123 pub const FIELD_COUNT: usize = 3;
10124 pub fn total_size(&self) -> usize {
10125 molecule::unpack_number(self.as_slice()) as usize
10126 }
10127 pub fn field_count(&self) -> usize {
10128 if self.total_size() == molecule::NUMBER_SIZE {
10129 0
10130 } else {
10131 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10132 }
10133 }
10134 pub fn count_extra_fields(&self) -> usize {
10135 self.field_count() - Self::FIELD_COUNT
10136 }
10137 pub fn has_extra_fields(&self) -> bool {
10138 Self::FIELD_COUNT != self.field_count()
10139 }
10140 pub fn lock(&self) -> BytesOpt {
10141 let slice = self.as_slice();
10142 let start = molecule::unpack_number(&slice[4..]) as usize;
10143 let end = molecule::unpack_number(&slice[8..]) as usize;
10144 BytesOpt::new_unchecked(self.0.slice(start..end))
10145 }
10146 pub fn input_type(&self) -> BytesOpt {
10147 let slice = self.as_slice();
10148 let start = molecule::unpack_number(&slice[8..]) as usize;
10149 let end = molecule::unpack_number(&slice[12..]) as usize;
10150 BytesOpt::new_unchecked(self.0.slice(start..end))
10151 }
10152 pub fn output_type(&self) -> BytesOpt {
10153 let slice = self.as_slice();
10154 let start = molecule::unpack_number(&slice[12..]) as usize;
10155 if self.has_extra_fields() {
10156 let end = molecule::unpack_number(&slice[16..]) as usize;
10157 BytesOpt::new_unchecked(self.0.slice(start..end))
10158 } else {
10159 BytesOpt::new_unchecked(self.0.slice(start..))
10160 }
10161 }
10162 pub fn as_reader<'r>(&'r self) -> WitnessArgsReader<'r> {
10163 WitnessArgsReader::new_unchecked(self.as_slice())
10164 }
10165}
10166impl molecule::prelude::Entity for WitnessArgs {
10167 type Builder = WitnessArgsBuilder;
10168 const NAME: &'static str = "WitnessArgs";
10169 fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
10170 WitnessArgs(data)
10171 }
10172 fn as_bytes(&self) -> molecule::bytes::Bytes {
10173 self.0.clone()
10174 }
10175 fn as_slice(&self) -> &[u8] {
10176 &self.0[..]
10177 }
10178 fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10179 WitnessArgsReader::from_slice(slice).map(|reader| reader.to_entity())
10180 }
10181 fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
10182 WitnessArgsReader::from_compatible_slice(slice).map(|reader| reader.to_entity())
10183 }
10184 fn new_builder() -> Self::Builder {
10185 ::core::default::Default::default()
10186 }
10187 fn as_builder(self) -> Self::Builder {
10188 Self::new_builder()
10189 .lock(self.lock())
10190 .input_type(self.input_type())
10191 .output_type(self.output_type())
10192 }
10193}
10194#[derive(Clone, Copy)]
10195pub struct WitnessArgsReader<'r>(&'r [u8]);
10196impl<'r> ::core::fmt::LowerHex for WitnessArgsReader<'r> {
10197 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10198 use molecule::hex_string;
10199 if f.alternate() {
10200 write!(f, "0x")?;
10201 }
10202 write!(f, "{}", hex_string(self.as_slice()))
10203 }
10204}
10205impl<'r> ::core::fmt::Debug for WitnessArgsReader<'r> {
10206 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10207 write!(f, "{}({:#x})", Self::NAME, self)
10208 }
10209}
10210impl<'r> ::core::fmt::Display for WitnessArgsReader<'r> {
10211 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
10212 write!(f, "{} {{ ", Self::NAME)?;
10213 write!(f, "{}: {}", "lock", self.lock())?;
10214 write!(f, ", {}: {}", "input_type", self.input_type())?;
10215 write!(f, ", {}: {}", "output_type", self.output_type())?;
10216 let extra_count = self.count_extra_fields();
10217 if extra_count != 0 {
10218 write!(f, ", .. ({} fields)", extra_count)?;
10219 }
10220 write!(f, " }}")
10221 }
10222}
10223impl<'r> WitnessArgsReader<'r> {
10224 pub const FIELD_COUNT: usize = 3;
10225 pub fn total_size(&self) -> usize {
10226 molecule::unpack_number(self.as_slice()) as usize
10227 }
10228 pub fn field_count(&self) -> usize {
10229 if self.total_size() == molecule::NUMBER_SIZE {
10230 0
10231 } else {
10232 (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1
10233 }
10234 }
10235 pub fn count_extra_fields(&self) -> usize {
10236 self.field_count() - Self::FIELD_COUNT
10237 }
10238 pub fn has_extra_fields(&self) -> bool {
10239 Self::FIELD_COUNT != self.field_count()
10240 }
10241 pub fn lock(&self) -> BytesOptReader<'r> {
10242 let slice = self.as_slice();
10243 let start = molecule::unpack_number(&slice[4..]) as usize;
10244 let end = molecule::unpack_number(&slice[8..]) as usize;
10245 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
10246 }
10247 pub fn input_type(&self) -> BytesOptReader<'r> {
10248 let slice = self.as_slice();
10249 let start = molecule::unpack_number(&slice[8..]) as usize;
10250 let end = molecule::unpack_number(&slice[12..]) as usize;
10251 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
10252 }
10253 pub fn output_type(&self) -> BytesOptReader<'r> {
10254 let slice = self.as_slice();
10255 let start = molecule::unpack_number(&slice[12..]) as usize;
10256 if self.has_extra_fields() {
10257 let end = molecule::unpack_number(&slice[16..]) as usize;
10258 BytesOptReader::new_unchecked(&self.as_slice()[start..end])
10259 } else {
10260 BytesOptReader::new_unchecked(&self.as_slice()[start..])
10261 }
10262 }
10263}
10264impl<'r> molecule::prelude::Reader<'r> for WitnessArgsReader<'r> {
10265 type Entity = WitnessArgs;
10266 const NAME: &'static str = "WitnessArgsReader";
10267 fn to_entity(&self) -> Self::Entity {
10268 Self::Entity::new_unchecked(self.as_slice().to_owned().into())
10269 }
10270 fn new_unchecked(slice: &'r [u8]) -> Self {
10271 WitnessArgsReader(slice)
10272 }
10273 fn as_slice(&self) -> &'r [u8] {
10274 self.0
10275 }
10276 fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
10277 use molecule::verification_error as ve;
10278 let slice_len = slice.len();
10279 if slice_len < molecule::NUMBER_SIZE {
10280 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len);
10281 }
10282 let total_size = molecule::unpack_number(slice) as usize;
10283 if slice_len != total_size {
10284 return ve!(Self, TotalSizeNotMatch, total_size, slice_len);
10285 }
10286 if slice_len < molecule::NUMBER_SIZE * 2 {
10287 return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE * 2, slice_len);
10288 }
10289 let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize;
10290 if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 {
10291 return ve!(Self, OffsetsNotMatch);
10292 }
10293 if slice_len < offset_first {
10294 return ve!(Self, HeaderIsBroken, offset_first, slice_len);
10295 }
10296 let field_count = offset_first / molecule::NUMBER_SIZE - 1;
10297 if field_count < Self::FIELD_COUNT {
10298 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10299 } else if !compatible && field_count > Self::FIELD_COUNT {
10300 return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
10301 };
10302 let mut offsets: Vec<usize> = slice[molecule::NUMBER_SIZE..offset_first]
10303 .chunks_exact(molecule::NUMBER_SIZE)
10304 .map(|x| molecule::unpack_number(x) as usize)
10305 .collect();
10306 offsets.push(total_size);
10307 if offsets.windows(2).any(|i| i[0] > i[1]) {
10308 return ve!(Self, OffsetsNotMatch);
10309 }
10310 BytesOptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
10311 BytesOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
10312 BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
10313 Ok(())
10314 }
10315}
10316#[derive(Clone, Debug, Default)]
10317pub struct WitnessArgsBuilder {
10318 pub(crate) lock: BytesOpt,
10319 pub(crate) input_type: BytesOpt,
10320 pub(crate) output_type: BytesOpt,
10321}
10322impl WitnessArgsBuilder {
10323 pub const FIELD_COUNT: usize = 3;
10324 pub fn lock<T>(mut self, v: T) -> Self
10325 where
10326 T: ::core::convert::Into<BytesOpt>,
10327 {
10328 self.lock = v.into();
10329 self
10330 }
10331 pub fn input_type<T>(mut self, v: T) -> Self
10332 where
10333 T: ::core::convert::Into<BytesOpt>,
10334 {
10335 self.input_type = v.into();
10336 self
10337 }
10338 pub fn output_type<T>(mut self, v: T) -> Self
10339 where
10340 T: ::core::convert::Into<BytesOpt>,
10341 {
10342 self.output_type = v.into();
10343 self
10344 }
10345}
10346impl molecule::prelude::Builder for WitnessArgsBuilder {
10347 type Entity = WitnessArgs;
10348 const NAME: &'static str = "WitnessArgsBuilder";
10349 fn expected_length(&self) -> usize {
10350 molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
10351 + self.lock.as_slice().len()
10352 + self.input_type.as_slice().len()
10353 + self.output_type.as_slice().len()
10354 }
10355 fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
10356 let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
10357 let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
10358 offsets.push(total_size);
10359 total_size += self.lock.as_slice().len();
10360 offsets.push(total_size);
10361 total_size += self.input_type.as_slice().len();
10362 offsets.push(total_size);
10363 total_size += self.output_type.as_slice().len();
10364 writer.write_all(&molecule::pack_number(total_size as molecule::Number))?;
10365 for offset in offsets.into_iter() {
10366 writer.write_all(&molecule::pack_number(offset as molecule::Number))?;
10367 }
10368 writer.write_all(self.lock.as_slice())?;
10369 writer.write_all(self.input_type.as_slice())?;
10370 writer.write_all(self.output_type.as_slice())?;
10371 Ok(())
10372 }
10373 fn build(&self) -> Self::Entity {
10374 let mut inner = Vec::with_capacity(self.expected_length());
10375 self.write(&mut inner)
10376 .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME));
10377 WitnessArgs::new_unchecked(inner.into())
10378 }
10379}