Skip to content

Commit

Permalink
feat: basic impl for molecule
Browse files Browse the repository at this point in the history
  • Loading branch information
driftluo committed Jul 10, 2024
1 parent aca6d8e commit ed102b7
Show file tree
Hide file tree
Showing 10 changed files with 1,655 additions and 207 deletions.
55 changes: 55 additions & 0 deletions util/gen-types/src/conversion/blockchain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ impl Pack<packed::Byte32> 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];
Expand All @@ -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<packed::ProposalShortId> for [u8; 10] {
fn pack(&self) -> packed::ProposalShortId {
packed::ProposalShortId::from_slice(&self[..])
Expand All @@ -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<packed::Bytes> for Bytes {
fn pack(&self) -> packed::Bytes {
let len = (self.len() as u32).to_le_bytes();
Expand All @@ -44,18 +66,46 @@ impl Pack<packed::Bytes> 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<Bytes> for packed::Bytes {
fn from(value: Bytes) -> Self {
(&value).into()
}
}

impl<'r> Unpack<Bytes> for packed::BytesReader<'r> {
fn unpack(&self) -> Bytes {
Bytes::from(self.raw_data().to_owned())
}
}

impl<'r> Into<Bytes> for packed::BytesReader<'r> {
fn into(self) -> Bytes {
Bytes::from(self.raw_data().to_owned())
}
}

impl Unpack<Bytes> for packed::Bytes {
fn unpack(&self) -> Bytes {
self.raw_data()
}
}

impl Into<Bytes> 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);
Expand All @@ -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);
61 changes: 61 additions & 0 deletions util/gen-types/src/conversion/blockchain/std_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,99 @@ impl Pack<packed::Uint64> for Capacity {
}
}

impl From<Capacity> 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<Capacity> for packed::Uint64Reader<'r> {
fn unpack(&self) -> Capacity {
Capacity::shannons(self.unpack())
}
}
impl_conversion_for_entity_unpack!(Capacity, Uint64);

impl<'r> Into<Capacity> for packed::Uint64Reader<'r> {
fn into(self) -> Capacity {
Capacity::shannons(self.into())
}
}
impl_conversion_for_entity_from!(Capacity, Uint64);

impl Pack<packed::Uint256> for U256 {
fn pack(&self) -> packed::Uint256 {
packed::Uint256::from_slice(&self.to_le_bytes()[..]).expect("impossible: fail to pack U256")
}
}

impl From<U256> 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<U256> for packed::Uint256Reader<'r> {
fn unpack(&self) -> U256 {
U256::from_little_endian(self.as_slice()).expect("internal error: fail to unpack U256")
}
}
impl_conversion_for_entity_unpack!(U256, Uint256);

impl<'r> Into<U256> 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<packed::Byte32> for H256 {
fn pack(&self) -> packed::Byte32 {
packed::Byte32::from_slice(self.as_bytes()).expect("impossible: fail to pack H256")
}
}

impl From<H256> 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<H256> for packed::Byte32Reader<'r> {
fn unpack(&self) -> H256 {
H256::from_slice(self.as_slice()).expect("internal error: fail to unpack H256")
}
}
impl_conversion_for_entity_unpack!(H256, Byte32);

impl<'r> Into<H256> 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);
Loading

0 comments on commit ed102b7

Please sign in to comment.