From 74032482e6ec423dd9fcb4a6dfa65266a6a44826 Mon Sep 17 00:00:00 2001 From: driftluo Date: Fri, 5 Jul 2024 14:34:39 +0800 Subject: [PATCH 1/3] feat: basic impl for molecule --- .../src/conversion/blockchain/mod.rs | 55 ++ .../src/conversion/blockchain/std_env.rs | 61 ++ util/gen-types/src/conversion/primitive.rs | 236 +++++++ util/gen-types/src/conversion/utilities.rs | 106 +++ util/gen-types/src/extension/capacity.rs | 6 +- .../src/extension/rust_core_traits.rs | 18 +- util/gen-types/src/extension/shortcut.rs | 20 +- util/gen-types/src/generated/blockchain.rs | 615 ++++++++++++++++-- util/gen-types/src/generated/extensions.rs | 591 ++++++++++++++--- util/gen-types/src/generated/protocols.rs | 154 ++++- 10 files changed, 1655 insertions(+), 207 deletions(-) diff --git a/util/gen-types/src/conversion/blockchain/mod.rs b/util/gen-types/src/conversion/blockchain/mod.rs index b80804fc09..3a73bd2084 100644 --- a/util/gen-types/src/conversion/blockchain/mod.rs +++ b/util/gen-types/src/conversion/blockchain/mod.rs @@ -9,6 +9,12 @@ impl Pack for [u8; 32] { } } +impl From<&[u8; 32]> for packed::Byte32 { + fn from(value: &[u8; 32]) -> Self { + packed::Byte32::from_slice(&value[..]).expect("impossible: fail to pack [u8; 32]") + } +} + impl<'r> Unpack<[u8; 32]> for packed::Byte32Reader<'r> { fn unpack(&self) -> [u8; 32] { let mut b = [0u8; 32]; @@ -18,6 +24,14 @@ impl<'r> Unpack<[u8; 32]> for packed::Byte32Reader<'r> { } impl_conversion_for_entity_unpack!([u8; 32], Byte32); +impl<'r> Into<[u8; 32]> for packed::Byte32Reader<'r> { + fn into(self) -> [u8; 32] { + let mut b = [0u8; 32]; + b.copy_from_slice(self.raw_data()); + b + } +} + impl Pack for [u8; 10] { fn pack(&self) -> packed::ProposalShortId { packed::ProposalShortId::from_slice(&self[..]) @@ -34,6 +48,14 @@ impl<'r> Unpack<[u8; 10]> for packed::ProposalShortIdReader<'r> { } impl_conversion_for_entity_unpack!([u8; 10], ProposalShortId); +impl<'r> Into<[u8; 10]> for packed::ProposalShortIdReader<'r> { + fn into(self) -> [u8; 10] { + let mut b = [0u8; 10]; + b.copy_from_slice(self.raw_data()); + b + } +} + impl Pack for Bytes { fn pack(&self) -> packed::Bytes { let len = (self.len() as u32).to_le_bytes(); @@ -44,18 +66,46 @@ impl Pack for Bytes { } } +impl From<&Bytes> for packed::Bytes { + fn from(value: &Bytes) -> Self { + let len = (value.len() as u32).to_le_bytes(); + let mut v = Vec::with_capacity(4 + value.len()); + v.extend_from_slice(&len[..]); + v.extend_from_slice(&value[..]); + packed::Bytes::new_unchecked(v.into()) + } +} + +impl From for packed::Bytes { + fn from(value: Bytes) -> Self { + (&value).into() + } +} + impl<'r> Unpack for packed::BytesReader<'r> { fn unpack(&self) -> Bytes { Bytes::from(self.raw_data().to_owned()) } } +impl<'r> Into for packed::BytesReader<'r> { + fn into(self) -> Bytes { + Bytes::from(self.raw_data().to_owned()) + } +} + impl Unpack for packed::Bytes { fn unpack(&self) -> Bytes { self.raw_data() } } +impl Into for packed::Bytes { + fn into(self) -> Bytes { + self.raw_data() + } +} + impl_conversion_for_vector!(Bytes, BytesVec, BytesVecReader); impl_conversion_for_packed_optional_pack!(Byte32, Byte32Opt); impl_conversion_for_packed_optional_pack!(CellOutput, CellOutputOpt); @@ -70,3 +120,8 @@ impl_conversion_for_packed_iterator_pack!(CellInput, CellInputVec); impl_conversion_for_packed_iterator_pack!(UncleBlock, UncleBlockVec); impl_conversion_for_packed_iterator_pack!(Header, HeaderVec); impl_conversion_for_packed_iterator_pack!(Byte32, Byte32Vec); + +impl_conversion_for_vector_from_into!(Bytes, BytesVec, BytesVecReader); +impl_conversion_for_packed_optional_from!(Byte32, Byte32Opt); +impl_conversion_for_packed_optional_from!(CellOutput, CellOutputOpt); +impl_conversion_for_packed_optional_from!(Script, ScriptOpt); diff --git a/util/gen-types/src/conversion/blockchain/std_env.rs b/util/gen-types/src/conversion/blockchain/std_env.rs index 5fd0f40751..93343709b3 100644 --- a/util/gen-types/src/conversion/blockchain/std_env.rs +++ b/util/gen-types/src/conversion/blockchain/std_env.rs @@ -10,6 +10,18 @@ impl Pack for Capacity { } } +impl From for packed::Uint64 { + fn from(value: Capacity) -> Self { + (&value).into() + } +} + +impl From<&Capacity> for packed::Uint64 { + fn from(value: &Capacity) -> Self { + value.as_u64().into() + } +} + impl<'r> Unpack for packed::Uint64Reader<'r> { fn unpack(&self) -> Capacity { Capacity::shannons(self.unpack()) @@ -17,12 +29,32 @@ impl<'r> Unpack for packed::Uint64Reader<'r> { } impl_conversion_for_entity_unpack!(Capacity, Uint64); +impl<'r> Into for packed::Uint64Reader<'r> { + fn into(self) -> Capacity { + Capacity::shannons(self.into()) + } +} +impl_conversion_for_entity_from!(Capacity, Uint64); + impl Pack for U256 { fn pack(&self) -> packed::Uint256 { packed::Uint256::from_slice(&self.to_le_bytes()[..]).expect("impossible: fail to pack U256") } } +impl From for packed::Uint256 { + fn from(value: U256) -> Self { + (&value).into() + } +} + +impl From<&U256> for packed::Uint256 { + fn from(value: &U256) -> Self { + packed::Uint256::from_slice(&value.to_le_bytes()[..]) + .expect("impossible: fail to pack U256") + } +} + impl<'r> Unpack for packed::Uint256Reader<'r> { fn unpack(&self) -> U256 { U256::from_little_endian(self.as_slice()).expect("internal error: fail to unpack U256") @@ -30,12 +62,31 @@ impl<'r> Unpack for packed::Uint256Reader<'r> { } impl_conversion_for_entity_unpack!(U256, Uint256); +impl<'r> Into for packed::Uint256Reader<'r> { + fn into(self) -> U256 { + U256::from_little_endian(self.as_slice()).expect("internal error: fail to unpack U256") + } +} +impl_conversion_for_entity_from!(U256, Uint256); + impl Pack for H256 { fn pack(&self) -> packed::Byte32 { packed::Byte32::from_slice(self.as_bytes()).expect("impossible: fail to pack H256") } } +impl From for packed::Byte32 { + fn from(value: H256) -> Self { + (&value).into() + } +} + +impl From<&H256> for packed::Byte32 { + fn from(value: &H256) -> Self { + packed::Byte32::from_slice(value.as_bytes()).expect("impossible: fail to pack H256") + } +} + impl<'r> Unpack for packed::Byte32Reader<'r> { fn unpack(&self) -> H256 { H256::from_slice(self.as_slice()).expect("internal error: fail to unpack H256") @@ -43,5 +94,15 @@ impl<'r> Unpack for packed::Byte32Reader<'r> { } impl_conversion_for_entity_unpack!(H256, Byte32); +impl<'r> Into for packed::Byte32Reader<'r> { + fn into(self) -> H256 { + H256::from_slice(self.as_slice()).expect("internal error: fail to unpack H256") + } +} +impl_conversion_for_entity_from!(H256, Byte32); + impl_conversion_for_option!(H256, Byte32Opt, Byte32OptReader); impl_conversion_for_vector!(Capacity, Uint64Vec, Uint64VecReader); + +impl_conversion_for_option_from_into!(H256, Byte32Opt, Byte32OptReader, Byte32); +impl_conversion_for_vector_from_into!(Capacity, Uint64Vec, Uint64VecReader); diff --git a/util/gen-types/src/conversion/primitive.rs b/util/gen-types/src/conversion/primitive.rs index 38d82d9228..80ef6b55de 100644 --- a/util/gen-types/src/conversion/primitive.rs +++ b/util/gen-types/src/conversion/primitive.rs @@ -1,5 +1,6 @@ #[cfg(not(feature = "std"))] use alloc::{borrow::ToOwned, str, string::String}; +use core::usize; #[cfg(feature = "std")] use std::str; @@ -12,6 +13,29 @@ impl Pack for bool { } } +impl From for packed::Bool { + fn from(value: bool) -> Self { + (&value).into() + } +} + +impl From<&bool> for packed::Bool { + fn from(value: &bool) -> Self { + let b = u8::from(*value); + packed::Bool::new_unchecked(Bytes::from(vec![b])) + } +} + +impl<'r> Into for packed::BoolReader<'r> { + fn into(self) -> bool { + match self.as_slice()[0] { + 0 => false, + 1 => true, + _ => unreachable!(), + } + } +} +impl_conversion_for_entity_from!(bool, Bool); impl<'r> Unpack for packed::BoolReader<'r> { fn unpack(&self) -> bool { match self.as_slice()[0] { @@ -29,24 +53,80 @@ impl Pack for u32 { } } +impl From for packed::Uint32 { + fn from(value: u32) -> Self { + (&value).into() + } +} + +impl From<&u32> for packed::Uint32 { + fn from(value: &u32) -> Self { + packed::Uint32::new_unchecked(Bytes::from(value.to_le_bytes().to_vec())) + } +} + impl Pack for u64 { fn pack(&self) -> packed::Uint64 { packed::Uint64::new_unchecked(Bytes::from(self.to_le_bytes().to_vec())) } } +impl From for packed::Uint64 { + fn from(value: u64) -> Self { + (&value).into() + } +} + +impl From<&u64> for packed::Uint64 { + fn from(value: &u64) -> Self { + packed::Uint64::new_unchecked(Bytes::from(value.to_le_bytes().to_vec())) + } +} + impl Pack for u128 { fn pack(&self) -> packed::Uint128 { packed::Uint128::new_unchecked(Bytes::from(self.to_le_bytes().to_vec())) } } +impl From for packed::Uint128 { + fn from(value: u128) -> Self { + (&value).into() + } +} + +impl From<&u128> for packed::Uint128 { + fn from(value: &u128) -> Self { + packed::Uint128::new_unchecked(Bytes::from(value.to_le_bytes().to_vec())) + } +} + impl Pack for usize { fn pack(&self) -> packed::Uint32 { (*self as u32).pack() } } +impl From for packed::Uint32 { + fn from(value: usize) -> Self { + (value as u32).into() + } +} + +impl From<&usize> for packed::Uint32 { + fn from(value: &usize) -> Self { + (*value as u32).into() + } +} + +impl<'r> Into for packed::Uint32Reader<'r> { + fn into(self) -> u32 { + let mut b = [0u8; 4]; + b.copy_from_slice(self.as_slice()); + u32::from_le_bytes(b) + } +} +impl_conversion_for_entity_from!(u32, Uint32); impl<'r> Unpack for packed::Uint32Reader<'r> { fn unpack(&self) -> u32 { let mut b = [0u8; 4]; @@ -56,6 +136,14 @@ impl<'r> Unpack for packed::Uint32Reader<'r> { } impl_conversion_for_entity_unpack!(u32, Uint32); +impl<'r> Into for packed::Uint64Reader<'r> { + fn into(self) -> u64 { + let mut b = [0u8; 8]; + b.copy_from_slice(self.as_slice()); + u64::from_le_bytes(b) + } +} +impl_conversion_for_entity_from!(u64, Uint64); impl<'r> Unpack for packed::Uint64Reader<'r> { fn unpack(&self) -> u64 { let mut b = [0u8; 8]; @@ -65,6 +153,14 @@ impl<'r> Unpack for packed::Uint64Reader<'r> { } impl_conversion_for_entity_unpack!(u64, Uint64); +impl<'r> Into for packed::Uint128Reader<'r> { + fn into(self) -> u128 { + let mut b = [0u8; 16]; + b.copy_from_slice(self.as_slice()); + u128::from_le_bytes(b) + } +} +impl_conversion_for_entity_from!(u128, Uint128); impl<'r> Unpack for packed::Uint128Reader<'r> { fn unpack(&self) -> u128 { let mut b = [0u8; 16]; @@ -74,6 +170,13 @@ impl<'r> Unpack for packed::Uint128Reader<'r> { } impl_conversion_for_entity_unpack!(u128, Uint128); +impl<'r> Into for packed::Uint32Reader<'r> { + fn into(self) -> usize { + let x: u32 = self.into(); + x as usize + } +} +impl_conversion_for_entity_from!(usize, Uint32); impl<'r> Unpack for packed::Uint32Reader<'r> { fn unpack(&self) -> usize { let x: u32 = self.unpack(); @@ -88,18 +191,62 @@ impl Pack for u32 { } } +impl From for packed::BeUint32 { + fn from(value: u32) -> Self { + (&value).into() + } +} + +impl From<&u32> for packed::BeUint32 { + fn from(value: &u32) -> Self { + packed::BeUint32::new_unchecked(Bytes::from(value.to_be_bytes().to_vec())) + } +} + impl Pack for u64 { fn pack(&self) -> packed::BeUint64 { packed::BeUint64::new_unchecked(Bytes::from(self.to_be_bytes().to_vec())) } } +impl From for packed::BeUint64 { + fn from(value: u64) -> Self { + (&value).into() + } +} + +impl From<&u64> for packed::BeUint64 { + fn from(value: &u64) -> Self { + packed::BeUint64::new_unchecked(Bytes::from(value.to_be_bytes().to_vec())) + } +} + impl Pack for usize { fn pack(&self) -> packed::BeUint32 { (*self as u32).pack() } } +impl From for packed::BeUint32 { + fn from(value: usize) -> Self { + (value as u32).into() + } +} + +impl From<&usize> for packed::BeUint32 { + fn from(value: &usize) -> Self { + (*value as u32).into() + } +} + +impl<'r> Into for packed::BeUint32Reader<'r> { + fn into(self) -> u32 { + let mut b = [0u8; 4]; + b.copy_from_slice(self.as_slice()); + u32::from_be_bytes(b) + } +} +impl_conversion_for_entity_from!(u32, BeUint32); impl<'r> Unpack for packed::BeUint32Reader<'r> { fn unpack(&self) -> u32 { let mut b = [0u8; 4]; @@ -109,6 +256,14 @@ impl<'r> Unpack for packed::BeUint32Reader<'r> { } impl_conversion_for_entity_unpack!(u32, BeUint32); +impl<'r> Into for packed::BeUint64Reader<'r> { + fn into(self) -> u64 { + let mut b = [0u8; 8]; + b.copy_from_slice(self.as_slice()); + u64::from_be_bytes(b) + } +} +impl_conversion_for_entity_from!(u64, BeUint64); impl<'r> Unpack for packed::BeUint64Reader<'r> { fn unpack(&self) -> u64 { let mut b = [0u8; 8]; @@ -118,6 +273,13 @@ impl<'r> Unpack for packed::BeUint64Reader<'r> { } impl_conversion_for_entity_unpack!(u64, BeUint64); +impl<'r> Into for packed::BeUint32Reader<'r> { + fn into(self) -> usize { + let x: u32 = self.into(); + x as usize + } +} +impl_conversion_for_entity_from!(usize, BeUint32); impl<'r> Unpack for packed::BeUint32Reader<'r> { fn unpack(&self) -> usize { let x: u32 = self.unpack(); @@ -136,6 +298,34 @@ impl Pack for [u8] { } } +impl From<&[u8]> for packed::Bytes { + fn from(value: &[u8]) -> Self { + let len = value.len(); + let mut vec: Vec = Vec::with_capacity(4 + len); + vec.extend_from_slice(&(len as u32).to_le_bytes()[..]); + vec.extend_from_slice(value); + packed::Bytes::new_unchecked(Bytes::from(vec)) + } +} + +impl From<[u8; N]> for packed::Bytes { + fn from(value: [u8; N]) -> Self { + (&value[..]).into() + } +} + +impl From<&[u8; N]> for packed::Bytes { + fn from(value: &[u8; N]) -> Self { + (&value[..]).into() + } +} + +impl<'r> Into> for packed::BytesReader<'r> { + fn into(self) -> Vec { + self.raw_data().to_owned() + } +} +impl_conversion_for_entity_from!(Vec, Bytes); impl<'r> Unpack> for packed::BytesReader<'r> { fn unpack(&self) -> Vec { self.raw_data().to_owned() @@ -149,6 +339,12 @@ impl Pack for str { } } +impl From<&str> for packed::Bytes { + fn from(value: &str) -> Self { + value.as_bytes().into() + } +} + impl<'r> packed::BytesReader<'r> { /// Converts self to a string slice. pub fn as_utf8(&self) -> Result<&str, str::Utf8Error> { @@ -178,12 +374,24 @@ impl Pack for String { } } +impl From for packed::Bytes { + fn from(value: String) -> Self { + value.as_str().into() + } +} + impl<'r> Unpack>> for packed::Uint64VecOptReader<'r> { fn unpack(&self) -> Option> { self.to_opt().map(|x| x.unpack()) } } +impl<'r> Into>> for packed::Uint64VecOptReader<'r> { + fn into(self) -> Option> { + self.to_opt().map(|x| x.into()) + } +} + impl_conversion_for_entity_unpack!(Option>, Uint64VecOpt); impl Pack for Option> { @@ -198,6 +406,24 @@ impl Pack for Option> { } } +impl From>> for packed::Uint64VecOpt { + fn from(value: Option>) -> Self { + (&value).into() + } +} + +impl From<&Option>> for packed::Uint64VecOpt { + fn from(value: &Option>) -> Self { + if let Some(inner) = value { + packed::Uint64VecOptBuilder::default() + .set(Some(inner.as_slice().into())) + .build() + } else { + packed::Uint64VecOpt::default() + } + } +} + impl_conversion_for_option!(bool, BoolOpt, BoolOptReader); impl_conversion_for_vector!(u32, Uint32Vec, Uint32VecReader); impl_conversion_for_vector!(usize, Uint32Vec, Uint32VecReader); @@ -206,3 +432,13 @@ impl_conversion_for_option_pack!(&str, BytesOpt); impl_conversion_for_option_pack!(String, BytesOpt); impl_conversion_for_option_pack!(Bytes, BytesOpt); impl_conversion_for_packed_optional_pack!(Bytes, BytesOpt); + +impl_conversion_for_option_from_into!(bool, BoolOpt, BoolOptReader, Bool); +impl_conversion_for_vector_from_into!(u32, Uint32Vec, Uint32VecReader); +impl_conversion_for_vector_from_into!(usize, Uint32Vec, Uint32VecReader); +impl_conversion_for_vector_from_into!(u64, Uint64Vec, Uint64VecReader); + +impl_conversion_for_option_from!(&str, BytesOpt, Bytes); +impl_conversion_for_option_from!(String, BytesOpt, Bytes); +impl_conversion_for_option_from!(Bytes, BytesOpt, Bytes); +impl_conversion_for_packed_optional_from!(Bytes, BytesOpt); diff --git a/util/gen-types/src/conversion/utilities.rs b/util/gen-types/src/conversion/utilities.rs index cd5faa9a78..7ddb20265b 100644 --- a/util/gen-types/src/conversion/utilities.rs +++ b/util/gen-types/src/conversion/utilities.rs @@ -8,6 +8,22 @@ macro_rules! impl_conversion_for_entity_unpack { }; } +macro_rules! impl_conversion_for_entity_from { + ($original:ty, $entity:ident) => { + impl From for $original { + fn from(v: packed::$entity) -> $original { + (&v).into() + } + } + + impl From<&packed::$entity> for $original { + fn from(v: &packed::$entity) -> $original { + v.as_reader().into() + } + } + }; +} + macro_rules! impl_conversion_for_option_pack { ($original:ty, $entity:ident) => { impl Pack for Option<$original> { @@ -22,6 +38,22 @@ macro_rules! impl_conversion_for_option_pack { }; } +macro_rules! impl_conversion_for_option_from { + ($original:ty, $entity:ident, $entity_inner:ident) => { + impl From> for packed::$entity { + fn from(value: Option<$original>) -> packed::$entity { + if let Some(inner) = value { + packed::$entity::new_unchecked( + Into::::into(inner).as_bytes(), + ) + } else { + packed::$entity::default() + } + } + } + }; +} + macro_rules! impl_conversion_for_option_unpack { ($original:ty, $entity:ident, $reader:ident) => { impl<'r> Unpack> for packed::$reader<'r> { @@ -33,6 +65,17 @@ macro_rules! impl_conversion_for_option_unpack { }; } +macro_rules! impl_conversion_for_option_into { + ($original:ty, $entity:ident, $reader:ident) => { + impl<'r> Into> for packed::$reader<'r> { + fn into(self) -> Option<$original> { + self.to_opt().map(|x| x.into()) + } + } + impl_conversion_for_entity_from!(Option<$original>, $entity); + }; +} + macro_rules! impl_conversion_for_option { ($original:ty, $entity:ident, $reader:ident) => { impl_conversion_for_option_pack!($original, $entity); @@ -40,6 +83,13 @@ macro_rules! impl_conversion_for_option { }; } +macro_rules! impl_conversion_for_option_from_into { + ($original:ty, $entity:ident, $reader:ident, $entity_inner:ident) => { + impl_conversion_for_option_from!($original, $entity, $entity_inner); + impl_conversion_for_option_into!($original, $entity, $reader); + }; +} + macro_rules! impl_conversion_for_vector_pack { ($original:ty, $entity:ident) => { impl Pack for [$original] { @@ -52,6 +102,30 @@ macro_rules! impl_conversion_for_vector_pack { }; } +macro_rules! impl_conversion_for_vector_from { + ($original:ty, $entity:ident) => { + impl From<&[$original]> for packed::$entity { + fn from(value: &[$original]) -> packed::$entity { + packed::$entity::new_builder() + .set(value.iter().map(|v| v.into()).collect()) + .build() + } + } + + impl From<[$original; N]> for packed::$entity { + fn from(value: [$original; N]) -> packed::$entity { + (&value[..]).into() + } + } + + impl From<&[$original; N]> for packed::$entity { + fn from(value: &[$original; N]) -> packed::$entity { + (&value[..]).into() + } + } + }; +} + macro_rules! impl_conversion_for_vector_unpack { ($original:ty, $entity:ident, $reader:ident) => { impl<'r> Unpack> for packed::$reader<'r> { @@ -63,6 +137,17 @@ macro_rules! impl_conversion_for_vector_unpack { }; } +macro_rules! impl_conversion_for_vector_into { + ($original:ty, $entity:ident, $reader:ident) => { + impl<'r> Into> for packed::$reader<'r> { + fn into(self) -> Vec<$original> { + self.iter().map(|x| x.into()).collect() + } + } + impl_conversion_for_entity_from!(Vec<$original>, $entity); + }; +} + macro_rules! impl_conversion_for_vector { ($original:ty, $entity:ident, $reader:ident) => { impl_conversion_for_vector_pack!($original, $entity); @@ -70,6 +155,13 @@ macro_rules! impl_conversion_for_vector { }; } +macro_rules! impl_conversion_for_vector_from_into { + ($original:ty, $entity:ident, $reader:ident) => { + impl_conversion_for_vector_from!($original, $entity); + impl_conversion_for_vector_into!($original, $entity, $reader); + }; +} + macro_rules! impl_conversion_for_packed_optional_pack { ($original:ident, $entity:ident) => { impl Pack for Option { @@ -84,6 +176,20 @@ macro_rules! impl_conversion_for_packed_optional_pack { }; } +macro_rules! impl_conversion_for_packed_optional_from { + ($original:ident, $entity:ident) => { + impl From> for packed::$entity { + fn from(value: Option) -> packed::$entity { + if let Some(ref inner) = value { + packed::$entity::new_unchecked(inner.as_bytes()) + } else { + packed::$entity::default() + } + } + } + }; +} + macro_rules! impl_conversion_for_packed_iterator_pack { ($item:ident, $vec:ident) => { impl PackVec for T diff --git a/util/gen-types/src/extension/capacity.rs b/util/gen-types/src/extension/capacity.rs index ac4478378f..7e958d49da 100644 --- a/util/gen-types/src/extension/capacity.rs +++ b/util/gen-types/src/extension/capacity.rs @@ -46,7 +46,7 @@ impl packed::CellOutput { /// [`occupied capacity`]: #method.occupied_capacity pub fn is_lack_of_capacity(&self, data_capacity: Capacity) -> CapacityResult { self.occupied_capacity(data_capacity) - .map(|cap| cap > self.capacity().unpack()) + .map(|cap| cap > self.capacity().into()) } } @@ -71,7 +71,7 @@ impl packed::CellOutputBuilder { .transpose() .and_then(|y| y.unwrap_or_else(Capacity::zero).safe_add(x)) }) - .map(|x| self.capacity(x.pack()).build()) + .map(|x| self.capacity(x.into()).build()) } } @@ -83,7 +83,7 @@ impl packed::CellOutputVec { self.as_reader() .iter() .map(|output| { - let cap: Capacity = output.capacity().unpack(); + let cap: Capacity = output.capacity().into(); cap }) .try_fold(Capacity::zero(), Capacity::safe_add) diff --git a/util/gen-types/src/extension/rust_core_traits.rs b/util/gen-types/src/extension/rust_core_traits.rs index 792895cf93..631224abdb 100644 --- a/util/gen-types/src/extension/rust_core_traits.rs +++ b/util/gen-types/src/extension/rust_core_traits.rs @@ -52,8 +52,8 @@ macro_rules! impl_cmp_partial_ord { impl ::core::cmp::Ord for packed::Uint32 { #[inline] fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - let self_val: u32 = self.unpack(); - let other_val: u32 = other.unpack(); + let self_val: u32 = self.as_reader().into(); + let other_val: u32 = other.as_reader().into(); self_val.cmp(&other_val) } } @@ -62,8 +62,8 @@ impl_cmp_partial_ord!(Uint32); impl ::core::cmp::Ord for packed::Uint64 { #[inline] fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - let self_val: u64 = self.unpack(); - let other_val: u64 = other.unpack(); + let self_val: u64 = self.as_reader().into(); + let other_val: u64 = other.as_reader().into(); self_val.cmp(&other_val) } } @@ -72,8 +72,8 @@ impl_cmp_partial_ord!(Uint64); impl ::core::cmp::Ord for packed::Uint128 { #[inline] fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - let self_val: u128 = self.unpack(); - let other_val: u128 = other.unpack(); + let self_val: u128 = self.as_reader().into(); + let other_val: u128 = other.as_reader().into(); self_val.cmp(&other_val) } } @@ -81,14 +81,14 @@ impl_cmp_partial_ord!(Uint128); #[cfg(feature = "std")] mod std_feature_mod { - use crate::{packed, prelude::*}; + use crate::packed; use numext_fixed_uint::U256; impl ::core::cmp::Ord for packed::Uint256 { #[inline] fn cmp(&self, other: &Self) -> ::core::cmp::Ordering { - let self_val: U256 = self.unpack(); - let other_val: U256 = other.unpack(); + let self_val: U256 = self.as_reader().into(); + let other_val: U256 = other.as_reader().into(); self_val.cmp(&other_val) } } diff --git a/util/gen-types/src/extension/shortcut.rs b/util/gen-types/src/extension/shortcut.rs index 499145ee90..6fcc732951 100644 --- a/util/gen-types/src/extension/shortcut.rs +++ b/util/gen-types/src/extension/shortcut.rs @@ -10,7 +10,7 @@ impl packed::Byte32 { /// Creates a new `Byte32` whose bits are all ones. pub fn max_value() -> Self { - [u8::max_value(); 32].pack() + [u8::max_value(); 32].into() } /// Checks whether all bits in self are zeros. @@ -20,7 +20,7 @@ impl packed::Byte32 { /// Creates a new `Bytes32`. pub fn new(v: [u8; 32]) -> Self { - v.pack() + v.into() } } @@ -29,7 +29,7 @@ impl packed::ProposalShortId { pub fn from_tx_hash(h: &packed::Byte32) -> Self { let mut inner = [0u8; 10]; inner.copy_from_slice(&h.as_slice()[..10]); - inner.pack() + inner.into() } /// Creates a new `ProposalShortId` whose bits are all zeros. @@ -39,7 +39,7 @@ impl packed::ProposalShortId { /// Creates a new `ProposalShortId`. pub fn new(v: [u8; 10]) -> Self { - v.pack() + v.into() } } @@ -48,20 +48,20 @@ impl packed::OutPoint { pub fn new(tx_hash: packed::Byte32, index: u32) -> Self { packed::OutPoint::new_builder() .tx_hash(tx_hash) - .index(index.pack()) + .index(index.into()) .build() } /// Creates a new null `OutPoint`. pub fn null() -> Self { packed::OutPoint::new_builder() - .index(u32::max_value().pack()) + .index(u32::max_value().into()) .build() } /// Checks whether self is a null `OutPoint`. pub fn is_null(&self) -> bool { - self.tx_hash().is_zero() && Unpack::::unpack(&self.index()) == u32::max_value() + self.tx_hash().is_zero() && Into::::into(self.index().as_reader()) == u32::max_value() } /// Generates a binary data to be used as a key for indexing cells in storage. @@ -82,7 +82,7 @@ impl packed::OutPoint { /// order, so as to traverse cells in the forward order too. pub fn to_cell_key(&self) -> Vec { let mut key = Vec::with_capacity(36); - let index: u32 = self.index().unpack(); + let index: u32 = self.index().as_reader().into(); key.extend_from_slice(self.tx_hash().as_slice()); key.extend_from_slice(&index.to_be_bytes()); key @@ -93,7 +93,7 @@ impl packed::CellInput { /// Creates a new `CellInput`. pub fn new(previous_output: packed::OutPoint, block_number: BlockNumber) -> Self { packed::CellInput::new_builder() - .since(block_number.pack()) + .since(block_number.into()) .previous_output(previous_output) .build() } @@ -110,7 +110,7 @@ impl packed::Script { .lock(self) .build() .as_bytes() - .pack() + .into() } /// Converts from bytes of [`CellbaseWitness`](struct.CellbaseWitness.html). diff --git a/util/gen-types/src/generated/blockchain.rs b/util/gen-types/src/generated/blockchain.rs index 3eb5b9b6b9..e9dae42fab 100644 --- a/util/gen-types/src/generated/blockchain.rs +++ b/util/gen-types/src/generated/blockchain.rs @@ -1,4 +1,4 @@ -// Generated by Molecule 0.7.5 +// Generated by Molecule 0.8.0 use molecule::prelude::*; #[derive(Clone)] @@ -143,6 +143,7 @@ impl<'r> molecule::prelude::Reader<'r> for Uint32Reader<'r> { Ok(()) } } +#[derive(Clone)] pub struct Uint32Builder(pub(crate) [Byte; 4]); impl ::core::fmt::Debug for Uint32Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { @@ -204,6 +205,54 @@ impl molecule::prelude::Builder for Uint32Builder { Uint32::new_unchecked(inner.into()) } } +impl From<[Byte; 4usize]> for Uint32 { + fn from(value: [Byte; 4usize]) -> Self { + Self::new_builder().set(value).build() + } +} +impl ::core::convert::TryFrom<&[Byte]> for Uint32 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[Byte]) -> Result { + Ok(Self::new_builder() + .set(<&[Byte; 4usize]>::try_from(value)?.clone()) + .build()) + } +} +impl From for [Byte; 4usize] { + #[track_caller] + fn from(value: Uint32) -> Self { + [value.nth0(), value.nth1(), value.nth2(), value.nth3()] + } +} +impl From<[u8; 4usize]> for Uint32 { + fn from(value: [u8; 4usize]) -> Self { + Uint32Reader::new_unchecked(&value).to_entity() + } +} +impl ::core::convert::TryFrom<&[u8]> for Uint32 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + Ok(<[u8; 4usize]>::try_from(value)?.into()) + } +} +impl From for [u8; 4usize] { + #[track_caller] + fn from(value: Uint32) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From> for &'a [u8; 4usize] { + #[track_caller] + fn from(value: Uint32Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From<&'a Uint32Reader<'a>> for &'a [u8; 4usize] { + #[track_caller] + fn from(value: &'a Uint32Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} #[derive(Clone)] pub struct Uint64(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Uint64 { @@ -379,6 +428,7 @@ impl<'r> molecule::prelude::Reader<'r> for Uint64Reader<'r> { Ok(()) } } +#[derive(Clone)] pub struct Uint64Builder(pub(crate) [Byte; 8]); impl ::core::fmt::Debug for Uint64Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { @@ -464,6 +514,63 @@ impl molecule::prelude::Builder for Uint64Builder { Uint64::new_unchecked(inner.into()) } } +impl From<[Byte; 8usize]> for Uint64 { + fn from(value: [Byte; 8usize]) -> Self { + Self::new_builder().set(value).build() + } +} +impl ::core::convert::TryFrom<&[Byte]> for Uint64 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[Byte]) -> Result { + Ok(Self::new_builder() + .set(<&[Byte; 8usize]>::try_from(value)?.clone()) + .build()) + } +} +impl From for [Byte; 8usize] { + #[track_caller] + fn from(value: Uint64) -> Self { + [ + value.nth0(), + value.nth1(), + value.nth2(), + value.nth3(), + value.nth4(), + value.nth5(), + value.nth6(), + value.nth7(), + ] + } +} +impl From<[u8; 8usize]> for Uint64 { + fn from(value: [u8; 8usize]) -> Self { + Uint64Reader::new_unchecked(&value).to_entity() + } +} +impl ::core::convert::TryFrom<&[u8]> for Uint64 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + Ok(<[u8; 8usize]>::try_from(value)?.into()) + } +} +impl From for [u8; 8usize] { + #[track_caller] + fn from(value: Uint64) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From> for &'a [u8; 8usize] { + #[track_caller] + fn from(value: Uint64Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From<&'a Uint64Reader<'a>> for &'a [u8; 8usize] { + #[track_caller] + fn from(value: &'a Uint64Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} #[derive(Clone)] pub struct Uint128(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Uint128 { @@ -695,6 +802,7 @@ impl<'r> molecule::prelude::Reader<'r> for Uint128Reader<'r> { Ok(()) } } +#[derive(Clone)] pub struct Uint128Builder(pub(crate) [Byte; 16]); impl ::core::fmt::Debug for Uint128Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { @@ -828,6 +936,71 @@ impl molecule::prelude::Builder for Uint128Builder { Uint128::new_unchecked(inner.into()) } } +impl From<[Byte; 16usize]> for Uint128 { + fn from(value: [Byte; 16usize]) -> Self { + Self::new_builder().set(value).build() + } +} +impl ::core::convert::TryFrom<&[Byte]> for Uint128 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[Byte]) -> Result { + Ok(Self::new_builder() + .set(<&[Byte; 16usize]>::try_from(value)?.clone()) + .build()) + } +} +impl From for [Byte; 16usize] { + #[track_caller] + fn from(value: Uint128) -> Self { + [ + value.nth0(), + value.nth1(), + value.nth2(), + value.nth3(), + value.nth4(), + value.nth5(), + value.nth6(), + value.nth7(), + value.nth8(), + value.nth9(), + value.nth10(), + value.nth11(), + value.nth12(), + value.nth13(), + value.nth14(), + value.nth15(), + ] + } +} +impl From<[u8; 16usize]> for Uint128 { + fn from(value: [u8; 16usize]) -> Self { + Uint128Reader::new_unchecked(&value).to_entity() + } +} +impl ::core::convert::TryFrom<&[u8]> for Uint128 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + Ok(<[u8; 16usize]>::try_from(value)?.into()) + } +} +impl From for [u8; 16usize] { + #[track_caller] + fn from(value: Uint128) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From> for &'a [u8; 16usize] { + #[track_caller] + fn from(value: Uint128Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From<&'a Uint128Reader<'a>> for &'a [u8; 16usize] { + #[track_caller] + fn from(value: &'a Uint128Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} #[derive(Clone)] pub struct Byte32(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Byte32 { @@ -1174,6 +1347,7 @@ impl<'r> molecule::prelude::Reader<'r> for Byte32Reader<'r> { Ok(()) } } +#[derive(Clone)] pub struct Byte32Builder(pub(crate) [Byte; 32]); impl ::core::fmt::Debug for Byte32Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { @@ -1403,6 +1577,87 @@ impl molecule::prelude::Builder for Byte32Builder { Byte32::new_unchecked(inner.into()) } } +impl From<[Byte; 32usize]> for Byte32 { + fn from(value: [Byte; 32usize]) -> Self { + Self::new_builder().set(value).build() + } +} +impl ::core::convert::TryFrom<&[Byte]> for Byte32 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[Byte]) -> Result { + Ok(Self::new_builder() + .set(<&[Byte; 32usize]>::try_from(value)?.clone()) + .build()) + } +} +impl From for [Byte; 32usize] { + #[track_caller] + fn from(value: Byte32) -> Self { + [ + value.nth0(), + value.nth1(), + value.nth2(), + value.nth3(), + value.nth4(), + value.nth5(), + value.nth6(), + value.nth7(), + value.nth8(), + value.nth9(), + value.nth10(), + value.nth11(), + value.nth12(), + value.nth13(), + value.nth14(), + value.nth15(), + value.nth16(), + value.nth17(), + value.nth18(), + value.nth19(), + value.nth20(), + value.nth21(), + value.nth22(), + value.nth23(), + value.nth24(), + value.nth25(), + value.nth26(), + value.nth27(), + value.nth28(), + value.nth29(), + value.nth30(), + value.nth31(), + ] + } +} +impl From<[u8; 32usize]> for Byte32 { + fn from(value: [u8; 32usize]) -> Self { + Byte32Reader::new_unchecked(&value).to_entity() + } +} +impl ::core::convert::TryFrom<&[u8]> for Byte32 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + Ok(<[u8; 32usize]>::try_from(value)?.into()) + } +} +impl From for [u8; 32usize] { + #[track_caller] + fn from(value: Byte32) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From> for &'a [u8; 32usize] { + #[track_caller] + fn from(value: Byte32Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From<&'a Byte32Reader<'a>> for &'a [u8; 32usize] { + #[track_caller] + fn from(value: &'a Byte32Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} #[derive(Clone)] pub struct Uint256(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Uint256 { @@ -1749,6 +2004,7 @@ impl<'r> molecule::prelude::Reader<'r> for Uint256Reader<'r> { Ok(()) } } +#[derive(Clone)] pub struct Uint256Builder(pub(crate) [Byte; 32]); impl ::core::fmt::Debug for Uint256Builder { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { @@ -1978,6 +2234,87 @@ impl molecule::prelude::Builder for Uint256Builder { Uint256::new_unchecked(inner.into()) } } +impl From<[Byte; 32usize]> for Uint256 { + fn from(value: [Byte; 32usize]) -> Self { + Self::new_builder().set(value).build() + } +} +impl ::core::convert::TryFrom<&[Byte]> for Uint256 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[Byte]) -> Result { + Ok(Self::new_builder() + .set(<&[Byte; 32usize]>::try_from(value)?.clone()) + .build()) + } +} +impl From for [Byte; 32usize] { + #[track_caller] + fn from(value: Uint256) -> Self { + [ + value.nth0(), + value.nth1(), + value.nth2(), + value.nth3(), + value.nth4(), + value.nth5(), + value.nth6(), + value.nth7(), + value.nth8(), + value.nth9(), + value.nth10(), + value.nth11(), + value.nth12(), + value.nth13(), + value.nth14(), + value.nth15(), + value.nth16(), + value.nth17(), + value.nth18(), + value.nth19(), + value.nth20(), + value.nth21(), + value.nth22(), + value.nth23(), + value.nth24(), + value.nth25(), + value.nth26(), + value.nth27(), + value.nth28(), + value.nth29(), + value.nth30(), + value.nth31(), + ] + } +} +impl From<[u8; 32usize]> for Uint256 { + fn from(value: [u8; 32usize]) -> Self { + Uint256Reader::new_unchecked(&value).to_entity() + } +} +impl ::core::convert::TryFrom<&[u8]> for Uint256 { + type Error = ::core::array::TryFromSliceError; + fn try_from(value: &[u8]) -> Result { + Ok(<[u8; 32usize]>::try_from(value)?.into()) + } +} +impl From for [u8; 32usize] { + #[track_caller] + fn from(value: Uint256) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From> for &'a [u8; 32usize] { + #[track_caller] + fn from(value: Uint256Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} +impl<'a> From<&'a Uint256Reader<'a>> for &'a [u8; 32usize] { + #[track_caller] + fn from(value: &'a Uint256Reader<'a>) -> Self { + ::core::convert::TryFrom::try_from(value.as_slice()).unwrap() + } +} #[derive(Clone)] pub struct Bytes(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Bytes { @@ -2151,7 +2488,7 @@ impl<'r> molecule::prelude::Reader<'r> for BytesReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct BytesBuilder(pub(crate) Vec); impl BytesBuilder { pub const ITEM_SIZE: usize = 1; @@ -2164,9 +2501,7 @@ impl BytesBuilder { self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } pub fn replace(&mut self, index: usize, v: Byte) -> Option { @@ -2221,6 +2556,30 @@ impl ::core::iter::IntoIterator for Bytes { BytesIterator(self, 0, len) } } +impl ::core::iter::FromIterator for Bytes { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for Bytes { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} +impl ::core::iter::FromIterator for Bytes { + fn from_iter>(iter: T) -> Self { + Self::new_builder() + .extend(iter.into_iter().map(Into::into)) + .build() + } +} +impl From> for Bytes { + fn from(v: Vec) -> Self { + Self::new_builder() + .set(v.into_iter().map(Into::into).collect()) + .build() + } +} #[derive(Clone)] pub struct BytesOpt(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for BytesOpt { @@ -2355,7 +2714,7 @@ impl<'r> molecule::prelude::Reader<'r> for BytesOptReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct BytesOptBuilder(pub(crate) Option); impl BytesOptBuilder { pub fn set(mut self, v: Option) -> Self { @@ -2385,6 +2744,11 @@ impl molecule::prelude::Builder for BytesOptBuilder { BytesOpt::new_unchecked(inner.into()) } } +impl From for BytesOpt { + fn from(value: Bytes) -> Self { + Self::new_builder().set(Some(value)).build() + } +} #[derive(Clone)] pub struct BytesOptVec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for BytesOptVec { @@ -2608,7 +2972,7 @@ impl<'r> molecule::prelude::Reader<'r> for BytesOptVecReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct BytesOptVecBuilder(pub(crate) Vec); impl BytesOptVecBuilder { pub fn set(mut self, v: Vec) -> Self { @@ -2620,9 +2984,7 @@ impl BytesOptVecBuilder { self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } pub fn replace(&mut self, index: usize, v: BytesOpt) -> Option { @@ -2725,6 +3087,16 @@ impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesOptVecReaderIterator<' self.2 - self.1 } } +impl ::core::iter::FromIterator for BytesOptVec { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for BytesOptVec { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} #[derive(Clone)] pub struct BytesVec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for BytesVec { @@ -2948,7 +3320,7 @@ impl<'r> molecule::prelude::Reader<'r> for BytesVecReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct BytesVecBuilder(pub(crate) Vec); impl BytesVecBuilder { pub fn set(mut self, v: Vec) -> Self { @@ -2960,9 +3332,7 @@ impl BytesVecBuilder { self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } pub fn replace(&mut self, index: usize, v: Bytes) -> Option { @@ -3065,6 +3435,16 @@ impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for BytesVecReaderIterator<'t, self.2 - self.1 } } +impl ::core::iter::FromIterator for BytesVec { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for BytesVec { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} #[derive(Clone)] pub struct Byte32Vec(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for Byte32Vec { @@ -3244,7 +3624,7 @@ impl<'r> molecule::prelude::Reader<'r> for Byte32VecReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct Byte32VecBuilder(pub(crate) Vec); impl Byte32VecBuilder { pub const ITEM_SIZE: usize = 32; @@ -3257,9 +3637,7 @@ impl Byte32VecBuilder { self } pub fn extend>(mut self, iter: T) -> Self { - for elem in iter { - self.0.push(elem); - } + self.0.extend(iter); self } pub fn replace(&mut self, index: usize, v: Byte32) -> Option { @@ -3337,6 +3715,16 @@ impl<'t: 'r, 'r> ::core::iter::ExactSizeIterator for Byte32VecReaderIterator<'t, self.2 - self.1 } } +impl ::core::iter::FromIterator for Byte32Vec { + fn from_iter>(iter: T) -> Self { + Self::new_builder().extend(iter).build() + } +} +impl From> for Byte32Vec { + fn from(v: Vec) -> Self { + Self::new_builder().set(v).build() + } +} #[derive(Clone)] pub struct ScriptOpt(molecule::bytes::Bytes); impl ::core::fmt::LowerHex for ScriptOpt { @@ -3471,7 +3859,7 @@ impl<'r> molecule::prelude::Reader<'r> for ScriptOptReader<'r> { Ok(()) } } -#[derive(Debug, Default)] +#[derive(Clone, Debug, Default)] pub struct ScriptOptBuilder(pub(crate) Option