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 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 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 Uint32Opt(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Uint32Opt {
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 Uint32Opt {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Uint32Opt {
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 Uint32Opt {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Uint32Opt::new_unchecked(v)
}
}
impl Uint32Opt {
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<Uint32> {
if self.is_none() {
None
} else {
Some(Uint32::new_unchecked(self.0.clone()))
}
}
pub fn as_reader<'r>(&'r self) -> Uint32OptReader<'r> {
Uint32OptReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Uint32Opt {
type Builder = Uint32OptBuilder;
const NAME: &'static str = "Uint32Opt";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Uint32Opt(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> {
Uint32OptReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
Uint32OptReader::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 Uint32OptReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for Uint32OptReader<'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 Uint32OptReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for Uint32OptReader<'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> Uint32OptReader<'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<Uint32Reader<'r>> {
if self.is_none() {
None
} else {
Some(Uint32Reader::new_unchecked(self.as_slice()))
}
}
}
impl<'r> molecule::prelude::Reader<'r> for Uint32OptReader<'r> {
type Entity = Uint32Opt;
const NAME: &'static str = "Uint32OptReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
Uint32OptReader(slice)
}
fn as_slice(&self) -> &'r [u8] {
self.0
}
fn verify(slice: &[u8], compatible: bool) -> molecule::error::VerificationResult<()> {
if !slice.is_empty() {
Uint32Reader::verify(&slice[..], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct Uint32OptBuilder(pub(crate) Option<Uint32>);
impl Uint32OptBuilder {
pub fn set(mut self, v: Option<Uint32>) -> Self {
self.0 = v;
self
}
}
impl molecule::prelude::Builder for Uint32OptBuilder {
type Entity = Uint32Opt;
const NAME: &'static str = "Uint32OptBuilder";
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));
Uint32Opt::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct Indexes(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for Indexes {
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 Indexes {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for Indexes {
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 Indexes {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
Indexes::new_unchecked(v)
}
}
impl Indexes {
const DEFAULT_VALUE: [u8; 4] = [0, 0, 0, 0];
pub const ITEM_SIZE: usize = 4;
pub fn total_size(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
}
pub fn item_count(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn len(&self) -> usize {
self.item_count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get(&self, idx: usize) -> Option<Uint32> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> Uint32 {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
Uint32::new_unchecked(self.0.slice(start..end))
}
pub fn as_reader<'r>(&'r self) -> IndexesReader<'r> {
IndexesReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for Indexes {
type Builder = IndexesBuilder;
const NAME: &'static str = "Indexes";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
Indexes(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> {
IndexesReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
IndexesReader::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 IndexesReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for IndexesReader<'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 IndexesReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for IndexesReader<'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> IndexesReader<'r> {
pub const ITEM_SIZE: usize = 4;
pub fn total_size(&self) -> usize {
molecule::NUMBER_SIZE + Self::ITEM_SIZE * self.item_count()
}
pub fn item_count(&self) -> usize {
molecule::unpack_number(self.as_slice()) as usize
}
pub fn len(&self) -> usize {
self.item_count()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn get(&self, idx: usize) -> Option<Uint32Reader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> Uint32Reader<'r> {
let start = molecule::NUMBER_SIZE + Self::ITEM_SIZE * idx;
let end = start + Self::ITEM_SIZE;
Uint32Reader::new_unchecked(&self.as_slice()[start..end])
}
}
impl<'r> molecule::prelude::Reader<'r> for IndexesReader<'r> {
type Entity = Indexes;
const NAME: &'static str = "IndexesReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
IndexesReader(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 IndexesBuilder(pub(crate) Vec<Uint32>);
impl IndexesBuilder {
pub const ITEM_SIZE: usize = 4;
pub fn set(mut self, v: Vec<Uint32>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: Uint32) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = Uint32>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: Uint32) -> Option<Uint32> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for IndexesBuilder {
type Entity = Indexes;
const NAME: &'static str = "IndexesBuilder";
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));
Indexes::new_unchecked(inner.into())
}
}
pub struct IndexesIterator(Indexes, usize, usize);
impl ::core::iter::Iterator for IndexesIterator {
type Item = Uint32;
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 IndexesIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for Indexes {
type Item = Uint32;
type IntoIter = IndexesIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
IndexesIterator(self, 0, len)
}
}
impl<'r> IndexesReader<'r> {
pub fn iter<'t>(&'t self) -> IndexesReaderIterator<'t, 'r> {
IndexesReaderIterator(&self, 0, self.len())
}
}
pub struct IndexesReaderIterator<'t, 'r>(&'t IndexesReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for IndexesReaderIterator<'t, 'r> {
type Item = Uint32Reader<'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 IndexesReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}
#[derive(Clone)]
pub struct CKBFSData(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for CKBFSData {
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 CKBFSData {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for CKBFSData {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "indexes", self.indexes())?;
write!(f, ", {}: {}", "checksum", self.checksum())?;
write!(f, ", {}: {}", "content_type", self.content_type())?;
write!(f, ", {}: {}", "filename", self.filename())?;
write!(f, ", {}: {}", "backlinks", self.backlinks())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for CKBFSData {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
CKBFSData::new_unchecked(v)
}
}
impl CKBFSData {
const DEFAULT_VALUE: [u8; 44] = [
44, 0, 0, 0, 24, 0, 0, 0, 28, 0, 0, 0, 32, 0, 0, 0, 36, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 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 indexes(&self) -> Indexes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Indexes::new_unchecked(self.0.slice(start..end))
}
pub fn checksum(&self) -> Uint32 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Uint32::new_unchecked(self.0.slice(start..end))
}
pub fn content_type(&self) -> Bytes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
Bytes::new_unchecked(self.0.slice(start..end))
}
pub fn filename(&self) -> Bytes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
Bytes::new_unchecked(self.0.slice(start..end))
}
pub fn backlinks(&self) -> BackLinkVec {
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;
BackLinkVec::new_unchecked(self.0.slice(start..end))
} else {
BackLinkVec::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> CKBFSDataReader<'r> {
CKBFSDataReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for CKBFSData {
type Builder = CKBFSDataBuilder;
const NAME: &'static str = "CKBFSData";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
CKBFSData(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> {
CKBFSDataReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
CKBFSDataReader::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()
.indexes(self.indexes())
.checksum(self.checksum())
.content_type(self.content_type())
.filename(self.filename())
.backlinks(self.backlinks())
}
}
#[derive(Clone, Copy)]
pub struct CKBFSDataReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for CKBFSDataReader<'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 CKBFSDataReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for CKBFSDataReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "indexes", self.indexes())?;
write!(f, ", {}: {}", "checksum", self.checksum())?;
write!(f, ", {}: {}", "content_type", self.content_type())?;
write!(f, ", {}: {}", "filename", self.filename())?;
write!(f, ", {}: {}", "backlinks", self.backlinks())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> CKBFSDataReader<'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 indexes(&self) -> IndexesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
IndexesReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn checksum(&self) -> Uint32Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Uint32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn content_type(&self) -> BytesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[12..]) as usize;
let end = molecule::unpack_number(&slice[16..]) as usize;
BytesReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn filename(&self) -> BytesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[16..]) as usize;
let end = molecule::unpack_number(&slice[20..]) as usize;
BytesReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn backlinks(&self) -> BackLinkVecReader<'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;
BackLinkVecReader::new_unchecked(&self.as_slice()[start..end])
} else {
BackLinkVecReader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for CKBFSDataReader<'r> {
type Entity = CKBFSData;
const NAME: &'static str = "CKBFSDataReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
CKBFSDataReader(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);
}
IndexesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
BytesReader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
BytesReader::verify(&slice[offsets[3]..offsets[4]], compatible)?;
BackLinkVecReader::verify(&slice[offsets[4]..offsets[5]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct CKBFSDataBuilder {
pub(crate) indexes: Indexes,
pub(crate) checksum: Uint32,
pub(crate) content_type: Bytes,
pub(crate) filename: Bytes,
pub(crate) backlinks: BackLinkVec,
}
impl CKBFSDataBuilder {
pub const FIELD_COUNT: usize = 5;
pub fn indexes(mut self, v: Indexes) -> Self {
self.indexes = v;
self
}
pub fn checksum(mut self, v: Uint32) -> Self {
self.checksum = v;
self
}
pub fn content_type(mut self, v: Bytes) -> Self {
self.content_type = v;
self
}
pub fn filename(mut self, v: Bytes) -> Self {
self.filename = v;
self
}
pub fn backlinks(mut self, v: BackLinkVec) -> Self {
self.backlinks = v;
self
}
}
impl molecule::prelude::Builder for CKBFSDataBuilder {
type Entity = CKBFSData;
const NAME: &'static str = "CKBFSDataBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.indexes.as_slice().len()
+ self.checksum.as_slice().len()
+ self.content_type.as_slice().len()
+ self.filename.as_slice().len()
+ self.backlinks.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.indexes.as_slice().len();
offsets.push(total_size);
total_size += self.checksum.as_slice().len();
offsets.push(total_size);
total_size += self.content_type.as_slice().len();
offsets.push(total_size);
total_size += self.filename.as_slice().len();
offsets.push(total_size);
total_size += self.backlinks.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.indexes.as_slice())?;
writer.write_all(self.checksum.as_slice())?;
writer.write_all(self.content_type.as_slice())?;
writer.write_all(self.filename.as_slice())?;
writer.write_all(self.backlinks.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));
CKBFSData::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct BackLink(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for BackLink {
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 BackLink {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for BackLink {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "indexes", self.indexes())?;
write!(f, ", {}: {}", "checksum", self.checksum())?;
write!(f, ", {}: {}", "tx_hash", self.tx_hash())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl ::core::default::Default for BackLink {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BackLink::new_unchecked(v)
}
}
impl BackLink {
const DEFAULT_VALUE: [u8; 56] = [
56, 0, 0, 0, 16, 0, 0, 0, 20, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 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 indexes(&self) -> Indexes {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
Indexes::new_unchecked(self.0.slice(start..end))
}
pub fn checksum(&self) -> Uint32 {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Uint32::new_unchecked(self.0.slice(start..end))
}
pub fn tx_hash(&self) -> Byte32 {
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;
Byte32::new_unchecked(self.0.slice(start..end))
} else {
Byte32::new_unchecked(self.0.slice(start..))
}
}
pub fn as_reader<'r>(&'r self) -> BackLinkReader<'r> {
BackLinkReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for BackLink {
type Builder = BackLinkBuilder;
const NAME: &'static str = "BackLink";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
BackLink(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> {
BackLinkReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BackLinkReader::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()
.indexes(self.indexes())
.checksum(self.checksum())
.tx_hash(self.tx_hash())
}
}
#[derive(Clone, Copy)]
pub struct BackLinkReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BackLinkReader<'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 BackLinkReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BackLinkReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{} {{ ", Self::NAME)?;
write!(f, "{}: {}", "indexes", self.indexes())?;
write!(f, ", {}: {}", "checksum", self.checksum())?;
write!(f, ", {}: {}", "tx_hash", self.tx_hash())?;
let extra_count = self.count_extra_fields();
if extra_count != 0 {
write!(f, ", .. ({} fields)", extra_count)?;
}
write!(f, " }}")
}
}
impl<'r> BackLinkReader<'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 indexes(&self) -> IndexesReader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[4..]) as usize;
let end = molecule::unpack_number(&slice[8..]) as usize;
IndexesReader::new_unchecked(&self.as_slice()[start..end])
}
pub fn checksum(&self) -> Uint32Reader<'r> {
let slice = self.as_slice();
let start = molecule::unpack_number(&slice[8..]) as usize;
let end = molecule::unpack_number(&slice[12..]) as usize;
Uint32Reader::new_unchecked(&self.as_slice()[start..end])
}
pub fn tx_hash(&self) -> Byte32Reader<'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;
Byte32Reader::new_unchecked(&self.as_slice()[start..end])
} else {
Byte32Reader::new_unchecked(&self.as_slice()[start..])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BackLinkReader<'r> {
type Entity = BackLink;
const NAME: &'static str = "BackLinkReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BackLinkReader(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);
}
IndexesReader::verify(&slice[offsets[0]..offsets[1]], compatible)?;
Uint32Reader::verify(&slice[offsets[1]..offsets[2]], compatible)?;
Byte32Reader::verify(&slice[offsets[2]..offsets[3]], compatible)?;
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BackLinkBuilder {
pub(crate) indexes: Indexes,
pub(crate) checksum: Uint32,
pub(crate) tx_hash: Byte32,
}
impl BackLinkBuilder {
pub const FIELD_COUNT: usize = 3;
pub fn indexes(mut self, v: Indexes) -> Self {
self.indexes = v;
self
}
pub fn checksum(mut self, v: Uint32) -> Self {
self.checksum = v;
self
}
pub fn tx_hash(mut self, v: Byte32) -> Self {
self.tx_hash = v;
self
}
}
impl molecule::prelude::Builder for BackLinkBuilder {
type Entity = BackLink;
const NAME: &'static str = "BackLinkBuilder";
fn expected_length(&self) -> usize {
molecule::NUMBER_SIZE * (Self::FIELD_COUNT + 1)
+ self.indexes.as_slice().len()
+ self.checksum.as_slice().len()
+ self.tx_hash.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.indexes.as_slice().len();
offsets.push(total_size);
total_size += self.checksum.as_slice().len();
offsets.push(total_size);
total_size += self.tx_hash.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.indexes.as_slice())?;
writer.write_all(self.checksum.as_slice())?;
writer.write_all(self.tx_hash.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));
BackLink::new_unchecked(inner.into())
}
}
#[derive(Clone)]
pub struct BackLinkVec(molecule::bytes::Bytes);
impl ::core::fmt::LowerHex for BackLinkVec {
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 BackLinkVec {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl ::core::fmt::Display for BackLinkVec {
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 BackLinkVec {
fn default() -> Self {
let v = molecule::bytes::Bytes::from_static(&Self::DEFAULT_VALUE);
BackLinkVec::new_unchecked(v)
}
}
impl BackLinkVec {
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<BackLink> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> BackLink {
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 {
BackLink::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;
BackLink::new_unchecked(self.0.slice(start..end))
}
}
pub fn as_reader<'r>(&'r self) -> BackLinkVecReader<'r> {
BackLinkVecReader::new_unchecked(self.as_slice())
}
}
impl molecule::prelude::Entity for BackLinkVec {
type Builder = BackLinkVecBuilder;
const NAME: &'static str = "BackLinkVec";
fn new_unchecked(data: molecule::bytes::Bytes) -> Self {
BackLinkVec(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> {
BackLinkVecReader::from_slice(slice).map(|reader| reader.to_entity())
}
fn from_compatible_slice(slice: &[u8]) -> molecule::error::VerificationResult<Self> {
BackLinkVecReader::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 BackLinkVecReader<'r>(&'r [u8]);
impl<'r> ::core::fmt::LowerHex for BackLinkVecReader<'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 BackLinkVecReader<'r> {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
write!(f, "{}({:#x})", Self::NAME, self)
}
}
impl<'r> ::core::fmt::Display for BackLinkVecReader<'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> BackLinkVecReader<'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<BackLinkReader<'r>> {
if idx >= self.len() {
None
} else {
Some(self.get_unchecked(idx))
}
}
pub fn get_unchecked(&self, idx: usize) -> BackLinkReader<'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 {
BackLinkReader::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;
BackLinkReader::new_unchecked(&self.as_slice()[start..end])
}
}
}
impl<'r> molecule::prelude::Reader<'r> for BackLinkVecReader<'r> {
type Entity = BackLinkVec;
const NAME: &'static str = "BackLinkVecReader";
fn to_entity(&self) -> Self::Entity {
Self::Entity::new_unchecked(self.as_slice().to_owned().into())
}
fn new_unchecked(slice: &'r [u8]) -> Self {
BackLinkVecReader(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];
BackLinkReader::verify(&slice[start..end], compatible)?;
}
Ok(())
}
}
#[derive(Debug, Default)]
pub struct BackLinkVecBuilder(pub(crate) Vec<BackLink>);
impl BackLinkVecBuilder {
pub fn set(mut self, v: Vec<BackLink>) -> Self {
self.0 = v;
self
}
pub fn push(mut self, v: BackLink) -> Self {
self.0.push(v);
self
}
pub fn extend<T: ::core::iter::IntoIterator<Item = BackLink>>(mut self, iter: T) -> Self {
for elem in iter {
self.0.push(elem);
}
self
}
pub fn replace(&mut self, index: usize, v: BackLink) -> Option<BackLink> {
self.0
.get_mut(index)
.map(|item| ::core::mem::replace(item, v))
}
}
impl molecule::prelude::Builder for BackLinkVecBuilder {
type Entity = BackLinkVec;
const NAME: &'static str = "BackLinkVecBuilder";
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));
BackLinkVec::new_unchecked(inner.into())
}
}
pub struct BackLinkVecIterator(BackLinkVec, usize, usize);
impl ::core::iter::Iterator for BackLinkVecIterator {
type Item = BackLink;
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 BackLinkVecIterator {
fn len(&self) -> usize {
self.2 - self.1
}
}
impl ::core::iter::IntoIterator for BackLinkVec {
type Item = BackLink;
type IntoIter = BackLinkVecIterator;
fn into_iter(self) -> Self::IntoIter {
let len = self.len();
BackLinkVecIterator(self, 0, len)
}
}
impl<'r> BackLinkVecReader<'r> {
pub fn iter<'t>(&'t self) -> BackLinkVecReaderIterator<'t, 'r> {
BackLinkVecReaderIterator(&self, 0, self.len())
}
}
pub struct BackLinkVecReaderIterator<'t, 'r>(&'t BackLinkVecReader<'r>, usize, usize);
impl<'t: 'r, 'r> ::core::iter::Iterator for BackLinkVecReaderIterator<'t, 'r> {
type Item = BackLinkReader<'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 BackLinkVecReaderIterator<'t, 'r> {
fn len(&self) -> usize {
self.2 - self.1
}
}