mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 14:31:02 +00:00
Introduce 128-bit shim for u128/i128 and use it for Balance (#163)
* Add 128-bit shim and use for balance. * Fix tests. * Fix wasm build.
This commit is contained in:
committed by
Robert Habermeier
parent
5f9aca2137
commit
24d7d38c62
Generated
+6
-6
@@ -145,7 +145,7 @@ version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
@@ -764,7 +764,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "integer-sqrt"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#f4cf61482096dc98c1273f46a10849d182b4c23c"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#886e9cb983c46498003878afe965d55caa762025"
|
||||
|
||||
[[package]]
|
||||
name = "interleaved-ordered"
|
||||
@@ -1081,7 +1081,7 @@ name = "num-integer"
|
||||
version = "0.1.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1091,7 +1091,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.2"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@@ -2153,7 +2153,7 @@ name = "substrate-runtime-primitives"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"integer-sqrt 0.1.0 (git+https://github.com/paritytech/integer-sqrt-rs.git)",
|
||||
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"substrate-codec 0.1.0",
|
||||
@@ -2840,7 +2840,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
|
||||
"checksum num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f8d26da319fb45674985c78f1d1caf99aa4941f785d384a2ae36d0740bc3e2fe"
|
||||
"checksum num-traits 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "9936036cc70fe4a8b2d338ab665900323290efb03983c86cbe235ae800ad8017"
|
||||
"checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364"
|
||||
"checksum num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775393e285254d2f5004596d69bb8bc1149754570dcc08cf30cabeba67955e28"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum ole32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5d2c49021782e5233cd243168edfa8037574afed4eba4bbaf538b3d8d1789d8c"
|
||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||
|
||||
Generated
+4
-4
@@ -194,7 +194,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "integer-sqrt"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#f4cf61482096dc98c1273f46a10849d182b4c23c"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#886e9cb983c46498003878afe965d55caa762025"
|
||||
|
||||
[[package]]
|
||||
name = "keccak-hash"
|
||||
@@ -249,7 +249,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.2"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@@ -618,7 +618,7 @@ name = "substrate-runtime-primitives"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"integer-sqrt 0.1.0 (git+https://github.com/paritytech/integer-sqrt-rs.git)",
|
||||
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"substrate-codec 0.1.0",
|
||||
@@ -863,7 +863,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2"
|
||||
"checksum memory_units 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "71d96e3f3c0b6325d8ccd83c33b28acb183edcb6c67938ba104ec546854b0882"
|
||||
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
|
||||
"checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364"
|
||||
"checksum num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775393e285254d2f5004596d69bb8bc1149754570dcc08cf30cabeba67955e28"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||
"checksum parity-wasm 0.27.6 (registry+https://github.com/rust-lang/crates.io-index)" = "bd4dc02a80a0315b109e48992c46942c79bcdb8fac416dd575d330ed9ced6cbd"
|
||||
|
||||
BIN
Binary file not shown.
Binary file not shown.
@@ -140,7 +140,7 @@ pub trait PolkadotApi {
|
||||
fn evaluate_block(&self, at: &Self::CheckedBlockId, block: Block) -> Result<()>;
|
||||
|
||||
/// Create a block builder on top of the parent block.
|
||||
fn build_block(&self, parent: &Self::CheckedBlockId, timestamp: u64) -> Result<Self::BlockBuilder>;
|
||||
fn build_block(&self, parent: &Self::CheckedBlockId, timestamp: Timestamp) -> Result<Self::BlockBuilder>;
|
||||
}
|
||||
|
||||
/// A checked block ID used for the substrate-client implementation of CheckedBlockId;
|
||||
|
||||
@@ -50,6 +50,8 @@ pub use primitives::block::Id as BlockId;
|
||||
pub use primitives::block::Log as Log;
|
||||
|
||||
/// An index to a block.
|
||||
/// 32-bits will allow for 136 years of blocks assuming 1 block per second.
|
||||
/// TODO: switch to u32
|
||||
pub type BlockNumber = u64;
|
||||
|
||||
/// Alias to Ed25519 pubkey that identifies an account on the relay chain. This will almost
|
||||
@@ -60,11 +62,11 @@ pub type AccountId = primitives::AuthorityId;
|
||||
/// exactly equivalent to what the substrate calls an "authority".
|
||||
pub type SessionKey = primitives::AuthorityId;
|
||||
|
||||
/// Indentifier for a chain.
|
||||
pub type ChainId = u64;
|
||||
/// Indentifier for a chain. 32-bit should be plenty.
|
||||
pub type ChainId = u32;
|
||||
|
||||
/// Index of a transaction in the relay chain.
|
||||
pub type Index = u64;
|
||||
/// Index of a transaction in the relay chain. 32-bit should be plenty.
|
||||
pub type Index = u32;
|
||||
|
||||
/// A hash of some data used by the relay chain.
|
||||
pub type Hash = primitives::H256;
|
||||
@@ -76,4 +78,10 @@ pub type Signature = runtime_primitives::Ed25519Signature;
|
||||
pub type Timestamp = u64;
|
||||
|
||||
/// The balance of an account.
|
||||
pub type Balance = u64;
|
||||
/// 128-bits (or 38 significant decimal figures) will allow for 10m currency (10^7) at a resolution
|
||||
/// to all for one second's worth of an annualised 50% reward be paid to a unit holder (10^11 unit
|
||||
/// denomination), or 10^18 total atomic units, to grow at 50%/year for 51 years (10^9 multiplier)
|
||||
/// for an eventual total of 10^27 units (27 significant decimal figures).
|
||||
/// We round denomination to 10^12 (12 sdf), and leave the other redundancy at the upper end so
|
||||
/// that 32 bits may be multiplied with a balance in 128 bits without worrying about overflow.
|
||||
pub type Balance = runtime_primitives::U128;
|
||||
|
||||
@@ -286,7 +286,7 @@ mod tests {
|
||||
let tx = UncheckedExtrinsic {
|
||||
extrinsic: Extrinsic {
|
||||
signed: [1; 32],
|
||||
index: 999u64,
|
||||
index: 999,
|
||||
function: Call::Timestamp(TimestampCall::set(135135)),
|
||||
},
|
||||
signature: primitives::hash::H512([0; 64]).into(),
|
||||
@@ -307,7 +307,7 @@ mod tests {
|
||||
fn serialize_checked() {
|
||||
let xt = Extrinsic {
|
||||
signed: hex!["0d71d1a9cad6f2ab773435a7dec1bac019994d05d1dd5eb3108211dcf25c9d1e"],
|
||||
index: 0u64,
|
||||
index: 0,
|
||||
function: Call::CouncilVoting(council::voting::Call::propose(Box::new(
|
||||
PrivCall::Consensus(consensus::PrivCall::set_code(
|
||||
vec![]
|
||||
@@ -316,7 +316,7 @@ mod tests {
|
||||
};
|
||||
let v = Slicable::encode(&xt);
|
||||
|
||||
let data = hex!["e00000000d71d1a9cad6f2ab773435a7dec1bac019994d05d1dd5eb3108211dcf25c9d1e000000000000000007000000000000006369D39D892B7B87A6769F90E14C618C2B84EBB293E2CC46640136E112C078C75619AC2E0815F2511568736623C055156C8FC427CE2AEE4AE2838F86EFE80208"];
|
||||
let data = hex!["e00000000d71d1a9cad6f2ab773435a7dec1bac019994d05d1dd5eb3108211dcf25c9d1e0000000007000000000000006369D39D892B7B87A6769F90E14C618C2B84EBB293E2CC46640136E112C078C75619AC2E0815F2511568736623C055156C8FC427CE2AEE4AE2838F86EFE80208"];
|
||||
let uxt: UncheckedExtrinsic = Slicable::decode(&mut &data[..]).unwrap();
|
||||
assert_eq!(uxt.extrinsic, xt);
|
||||
|
||||
|
||||
+4
-4
@@ -159,7 +159,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "integer-sqrt"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#f4cf61482096dc98c1273f46a10849d182b4c23c"
|
||||
source = "git+https://github.com/paritytech/integer-sqrt-rs.git#886e9cb983c46498003878afe965d55caa762025"
|
||||
|
||||
[[package]]
|
||||
name = "keccak-hash"
|
||||
@@ -214,7 +214,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.2"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
@@ -618,7 +618,7 @@ name = "substrate-runtime-primitives"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"integer-sqrt 0.1.0 (git+https://github.com/paritytech/integer-sqrt-rs.git)",
|
||||
"num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"substrate-codec 0.1.0",
|
||||
@@ -863,7 +863,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
"checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2"
|
||||
"checksum memory_units 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "71d96e3f3c0b6325d8ccd83c33b28acb183edcb6c67938ba104ec546854b0882"
|
||||
"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
|
||||
"checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364"
|
||||
"checksum num-traits 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "775393e285254d2f5004596d69bb8bc1149754570dcc08cf30cabeba67955e28"
|
||||
"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
|
||||
"checksum owning_ref 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "cdf84f41639e037b484f93433aa3897863b561ed65c6e59c7073d7c561710f37"
|
||||
"checksum parity-wasm 0.27.6 (registry+https://github.com/rust-lang/crates.io-index)" = "bd4dc02a80a0315b109e48992c46942c79bcdb8fac416dd575d330ed9ced6cbd"
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -154,8 +154,8 @@ fn poc_1_testnet_config() -> ChainConfig {
|
||||
staking: Some(StakingConfig {
|
||||
current_era: 0,
|
||||
intentions: initial_authorities.clone(),
|
||||
transaction_fee: 100,
|
||||
balances: endowed_accounts.iter().map(|&k|(k, 1u64 << 60)).collect(),
|
||||
transaction_fee: 100.into(),
|
||||
balances: endowed_accounts.iter().map(|&k|(k, (1u128 << 60).into())).collect(),
|
||||
validator_count: 12,
|
||||
sessions_per_era: 24, // 24 hours per era.
|
||||
bonding_duration: 90, // 90 days per bond.
|
||||
@@ -163,13 +163,13 @@ fn poc_1_testnet_config() -> ChainConfig {
|
||||
democracy: Some(DemocracyConfig {
|
||||
launch_period: 120 * 24 * 14, // 2 weeks per public referendum
|
||||
voting_period: 120 * 24 * 28, // 4 weeks to discuss & vote on an active referendum
|
||||
minimum_deposit: 1000, // 1000 as the minimum deposit for a referendum
|
||||
minimum_deposit: 1000.into(), // 1000 as the minimum deposit for a referendum
|
||||
}),
|
||||
council: Some(CouncilConfig {
|
||||
active_council: vec![],
|
||||
candidacy_bond: 1000, // 1000 to become a council candidate
|
||||
voter_bond: 100, // 100 down to vote for a candidate
|
||||
present_slash_per_voter: 1, // slash by 1 per voter for an invalid presentation.
|
||||
candidacy_bond: 1000.into(), // 1000 to become a council candidate
|
||||
voter_bond: 100.into(), // 100 down to vote for a candidate
|
||||
present_slash_per_voter: 1.into(), // slash by 1 per voter for an invalid presentation.
|
||||
carry_count: 24, // carry over the 24 runners-up to the next council election
|
||||
presentation_duration: 120 * 24, // one day for presenting winners.
|
||||
approval_voting_period: 7 * 120 * 24, // one week period between possible council elections.
|
||||
@@ -212,8 +212,8 @@ fn testnet_config(initial_authorities: Vec<AuthorityId>) -> ChainConfig {
|
||||
staking: Some(StakingConfig {
|
||||
current_era: 0,
|
||||
intentions: initial_authorities.clone(),
|
||||
transaction_fee: 1,
|
||||
balances: endowed_accounts.iter().map(|&k|(k, 1u64 << 60)).collect(),
|
||||
transaction_fee: 1.into(),
|
||||
balances: endowed_accounts.iter().map(|&k|(k, (1u128 << 60).into())).collect(),
|
||||
validator_count: 2,
|
||||
sessions_per_era: 5,
|
||||
bonding_duration: 2,
|
||||
@@ -221,13 +221,13 @@ fn testnet_config(initial_authorities: Vec<AuthorityId>) -> ChainConfig {
|
||||
democracy: Some(DemocracyConfig {
|
||||
launch_period: 9,
|
||||
voting_period: 18,
|
||||
minimum_deposit: 10,
|
||||
minimum_deposit: 10.into(),
|
||||
}),
|
||||
council: Some(CouncilConfig {
|
||||
active_council: endowed_accounts.iter().filter(|a| initial_authorities.iter().find(|b| a == b).is_none()).map(|a| (a.clone(), 1000000)).collect(),
|
||||
candidacy_bond: 10,
|
||||
voter_bond: 2,
|
||||
present_slash_per_voter: 1,
|
||||
candidacy_bond: 10.into(),
|
||||
voter_bond: 2.into(),
|
||||
present_slash_per_voter: 1.into(),
|
||||
carry_count: 4,
|
||||
presentation_duration: 10,
|
||||
approval_voting_period: 20,
|
||||
|
||||
@@ -37,7 +37,7 @@ use std::sync::Arc;
|
||||
use polkadot_api::PolkadotApi;
|
||||
use primitives::{AccountId, Timestamp};
|
||||
use runtime::{Block, UncheckedExtrinsic, TimestampCall, Call};
|
||||
use substrate_runtime_primitives::traits::Checkable;
|
||||
use substrate_runtime_primitives::traits::{Bounded, Checkable};
|
||||
use transaction_pool::{Pool, Readiness};
|
||||
use transaction_pool::scoring::{Change, Choice};
|
||||
|
||||
@@ -295,7 +295,7 @@ impl<'a, T: 'a + PolkadotApi> transaction_pool::Ready<VerifiedTransaction> for R
|
||||
// transaction-pool trait.
|
||||
let (api_handle, at_block) = (&self.api_handle, &self.at_block);
|
||||
let next_index = self.known_indices.entry(sender)
|
||||
.or_insert_with(|| api_handle.index(at_block, sender).ok().unwrap_or_else(u64::max_value));
|
||||
.or_insert_with(|| api_handle.index(at_block, sender).ok().unwrap_or_else(Bounded::max_value));
|
||||
|
||||
trace!(target: "transaction-pool", "Next index for sender is {}; xt index is {}", next_index, xt.inner.index);
|
||||
|
||||
|
||||
@@ -344,7 +344,7 @@ macro_rules! impl_non_endians {
|
||||
)* }
|
||||
}
|
||||
|
||||
impl_endians!(u16, u32, u64, usize, i16, i32, i64, isize);
|
||||
impl_endians!(u16, u32, u64, u128, usize, i16, i32, i64, i128, isize);
|
||||
impl_non_endians!(i8, [u8; 1], [u8; 2], [u8; 3], [u8; 4], [u8; 5], [u8; 6], [u8; 7], [u8; 8],
|
||||
[u8; 10], [u8; 12], [u8; 14], [u8; 16], [u8; 20], [u8; 24], [u8; 28], [u8; 32], [u8; 40],
|
||||
[u8; 48], [u8; 56], [u8; 64], [u8; 80], [u8; 96], [u8; 112], [u8; 128], bool);
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,148 @@
|
||||
// Copyright 2017 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate Demo.
|
||||
|
||||
// Substrate Demo is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate Demo is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate Demo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! 128-bit shims so that we can make it `serde::Serialize`-able, since Serde is borked for
|
||||
//! the new 128-bit datatypes.
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use serde::ser::{Serialize, Serializer, SerializeTuple};
|
||||
use codec::{Slicable, Input};
|
||||
use integer_sqrt::IntegerSquareRoot;
|
||||
use num_traits::{Zero, One, Bounded};
|
||||
use rstd::ops::{Add, Sub, Mul, Div, Rem, AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
|
||||
use traits::As;
|
||||
|
||||
/// A 128-bit uint shim.
|
||||
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone, Copy, Default)]
|
||||
pub struct U128(u128);
|
||||
|
||||
/// A 128-bit int shim.
|
||||
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Clone, Copy, Default)]
|
||||
pub struct I128(i128);
|
||||
|
||||
macro_rules! impl_as {
|
||||
( $f:ident, $i:ty ) => {
|
||||
impl_as!($f, $i: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
|
||||
};
|
||||
( $f:ident, $i:ty : $t:ty $(, $rest:ty)* ) => {
|
||||
impl As<$t> for $f {
|
||||
fn as_(self) -> $t { self.0 as $t }
|
||||
fn sa(t: $t) -> Self { $f(t as $i) }
|
||||
}
|
||||
impl_as!($f, $i : $( $rest ),*);
|
||||
};
|
||||
( $f:ident, $i:ty : ) => {}
|
||||
}
|
||||
|
||||
|
||||
macro_rules! impl_for {
|
||||
($type:ident, $inner:ty) => {
|
||||
impl IntegerSquareRoot for $type {
|
||||
fn integer_sqrt(&self) -> Self {
|
||||
$type(self.0.integer_sqrt())
|
||||
}
|
||||
fn integer_sqrt_checked(&self) -> Option<Self> {
|
||||
self.0.integer_sqrt_checked().map(|x| $type(x))
|
||||
}
|
||||
}
|
||||
macro_rules! impl_bin_op {
|
||||
($op:ident, $f:ident) => {
|
||||
impl $op for $type {
|
||||
type Output = Self;
|
||||
fn $f (self, other: Self) -> Self {
|
||||
$type((self.0).$f(other.0))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
macro_rules! impl_assign_op {
|
||||
($op:ident, $f:ident) => {
|
||||
impl $op for $type {
|
||||
fn $f (&mut self, other: Self) {
|
||||
(&mut self.0).$f(other.0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl_bin_op!(Add, add);
|
||||
impl_bin_op!(Sub, sub);
|
||||
impl_bin_op!(Mul, mul);
|
||||
impl_bin_op!(Div, div);
|
||||
impl_bin_op!(Rem, rem);
|
||||
impl_assign_op!(AddAssign, add_assign);
|
||||
impl_assign_op!(SubAssign, sub_assign);
|
||||
impl_assign_op!(MulAssign, mul_assign);
|
||||
impl_assign_op!(DivAssign, div_assign);
|
||||
impl_assign_op!(RemAssign, rem_assign);
|
||||
|
||||
impl Zero for $type {
|
||||
fn zero() -> Self {
|
||||
$type(Zero::zero())
|
||||
}
|
||||
fn is_zero(&self) -> bool {
|
||||
self.0.is_zero()
|
||||
}
|
||||
}
|
||||
impl One for $type {
|
||||
fn one() -> Self {
|
||||
$type(One::one())
|
||||
}
|
||||
}
|
||||
|
||||
impl Bounded for $type {
|
||||
fn min_value() -> Self {
|
||||
$type(Bounded::min_value())
|
||||
}
|
||||
fn max_value() -> Self {
|
||||
$type(Bounded::max_value())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Serialize for $type {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
let mut seq = serializer.serialize_tuple(2)?;
|
||||
seq.serialize_element(&(self.0 as u64))?;
|
||||
seq.serialize_element(&((self.0 >> 64) as u64))?;
|
||||
seq.end()
|
||||
}
|
||||
}
|
||||
|
||||
impl Slicable for $type {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
Slicable::decode(input).map($type)
|
||||
}
|
||||
|
||||
fn using_encoded<R, F: FnOnce(&[u8]) -> R>(&self, f: F) -> R {
|
||||
self.0.using_encoded(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<$inner> for $type {
|
||||
fn from(v: $inner) -> Self {
|
||||
$type(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl_as!($type, $inner);
|
||||
}
|
||||
}
|
||||
|
||||
impl_for!(U128, u128);
|
||||
impl_for!(I128, i128);
|
||||
@@ -45,6 +45,9 @@ pub mod testing;
|
||||
pub mod traits;
|
||||
pub mod generic;
|
||||
|
||||
mod int128;
|
||||
pub use int128::{I128, U128};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
pub type BuiltExternalities = HashMap<Vec<u8>, Vec<u8>>;
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ pub trait As<T> {
|
||||
macro_rules! impl_numerics {
|
||||
( $( $t:ty ),* ) => {
|
||||
$(
|
||||
impl_numerics!($t: u8, u16, u32, u64, usize, i8, i16, i32, i64, isize,);
|
||||
impl_numerics!($t: u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize,);
|
||||
)*
|
||||
};
|
||||
( $f:ty : $t:ty, $( $rest:ty, )* ) => {
|
||||
@@ -74,7 +74,7 @@ macro_rules! impl_numerics {
|
||||
( $f:ty : ) => {}
|
||||
}
|
||||
|
||||
impl_numerics!(u8, u16, u32, u64, usize, i8, i16, i32, i64, isize);
|
||||
impl_numerics!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);
|
||||
|
||||
pub struct Identity;
|
||||
impl<T> Convert<T, T> for Identity {
|
||||
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user