-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4073 from EthanYuan/refactor-ckb-types
refactor: split ckb-gen-types from ckb-types
- Loading branch information
Showing
56 changed files
with
1,213 additions
and
668 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[package] | ||
name = "ckb-gen-types" | ||
version = "0.112.0-pre" | ||
authors = ["Nervos Core Dev <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT" | ||
description = "Provides the generated types for CKB." | ||
homepage = "https://github.com/nervosnetwork/ckb" | ||
repository = "https://github.com/nervosnetwork/ckb" | ||
|
||
[dev-dependencies] | ||
ckb-hash = {path = "../hash", version = "= 0.112.0-pre"} | ||
|
||
[features] | ||
default = ["std"] | ||
# Enable the `calc-hash` extension for CKB contract development in `no-std` env | ||
calc-hash = ["ckb-hash/ckb-contract"] | ||
# Enable the `check-data` extension for CKB contract development in `no-std` env | ||
check-data = [] | ||
# Enable the `serialized-size` extension for CKB contract development in `no-std` env | ||
serialized-size = ["calc-hash"] | ||
# Enable all in `std` env | ||
std = ["molecule/std", "ckb-fixed-hash", "ckb-error", "ckb-occupied-capacity", "numext-fixed-uint"] | ||
|
||
[dependencies] | ||
cfg-if = "1.0" | ||
ckb-hash = { path = "../hash", version = "= 0.112.0-pre" } | ||
molecule = { version = "0.7.5", default-features = false } | ||
ckb-fixed-hash = { path = "../fixed-hash", version = "= 0.112.0-pre", optional = true } | ||
ckb-error = { path = "../../error", version = "= 0.112.0-pre", optional = true } | ||
ckb-occupied-capacity = { path = "../occupied-capacity", version = "= 0.112.0-pre", optional = true } | ||
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"], optional = true } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#[cfg(feature = "std")] | ||
mod std_env; | ||
|
||
use crate::{borrow::ToOwned, bytes::Bytes, generated::packed, prelude::*, vec::Vec}; | ||
|
||
impl Pack<packed::Byte32> for [u8; 32] { | ||
fn pack(&self) -> packed::Byte32 { | ||
packed::Byte32::from_slice(&self[..]).expect("impossible: fail to pack [u8; 32]") | ||
} | ||
} | ||
|
||
impl Pack<packed::ProposalShortId> for [u8; 10] { | ||
fn pack(&self) -> packed::ProposalShortId { | ||
packed::ProposalShortId::from_slice(&self[..]) | ||
.expect("impossible: fail to pack to ProposalShortId") | ||
} | ||
} | ||
|
||
impl Pack<packed::Bytes> for Bytes { | ||
fn pack(&self) -> packed::Bytes { | ||
let len = (self.len() as u32).to_le_bytes(); | ||
let mut v = Vec::with_capacity(4 + self.len()); | ||
v.extend_from_slice(&len[..]); | ||
v.extend_from_slice(&self[..]); | ||
packed::Bytes::new_unchecked(v.into()) | ||
} | ||
} | ||
|
||
impl<'r> Unpack<Bytes> for packed::BytesReader<'r> { | ||
fn unpack(&self) -> Bytes { | ||
Bytes::from(self.raw_data().to_owned()) | ||
} | ||
} | ||
|
||
impl Unpack<Bytes> for packed::Bytes { | ||
fn unpack(&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); | ||
impl_conversion_for_packed_optional_pack!(Script, ScriptOpt); | ||
impl_conversion_for_packed_iterator_pack!(ProposalShortId, ProposalShortIdVec); | ||
impl_conversion_for_packed_iterator_pack!(Bytes, BytesVec); | ||
impl_conversion_for_packed_iterator_pack!(Transaction, TransactionVec); | ||
impl_conversion_for_packed_iterator_pack!(OutPoint, OutPointVec); | ||
impl_conversion_for_packed_iterator_pack!(CellDep, CellDepVec); | ||
impl_conversion_for_packed_iterator_pack!(CellOutput, CellOutputVec); | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use ckb_fixed_hash::H256; | ||
use ckb_occupied_capacity::Capacity; | ||
use numext_fixed_uint::U256; | ||
|
||
use crate::{packed, prelude::*}; | ||
|
||
impl Pack<packed::Uint64> for Capacity { | ||
fn pack(&self) -> packed::Uint64 { | ||
self.as_u64().pack() | ||
} | ||
} | ||
|
||
impl<'r> Unpack<Capacity> for packed::Uint64Reader<'r> { | ||
fn unpack(&self) -> Capacity { | ||
Capacity::shannons(self.unpack()) | ||
} | ||
} | ||
impl_conversion_for_entity_unpack!(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<'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 Pack<packed::Byte32> for H256 { | ||
fn pack(&self) -> packed::Byte32 { | ||
packed::Byte32::from_slice(self.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_conversion_for_option!(H256, Byte32Opt, Byte32OptReader); | ||
impl_conversion_for_vector!(Capacity, Uint64Vec, Uint64VecReader); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[macro_use] | ||
mod utilities; | ||
|
||
mod blockchain; | ||
mod network; | ||
mod primitive; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
macro_rules! impl_conversion_for_entity_unpack { | ||
($original:ty, $entity:ident) => { | ||
impl Unpack<$original> for packed::$entity { | ||
fn unpack(&self) -> $original { | ||
self.as_reader().unpack() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_option_pack { | ||
($original:ty, $entity:ident) => { | ||
impl Pack<packed::$entity> for Option<$original> { | ||
fn pack(&self) -> packed::$entity { | ||
if let Some(ref inner) = self { | ||
packed::$entity::new_unchecked(inner.pack().as_bytes()) | ||
} else { | ||
packed::$entity::default() | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_option_unpack { | ||
($original:ty, $entity:ident, $reader:ident) => { | ||
impl<'r> Unpack<Option<$original>> for packed::$reader<'r> { | ||
fn unpack(&self) -> Option<$original> { | ||
self.to_opt().map(|x| x.unpack()) | ||
} | ||
} | ||
impl_conversion_for_entity_unpack!(Option<$original>, $entity); | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_option { | ||
($original:ty, $entity:ident, $reader:ident) => { | ||
impl_conversion_for_option_pack!($original, $entity); | ||
impl_conversion_for_option_unpack!($original, $entity, $reader); | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_vector_pack { | ||
($original:ty, $entity:ident) => { | ||
impl Pack<packed::$entity> for [$original] { | ||
fn pack(&self) -> packed::$entity { | ||
packed::$entity::new_builder() | ||
.set(self.iter().map(|v| v.pack()).collect()) | ||
.build() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_vector_unpack { | ||
($original:ty, $entity:ident, $reader:ident) => { | ||
impl<'r> Unpack<Vec<$original>> for packed::$reader<'r> { | ||
fn unpack(&self) -> Vec<$original> { | ||
self.iter().map(|x| x.unpack()).collect() | ||
} | ||
} | ||
impl_conversion_for_entity_unpack!(Vec<$original>, $entity); | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_vector { | ||
($original:ty, $entity:ident, $reader:ident) => { | ||
impl_conversion_for_vector_pack!($original, $entity); | ||
impl_conversion_for_vector_unpack!($original, $entity, $reader); | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_packed_optional_pack { | ||
($original:ident, $entity:ident) => { | ||
impl Pack<packed::$entity> for Option<packed::$original> { | ||
fn pack(&self) -> packed::$entity { | ||
if let Some(ref inner) = self { | ||
packed::$entity::new_unchecked(inner.as_bytes()) | ||
} else { | ||
packed::$entity::default() | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! impl_conversion_for_packed_iterator_pack { | ||
($item:ident, $vec:ident) => { | ||
impl<T> PackVec<packed::$vec, packed::$item> for T | ||
where | ||
T: IntoIterator<Item = packed::$item>, | ||
{ | ||
fn pack(self) -> packed::$vec { | ||
packed::$vec::new_builder().extend(self).build() | ||
} | ||
} | ||
}; | ||
} |
Oops, something went wrong.