diff --git a/Cargo.lock b/Cargo.lock index 3ef9273..9bafeea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -77,18 +77,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "ckb-channel" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c37f58ccee88155144067490986221d6e65b721f9184ce4f775621d8c453247e" +checksum = "44e1a2db7097ef2cd6e7868c4adc45c7863c5abee83fba9ecd428b83cd77f536" dependencies = [ "crossbeam-channel", ] [[package]] name = "ckb-error" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "548dad083a5846996d38fb2680e4fd20737c23be4c6dbbc897bba63fc69427fd" +checksum = "44f068a6fd2bc852172c6bc33465e757953299dfed0aa66a3d52d5a2c7e5272a" dependencies = [ "anyhow", "ckb-occupied-capacity", @@ -98,9 +98,9 @@ dependencies = [ [[package]] name = "ckb-fixed-hash" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "87e16c26d073b6ce9820154d8fb067d95dbfcb91faa693162341e90f26c3363f" +checksum = "1764c5697371b60dba3af51880dda454508fa4a74be5c27f8c32f00fda0e356f" dependencies = [ "ckb-fixed-hash-core", "ckb-fixed-hash-macros", @@ -108,9 +108,9 @@ dependencies = [ [[package]] name = "ckb-fixed-hash-core" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14c7cda98b0ed0f7c46788e5399b1c69423cad02cf9b8f9f77e3ed7cfbd95dcc" +checksum = "616d99b6dff9b5c76b241fe894c9d0899d2bc8c9971c131e1a93cec4035fa032" dependencies = [ "faster-hex", "serde", @@ -119,9 +119,9 @@ dependencies = [ [[package]] name = "ckb-fixed-hash-macros" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcc8f5a4d66e01da9f8024aea5a49ee0c93f7a927e932b37c162c7afd38959f4" +checksum = "47435298b4a697851c58c2d4347cf50d1d9b5893f3534acce4cdc2aae6a8b37e" dependencies = [ "ckb-fixed-hash-core", "proc-macro2", @@ -131,9 +131,9 @@ dependencies = [ [[package]] name = "ckb-hash" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380702c09207ac378708f075d72b336adeb17b8887dcfcc1bf9460e10ea1e285" +checksum = "5d67c434be841be84498d2cc2d4a17bec9f10e5705d75e017e4afc62a1ceef58" dependencies = [ "blake2b-ref 0.2.1", "blake2b-rs", @@ -141,9 +141,9 @@ dependencies = [ [[package]] name = "ckb-occupied-capacity" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c9aa5e0ccfc275af08d2423b8e7cbd98c980d2565950a4e3efc13182f0ae1bf" +checksum = "80dfbabc77dc083604adea5587ffa3e8a6ce0afcb92f75928575c6728981edfc" dependencies = [ "ckb-occupied-capacity-core", "ckb-occupied-capacity-macros", @@ -151,18 +151,18 @@ dependencies = [ [[package]] name = "ckb-occupied-capacity-core" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "118085f13bf4d4a160c6df360adcc2142ab40f7cf0aea058d2a5a4ca4dd61172" +checksum = "737d292e26c436dc78b8a84cf9a402bf263170ff7fe3e4ff2312fc8d03083915" dependencies = [ "serde", ] [[package]] name = "ckb-occupied-capacity-macros" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ca47bd5b0c381284f3f858cc6fd9087adecc384b1535e869a231061a23a7b59" +checksum = "7614dc55f767154813cf60373ff64260e106de6b83461e0470fd51e95ffa671f" dependencies = [ "ckb-occupied-capacity-core", "quote", @@ -171,9 +171,9 @@ dependencies = [ [[package]] name = "ckb-rational" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6942caea45b9da94b0321e738f6d58ba9f3d03347936d76924d102d360300bc4" +checksum = "d518df53c3fb2ff02e9824ad0ee0e4eb965038311f09bc88b7e3eb4c68237e4e" dependencies = [ "numext-fixed-uint", "serde", @@ -203,9 +203,9 @@ dependencies = [ [[package]] name = "ckb-types" -version = "0.103.0" +version = "0.101.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2c24d49d19a4b367ba9be3538e404131e5b911ecf8bba93bc08776377d06e67" +checksum = "f4c833f4a2c4635da013d7445e016b6edcaf10ba35cdba0c633f37faef8b9b0a" dependencies = [ "bit-vec", "bitflags", diff --git a/Cargo.toml b/Cargo.toml index f39dcbc..7c06c8e 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ edition = "2018" cfg-if = "1.0" blake2b-ref = "0.3" ckb-std = {version = "0.9.0", optional = true} -ckb-types = {version = "0.103.0", optional = true} +ckb-types = {version = "0.101.1", optional = true} molecule = {version = "0.7.2", default_features = false, optional = true} sparse-merkle-tree = {version = "0.5.3", default_features = false} diff --git a/src/common.mol b/src/common.mol index ad20dfe..4b6d871 100644 --- a/src/common.mol +++ b/src/common.mol @@ -3,6 +3,7 @@ array Byte32 [byte; 32]; array Uint16 [byte; 2]; array Uint32 [byte; 4]; +array Uint64 [byte; 8]; vector Bytes ; array CotaId [byte; 20]; diff --git a/src/common.rs b/src/common.rs index 1057457..d39fc43 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1140,6 +1140,310 @@ impl molecule::prelude::Builder for Uint32Builder { } } #[derive(Clone)] +pub struct Uint64(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint64 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for Uint64 { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0, 0, 0, 0, 0]; + Uint64::new_unchecked(v.into()) + } +} +impl Uint64 { + pub const ITEM_COUNT: usize = 8; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 8; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> Uint64Reader<'r> { + Uint64Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Uint64 { + type Builder = Uint64Builder; + + const NAME: &'static str = "Uint64"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint64(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint64Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint64Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct Uint64Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint64Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> Uint64Reader<'r> { + pub const ITEM_COUNT: usize = 8; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 8; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> { + type Entity = Uint64; + + const NAME: &'static str = "Uint64Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint64Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct Uint64Builder(pub(crate) [Byte; 8]); +impl ::core::fmt::Debug for Uint64Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for Uint64Builder { + fn default() -> Self { + Uint64Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl Uint64Builder { + pub const ITEM_COUNT: usize = 8; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 8; + + pub fn set(mut self, v: [Byte; 8]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } +} +impl molecule::prelude::Builder for Uint64Builder { + type Entity = Uint64; + + const NAME: &'static str = "Uint64Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint64::new_unchecked(inner.into()) + } +} +#[derive(Clone)] pub struct Bytes(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Bytes { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { diff --git a/src/lib.rs b/src/lib.rs index 43411ee..fcc9a4b 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod common; pub mod define; pub mod mint; +pub mod raw_tx; pub mod registry; pub mod smt; pub mod transfer; diff --git a/src/raw_tx.mol b/src/raw_tx.mol new file mode 100644 index 0000000..627f90a --- /dev/null +++ b/src/raw_tx.mol @@ -0,0 +1,59 @@ +import common; + +array H256 [byte; 32]; + +vector BytesVec ; +vector Byte32Vec ; +vector Uint32Vec ; +vector H256Vec ; + +option ScriptOpt (Script); +vector CellDepVec ; +vector CellInputVec ; +vector CellOutputVec ; + +table Script { + code_hash: Byte32, + hash_type: byte, + args: Bytes, +} + +struct OutPoint { + tx_hash: Byte32, + index: Uint32, +} + +struct CellInput { + since: Uint64, + previous_output: OutPoint, +} + +table CellOutput { + capacity: Uint64, + lock: Script, + type_: ScriptOpt, +} + +struct CellDep { + out_point: OutPoint, + dep_type: byte, +} + +table RawTransaction { + version: Uint32, + cell_deps: CellDepVec, + header_deps: Byte32Vec, + inputs: CellInputVec, + outputs: CellOutputVec, + outputs_data: BytesVec, +} + +table MerkleProof { + indices: Uint32Vec, + lemmas: H256Vec, +} + +table TransactionProof { + witnesses_root: H256, + proof: MerkleProof, +} \ No newline at end of file diff --git a/src/raw_tx.rs b/src/raw_tx.rs new file mode 100644 index 0000000..0197726 --- /dev/null +++ b/src/raw_tx.rs @@ -0,0 +1,5400 @@ +// Generated by Molecule 0.7.3 + +#![allow(unused_imports)] +#![allow(dead_code)] +#![allow(clippy::if_same_then_else)] + +use super::ckb_types::prelude::*; +use super::molecule::{self, prelude::*}; +extern crate alloc; +pub use alloc::vec::*; +// these lines above are manually added + +use super::common::*; +use molecule::prelude::*; +#[derive(Clone)] +pub struct H256(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for H256 { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl ::core::default::Default for H256 { + fn default() -> Self { + let v: Vec = vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, + ]; + H256::new_unchecked(v.into()) + } +} +impl H256 { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> Byte { + Byte::new_unchecked(self.0.slice(0..1)) + } + + pub fn nth1(&self) -> Byte { + Byte::new_unchecked(self.0.slice(1..2)) + } + + pub fn nth2(&self) -> Byte { + Byte::new_unchecked(self.0.slice(2..3)) + } + + pub fn nth3(&self) -> Byte { + Byte::new_unchecked(self.0.slice(3..4)) + } + + pub fn nth4(&self) -> Byte { + Byte::new_unchecked(self.0.slice(4..5)) + } + + pub fn nth5(&self) -> Byte { + Byte::new_unchecked(self.0.slice(5..6)) + } + + pub fn nth6(&self) -> Byte { + Byte::new_unchecked(self.0.slice(6..7)) + } + + pub fn nth7(&self) -> Byte { + Byte::new_unchecked(self.0.slice(7..8)) + } + + pub fn nth8(&self) -> Byte { + Byte::new_unchecked(self.0.slice(8..9)) + } + + pub fn nth9(&self) -> Byte { + Byte::new_unchecked(self.0.slice(9..10)) + } + + pub fn nth10(&self) -> Byte { + Byte::new_unchecked(self.0.slice(10..11)) + } + + pub fn nth11(&self) -> Byte { + Byte::new_unchecked(self.0.slice(11..12)) + } + + pub fn nth12(&self) -> Byte { + Byte::new_unchecked(self.0.slice(12..13)) + } + + pub fn nth13(&self) -> Byte { + Byte::new_unchecked(self.0.slice(13..14)) + } + + pub fn nth14(&self) -> Byte { + Byte::new_unchecked(self.0.slice(14..15)) + } + + pub fn nth15(&self) -> Byte { + Byte::new_unchecked(self.0.slice(15..16)) + } + + pub fn nth16(&self) -> Byte { + Byte::new_unchecked(self.0.slice(16..17)) + } + + pub fn nth17(&self) -> Byte { + Byte::new_unchecked(self.0.slice(17..18)) + } + + pub fn nth18(&self) -> Byte { + Byte::new_unchecked(self.0.slice(18..19)) + } + + pub fn nth19(&self) -> Byte { + Byte::new_unchecked(self.0.slice(19..20)) + } + + pub fn nth20(&self) -> Byte { + Byte::new_unchecked(self.0.slice(20..21)) + } + + pub fn nth21(&self) -> Byte { + Byte::new_unchecked(self.0.slice(21..22)) + } + + pub fn nth22(&self) -> Byte { + Byte::new_unchecked(self.0.slice(22..23)) + } + + pub fn nth23(&self) -> Byte { + Byte::new_unchecked(self.0.slice(23..24)) + } + + pub fn nth24(&self) -> Byte { + Byte::new_unchecked(self.0.slice(24..25)) + } + + pub fn nth25(&self) -> Byte { + Byte::new_unchecked(self.0.slice(25..26)) + } + + pub fn nth26(&self) -> Byte { + Byte::new_unchecked(self.0.slice(26..27)) + } + + pub fn nth27(&self) -> Byte { + Byte::new_unchecked(self.0.slice(27..28)) + } + + pub fn nth28(&self) -> Byte { + Byte::new_unchecked(self.0.slice(28..29)) + } + + pub fn nth29(&self) -> Byte { + Byte::new_unchecked(self.0.slice(29..30)) + } + + pub fn nth30(&self) -> Byte { + Byte::new_unchecked(self.0.slice(30..31)) + } + + pub fn nth31(&self) -> Byte { + Byte::new_unchecked(self.0.slice(31..32)) + } + + pub fn raw_data(&self) -> molecule::bytes::Bytes { + self.as_bytes() + } + + pub fn as_reader<'r>(&'r self) -> H256Reader<'r> { + H256Reader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for H256 { + type Builder = H256Builder; + + const NAME: &'static str = "H256"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + H256(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256Reader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256Reader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().set([ + self.nth0(), + self.nth1(), + self.nth2(), + self.nth3(), + self.nth4(), + self.nth5(), + self.nth6(), + self.nth7(), + self.nth8(), + self.nth9(), + self.nth10(), + self.nth11(), + self.nth12(), + self.nth13(), + self.nth14(), + self.nth15(), + self.nth16(), + self.nth17(), + self.nth18(), + self.nth19(), + self.nth20(), + self.nth21(), + self.nth22(), + self.nth23(), + self.nth24(), + self.nth25(), + self.nth26(), + self.nth27(), + self.nth28(), + self.nth29(), + self.nth30(), + self.nth31(), + ]) + } +} +#[derive(Clone, Copy)] +pub struct H256Reader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for H256Reader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + let raw_data = hex_string(&self.raw_data()); + write!(f, "{}(0x{})", Self::NAME, raw_data) + } +} +impl<'r> H256Reader<'r> { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn nth0(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[0..1]) + } + + pub fn nth1(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[1..2]) + } + + pub fn nth2(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[2..3]) + } + + pub fn nth3(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[3..4]) + } + + pub fn nth4(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[4..5]) + } + + pub fn nth5(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[5..6]) + } + + pub fn nth6(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[6..7]) + } + + pub fn nth7(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[7..8]) + } + + pub fn nth8(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[8..9]) + } + + pub fn nth9(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[9..10]) + } + + pub fn nth10(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[10..11]) + } + + pub fn nth11(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[11..12]) + } + + pub fn nth12(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[12..13]) + } + + pub fn nth13(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[13..14]) + } + + pub fn nth14(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[14..15]) + } + + pub fn nth15(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[15..16]) + } + + pub fn nth16(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[16..17]) + } + + pub fn nth17(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[17..18]) + } + + pub fn nth18(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[18..19]) + } + + pub fn nth19(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[19..20]) + } + + pub fn nth20(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[20..21]) + } + + pub fn nth21(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[21..22]) + } + + pub fn nth22(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[22..23]) + } + + pub fn nth23(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[23..24]) + } + + pub fn nth24(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[24..25]) + } + + pub fn nth25(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[25..26]) + } + + pub fn nth26(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[26..27]) + } + + pub fn nth27(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[27..28]) + } + + pub fn nth28(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[28..29]) + } + + pub fn nth29(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[29..30]) + } + + pub fn nth30(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[30..31]) + } + + pub fn nth31(&self) -> ByteReader<'r> { + ByteReader::new_unchecked(&self.as_slice()[31..32]) + } + + pub fn raw_data(&self) -> &'r [u8] { + self.as_slice() + } +} +impl<'r> molecule::prelude::Reader<'r> for H256Reader<'r> { + type Entity = H256; + + const NAME: &'static str = "H256Reader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + H256Reader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len != Self::TOTAL_SIZE { + return ve!(Self, TotalSizeNotMatch, Self::TOTAL_SIZE, slice_len); + } + Ok(()) + } +} +pub struct H256Builder(pub(crate) [Byte; 32]); +impl ::core::fmt::Debug for H256Builder { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:?})", Self::NAME, &self.0[..]) + } +} +impl ::core::default::Default for H256Builder { + fn default() -> Self { + H256Builder([ + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + Byte::default(), + ]) + } +} +impl H256Builder { + pub const ITEM_COUNT: usize = 32; + pub const ITEM_SIZE: usize = 1; + pub const TOTAL_SIZE: usize = 32; + + pub fn set(mut self, v: [Byte; 32]) -> Self { + self.0 = v; + self + } + + pub fn nth0(mut self, v: Byte) -> Self { + self.0[0] = v; + self + } + + pub fn nth1(mut self, v: Byte) -> Self { + self.0[1] = v; + self + } + + pub fn nth2(mut self, v: Byte) -> Self { + self.0[2] = v; + self + } + + pub fn nth3(mut self, v: Byte) -> Self { + self.0[3] = v; + self + } + + pub fn nth4(mut self, v: Byte) -> Self { + self.0[4] = v; + self + } + + pub fn nth5(mut self, v: Byte) -> Self { + self.0[5] = v; + self + } + + pub fn nth6(mut self, v: Byte) -> Self { + self.0[6] = v; + self + } + + pub fn nth7(mut self, v: Byte) -> Self { + self.0[7] = v; + self + } + + pub fn nth8(mut self, v: Byte) -> Self { + self.0[8] = v; + self + } + + pub fn nth9(mut self, v: Byte) -> Self { + self.0[9] = v; + self + } + + pub fn nth10(mut self, v: Byte) -> Self { + self.0[10] = v; + self + } + + pub fn nth11(mut self, v: Byte) -> Self { + self.0[11] = v; + self + } + + pub fn nth12(mut self, v: Byte) -> Self { + self.0[12] = v; + self + } + + pub fn nth13(mut self, v: Byte) -> Self { + self.0[13] = v; + self + } + + pub fn nth14(mut self, v: Byte) -> Self { + self.0[14] = v; + self + } + + pub fn nth15(mut self, v: Byte) -> Self { + self.0[15] = v; + self + } + + pub fn nth16(mut self, v: Byte) -> Self { + self.0[16] = v; + self + } + + pub fn nth17(mut self, v: Byte) -> Self { + self.0[17] = v; + self + } + + pub fn nth18(mut self, v: Byte) -> Self { + self.0[18] = v; + self + } + + pub fn nth19(mut self, v: Byte) -> Self { + self.0[19] = v; + self + } + + pub fn nth20(mut self, v: Byte) -> Self { + self.0[20] = v; + self + } + + pub fn nth21(mut self, v: Byte) -> Self { + self.0[21] = v; + self + } + + pub fn nth22(mut self, v: Byte) -> Self { + self.0[22] = v; + self + } + + pub fn nth23(mut self, v: Byte) -> Self { + self.0[23] = v; + self + } + + pub fn nth24(mut self, v: Byte) -> Self { + self.0[24] = v; + self + } + + pub fn nth25(mut self, v: Byte) -> Self { + self.0[25] = v; + self + } + + pub fn nth26(mut self, v: Byte) -> Self { + self.0[26] = v; + self + } + + pub fn nth27(mut self, v: Byte) -> Self { + self.0[27] = v; + self + } + + pub fn nth28(mut self, v: Byte) -> Self { + self.0[28] = v; + self + } + + pub fn nth29(mut self, v: Byte) -> Self { + self.0[29] = v; + self + } + + pub fn nth30(mut self, v: Byte) -> Self { + self.0[30] = v; + self + } + + pub fn nth31(mut self, v: Byte) -> Self { + self.0[31] = v; + self + } +} +impl molecule::prelude::Builder for H256Builder { + type Entity = H256; + + const NAME: &'static str = "H256Builder"; + + fn expected_length(&self) -> usize { + Self::TOTAL_SIZE + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(self.0[0].as_slice())?; + writer.write_all(self.0[1].as_slice())?; + writer.write_all(self.0[2].as_slice())?; + writer.write_all(self.0[3].as_slice())?; + writer.write_all(self.0[4].as_slice())?; + writer.write_all(self.0[5].as_slice())?; + writer.write_all(self.0[6].as_slice())?; + writer.write_all(self.0[7].as_slice())?; + writer.write_all(self.0[8].as_slice())?; + writer.write_all(self.0[9].as_slice())?; + writer.write_all(self.0[10].as_slice())?; + writer.write_all(self.0[11].as_slice())?; + writer.write_all(self.0[12].as_slice())?; + writer.write_all(self.0[13].as_slice())?; + writer.write_all(self.0[14].as_slice())?; + writer.write_all(self.0[15].as_slice())?; + writer.write_all(self.0[16].as_slice())?; + writer.write_all(self.0[17].as_slice())?; + writer.write_all(self.0[18].as_slice())?; + writer.write_all(self.0[19].as_slice())?; + writer.write_all(self.0[20].as_slice())?; + writer.write_all(self.0[21].as_slice())?; + writer.write_all(self.0[22].as_slice())?; + writer.write_all(self.0[23].as_slice())?; + writer.write_all(self.0[24].as_slice())?; + writer.write_all(self.0[25].as_slice())?; + writer.write_all(self.0[26].as_slice())?; + writer.write_all(self.0[27].as_slice())?; + writer.write_all(self.0[28].as_slice())?; + writer.write_all(self.0[29].as_slice())?; + writer.write_all(self.0[30].as_slice())?; + writer.write_all(self.0[31].as_slice())?; + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + H256::new_unchecked(inner.into()) + } +} +#[derive(Clone)] +pub struct BytesVec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for BytesVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for BytesVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for BytesVec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for BytesVec { + fn default() -> Self { + let v: Vec = vec![4, 0, 0, 0]; + BytesVec::new_unchecked(v.into()) + } +} +impl BytesVec { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Bytes { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + Bytes::new_unchecked(self.0.slice(start..)) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + Bytes::new_unchecked(self.0.slice(start..end)) + } + } + + pub fn as_reader<'r>(&'r self) -> BytesVecReader<'r> { + BytesVecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for BytesVec { + type Builder = BytesVecBuilder; + + const NAME: &'static str = "BytesVec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + BytesVec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + BytesVecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + BytesVecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct BytesVecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for BytesVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for BytesVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for BytesVecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> BytesVecReader<'r> { + pub fn total_size(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn item_count(&self) -> usize { + if self.total_size() == molecule::NUMBER_SIZE { + 0 + } else { + (molecule::unpack_number(&self.as_slice()[molecule::NUMBER_SIZE..]) as usize / 4) - 1 + } + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> BytesReader<'r> { + let slice = self.as_slice(); + let start_idx = molecule::NUMBER_SIZE * (1 + idx); + let start = molecule::unpack_number(&slice[start_idx..]) as usize; + if idx == self.len() - 1 { + BytesReader::new_unchecked(&self.as_slice()[start..]) + } else { + let end_idx = start_idx + molecule::NUMBER_SIZE; + let end = molecule::unpack_number(&slice[end_idx..]) as usize; + BytesReader::new_unchecked(&self.as_slice()[start..end]) + } + } +} +impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> { + type Entity = BytesVec; + + const NAME: &'static str = "BytesVecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + BytesVecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let total_size = molecule::unpack_number(slice) as usize; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + if slice_len == molecule::NUMBER_SIZE { + return Ok(()); + } + if slice_len < molecule::NUMBER_SIZE * 2 { + return ve!( + Self, + TotalSizeNotMatch, + molecule::NUMBER_SIZE * 2, + slice_len + ); + } + let offset_first = molecule::unpack_number(&slice[molecule::NUMBER_SIZE..]) as usize; + if offset_first % molecule::NUMBER_SIZE != 0 || offset_first < molecule::NUMBER_SIZE * 2 { + return ve!(Self, OffsetsNotMatch); + } + if slice_len < offset_first { + return ve!(Self, HeaderIsBroken, offset_first, slice_len); + } + let mut offsets: Vec = slice[molecule::NUMBER_SIZE..offset_first] + .chunks_exact(molecule::NUMBER_SIZE) + .map(|x| molecule::unpack_number(x) as usize) + .collect(); + offsets.push(total_size); + if offsets.windows(2).any(|i| i[0] > i[1]) { + return ve!(Self, OffsetsNotMatch); + } + for pair in offsets.windows(2) { + let start = pair[0]; + let end = pair[1]; + BytesReader::verify(&slice[start..end], compatible)?; + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct BytesVecBuilder(pub(crate) Vec); +impl BytesVecBuilder { + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Bytes) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Bytes) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for BytesVecBuilder { + type Entity = BytesVec; + + const NAME: &'static str = "BytesVecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE * (self.0.len() + 1) + + self + .0 + .iter() + .map(|inner| inner.as_slice().len()) + .sum::() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + let item_count = self.0.len(); + if item_count == 0 { + writer.write_all(&molecule::pack_number( + molecule::NUMBER_SIZE as molecule::Number, + ))?; + } else { + let (total_size, offsets) = self.0.iter().fold( + ( + molecule::NUMBER_SIZE * (item_count + 1), + Vec::with_capacity(item_count), + ), + |(start, mut offsets), inner| { + offsets.push(start); + (start + inner.as_slice().len(), offsets) + }, + ); + writer.write_all(&molecule::pack_number(total_size as molecule::Number))?; + for offset in offsets.into_iter() { + writer.write_all(&molecule::pack_number(offset as molecule::Number))?; + } + for inner in self.0.iter() { + writer.write_all(inner.as_slice())?; + } + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + BytesVec::new_unchecked(inner.into()) + } +} +pub struct BytesVecIterator(BytesVec, usize, usize); +impl ::core::iter::Iterator for BytesVecIterator { + type Item = Bytes; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for BytesVecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for BytesVec { + type IntoIter = BytesVecIterator; + type Item = Bytes; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + BytesVecIterator(self, 0, len) + } +} +impl<'r> BytesVecReader<'r> { + pub fn iter<'t>(&'t self) -> BytesVecReaderIterator<'t, 'r> { + BytesVecReaderIterator(&self, 0, self.len()) + } +} +pub struct BytesVecReaderIterator<'t, 'r>(&'t BytesVecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for BytesVecReaderIterator<'t, 'r> { + type Item = BytesReader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct Byte32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Byte32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Byte32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Byte32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for Byte32Vec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Byte32Vec::new_unchecked(v.into()) + } +} +impl Byte32Vec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> Byte32VecReader<'r> { + Byte32VecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Byte32Vec { + type Builder = Byte32VecBuilder; + + const NAME: &'static str = "Byte32Vec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Byte32Vec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Byte32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct Byte32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Byte32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Byte32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Byte32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> Byte32VecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Byte32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Byte32Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> { + type Entity = Byte32Vec; + + const NAME: &'static str = "Byte32VecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Byte32VecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct Byte32VecBuilder(pub(crate) Vec); +impl Byte32VecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Byte32) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Byte32) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for Byte32VecBuilder { + type Entity = Byte32Vec; + + const NAME: &'static str = "Byte32VecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Byte32Vec::new_unchecked(inner.into()) + } +} +pub struct Byte32VecIterator(Byte32Vec, usize, usize); +impl ::core::iter::Iterator for Byte32VecIterator { + type Item = Byte32; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for Byte32VecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for Byte32Vec { + type IntoIter = Byte32VecIterator; + type Item = Byte32; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + Byte32VecIterator(self, 0, len) + } +} +impl<'r> Byte32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Byte32VecReaderIterator<'t, 'r> { + Byte32VecReaderIterator(&self, 0, self.len()) + } +} +pub struct Byte32VecReaderIterator<'t, 'r>(&'t Byte32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Byte32VecReaderIterator<'t, 'r> { + type Item = Byte32Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct Uint32Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for Uint32Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for Uint32Vec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + Uint32Vec::new_unchecked(v.into()) + } +} +impl Uint32Vec { + pub const ITEM_SIZE: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Uint32 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> Uint32VecReader<'r> { + Uint32VecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for Uint32Vec { + type Builder = Uint32VecBuilder; + + const NAME: &'static str = "Uint32Vec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + Uint32Vec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + Uint32VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct Uint32VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for Uint32VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> Uint32VecReader<'r> { + pub const ITEM_SIZE: usize = 4; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + Uint32Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for Uint32VecReader<'r> { + type Entity = Uint32Vec; + + const NAME: &'static str = "Uint32VecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + Uint32VecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct Uint32VecBuilder(pub(crate) Vec); +impl Uint32VecBuilder { + pub const ITEM_SIZE: usize = 4; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: Uint32) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: Uint32) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for Uint32VecBuilder { + type Entity = Uint32Vec; + + const NAME: &'static str = "Uint32VecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + Uint32Vec::new_unchecked(inner.into()) + } +} +pub struct Uint32VecIterator(Uint32Vec, usize, usize); +impl ::core::iter::Iterator for Uint32VecIterator { + type Item = Uint32; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for Uint32VecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for Uint32Vec { + type IntoIter = Uint32VecIterator; + type Item = Uint32; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + Uint32VecIterator(self, 0, len) + } +} +impl<'r> Uint32VecReader<'r> { + pub fn iter<'t>(&'t self) -> Uint32VecReaderIterator<'t, 'r> { + Uint32VecReaderIterator(&self, 0, self.len()) + } +} +pub struct Uint32VecReaderIterator<'t, 'r>(&'t Uint32VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for Uint32VecReaderIterator<'t, 'r> { + type Item = Uint32Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Uint32VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct H256Vec(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for H256Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for H256Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for H256Vec { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl ::core::default::Default for H256Vec { + fn default() -> Self { + let v: Vec = vec![0, 0, 0, 0]; + H256Vec::new_unchecked(v.into()) + } +} +impl H256Vec { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> H256 { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + H256::new_unchecked(self.0.slice(start..end)) + } + + pub fn as_reader<'r>(&'r self) -> H256VecReader<'r> { + H256VecReader::new_unchecked(self.as_slice()) + } +} +impl molecule::prelude::Entity for H256Vec { + type Builder = H256VecBuilder; + + const NAME: &'static str = "H256Vec"; + + fn new_unchecked(data: molecule::bytes::Bytes) -> Self { + H256Vec(data) + } + + fn as_bytes(&self) -> molecule::bytes::Bytes { + self.0.clone() + } + + fn as_slice(&self) -> &[u8] { + &self.0[..] + } + + fn from_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256VecReader::from_slice(slice).map(|reader| reader.to_entity()) + } + + fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult { + H256VecReader::from_compatible_slice(slice).map(|reader| reader.to_entity()) + } + + fn new_builder() -> Self::Builder { + ::core::default::Default::default() + } + + fn as_builder(self) -> Self::Builder { + Self::new_builder().extend(self.into_iter()) + } +} +#[derive(Clone, Copy)] +pub struct H256VecReader<'r>(&'r [u8]); +impl<'r> ::core::fmt::LowerHex for H256VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl<'r> ::core::fmt::Debug for H256VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl<'r> ::core::fmt::Display for H256VecReader<'r> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{} [", Self::NAME)?; + for i in 0..self.len() { + if i == 0 { + write!(f, "{}", self.get_unchecked(i))?; + } else { + write!(f, ", {}", self.get_unchecked(i))?; + } + } + write!(f, "]") + } +} +impl<'r> H256VecReader<'r> { + pub const ITEM_SIZE: usize = 32; + + pub fn total_size(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count() + } + + pub fn item_count(&self) -> usize { + molecule::unpack_number(self.as_slice()) as usize + } + + pub fn len(&self) -> usize { + self.item_count() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn get(&self, idx: usize) -> Option> { + if idx >= self.len() { + None + } else { + Some(self.get_unchecked(idx)) + } + } + + pub fn get_unchecked(&self, idx: usize) -> H256Reader<'r> { + let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx; + let end = start + Self::ITEM_SIZE; + H256Reader::new_unchecked(&self.as_slice()[start..end]) + } +} +impl<'r> molecule::prelude::Reader<'r> for H256VecReader<'r> { + type Entity = H256Vec; + + const NAME: &'static str = "H256VecReader"; + + fn to_entity(&self) -> Self::Entity { + Self::Entity::new_unchecked(self.as_slice().to_owned().into()) + } + + fn new_unchecked(slice: &'r [u8]) -> Self { + H256VecReader(slice) + } + + fn as_slice(&self) -> &'r [u8] { + self.0 + } + + fn verify(slice: &[u8], _compatible: bool) -> molecule::error::VerificationResult<()> { + use molecule::verification_error as ve; + let slice_len = slice.len(); + if slice_len < molecule::NUMBER_SIZE { + return ve!(Self, HeaderIsBroken, molecule::NUMBER_SIZE, slice_len); + } + let item_count = molecule::unpack_number(slice) as usize; + if item_count == 0 { + if slice_len != molecule::NUMBER_SIZE { + return ve!(Self, TotalSizeNotMatch, molecule::NUMBER_SIZE, slice_len); + } + return Ok(()); + } + let total_size = molecule::NUMBER_SIZE + Self::ITEM_SIZE * item_count; + if slice_len != total_size { + return ve!(Self, TotalSizeNotMatch, total_size, slice_len); + } + Ok(()) + } +} +#[derive(Debug, Default)] +pub struct H256VecBuilder(pub(crate) Vec); +impl H256VecBuilder { + pub const ITEM_SIZE: usize = 32; + + pub fn set(mut self, v: Vec) -> Self { + self.0 = v; + self + } + + pub fn push(mut self, v: H256) -> Self { + self.0.push(v); + self + } + + pub fn extend>(mut self, iter: T) -> Self { + for elem in iter { + self.0.push(elem); + } + self + } + + pub fn replace(&mut self, index: usize, v: H256) -> Option { + self.0 + .get_mut(index) + .map(|item| ::core::mem::replace(item, v)) + } +} +impl molecule::prelude::Builder for H256VecBuilder { + type Entity = H256Vec; + + const NAME: &'static str = "H256VecBuilder"; + + fn expected_length(&self) -> usize { + molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len() + } + + fn write(&self, writer: &mut W) -> molecule::io::Result<()> { + writer.write_all(&molecule::pack_number(self.0.len() as molecule::Number))?; + for inner in &self.0[..] { + writer.write_all(inner.as_slice())?; + } + Ok(()) + } + + fn build(&self) -> Self::Entity { + let mut inner = Vec::with_capacity(self.expected_length()); + self.write(&mut inner) + .unwrap_or_else(|_| panic!("{} build should be ok", Self::NAME)); + H256Vec::new_unchecked(inner.into()) + } +} +pub struct H256VecIterator(H256Vec, usize, usize); +impl ::core::iter::Iterator for H256VecIterator { + type Item = H256; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl ::core::iter::ExactSizeIterator for H256VecIterator { + fn len(&self) -> usize { + self.2 - self.1 + } +} +impl ::core::iter::IntoIterator for H256Vec { + type IntoIter = H256VecIterator; + type Item = H256; + + fn into_iter(self) -> Self::IntoIter { + let len = self.len(); + H256VecIterator(self, 0, len) + } +} +impl<'r> H256VecReader<'r> { + pub fn iter<'t>(&'t self) -> H256VecReaderIterator<'t, 'r> { + H256VecReaderIterator(&self, 0, self.len()) + } +} +pub struct H256VecReaderIterator<'t, 'r>(&'t H256VecReader<'r>, usize, usize); +impl<'t: 'r, 'r> ::core::iter::Iterator for H256VecReaderIterator<'t, 'r> { + type Item = H256Reader<'t>; + + fn next(&mut self) -> Option { + if self.1 >= self.2 { + None + } else { + let ret = self.0.get_unchecked(self.1); + self.1 += 1; + Some(ret) + } + } +} +impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for H256VecReaderIterator<'t, 'r> { + fn len(&self) -> usize { + self.2 - self.1 + } +} +#[derive(Clone)] +pub struct ScriptOpt(molecule::bytes::Bytes); +impl ::core::fmt::LowerHex for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + use molecule::hex_string; + if f.alternate() { + write!(f, "0x")?; + } + write!(f, "{}", hex_string(self.as_slice())) + } +} +impl ::core::fmt::Debug for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + write!(f, "{}({:#x})", Self::NAME, self) + } +} +impl ::core::fmt::Display for ScriptOpt { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { + if let Some(v) = self.to_opt() { + write!(f, "{}(Some({}))", Self::NAME, v) + } else { + write!(f, "{}(None)", Self::NAME) + } + } +} +impl ::core::default::Default for ScriptOpt { + fn default() -> Self { + let v: Vec = vec![]; + ScriptOpt::new_unchecked(v.into()) + } +} +impl ScriptOpt { + pub fn is_none(&self) -> bool { + self.0.is_empty() + } + + pub fn is_some(&self) -> bool { + !self.0.is_empty() + } + + pub fn to_opt(&self) -> Option