use molecule::prelude::*;
#[derive(Clone)]
pub struct Uint32(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Uint32 {
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 Uint32 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Uint32 {
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 Uint32 {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Uint32::new_unchecked(v)
}
}
impl Uint32 {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const TOTAL_SIZE: usize = 4;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 4;
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 raw_data(&self) -> molecule::bytes::Bytes {
self.as_bytes()
}
pub fn as_reader<'r>(&'r self) -> Uint32Reader<'r> {
Uint32Reader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Uint32 {
type Builder = Uint32Builder;
const NAME: &'static str = "Uint32";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Uint32(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<Self> {
Uint32Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
Uint32Reader::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()])
}
}
#[derive(Clone, Copy)]
pub struct Uint32Reader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for Uint32Reader<'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 Uint32Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for Uint32Reader<'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> Uint32Reader<'r> {
pub const TOTAL_SIZE: usize = 4;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 4;
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 raw_data(&self) -> &'r [u8] {
self.as_slice()
}
}
impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> {
type Entity = Uint32;
const NAME: &'static str = "Uint32Reader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
Uint32Reader(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 Uint32Builder(pub(crate) [Byte; 4]);
impl ::core::fmt::Debug for Uint32Builder {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:?})", Self::NAME, &self.0[..])
}
}
impl ::core::default::Default for Uint32Builder {
fn default() -> Self {
Uint32Builder([
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
])
}
}
impl Uint32Builder {
pub const TOTAL_SIZE: usize = 4;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 4;
pub fn set(mut self, v: [Byte; 4]) -> 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
}
}
impl molecule::prelude::Builder for Uint32Builder {
type Entity = Uint32;
const NAME: &'static str = "Uint32Builder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::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())?;
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));
Uint32::new_unchecked(inner.into())
}
}
#[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 = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Uint64::new_unchecked(v)
}
}
impl Uint64 {
const DEFAULT_VALUE: [u8; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
pub const TOTAL_SIZE: usize = 8;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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<Self> {
Uint64Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
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 TOTAL_SIZE: usize = 8;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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 TOTAL_SIZE: usize = 8;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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<W: molecule::io::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 Uint128(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Uint128 {
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 Uint128 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Uint128 {
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 Uint128 {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Uint128::new_unchecked(v)
}
}
impl Uint128 {
const DEFAULT_VALUE: [u8; 16] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const TOTAL_SIZE: usize = 16;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 16;
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 raw_data(&self) -> molecule::bytes::Bytes {
self.as_bytes()
}
pub fn as_reader<'r>(&'r self) -> Uint128Reader<'r> {
Uint128Reader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Uint128 {
type Builder = Uint128Builder;
const NAME: &'static str = "Uint128";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Uint128(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<Self> {
Uint128Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
Uint128Reader::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(),
])
}
}
#[derive(Clone, Copy)]
pub struct Uint128Reader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for Uint128Reader<'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 Uint128Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for Uint128Reader<'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> Uint128Reader<'r> {
pub const TOTAL_SIZE: usize = 16;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 16;
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 raw_data(&self) -> &'r [u8] {
self.as_slice()
}
}
impl<'r> molecule::prelude::Reader<'r> for Uint128Reader<'r> {
type Entity = Uint128;
const NAME: &'static str = "Uint128Reader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
Uint128Reader(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 Uint128Builder(pub(crate) [Byte; 16]);
impl ::core::fmt::Debug for Uint128Builder {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:?})", Self::NAME, &self.0[..])
}
}
impl ::core::default::Default for Uint128Builder {
fn default() -> Self {
Uint128Builder([
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 Uint128Builder {
pub const TOTAL_SIZE: usize = 16;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 16;
pub fn set(mut self, v: [Byte; 16]) -> 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
}
}
impl molecule::prelude::Builder for Uint128Builder {
type Entity = Uint128;
const NAME: &'static str = "Uint128Builder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::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())?;
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));
Uint128::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Byte32(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Byte32 {
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 Byte32 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Byte32 {
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 Byte32 {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Byte32::new_unchecked(v)
}
}
impl Byte32 {
const DEFAULT_VALUE: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
];
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Byte32 {
type Builder = Byte32Builder;
const NAME: &'static str = "Byte32";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Byte32(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<Self> {
Byte32Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
Byte32Reader::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 Byte32Reader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for Byte32Reader<'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 Byte32Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for Byte32Reader<'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> Byte32Reader<'r> {
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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 Byte32Reader<'r> {
type Entity = Byte32;
const NAME: &'static str = "Byte32Reader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
Byte32Reader(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 Byte32Builder(pub(crate) [Byte; 32]);
impl ::core::fmt::Debug for Byte32Builder {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:?})", Self::NAME, &self.0[..])
}
}
impl ::core::default::Default for Byte32Builder {
fn default() -> Self {
Byte32Builder([
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 Byte32Builder {
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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 Byte32Builder {
type Entity = Byte32;
const NAME: &'static str = "Byte32Builder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::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));
Byte32::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Uint256(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Uint256 {
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 Uint256 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Uint256 {
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 Uint256 {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Uint256::new_unchecked(v)
}
}
impl Uint256 {
const DEFAULT_VALUE: [u8; 32] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0,
];
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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) -> Uint256Reader<'r> {
Uint256Reader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Uint256 {
type Builder = Uint256Builder;
const NAME: &'static str = "Uint256";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Uint256(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<Self> {
Uint256Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
Uint256Reader::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 Uint256Reader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for Uint256Reader<'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 Uint256Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for Uint256Reader<'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> Uint256Reader<'r> {
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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 Uint256Reader<'r> {
type Entity = Uint256;
const NAME: &'static str = "Uint256Reader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
Uint256Reader(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 Uint256Builder(pub(crate) [Byte; 32]);
impl ::core::fmt::Debug for Uint256Builder {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:?})", Self::NAME, &self.0[..])
}
}
impl ::core::default::Default for Uint256Builder {
fn default() -> Self {
Uint256Builder([
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 Uint256Builder {
pub const TOTAL_SIZE: usize = 32;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: 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 Uint256Builder {
type Entity = Uint256;
const NAME: &'static str = "Uint256Builder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::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));
Uint256::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 {
use molecule::hex_string;
if f.alternate() {
write!(f, "0x")?;
}
write!(f, "{}", hex_string(self.as_slice()))
}
}
impl ::core::fmt::Debug for Bytes {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Bytes {
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 Bytes {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Bytes::new_unchecked(v)
}
}
impl Bytes {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const ITEM_SIZE: usize = 1;
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<Byte> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> Byte {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
Byte::new_unchecked(self.0.slice(start..end))
}
pub fn raw_data(&self) -> molecule::bytes::Bytes {
self.0.slice(molecule::NUMBER_SIZE..)
}
pub fn as_reader<'r>(&'r self) -> BytesReader<'r> {
BytesReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Bytes {
type Builder = BytesBuilder;
const NAME: &'static str = "Bytes";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Bytes(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<Self> {
BytesReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BytesReader::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 BytesReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BytesReader<'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 BytesReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BytesReader<'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> BytesReader<'r> {
pub const ITEM_SIZE: usize = 1;
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<ByteReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> ByteReader<'r> {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
ByteReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn raw_data(&self) -> &'r [u8] {
&self.as_slice()[molecule::NUMBER_SIZE..]
}
}
impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> {
type Entity = Bytes;
const NAME: &'static str = "BytesReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BytesReader(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 BytesBuilder(pub(crate) Vec<Byte>);
impl BytesBuilder {
pub const ITEM_SIZE: usize = 1;
pub fn set(mut self, v: Vec<Byte>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: Byte) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = Byte>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: Byte) -> Option<Byte> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for BytesBuilder {
type Entity = Bytes;
const NAME: &'static str = "BytesBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
}
fn write<W: molecule::io::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));
Bytes::new_unchecked(inner.into())
}
}
pub struct BytesIterator(Bytes, usize, usize);
impl ::core::iter::Iterator for BytesIterator {
type Item = Byte;
fn next(&mut self) -> Option<Self::Item> {
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 BytesIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for Bytes {
type Item = Byte;
type IntoIter = BytesIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
BytesIterator(self, 0, len)
}
}
#[derive(Clone)]
pub struct BytesOpt(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for BytesOpt {
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 BytesOpt {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for BytesOpt {
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 BytesOpt {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BytesOpt::new_unchecked(v)
}
}
impl BytesOpt {
const DEFAULT_VALUE: [u8; 0] = [];
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<Bytes> {
if self.is_none() {
None
} else {
Some(Bytes::new_unchecked(self.0.clone()))
}
}
pub fn as_reader<'r>(&'r self) -> BytesOptReader<'r> {
BytesOptReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for BytesOpt {
type Builder = BytesOptBuilder;
const NAME: &'static str = "BytesOpt";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
BytesOpt(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<Self> {
BytesOptReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BytesOptReader::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.to_opt())
}
}
#[derive(Clone, Copy)]
pub struct BytesOptReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BytesOptReader<'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 BytesOptReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BytesOptReader<'r> {
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<'r> BytesOptReader<'r> {
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<BytesReader<'r>> {
if self.is_none() {
None
} else {
Some(BytesReader::new_unchecked(self.as_slice()))
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BytesOptReader<'r> {
type Entity = BytesOpt;
const NAME: &'static str = "BytesOptReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BytesOptReader(slice)
}
fn as_slice(&self) -> &'r [u8] {
self.0
}
fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
if !slice.is_empty() {
BytesReader::verify(&slice[..], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BytesOptBuilder(pub(crate) Option<Bytes>);
impl BytesOptBuilder {
pub fn set(mut self, v: Option<Bytes>) -> Self {
self.0 = v;
self
}
}
impl molecule::prelude::Builder for BytesOptBuilder {
type Entity = BytesOpt;
const NAME: &'static str = "BytesOptBuilder";
fn expected_length(&self) -> usize {
self.0
.as_ref()
.map(|ref inner| inner.as_slice().len())
.unwrap_or(0)
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
self.0
.as_ref()
.map(|ref inner| writer.write_all(inner.as_slice()))
.unwrap_or(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));
BytesOpt::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct BytesOptVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for BytesOptVec {
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 BytesOptVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for BytesOptVec {
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 BytesOptVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BytesOptVec::new_unchecked(v)
}
}
impl BytesOptVec {
const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
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<BytesOpt> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> BytesOpt {
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 {
BytesOpt::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;
BytesOpt::new_unchecked(self.0.slice(start..end))
}
}
pub fn as_reader<'r>(&'r self) -> BytesOptVecReader<'r> {
BytesOptVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for BytesOptVec {
type Builder = BytesOptVecBuilder;
const NAME: &'static str = "BytesOptVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
BytesOptVec(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<Self> {
BytesOptVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BytesOptVecReader::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 BytesOptVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BytesOptVecReader<'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 BytesOptVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BytesOptVecReader<'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> BytesOptVecReader<'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<BytesOptReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> BytesOptReader<'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 {
BytesOptReader::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;
BytesOptReader::new_unchecked(&self.as_slice()[start..end])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BytesOptVecReader<'r> {
type Entity = BytesOptVec;
const NAME: &'static str = "BytesOptVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BytesOptVecReader(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<usize> = 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];
BytesOptReader::verify(&slice[start..end], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BytesOptVecBuilder(pub(crate) Vec<BytesOpt>);
impl BytesOptVecBuilder {
pub fn set(mut self, v: Vec<BytesOpt>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: BytesOpt) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = BytesOpt>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: BytesOpt) -> Option<BytesOpt> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for BytesOptVecBuilder {
type Entity = BytesOptVec;
const NAME: &'static str = "BytesOptVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (self.0.len() + 1)
+ self
.0
.iter()
.map(|inner| inner.as_slice().len())
.sum::<usize>()
}
fn write<W: molecule::io::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));
BytesOptVec::new_unchecked(inner.into())
}
}
pub struct BytesOptVecIterator(BytesOptVec, usize, usize);
impl ::core::iter::Iterator for BytesOptVecIterator {
type Item = BytesOpt;
fn next(&mut self) -> Option<Self::Item> {
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 BytesOptVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for BytesOptVec {
type Item = BytesOpt;
type IntoIter = BytesOptVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
BytesOptVecIterator(self, 0, len)
}
}
impl<'r> BytesOptVecReader<'r> {
pub fn iter<'t>(&'t self) -> BytesOptVecReaderIterator<'t, 'r> {
BytesOptVecReaderIterator(&self, 0, self.len())
}
}
pub struct BytesOptVecReaderIterator<'t, 'r>(&'t BytesOptVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for BytesOptVecReaderIterator<'t, 'r> {
type Item = BytesOptReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 BytesOptVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[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 = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BytesVec::new_unchecked(v)
}
}
impl BytesVec {
const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
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<Bytes> {
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<Self> {
BytesVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
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<BytesReader<'r>> {
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<usize> = 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<Bytes>);
impl BytesVecBuilder {
pub fn set(mut self, v: Vec<Bytes>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: Bytes) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = Bytes>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: Bytes) -> Option<Bytes> {
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::<usize>()
}
fn write<W: molecule::io::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<Self::Item> {
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 Item = Bytes;
type IntoIter = BytesVecIterator;
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<Self::Item> {
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 = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Byte32Vec::new_unchecked(v)
}
}
impl Byte32Vec {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
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<Byte32> {
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<Self> {
Byte32VecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
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<Byte32Reader<'r>> {
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<Byte32>);
impl Byte32VecBuilder {
pub const ITEM_SIZE: usize = 32;
pub fn set(mut self, v: Vec<Byte32>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: Byte32) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = Byte32>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: Byte32) -> Option<Byte32> {
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<W: molecule::io::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<Self::Item> {
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 Item = Byte32;
type IntoIter = Byte32VecIterator;
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<Self::Item> {
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 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 = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
ScriptOpt::new_unchecked(v)
}
}
impl ScriptOpt {
const DEFAULT_VALUE: [u8; 0] = [];
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<Script> {
if self.is_none() {
None
} else {
Some(Script::new_unchecked(self.0.clone()))
}
}
pub fn as_reader<'r>(&'r self) -> ScriptOptReader<'r> {
ScriptOptReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for ScriptOpt {
type Builder = ScriptOptBuilder;
const NAME: &'static str = "ScriptOpt";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
ScriptOpt(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<Self> {
ScriptOptReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
ScriptOptReader::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.to_opt())
}
}
#[derive(Clone, Copy)]
pub struct ScriptOptReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for ScriptOptReader<'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 ScriptOptReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for ScriptOptReader<'r> {
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<'r> ScriptOptReader<'r> {
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<ScriptReader<'r>> {
if self.is_none() {
None
} else {
Some(ScriptReader::new_unchecked(self.as_slice()))
}
}
}
impl<'r> molecule::prelude::Reader<'r> for ScriptOptReader<'r> {
type Entity = ScriptOpt;
const NAME: &'static str = "ScriptOptReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
ScriptOptReader(slice)
}
fn as_slice(&self) -> &'r [u8] {
self.0
}
fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
if !slice.is_empty() {
ScriptReader::verify(&slice[..], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct ScriptOptBuilder(pub(crate) Option<Script>);
impl ScriptOptBuilder {
pub fn set(mut self, v: Option<Script>) -> Self {
self.0 = v;
self
}
}
impl molecule::prelude::Builder for ScriptOptBuilder {
type Entity = ScriptOpt;
const NAME: &'static str = "ScriptOptBuilder";
fn expected_length(&self) -> usize {
self.0
.as_ref()
.map(|ref inner| inner.as_slice().len())
.unwrap_or(0)
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
self.0
.as_ref()
.map(|ref inner| writer.write_all(inner.as_slice()))
.unwrap_or(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));
ScriptOpt::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct ProposalShortId(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for ProposalShortId {
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 ProposalShortId {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for ProposalShortId {
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 ProposalShortId {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
ProposalShortId::new_unchecked(v)
}
}
impl ProposalShortId {
const DEFAULT_VALUE: [u8; 10] = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
pub const TOTAL_SIZE: usize = 10;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 10;
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 raw_data(&self) -> molecule::bytes::Bytes {
self.as_bytes()
}
pub fn as_reader<'r>(&'r self) -> ProposalShortIdReader<'r> {
ProposalShortIdReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for ProposalShortId {
type Builder = ProposalShortIdBuilder;
const NAME: &'static str = "ProposalShortId";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
ProposalShortId(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<Self> {
ProposalShortIdReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
ProposalShortIdReader::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(),
])
}
}
#[derive(Clone, Copy)]
pub struct ProposalShortIdReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for ProposalShortIdReader<'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 ProposalShortIdReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for ProposalShortIdReader<'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> ProposalShortIdReader<'r> {
pub const TOTAL_SIZE: usize = 10;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 10;
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 raw_data(&self) -> &'r [u8] {
self.as_slice()
}
}
impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdReader<'r> {
type Entity = ProposalShortId;
const NAME: &'static str = "ProposalShortIdReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
ProposalShortIdReader(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 ProposalShortIdBuilder(pub(crate) [Byte; 10]);
impl ::core::fmt::Debug for ProposalShortIdBuilder {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:?})", Self::NAME, &self.0[..])
}
}
impl ::core::default::Default for ProposalShortIdBuilder {
fn default() -> Self {
ProposalShortIdBuilder([
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
Byte::default(),
])
}
}
impl ProposalShortIdBuilder {
pub const TOTAL_SIZE: usize = 10;
pub const ITEM_SIZE: usize = 1;
pub const ITEM_COUNT: usize = 10;
pub fn set(mut self, v: [Byte; 10]) -> 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
}
}
impl molecule::prelude::Builder for ProposalShortIdBuilder {
type Entity = ProposalShortId;
const NAME: &'static str = "ProposalShortIdBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::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())?;
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));
ProposalShortId::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct UncleBlockVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for UncleBlockVec {
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 UncleBlockVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for UncleBlockVec {
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 UncleBlockVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
UncleBlockVec::new_unchecked(v)
}
}
impl UncleBlockVec {
const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
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<UncleBlock> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> UncleBlock {
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 {
UncleBlock::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;
UncleBlock::new_unchecked(self.0.slice(start..end))
}
}
pub fn as_reader<'r>(&'r self) -> UncleBlockVecReader<'r> {
UncleBlockVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for UncleBlockVec {
type Builder = UncleBlockVecBuilder;
const NAME: &'static str = "UncleBlockVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
UncleBlockVec(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<Self> {
UncleBlockVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
UncleBlockVecReader::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 UncleBlockVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for UncleBlockVecReader<'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 UncleBlockVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for UncleBlockVecReader<'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> UncleBlockVecReader<'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<UncleBlockReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> UncleBlockReader<'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 {
UncleBlockReader::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;
UncleBlockReader::new_unchecked(&self.as_slice()[start..end])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for UncleBlockVecReader<'r> {
type Entity = UncleBlockVec;
const NAME: &'static str = "UncleBlockVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
UncleBlockVecReader(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<usize> = 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];
UncleBlockReader::verify(&slice[start..end], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct UncleBlockVecBuilder(pub(crate) Vec<UncleBlock>);
impl UncleBlockVecBuilder {
pub fn set(mut self, v: Vec<UncleBlock>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: UncleBlock) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = UncleBlock>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: UncleBlock) -> Option<UncleBlock> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for UncleBlockVecBuilder {
type Entity = UncleBlockVec;
const NAME: &'static str = "UncleBlockVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (self.0.len() + 1)
+ self
.0
.iter()
.map(|inner| inner.as_slice().len())
.sum::<usize>()
}
fn write<W: molecule::io::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));
UncleBlockVec::new_unchecked(inner.into())
}
}
pub struct UncleBlockVecIterator(UncleBlockVec, usize, usize);
impl ::core::iter::Iterator for UncleBlockVecIterator {
type Item = UncleBlock;
fn next(&mut self) -> Option<Self::Item> {
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 UncleBlockVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for UncleBlockVec {
type Item = UncleBlock;
type IntoIter = UncleBlockVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
UncleBlockVecIterator(self, 0, len)
}
}
impl<'r> UncleBlockVecReader<'r> {
pub fn iter<'t>(&'t self) -> UncleBlockVecReaderIterator<'t, 'r> {
UncleBlockVecReaderIterator(&self, 0, self.len())
}
}
pub struct UncleBlockVecReaderIterator<'t, 'r>(&'t UncleBlockVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for UncleBlockVecReaderIterator<'t, 'r> {
type Item = UncleBlockReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 UncleBlockVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct TransactionVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for TransactionVec {
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 TransactionVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for TransactionVec {
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 TransactionVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
TransactionVec::new_unchecked(v)
}
}
impl TransactionVec {
const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
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<Transaction> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> Transaction {
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 {
Transaction::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;
Transaction::new_unchecked(self.0.slice(start..end))
}
}
pub fn as_reader<'r>(&'r self) -> TransactionVecReader<'r> {
TransactionVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for TransactionVec {
type Builder = TransactionVecBuilder;
const NAME: &'static str = "TransactionVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
TransactionVec(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<Self> {
TransactionVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
TransactionVecReader::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 TransactionVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for TransactionVecReader<'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 TransactionVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for TransactionVecReader<'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> TransactionVecReader<'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<TransactionReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> TransactionReader<'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 {
TransactionReader::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;
TransactionReader::new_unchecked(&self.as_slice()[start..end])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for TransactionVecReader<'r> {
type Entity = TransactionVec;
const NAME: &'static str = "TransactionVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
TransactionVecReader(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<usize> = 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];
TransactionReader::verify(&slice[start..end], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct TransactionVecBuilder(pub(crate) Vec<Transaction>);
impl TransactionVecBuilder {
pub fn set(mut self, v: Vec<Transaction>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: Transaction) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = Transaction>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: Transaction) -> Option<Transaction> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for TransactionVecBuilder {
type Entity = TransactionVec;
const NAME: &'static str = "TransactionVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (self.0.len() + 1)
+ self
.0
.iter()
.map(|inner| inner.as_slice().len())
.sum::<usize>()
}
fn write<W: molecule::io::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));
TransactionVec::new_unchecked(inner.into())
}
}
pub struct TransactionVecIterator(TransactionVec, usize, usize);
impl ::core::iter::Iterator for TransactionVecIterator {
type Item = Transaction;
fn next(&mut self) -> Option<Self::Item> {
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 TransactionVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for TransactionVec {
type Item = Transaction;
type IntoIter = TransactionVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
TransactionVecIterator(self, 0, len)
}
}
impl<'r> TransactionVecReader<'r> {
pub fn iter<'t>(&'t self) -> TransactionVecReaderIterator<'t, 'r> {
TransactionVecReaderIterator(&self, 0, self.len())
}
}
pub struct TransactionVecReaderIterator<'t, 'r>(&'t TransactionVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for TransactionVecReaderIterator<'t, 'r> {
type Item = TransactionReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 TransactionVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct ProposalShortIdVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for ProposalShortIdVec {
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 ProposalShortIdVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for ProposalShortIdVec {
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 ProposalShortIdVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
ProposalShortIdVec::new_unchecked(v)
}
}
impl ProposalShortIdVec {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const ITEM_SIZE: usize = 10;
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<ProposalShortId> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> ProposalShortId {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
ProposalShortId::new_unchecked(self.0.slice(start..end))
}
pub fn as_reader<'r>(&'r self) -> ProposalShortIdVecReader<'r> {
ProposalShortIdVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for ProposalShortIdVec {
type Builder = ProposalShortIdVecBuilder;
const NAME: &'static str = "ProposalShortIdVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
ProposalShortIdVec(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<Self> {
ProposalShortIdVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
ProposalShortIdVecReader::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 ProposalShortIdVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for ProposalShortIdVecReader<'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 ProposalShortIdVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for ProposalShortIdVecReader<'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> ProposalShortIdVecReader<'r> {
pub const ITEM_SIZE: usize = 10;
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<ProposalShortIdReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> ProposalShortIdReader<'r> {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
ProposalShortIdReader::new_unchecked(&self.as_slice()[start..end])
}
}
impl<'r> molecule::prelude::Reader<'r> for ProposalShortIdVecReader<'r> {
type Entity = ProposalShortIdVec;
const NAME: &'static str = "ProposalShortIdVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
ProposalShortIdVecReader(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 ProposalShortIdVecBuilder(pub(crate) Vec<ProposalShortId>);
impl ProposalShortIdVecBuilder {
pub const ITEM_SIZE: usize = 10;
pub fn set(mut self, v: Vec<ProposalShortId>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: ProposalShortId) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = ProposalShortId>>(
mut self,
iter: T,
) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: ProposalShortId) -> Option<ProposalShortId> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for ProposalShortIdVecBuilder {
type Entity = ProposalShortIdVec;
const NAME: &'static str = "ProposalShortIdVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
}
fn write<W: molecule::io::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));
ProposalShortIdVec::new_unchecked(inner.into())
}
}
pub struct ProposalShortIdVecIterator(ProposalShortIdVec, usize, usize);
impl ::core::iter::Iterator for ProposalShortIdVecIterator {
type Item = ProposalShortId;
fn next(&mut self) -> Option<Self::Item> {
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 ProposalShortIdVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for ProposalShortIdVec {
type Item = ProposalShortId;
type IntoIter = ProposalShortIdVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
ProposalShortIdVecIterator(self, 0, len)
}
}
impl<'r> ProposalShortIdVecReader<'r> {
pub fn iter<'t>(&'t self) -> ProposalShortIdVecReaderIterator<'t, 'r> {
ProposalShortIdVecReaderIterator(&self, 0, self.len())
}
}
pub struct ProposalShortIdVecReaderIterator<'t, 'r>(&'t ProposalShortIdVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for ProposalShortIdVecReaderIterator<'t, 'r> {
type Item = ProposalShortIdReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 ProposalShortIdVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct CellDepVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellDepVec {
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 CellDepVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellDepVec {
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 CellDepVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellDepVec::new_unchecked(v)
}
}
impl CellDepVec {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const ITEM_SIZE: usize = 37;
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<CellDep> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellDep {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
CellDep::new_unchecked(self.0.slice(start..end))
}
pub fn as_reader<'r>(&'r self) -> CellDepVecReader<'r> {
CellDepVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellDepVec {
type Builder = CellDepVecBuilder;
const NAME: &'static str = "CellDepVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellDepVec(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<Self> {
CellDepVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellDepVecReader::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 CellDepVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellDepVecReader<'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 CellDepVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellDepVecReader<'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> CellDepVecReader<'r> {
pub const ITEM_SIZE: usize = 37;
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<CellDepReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellDepReader<'r> {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
CellDepReader::new_unchecked(&self.as_slice()[start..end])
}
}
impl<'r> molecule::prelude::Reader<'r> for CellDepVecReader<'r> {
type Entity = CellDepVec;
const NAME: &'static str = "CellDepVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellDepVecReader(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 CellDepVecBuilder(pub(crate) Vec<CellDep>);
impl CellDepVecBuilder {
pub const ITEM_SIZE: usize = 37;
pub fn set(mut self, v: Vec<CellDep>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: CellDep) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = CellDep>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: CellDep) -> Option<CellDep> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for CellDepVecBuilder {
type Entity = CellDepVec;
const NAME: &'static str = "CellDepVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
}
fn write<W: molecule::io::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));
CellDepVec::new_unchecked(inner.into())
}
}
pub struct CellDepVecIterator(CellDepVec, usize, usize);
impl ::core::iter::Iterator for CellDepVecIterator {
type Item = CellDep;
fn next(&mut self) -> Option<Self::Item> {
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 CellDepVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for CellDepVec {
type Item = CellDep;
type IntoIter = CellDepVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
CellDepVecIterator(self, 0, len)
}
}
impl<'r> CellDepVecReader<'r> {
pub fn iter<'t>(&'t self) -> CellDepVecReaderIterator<'t, 'r> {
CellDepVecReaderIterator(&self, 0, self.len())
}
}
pub struct CellDepVecReaderIterator<'t, 'r>(&'t CellDepVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for CellDepVecReaderIterator<'t, 'r> {
type Item = CellDepReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 CellDepVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct CellInputVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellInputVec {
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 CellInputVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellInputVec {
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 CellInputVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellInputVec::new_unchecked(v)
}
}
impl CellInputVec {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const ITEM_SIZE: usize = 44;
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<CellInput> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellInput {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
CellInput::new_unchecked(self.0.slice(start..end))
}
pub fn as_reader<'r>(&'r self) -> CellInputVecReader<'r> {
CellInputVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellInputVec {
type Builder = CellInputVecBuilder;
const NAME: &'static str = "CellInputVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellInputVec(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<Self> {
CellInputVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellInputVecReader::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 CellInputVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellInputVecReader<'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 CellInputVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellInputVecReader<'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> CellInputVecReader<'r> {
pub const ITEM_SIZE: usize = 44;
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<CellInputReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellInputReader<'r> {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
CellInputReader::new_unchecked(&self.as_slice()[start..end])
}
}
impl<'r> molecule::prelude::Reader<'r> for CellInputVecReader<'r> {
type Entity = CellInputVec;
const NAME: &'static str = "CellInputVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellInputVecReader(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 CellInputVecBuilder(pub(crate) Vec<CellInput>);
impl CellInputVecBuilder {
pub const ITEM_SIZE: usize = 44;
pub fn set(mut self, v: Vec<CellInput>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: CellInput) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = CellInput>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: CellInput) -> Option<CellInput> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for CellInputVecBuilder {
type Entity = CellInputVec;
const NAME: &'static str = "CellInputVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.0.len()
}
fn write<W: molecule::io::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));
CellInputVec::new_unchecked(inner.into())
}
}
pub struct CellInputVecIterator(CellInputVec, usize, usize);
impl ::core::iter::Iterator for CellInputVecIterator {
type Item = CellInput;
fn next(&mut self) -> Option<Self::Item> {
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 CellInputVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for CellInputVec {
type Item = CellInput;
type IntoIter = CellInputVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
CellInputVecIterator(self, 0, len)
}
}
impl<'r> CellInputVecReader<'r> {
pub fn iter<'t>(&'t self) -> CellInputVecReaderIterator<'t, 'r> {
CellInputVecReaderIterator(&self, 0, self.len())
}
}
pub struct CellInputVecReaderIterator<'t, 'r>(&'t CellInputVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for CellInputVecReaderIterator<'t, 'r> {
type Item = CellInputReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 CellInputVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct CellOutputVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellOutputVec {
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 CellOutputVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellOutputVec {
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 CellOutputVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellOutputVec::new_unchecked(v)
}
}
impl CellOutputVec {
const DEFAULT_VALUE: [u8; 4] = [4, 0, 0, 0];
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<CellOutput> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellOutput {
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 {
CellOutput::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;
CellOutput::new_unchecked(self.0.slice(start..end))
}
}
pub fn as_reader<'r>(&'r self) -> CellOutputVecReader<'r> {
CellOutputVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellOutputVec {
type Builder = CellOutputVecBuilder;
const NAME: &'static str = "CellOutputVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellOutputVec(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<Self> {
CellOutputVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellOutputVecReader::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 CellOutputVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellOutputVecReader<'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 CellOutputVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellOutputVecReader<'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> CellOutputVecReader<'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<CellOutputReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> CellOutputReader<'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 {
CellOutputReader::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;
CellOutputReader::new_unchecked(&self.as_slice()[start..end])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for CellOutputVecReader<'r> {
type Entity = CellOutputVec;
const NAME: &'static str = "CellOutputVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellOutputVecReader(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<usize> = 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];
CellOutputReader::verify(&slice[start..end], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct CellOutputVecBuilder(pub(crate) Vec<CellOutput>);
impl CellOutputVecBuilder {
pub fn set(mut self, v: Vec<CellOutput>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: CellOutput) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = CellOutput>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: CellOutput) -> Option<CellOutput> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for CellOutputVecBuilder {
type Entity = CellOutputVec;
const NAME: &'static str = "CellOutputVecBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (self.0.len() + 1)
+ self
.0
.iter()
.map(|inner| inner.as_slice().len())
.sum::<usize>()
}
fn write<W: molecule::io::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));
CellOutputVec::new_unchecked(inner.into())
}
}
pub struct CellOutputVecIterator(CellOutputVec, usize, usize);
impl ::core::iter::Iterator for CellOutputVecIterator {
type Item = CellOutput;
fn next(&mut self) -> Option<Self::Item> {
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 CellOutputVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for CellOutputVec {
type Item = CellOutput;
type IntoIter = CellOutputVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
CellOutputVecIterator(self, 0, len)
}
}
impl<'r> CellOutputVecReader<'r> {
pub fn iter<'t>(&'t self) -> CellOutputVecReaderIterator<'t, 'r> {
CellOutputVecReaderIterator(&self, 0, self.len())
}
}
pub struct CellOutputVecReaderIterator<'t, 'r>(&'t CellOutputVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for CellOutputVecReaderIterator<'t, 'r> {
type Item = CellOutputReader<'t>;
fn next(&mut self) -> Option<Self::Item> {
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 CellOutputVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct Script(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Script {
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 Script {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Script {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "code_hash", self.code_hash())?;
write!(f, ", {}: {}", "hash_type", self.hash_type())?;
write!(f, ", {}: {}", "args", self.args())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for Script {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Script::new_unchecked(v)
}
}
impl Script {
const DEFAULT_VALUE: [u8; 53] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn code_hash(&self) -> Byte32 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Byte32::new_unchecked(self.0.slice(start..end))
}
pub fn hash_type(&self) -> Byte {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Byte::new_unchecked(self.0.slice(start..end))
}
pub fn args(&self) -> Bytes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
Bytes::new_unchecked(self.0.slice(start..end))
} else {
Bytes::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> ScriptReader<'r> {
ScriptReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Script {
type Builder = ScriptBuilder;
const NAME: &'static str = "Script";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Script(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<Self> {
ScriptReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
ScriptReader::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()
.code_hash(self.code_hash())
.hash_type(self.hash_type())
.args(self.args())
}
}
#[derive(Clone, Copy)]
pub struct ScriptReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for ScriptReader<'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 ScriptReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for ScriptReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "code_hash", self.code_hash())?;
write!(f, ", {}: {}", "hash_type", self.hash_type())?;
write!(f, ", {}: {}", "args", self.args())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> ScriptReader<'r> {
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn code_hash(&self) -> Byte32Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Byte32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn hash_type(&self) -> ByteReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
ByteReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn args(&self) -> BytesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
BytesReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for ScriptReader<'r> {
type Entity = Script;
const NAME: &'static str = "ScriptReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
ScriptReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
Byte32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
ByteReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct ScriptBuilder {
pub(crate) code_hash: Byte32,
pub(crate) hash_type: Byte,
pub(crate) args: Bytes,
}
impl ScriptBuilder {
pub const FIELD_COUNT: usize = 3;
pub fn code_hash(mut self, v: Byte32) -> Self {
self.code_hash = v;
self
}
pub fn hash_type(mut self, v: Byte) -> Self {
self.hash_type = v;
self
}
pub fn args(mut self, v: Bytes) -> Self {
self.args = v;
self
}
}
impl molecule::prelude::Builder for ScriptBuilder {
type Entity = Script;
const NAME: &'static str = "ScriptBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.code_hash.as_slice().len()
+ self.hash_type.as_slice().len()
+ self.args.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.code_hash.as_slice().len();
offsets.push(total_size);
total_size += self.hash_type.as_slice().len();
offsets.push(total_size);
total_size += self.args.as_slice().len();
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))?;
}
writer.write_all(self.code_hash.as_slice())?;
writer.write_all(self.hash_type.as_slice())?;
writer.write_all(self.args.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));
Script::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct OutPoint(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for OutPoint {
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 OutPoint {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for OutPoint {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
write!(f, ", {}: {}", "index", self.index())?;
write!(f, " }}")
}
}
impl ::core::default::Default for OutPoint {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
OutPoint::new_unchecked(v)
}
}
impl OutPoint {
const DEFAULT_VALUE: [u8; 36] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 36;
pub const FIELD_SIZES: [usize; 2] = [32, 4];
pub const FIELD_COUNT: usize = 2;
pub fn tx_hash(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(0..32))
}
pub fn index(&self) -> Uint32 {
Uint32::new_unchecked(self.0.slice(32..36))
}
pub fn as_reader<'r>(&'r self) -> OutPointReader<'r> {
OutPointReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for OutPoint {
type Builder = OutPointBuilder;
const NAME: &'static str = "OutPoint";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
OutPoint(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<Self> {
OutPointReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
OutPointReader::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()
.tx_hash(self.tx_hash())
.index(self.index())
}
}
#[derive(Clone, Copy)]
pub struct OutPointReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for OutPointReader<'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 OutPointReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for OutPointReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "tx_hash", self.tx_hash())?;
write!(f, ", {}: {}", "index", self.index())?;
write!(f, " }}")
}
}
impl<'r> OutPointReader<'r> {
pub const TOTAL_SIZE: usize = 36;
pub const FIELD_SIZES: [usize; 2] = [32, 4];
pub const FIELD_COUNT: usize = 2;
pub fn tx_hash(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[0..32])
}
pub fn index(&self) -> Uint32Reader<'r> {
Uint32Reader::new_unchecked(&self.as_slice()[32..36])
}
}
impl<'r> molecule::prelude::Reader<'r> for OutPointReader<'r> {
type Entity = OutPoint;
const NAME: &'static str = "OutPointReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
OutPointReader(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(())
}
}
#[derive(Debug, Default)]
pub struct OutPointBuilder {
pub(crate) tx_hash: Byte32,
pub(crate) index: Uint32,
}
impl OutPointBuilder {
pub const TOTAL_SIZE: usize = 36;
pub const FIELD_SIZES: [usize; 2] = [32, 4];
pub const FIELD_COUNT: usize = 2;
pub fn tx_hash(mut self, v: Byte32) -> Self {
self.tx_hash = v;
self
}
pub fn index(mut self, v: Uint32) -> Self {
self.index = v;
self
}
}
impl molecule::prelude::Builder for OutPointBuilder {
type Entity = OutPoint;
const NAME: &'static str = "OutPointBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
writer.write_all(self.tx_hash.as_slice())?;
writer.write_all(self.index.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));
OutPoint::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct CellInput(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellInput {
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 CellInput {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellInput {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "since", self.since())?;
write!(f, ", {}: {}", "previous_output", self.previous_output())?;
write!(f, " }}")
}
}
impl ::core::default::Default for CellInput {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellInput::new_unchecked(v)
}
}
impl CellInput {
const DEFAULT_VALUE: [u8; 44] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 44;
pub const FIELD_SIZES: [usize; 2] = [8, 36];
pub const FIELD_COUNT: usize = 2;
pub fn since(&self) -> Uint64 {
Uint64::new_unchecked(self.0.slice(0..8))
}
pub fn previous_output(&self) -> OutPoint {
OutPoint::new_unchecked(self.0.slice(8..44))
}
pub fn as_reader<'r>(&'r self) -> CellInputReader<'r> {
CellInputReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellInput {
type Builder = CellInputBuilder;
const NAME: &'static str = "CellInput";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellInput(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<Self> {
CellInputReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellInputReader::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()
.since(self.since())
.previous_output(self.previous_output())
}
}
#[derive(Clone, Copy)]
pub struct CellInputReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellInputReader<'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 CellInputReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellInputReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "since", self.since())?;
write!(f, ", {}: {}", "previous_output", self.previous_output())?;
write!(f, " }}")
}
}
impl<'r> CellInputReader<'r> {
pub const TOTAL_SIZE: usize = 44;
pub const FIELD_SIZES: [usize; 2] = [8, 36];
pub const FIELD_COUNT: usize = 2;
pub fn since(&self) -> Uint64Reader<'r> {
Uint64Reader::new_unchecked(&self.as_slice()[0..8])
}
pub fn previous_output(&self) -> OutPointReader<'r> {
OutPointReader::new_unchecked(&self.as_slice()[8..44])
}
}
impl<'r> molecule::prelude::Reader<'r> for CellInputReader<'r> {
type Entity = CellInput;
const NAME: &'static str = "CellInputReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellInputReader(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(())
}
}
#[derive(Debug, Default)]
pub struct CellInputBuilder {
pub(crate) since: Uint64,
pub(crate) previous_output: OutPoint,
}
impl CellInputBuilder {
pub const TOTAL_SIZE: usize = 44;
pub const FIELD_SIZES: [usize; 2] = [8, 36];
pub const FIELD_COUNT: usize = 2;
pub fn since(mut self, v: Uint64) -> Self {
self.since = v;
self
}
pub fn previous_output(mut self, v: OutPoint) -> Self {
self.previous_output = v;
self
}
}
impl molecule::prelude::Builder for CellInputBuilder {
type Entity = CellInput;
const NAME: &'static str = "CellInputBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
writer.write_all(self.since.as_slice())?;
writer.write_all(self.previous_output.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));
CellInput::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct CellOutput(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellOutput {
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 CellOutput {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellOutput {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "capacity", self.capacity())?;
write!(f, ", {}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "type_", self.type_())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for CellOutput {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellOutput::new_unchecked(v)
}
}
impl CellOutput {
const DEFAULT_VALUE: [u8; 77] = [
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,
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn capacity(&self) -> Uint64 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Uint64::new_unchecked(self.0.slice(start..end))
}
pub fn lock(&self) -> Script {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Script::new_unchecked(self.0.slice(start..end))
}
pub fn type_(&self) -> ScriptOpt {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
ScriptOpt::new_unchecked(self.0.slice(start..end))
} else {
ScriptOpt::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> CellOutputReader<'r> {
CellOutputReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellOutput {
type Builder = CellOutputBuilder;
const NAME: &'static str = "CellOutput";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellOutput(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<Self> {
CellOutputReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellOutputReader::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()
.capacity(self.capacity())
.lock(self.lock())
.type_(self.type_())
}
}
#[derive(Clone, Copy)]
pub struct CellOutputReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellOutputReader<'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 CellOutputReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellOutputReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "capacity", self.capacity())?;
write!(f, ", {}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "type_", self.type_())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> CellOutputReader<'r> {
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn capacity(&self) -> Uint64Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Uint64Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn lock(&self) -> ScriptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
ScriptReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn type_(&self) -> ScriptOptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
ScriptOptReader::new_unchecked(&self.as_slice()[start..end])
} else {
ScriptOptReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for CellOutputReader<'r> {
type Entity = CellOutput;
const NAME: &'static str = "CellOutputReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellOutputReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
Uint64Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
ScriptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
ScriptOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct CellOutputBuilder {
pub(crate) capacity: Uint64,
pub(crate) lock: Script,
pub(crate) type_: ScriptOpt,
}
impl CellOutputBuilder {
pub const FIELD_COUNT: usize = 3;
pub fn capacity(mut self, v: Uint64) -> Self {
self.capacity = v;
self
}
pub fn lock(mut self, v: Script) -> Self {
self.lock = v;
self
}
pub fn type_(mut self, v: ScriptOpt) -> Self {
self.type_ = v;
self
}
}
impl molecule::prelude::Builder for CellOutputBuilder {
type Entity = CellOutput;
const NAME: &'static str = "CellOutputBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.capacity.as_slice().len()
+ self.lock.as_slice().len()
+ self.type_.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.capacity.as_slice().len();
offsets.push(total_size);
total_size += self.lock.as_slice().len();
offsets.push(total_size);
total_size += self.type_.as_slice().len();
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))?;
}
writer.write_all(self.capacity.as_slice())?;
writer.write_all(self.lock.as_slice())?;
writer.write_all(self.type_.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));
CellOutput::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct CellDep(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellDep {
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 CellDep {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellDep {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "out_point", self.out_point())?;
write!(f, ", {}: {}", "dep_type", self.dep_type())?;
write!(f, " }}")
}
}
impl ::core::default::Default for CellDep {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellDep::new_unchecked(v)
}
}
impl CellDep {
const DEFAULT_VALUE: [u8; 37] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 37;
pub const FIELD_SIZES: [usize; 2] = [36, 1];
pub const FIELD_COUNT: usize = 2;
pub fn out_point(&self) -> OutPoint {
OutPoint::new_unchecked(self.0.slice(0..36))
}
pub fn dep_type(&self) -> Byte {
Byte::new_unchecked(self.0.slice(36..37))
}
pub fn as_reader<'r>(&'r self) -> CellDepReader<'r> {
CellDepReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellDep {
type Builder = CellDepBuilder;
const NAME: &'static str = "CellDep";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellDep(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<Self> {
CellDepReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellDepReader::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()
.out_point(self.out_point())
.dep_type(self.dep_type())
}
}
#[derive(Clone, Copy)]
pub struct CellDepReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellDepReader<'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 CellDepReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellDepReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "out_point", self.out_point())?;
write!(f, ", {}: {}", "dep_type", self.dep_type())?;
write!(f, " }}")
}
}
impl<'r> CellDepReader<'r> {
pub const TOTAL_SIZE: usize = 37;
pub const FIELD_SIZES: [usize; 2] = [36, 1];
pub const FIELD_COUNT: usize = 2;
pub fn out_point(&self) -> OutPointReader<'r> {
OutPointReader::new_unchecked(&self.as_slice()[0..36])
}
pub fn dep_type(&self) -> ByteReader<'r> {
ByteReader::new_unchecked(&self.as_slice()[36..37])
}
}
impl<'r> molecule::prelude::Reader<'r> for CellDepReader<'r> {
type Entity = CellDep;
const NAME: &'static str = "CellDepReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellDepReader(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(())
}
}
#[derive(Debug, Default)]
pub struct CellDepBuilder {
pub(crate) out_point: OutPoint,
pub(crate) dep_type: Byte,
}
impl CellDepBuilder {
pub const TOTAL_SIZE: usize = 37;
pub const FIELD_SIZES: [usize; 2] = [36, 1];
pub const FIELD_COUNT: usize = 2;
pub fn out_point(mut self, v: OutPoint) -> Self {
self.out_point = v;
self
}
pub fn dep_type(mut self, v: Byte) -> Self {
self.dep_type = v;
self
}
}
impl molecule::prelude::Builder for CellDepBuilder {
type Entity = CellDep;
const NAME: &'static str = "CellDepBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
writer.write_all(self.out_point.as_slice())?;
writer.write_all(self.dep_type.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));
CellDep::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct RawTransaction(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for RawTransaction {
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 RawTransaction {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for RawTransaction {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "version", self.version())?;
write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
write!(f, ", {}: {}", "header_deps", self.header_deps())?;
write!(f, ", {}: {}", "inputs", self.inputs())?;
write!(f, ", {}: {}", "outputs", self.outputs())?;
write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for RawTransaction {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
RawTransaction::new_unchecked(v)
}
}
impl RawTransaction {
const DEFAULT_VALUE: [u8; 52] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 6;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn version(&self) -> Uint32 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Uint32::new_unchecked(self.0.slice(start..end))
}
pub fn cell_deps(&self) -> CellDepVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
CellDepVec::new_unchecked(self.0.slice(start..end))
}
pub fn header_deps(&self) -> Byte32Vec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
Byte32Vec::new_unchecked(self.0.slice(start..end))
}
pub fn inputs(&self) -> CellInputVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
CellInputVec::new_unchecked(self.0.slice(start..end))
}
pub fn outputs(&self) -> CellOutputVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
let end = molecule::unpack_number(&slice[24..]) as usize;
CellOutputVec::new_unchecked(self.0.slice(start..end))
}
pub fn outputs_data(&self) -> BytesVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[24..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[28..]) as usize;
BytesVec::new_unchecked(self.0.slice(start..end))
} else {
BytesVec::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> RawTransactionReader<'r> {
RawTransactionReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for RawTransaction {
type Builder = RawTransactionBuilder;
const NAME: &'static str = "RawTransaction";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
RawTransaction(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<Self> {
RawTransactionReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
RawTransactionReader::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()
.version(self.version())
.cell_deps(self.cell_deps())
.header_deps(self.header_deps())
.inputs(self.inputs())
.outputs(self.outputs())
.outputs_data(self.outputs_data())
}
}
#[derive(Clone, Copy)]
pub struct RawTransactionReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for RawTransactionReader<'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 RawTransactionReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for RawTransactionReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "version", self.version())?;
write!(f, ", {}: {}", "cell_deps", self.cell_deps())?;
write!(f, ", {}: {}", "header_deps", self.header_deps())?;
write!(f, ", {}: {}", "inputs", self.inputs())?;
write!(f, ", {}: {}", "outputs", self.outputs())?;
write!(f, ", {}: {}", "outputs_data", self.outputs_data())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> RawTransactionReader<'r> {
pub const FIELD_COUNT: usize = 6;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn version(&self) -> Uint32Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Uint32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn cell_deps(&self) -> CellDepVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
CellDepVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn header_deps(&self) -> Byte32VecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
Byte32VecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn inputs(&self) -> CellInputVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
CellInputVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn outputs(&self) -> CellOutputVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
let end = molecule::unpack_number(&slice[24..]) as usize;
CellOutputVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn outputs_data(&self) -> BytesVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[24..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[28..]) as usize;
BytesVecReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesVecReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for RawTransactionReader<'r> {
type Entity = RawTransaction;
const NAME: &'static str = "RawTransactionReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
RawTransactionReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
Uint32Reader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
CellDepVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
Byte32VecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
CellInputVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
CellOutputVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
BytesVecReader::verify(&slice[offsets[5]..offsets[6]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct RawTransactionBuilder {
pub(crate) version: Uint32,
pub(crate) cell_deps: CellDepVec,
pub(crate) header_deps: Byte32Vec,
pub(crate) inputs: CellInputVec,
pub(crate) outputs: CellOutputVec,
pub(crate) outputs_data: BytesVec,
}
impl RawTransactionBuilder {
pub const FIELD_COUNT: usize = 6;
pub fn version(mut self, v: Uint32) -> Self {
self.version = v;
self
}
pub fn cell_deps(mut self, v: CellDepVec) -> Self {
self.cell_deps = v;
self
}
pub fn header_deps(mut self, v: Byte32Vec) -> Self {
self.header_deps = v;
self
}
pub fn inputs(mut self, v: CellInputVec) -> Self {
self.inputs = v;
self
}
pub fn outputs(mut self, v: CellOutputVec) -> Self {
self.outputs = v;
self
}
pub fn outputs_data(mut self, v: BytesVec) -> Self {
self.outputs_data = v;
self
}
}
impl molecule::prelude::Builder for RawTransactionBuilder {
type Entity = RawTransaction;
const NAME: &'static str = "RawTransactionBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.version.as_slice().len()
+ self.cell_deps.as_slice().len()
+ self.header_deps.as_slice().len()
+ self.inputs.as_slice().len()
+ self.outputs.as_slice().len()
+ self.outputs_data.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.version.as_slice().len();
offsets.push(total_size);
total_size += self.cell_deps.as_slice().len();
offsets.push(total_size);
total_size += self.header_deps.as_slice().len();
offsets.push(total_size);
total_size += self.inputs.as_slice().len();
offsets.push(total_size);
total_size += self.outputs.as_slice().len();
offsets.push(total_size);
total_size += self.outputs_data.as_slice().len();
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))?;
}
writer.write_all(self.version.as_slice())?;
writer.write_all(self.cell_deps.as_slice())?;
writer.write_all(self.header_deps.as_slice())?;
writer.write_all(self.inputs.as_slice())?;
writer.write_all(self.outputs.as_slice())?;
writer.write_all(self.outputs_data.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));
RawTransaction::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Transaction(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Transaction {
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 Transaction {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Transaction {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "raw", self.raw())?;
write!(f, ", {}: {}", "witnesses", self.witnesses())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for Transaction {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Transaction::new_unchecked(v)
}
}
impl Transaction {
const DEFAULT_VALUE: [u8; 68] = [
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,
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,
0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn raw(&self) -> RawTransaction {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
RawTransaction::new_unchecked(self.0.slice(start..end))
}
pub fn witnesses(&self) -> BytesVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
BytesVec::new_unchecked(self.0.slice(start..end))
} else {
BytesVec::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> TransactionReader<'r> {
TransactionReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Transaction {
type Builder = TransactionBuilder;
const NAME: &'static str = "Transaction";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Transaction(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<Self> {
TransactionReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
TransactionReader::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()
.raw(self.raw())
.witnesses(self.witnesses())
}
}
#[derive(Clone, Copy)]
pub struct TransactionReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for TransactionReader<'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 TransactionReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for TransactionReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "raw", self.raw())?;
write!(f, ", {}: {}", "witnesses", self.witnesses())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> TransactionReader<'r> {
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn raw(&self) -> RawTransactionReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
RawTransactionReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn witnesses(&self) -> BytesVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
BytesVecReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesVecReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for TransactionReader<'r> {
type Entity = Transaction;
const NAME: &'static str = "TransactionReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
TransactionReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
RawTransactionReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
BytesVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct TransactionBuilder {
pub(crate) raw: RawTransaction,
pub(crate) witnesses: BytesVec,
}
impl TransactionBuilder {
pub const FIELD_COUNT: usize = 2;
pub fn raw(mut self, v: RawTransaction) -> Self {
self.raw = v;
self
}
pub fn witnesses(mut self, v: BytesVec) -> Self {
self.witnesses = v;
self
}
}
impl molecule::prelude::Builder for TransactionBuilder {
type Entity = Transaction;
const NAME: &'static str = "TransactionBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.raw.as_slice().len()
+ self.witnesses.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.raw.as_slice().len();
offsets.push(total_size);
total_size += self.witnesses.as_slice().len();
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))?;
}
writer.write_all(self.raw.as_slice())?;
writer.write_all(self.witnesses.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));
Transaction::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct RawHeader(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for RawHeader {
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 RawHeader {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for RawHeader {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "version", self.version())?;
write!(f, ", {}: {}", "compact_target", self.compact_target())?;
write!(f, ", {}: {}", "timestamp", self.timestamp())?;
write!(f, ", {}: {}", "number", self.number())?;
write!(f, ", {}: {}", "epoch", self.epoch())?;
write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
write!(f, ", {}: {}", "dao", self.dao())?;
write!(f, " }}")
}
}
impl ::core::default::Default for RawHeader {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
RawHeader::new_unchecked(v)
}
}
impl RawHeader {
const DEFAULT_VALUE: [u8; 192] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 192;
pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
pub const FIELD_COUNT: usize = 10;
pub fn version(&self) -> Uint32 {
Uint32::new_unchecked(self.0.slice(0..4))
}
pub fn compact_target(&self) -> Uint32 {
Uint32::new_unchecked(self.0.slice(4..8))
}
pub fn timestamp(&self) -> Uint64 {
Uint64::new_unchecked(self.0.slice(8..16))
}
pub fn number(&self) -> Uint64 {
Uint64::new_unchecked(self.0.slice(16..24))
}
pub fn epoch(&self) -> Uint64 {
Uint64::new_unchecked(self.0.slice(24..32))
}
pub fn parent_hash(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(32..64))
}
pub fn transactions_root(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(64..96))
}
pub fn proposals_hash(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(96..128))
}
pub fn extra_hash(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(128..160))
}
pub fn dao(&self) -> Byte32 {
Byte32::new_unchecked(self.0.slice(160..192))
}
pub fn as_reader<'r>(&'r self) -> RawHeaderReader<'r> {
RawHeaderReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for RawHeader {
type Builder = RawHeaderBuilder;
const NAME: &'static str = "RawHeader";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
RawHeader(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<Self> {
RawHeaderReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
RawHeaderReader::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()
.version(self.version())
.compact_target(self.compact_target())
.timestamp(self.timestamp())
.number(self.number())
.epoch(self.epoch())
.parent_hash(self.parent_hash())
.transactions_root(self.transactions_root())
.proposals_hash(self.proposals_hash())
.extra_hash(self.extra_hash())
.dao(self.dao())
}
}
#[derive(Clone, Copy)]
pub struct RawHeaderReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for RawHeaderReader<'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 RawHeaderReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for RawHeaderReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "version", self.version())?;
write!(f, ", {}: {}", "compact_target", self.compact_target())?;
write!(f, ", {}: {}", "timestamp", self.timestamp())?;
write!(f, ", {}: {}", "number", self.number())?;
write!(f, ", {}: {}", "epoch", self.epoch())?;
write!(f, ", {}: {}", "parent_hash", self.parent_hash())?;
write!(f, ", {}: {}", "transactions_root", self.transactions_root())?;
write!(f, ", {}: {}", "proposals_hash", self.proposals_hash())?;
write!(f, ", {}: {}", "extra_hash", self.extra_hash())?;
write!(f, ", {}: {}", "dao", self.dao())?;
write!(f, " }}")
}
}
impl<'r> RawHeaderReader<'r> {
pub const TOTAL_SIZE: usize = 192;
pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
pub const FIELD_COUNT: usize = 10;
pub fn version(&self) -> Uint32Reader<'r> {
Uint32Reader::new_unchecked(&self.as_slice()[0..4])
}
pub fn compact_target(&self) -> Uint32Reader<'r> {
Uint32Reader::new_unchecked(&self.as_slice()[4..8])
}
pub fn timestamp(&self) -> Uint64Reader<'r> {
Uint64Reader::new_unchecked(&self.as_slice()[8..16])
}
pub fn number(&self) -> Uint64Reader<'r> {
Uint64Reader::new_unchecked(&self.as_slice()[16..24])
}
pub fn epoch(&self) -> Uint64Reader<'r> {
Uint64Reader::new_unchecked(&self.as_slice()[24..32])
}
pub fn parent_hash(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[32..64])
}
pub fn transactions_root(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[64..96])
}
pub fn proposals_hash(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[96..128])
}
pub fn extra_hash(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[128..160])
}
pub fn dao(&self) -> Byte32Reader<'r> {
Byte32Reader::new_unchecked(&self.as_slice()[160..192])
}
}
impl<'r> molecule::prelude::Reader<'r> for RawHeaderReader<'r> {
type Entity = RawHeader;
const NAME: &'static str = "RawHeaderReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
RawHeaderReader(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(())
}
}
#[derive(Debug, Default)]
pub struct RawHeaderBuilder {
pub(crate) version: Uint32,
pub(crate) compact_target: Uint32,
pub(crate) timestamp: Uint64,
pub(crate) number: Uint64,
pub(crate) epoch: Uint64,
pub(crate) parent_hash: Byte32,
pub(crate) transactions_root: Byte32,
pub(crate) proposals_hash: Byte32,
pub(crate) extra_hash: Byte32,
pub(crate) dao: Byte32,
}
impl RawHeaderBuilder {
pub const TOTAL_SIZE: usize = 192;
pub const FIELD_SIZES: [usize; 10] = [4, 4, 8, 8, 8, 32, 32, 32, 32, 32];
pub const FIELD_COUNT: usize = 10;
pub fn version(mut self, v: Uint32) -> Self {
self.version = v;
self
}
pub fn compact_target(mut self, v: Uint32) -> Self {
self.compact_target = v;
self
}
pub fn timestamp(mut self, v: Uint64) -> Self {
self.timestamp = v;
self
}
pub fn number(mut self, v: Uint64) -> Self {
self.number = v;
self
}
pub fn epoch(mut self, v: Uint64) -> Self {
self.epoch = v;
self
}
pub fn parent_hash(mut self, v: Byte32) -> Self {
self.parent_hash = v;
self
}
pub fn transactions_root(mut self, v: Byte32) -> Self {
self.transactions_root = v;
self
}
pub fn proposals_hash(mut self, v: Byte32) -> Self {
self.proposals_hash = v;
self
}
pub fn extra_hash(mut self, v: Byte32) -> Self {
self.extra_hash = v;
self
}
pub fn dao(mut self, v: Byte32) -> Self {
self.dao = v;
self
}
}
impl molecule::prelude::Builder for RawHeaderBuilder {
type Entity = RawHeader;
const NAME: &'static str = "RawHeaderBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
writer.write_all(self.version.as_slice())?;
writer.write_all(self.compact_target.as_slice())?;
writer.write_all(self.timestamp.as_slice())?;
writer.write_all(self.number.as_slice())?;
writer.write_all(self.epoch.as_slice())?;
writer.write_all(self.parent_hash.as_slice())?;
writer.write_all(self.transactions_root.as_slice())?;
writer.write_all(self.proposals_hash.as_slice())?;
writer.write_all(self.extra_hash.as_slice())?;
writer.write_all(self.dao.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));
RawHeader::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Header(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Header {
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 Header {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Header {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "raw", self.raw())?;
write!(f, ", {}: {}", "nonce", self.nonce())?;
write!(f, " }}")
}
}
impl ::core::default::Default for Header {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Header::new_unchecked(v)
}
}
impl Header {
const DEFAULT_VALUE: [u8; 208] = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const TOTAL_SIZE: usize = 208;
pub const FIELD_SIZES: [usize; 2] = [192, 16];
pub const FIELD_COUNT: usize = 2;
pub fn raw(&self) -> RawHeader {
RawHeader::new_unchecked(self.0.slice(0..192))
}
pub fn nonce(&self) -> Uint128 {
Uint128::new_unchecked(self.0.slice(192..208))
}
pub fn as_reader<'r>(&'r self) -> HeaderReader<'r> {
HeaderReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Header {
type Builder = HeaderBuilder;
const NAME: &'static str = "Header";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Header(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<Self> {
HeaderReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
HeaderReader::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().raw(self.raw()).nonce(self.nonce())
}
}
#[derive(Clone, Copy)]
pub struct HeaderReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for HeaderReader<'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 HeaderReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for HeaderReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "raw", self.raw())?;
write!(f, ", {}: {}", "nonce", self.nonce())?;
write!(f, " }}")
}
}
impl<'r> HeaderReader<'r> {
pub const TOTAL_SIZE: usize = 208;
pub const FIELD_SIZES: [usize; 2] = [192, 16];
pub const FIELD_COUNT: usize = 2;
pub fn raw(&self) -> RawHeaderReader<'r> {
RawHeaderReader::new_unchecked(&self.as_slice()[0..192])
}
pub fn nonce(&self) -> Uint128Reader<'r> {
Uint128Reader::new_unchecked(&self.as_slice()[192..208])
}
}
impl<'r> molecule::prelude::Reader<'r> for HeaderReader<'r> {
type Entity = Header;
const NAME: &'static str = "HeaderReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
HeaderReader(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(())
}
}
#[derive(Debug, Default)]
pub struct HeaderBuilder {
pub(crate) raw: RawHeader,
pub(crate) nonce: Uint128,
}
impl HeaderBuilder {
pub const TOTAL_SIZE: usize = 208;
pub const FIELD_SIZES: [usize; 2] = [192, 16];
pub const FIELD_COUNT: usize = 2;
pub fn raw(mut self, v: RawHeader) -> Self {
self.raw = v;
self
}
pub fn nonce(mut self, v: Uint128) -> Self {
self.nonce = v;
self
}
}
impl molecule::prelude::Builder for HeaderBuilder {
type Entity = Header;
const NAME: &'static str = "HeaderBuilder";
fn expected_length(&self) -> usize {
Self::TOTAL_SIZE
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
writer.write_all(self.raw.as_slice())?;
writer.write_all(self.nonce.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));
Header::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct UncleBlock(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for UncleBlock {
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 UncleBlock {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for UncleBlock {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for UncleBlock {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
UncleBlock::new_unchecked(v)
}
}
impl UncleBlock {
const DEFAULT_VALUE: [u8; 224] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> Header {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Header::new_unchecked(self.0.slice(start..end))
}
pub fn proposals(&self) -> ProposalShortIdVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
} else {
ProposalShortIdVec::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> UncleBlockReader<'r> {
UncleBlockReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for UncleBlock {
type Builder = UncleBlockBuilder;
const NAME: &'static str = "UncleBlock";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
UncleBlock(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<Self> {
UncleBlockReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
UncleBlockReader::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()
.header(self.header())
.proposals(self.proposals())
}
}
#[derive(Clone, Copy)]
pub struct UncleBlockReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for UncleBlockReader<'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 UncleBlockReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for UncleBlockReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> UncleBlockReader<'r> {
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> HeaderReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
HeaderReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
} else {
ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for UncleBlockReader<'r> {
type Entity = UncleBlock;
const NAME: &'static str = "UncleBlockReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
UncleBlockReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
ProposalShortIdVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct UncleBlockBuilder {
pub(crate) header: Header,
pub(crate) proposals: ProposalShortIdVec,
}
impl UncleBlockBuilder {
pub const FIELD_COUNT: usize = 2;
pub fn header(mut self, v: Header) -> Self {
self.header = v;
self
}
pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
self.proposals = v;
self
}
}
impl molecule::prelude::Builder for UncleBlockBuilder {
type Entity = UncleBlock;
const NAME: &'static str = "UncleBlockBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.header.as_slice().len()
+ self.proposals.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.header.as_slice().len();
offsets.push(total_size);
total_size += self.proposals.as_slice().len();
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))?;
}
writer.write_all(self.header.as_slice())?;
writer.write_all(self.proposals.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));
UncleBlock::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Block(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Block {
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 Block {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Block {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "uncles", self.uncles())?;
write!(f, ", {}: {}", "transactions", self.transactions())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for Block {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Block::new_unchecked(v)
}
}
impl Block {
const DEFAULT_VALUE: [u8; 240] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0,
0, 0, 0,
];
pub const FIELD_COUNT: usize = 4;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> Header {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Header::new_unchecked(self.0.slice(start..end))
}
pub fn uncles(&self) -> UncleBlockVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
UncleBlockVec::new_unchecked(self.0.slice(start..end))
}
pub fn transactions(&self) -> TransactionVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
TransactionVec::new_unchecked(self.0.slice(start..end))
}
pub fn proposals(&self) -> ProposalShortIdVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[20..]) as usize;
ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
} else {
ProposalShortIdVec::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> BlockReader<'r> {
BlockReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Block {
type Builder = BlockBuilder;
const NAME: &'static str = "Block";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Block(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<Self> {
BlockReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BlockReader::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()
.header(self.header())
.uncles(self.uncles())
.transactions(self.transactions())
.proposals(self.proposals())
}
}
#[derive(Clone, Copy)]
pub struct BlockReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BlockReader<'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 BlockReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BlockReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "uncles", self.uncles())?;
write!(f, ", {}: {}", "transactions", self.transactions())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> BlockReader<'r> {
pub const FIELD_COUNT: usize = 4;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> HeaderReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
HeaderReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn uncles(&self) -> UncleBlockVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn transactions(&self) -> TransactionVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[20..]) as usize;
ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
} else {
ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BlockReader<'r> {
type Entity = Block;
const NAME: &'static str = "BlockReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BlockReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BlockBuilder {
pub(crate) header: Header,
pub(crate) uncles: UncleBlockVec,
pub(crate) transactions: TransactionVec,
pub(crate) proposals: ProposalShortIdVec,
}
impl BlockBuilder {
pub const FIELD_COUNT: usize = 4;
pub fn header(mut self, v: Header) -> Self {
self.header = v;
self
}
pub fn uncles(mut self, v: UncleBlockVec) -> Self {
self.uncles = v;
self
}
pub fn transactions(mut self, v: TransactionVec) -> Self {
self.transactions = v;
self
}
pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
self.proposals = v;
self
}
}
impl molecule::prelude::Builder for BlockBuilder {
type Entity = Block;
const NAME: &'static str = "BlockBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.header.as_slice().len()
+ self.uncles.as_slice().len()
+ self.transactions.as_slice().len()
+ self.proposals.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.header.as_slice().len();
offsets.push(total_size);
total_size += self.uncles.as_slice().len();
offsets.push(total_size);
total_size += self.transactions.as_slice().len();
offsets.push(total_size);
total_size += self.proposals.as_slice().len();
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))?;
}
writer.write_all(self.header.as_slice())?;
writer.write_all(self.uncles.as_slice())?;
writer.write_all(self.transactions.as_slice())?;
writer.write_all(self.proposals.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));
Block::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct BlockV1(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for BlockV1 {
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 BlockV1 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for BlockV1 {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "uncles", self.uncles())?;
write!(f, ", {}: {}", "transactions", self.transactions())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
write!(f, ", {}: {}", "extension", self.extension())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for BlockV1 {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BlockV1::new_unchecked(v)
}
}
impl BlockV1 {
const DEFAULT_VALUE: [u8; 248] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 5;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> Header {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Header::new_unchecked(self.0.slice(start..end))
}
pub fn uncles(&self) -> UncleBlockVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
UncleBlockVec::new_unchecked(self.0.slice(start..end))
}
pub fn transactions(&self) -> TransactionVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
TransactionVec::new_unchecked(self.0.slice(start..end))
}
pub fn proposals(&self) -> ProposalShortIdVec {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
ProposalShortIdVec::new_unchecked(self.0.slice(start..end))
}
pub fn extension(&self) -> Bytes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[24..]) as usize;
Bytes::new_unchecked(self.0.slice(start..end))
} else {
Bytes::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> BlockV1Reader<'r> {
BlockV1Reader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for BlockV1 {
type Builder = BlockV1Builder;
const NAME: &'static str = "BlockV1";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
BlockV1(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<Self> {
BlockV1Reader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BlockV1Reader::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()
.header(self.header())
.uncles(self.uncles())
.transactions(self.transactions())
.proposals(self.proposals())
.extension(self.extension())
}
}
#[derive(Clone, Copy)]
pub struct BlockV1Reader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BlockV1Reader<'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 BlockV1Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BlockV1Reader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "header", self.header())?;
write!(f, ", {}: {}", "uncles", self.uncles())?;
write!(f, ", {}: {}", "transactions", self.transactions())?;
write!(f, ", {}: {}", "proposals", self.proposals())?;
write!(f, ", {}: {}", "extension", self.extension())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> BlockV1Reader<'r> {
pub const FIELD_COUNT: usize = 5;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn header(&self) -> HeaderReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
HeaderReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn uncles(&self) -> UncleBlockVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
UncleBlockVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn transactions(&self) -> TransactionVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
TransactionVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn proposals(&self) -> ProposalShortIdVecReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
ProposalShortIdVecReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn extension(&self) -> BytesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[20..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[24..]) as usize;
BytesReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BlockV1Reader<'r> {
type Entity = BlockV1;
const NAME: &'static str = "BlockV1Reader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BlockV1Reader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
HeaderReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
UncleBlockVecReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
TransactionVecReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
ProposalShortIdVecReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
BytesReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BlockV1Builder {
pub(crate) header: Header,
pub(crate) uncles: UncleBlockVec,
pub(crate) transactions: TransactionVec,
pub(crate) proposals: ProposalShortIdVec,
pub(crate) extension: Bytes,
}
impl BlockV1Builder {
pub const FIELD_COUNT: usize = 5;
pub fn header(mut self, v: Header) -> Self {
self.header = v;
self
}
pub fn uncles(mut self, v: UncleBlockVec) -> Self {
self.uncles = v;
self
}
pub fn transactions(mut self, v: TransactionVec) -> Self {
self.transactions = v;
self
}
pub fn proposals(mut self, v: ProposalShortIdVec) -> Self {
self.proposals = v;
self
}
pub fn extension(mut self, v: Bytes) -> Self {
self.extension = v;
self
}
}
impl molecule::prelude::Builder for BlockV1Builder {
type Entity = BlockV1;
const NAME: &'static str = "BlockV1Builder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.header.as_slice().len()
+ self.uncles.as_slice().len()
+ self.transactions.as_slice().len()
+ self.proposals.as_slice().len()
+ self.extension.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.header.as_slice().len();
offsets.push(total_size);
total_size += self.uncles.as_slice().len();
offsets.push(total_size);
total_size += self.transactions.as_slice().len();
offsets.push(total_size);
total_size += self.proposals.as_slice().len();
offsets.push(total_size);
total_size += self.extension.as_slice().len();
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))?;
}
writer.write_all(self.header.as_slice())?;
writer.write_all(self.uncles.as_slice())?;
writer.write_all(self.transactions.as_slice())?;
writer.write_all(self.proposals.as_slice())?;
writer.write_all(self.extension.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));
BlockV1::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct CellbaseWitness(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CellbaseWitness {
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 CellbaseWitness {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CellbaseWitness {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "message", self.message())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for CellbaseWitness {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CellbaseWitness::new_unchecked(v)
}
}
impl CellbaseWitness {
const DEFAULT_VALUE: [u8; 69] = [
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,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn lock(&self) -> Script {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Script::new_unchecked(self.0.slice(start..end))
}
pub fn message(&self) -> Bytes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
Bytes::new_unchecked(self.0.slice(start..end))
} else {
Bytes::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> CellbaseWitnessReader<'r> {
CellbaseWitnessReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CellbaseWitness {
type Builder = CellbaseWitnessBuilder;
const NAME: &'static str = "CellbaseWitness";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CellbaseWitness(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<Self> {
CellbaseWitnessReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CellbaseWitnessReader::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()
.lock(self.lock())
.message(self.message())
}
}
#[derive(Clone, Copy)]
pub struct CellbaseWitnessReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CellbaseWitnessReader<'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 CellbaseWitnessReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CellbaseWitnessReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "message", self.message())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> CellbaseWitnessReader<'r> {
pub const FIELD_COUNT: usize = 2;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn lock(&self) -> ScriptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
ScriptReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn message(&self) -> BytesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[12..]) as usize;
BytesReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for CellbaseWitnessReader<'r> {
type Entity = CellbaseWitness;
const NAME: &'static str = "CellbaseWitnessReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CellbaseWitnessReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
ScriptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
BytesReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct CellbaseWitnessBuilder {
pub(crate) lock: Script,
pub(crate) message: Bytes,
}
impl CellbaseWitnessBuilder {
pub const FIELD_COUNT: usize = 2;
pub fn lock(mut self, v: Script) -> Self {
self.lock = v;
self
}
pub fn message(mut self, v: Bytes) -> Self {
self.message = v;
self
}
}
impl molecule::prelude::Builder for CellbaseWitnessBuilder {
type Entity = CellbaseWitness;
const NAME: &'static str = "CellbaseWitnessBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.lock.as_slice().len()
+ self.message.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.lock.as_slice().len();
offsets.push(total_size);
total_size += self.message.as_slice().len();
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))?;
}
writer.write_all(self.lock.as_slice())?;
writer.write_all(self.message.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));
CellbaseWitness::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct WitnessArgs(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for WitnessArgs {
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 WitnessArgs {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for WitnessArgs {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "input_type", self.input_type())?;
write!(f, ", {}: {}", "output_type", self.output_type())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for WitnessArgs {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
WitnessArgs::new_unchecked(v)
}
}
impl WitnessArgs {
const DEFAULT_VALUE: [u8; 16] = [16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0];
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn lock(&self) -> BytesOpt {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
BytesOpt::new_unchecked(self.0.slice(start..end))
}
pub fn input_type(&self) -> BytesOpt {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
BytesOpt::new_unchecked(self.0.slice(start..end))
}
pub fn output_type(&self) -> BytesOpt {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
BytesOpt::new_unchecked(self.0.slice(start..end))
} else {
BytesOpt::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> WitnessArgsReader<'r> {
WitnessArgsReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for WitnessArgs {
type Builder = WitnessArgsBuilder;
const NAME: &'static str = "WitnessArgs";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
WitnessArgs(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<Self> {
WitnessArgsReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
WitnessArgsReader::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()
.lock(self.lock())
.input_type(self.input_type())
.output_type(self.output_type())
}
}
#[derive(Clone, Copy)]
pub struct WitnessArgsReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for WitnessArgsReader<'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 WitnessArgsReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for WitnessArgsReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "lock", self.lock())?;
write!(f, ", {}: {}", "input_type", self.input_type())?;
write!(f, ", {}: {}", "output_type", self.output_type())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> WitnessArgsReader<'r> {
pub const FIELD_COUNT: usize = 3;
pub fn total_size(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn field_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 count_extra_fields(&self) -> usize {
self.field_count() - Self::FIELD_COUNT
}
pub fn has_extra_fields(&self) -> bool {
Self::FIELD_COUNT != self.field_count()
}
pub fn lock(&self) -> BytesOptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
BytesOptReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn input_type(&self) -> BytesOptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
BytesOptReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn output_type(&self) -> BytesOptReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
if self.has_extra_fields() {
let end = molecule::unpack_number(&slice[16..]) as usize;
BytesOptReader::new_unchecked(&self.as_slice()[start..end])
} else {
BytesOptReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for WitnessArgsReader<'r> {
type Entity = WitnessArgs;
const NAME: &'static str = "WitnessArgsReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
WitnessArgsReader(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 * 2 {
return ve!(Self, HeaderIsBroken, 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 field_count = offset_first / molecule::NUMBER_SIZE - 1;
if field_count < Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
} else if !compatible && field_count > Self::FIELD_COUNT {
return ve!(Self, FieldCountNotMatch, Self::FIELD_COUNT, field_count);
};
let mut offsets: Vec<usize> = 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);
}
BytesOptReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
BytesOptReader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
BytesOptReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct WitnessArgsBuilder {
pub(crate) lock: BytesOpt,
pub(crate) input_type: BytesOpt,
pub(crate) output_type: BytesOpt,
}
impl WitnessArgsBuilder {
pub const FIELD_COUNT: usize = 3;
pub fn lock(mut self, v: BytesOpt) -> Self {
self.lock = v;
self
}
pub fn input_type(mut self, v: BytesOpt) -> Self {
self.input_type = v;
self
}
pub fn output_type(mut self, v: BytesOpt) -> Self {
self.output_type = v;
self
}
}
impl molecule::prelude::Builder for WitnessArgsBuilder {
type Entity = WitnessArgs;
const NAME: &'static str = "WitnessArgsBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.lock.as_slice().len()
+ self.input_type.as_slice().len()
+ self.output_type.as_slice().len()
}
fn write<W: molecule::io::Write>(&self, writer: &mut W) -> molecule::io::Result<()> {
let mut total_size = molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1);
let mut offsets = Vec::with_capacity(Self::FIELD_COUNT);
offsets.push(total_size);
total_size += self.lock.as_slice().len();
offsets.push(total_size);
total_size += self.input_type.as_slice().len();
offsets.push(total_size);
total_size += self.output_type.as_slice().len();
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))?;
}
writer.write_all(self.lock.as_slice())?;
writer.write_all(self.input_type.as_slice())?;
writer.write_all(self.output_type.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));
WitnessArgs::new_unchecked(inner.into())
}
}