Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

relax input conditions of fixed::checked_from_integer #11159

Merged
merged 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ pub mod pallet {
// loss.
assert!(
<Multiplier as sp_runtime::traits::Bounded>::max_value() >=
Multiplier::checked_from_integer(
Multiplier::checked_from_integer::<u128>(
T::BlockWeights::get().max_block.try_into().unwrap()
)
.unwrap(),
Expand Down
16 changes: 9 additions & 7 deletions primitives/arithmetic/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ pub trait FixedPointNumber:
/// Creates `self` from an integer number `int`.
///
/// Returns `None` if `int` exceeds accuracy.
fn checked_from_integer(int: Self::Inner) -> Option<Self> {
fn checked_from_integer<N: Into<Self::Inner>>(int: N) -> Option<Self> {
let int: Self::Inner = int.into();
int.checked_mul(&Self::DIV).map(Self::from_inner)
}

Expand Down Expand Up @@ -898,31 +899,32 @@ macro_rules! implement_fixed {
let accuracy = $name::accuracy();

// Case where integer fits.
let a = $name::checked_from_integer(42).expect("42 * accuracy <= inner_max; qed");
let a = $name::checked_from_integer::<$inner_type>(42)
.expect("42 * accuracy <= inner_max; qed");
assert_eq!(a.into_inner(), 42 * accuracy);

// Max integer that fit.
let a = $name::checked_from_integer(inner_max / accuracy)
let a = $name::checked_from_integer::<$inner_type>(inner_max / accuracy)
.expect("(inner_max / accuracy) * accuracy <= inner_max; qed");
assert_eq!(a.into_inner(), (inner_max / accuracy) * accuracy);

// Case where integer doesn't fit, so it returns `None`.
let a = $name::checked_from_integer(inner_max / accuracy + 1);
let a = $name::checked_from_integer::<$inner_type>(inner_max / accuracy + 1);
assert_eq!(a, None);

if $name::SIGNED {
// Case where integer fits.
let a = $name::checked_from_integer(0.saturating_sub(42))
let a = $name::checked_from_integer::<$inner_type>(0.saturating_sub(42))
.expect("-42 * accuracy >= inner_min; qed");
assert_eq!(a.into_inner(), 0 - 42 * accuracy);

// Min integer that fit.
let a = $name::checked_from_integer(inner_min / accuracy)
let a = $name::checked_from_integer::<$inner_type>(inner_min / accuracy)
.expect("(inner_min / accuracy) * accuracy <= inner_min; qed");
assert_eq!(a.into_inner(), (inner_min / accuracy) * accuracy);

// Case where integer doesn't fit, so it returns `None`.
let a = $name::checked_from_integer(inner_min / accuracy - 1);
let a = $name::checked_from_integer::<$inner_type>(inner_min / accuracy - 1);
assert_eq!(a, None);
}
}
Expand Down