mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-28 05:07:55 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -17,7 +17,10 @@
|
||||
|
||||
//! Provides some utilities to define a piecewise linear function.
|
||||
|
||||
use crate::{Perbill, traits::{AtLeast32BitUnsigned, SaturatedConversion}};
|
||||
use crate::{
|
||||
traits::{AtLeast32BitUnsigned, SaturatedConversion},
|
||||
Perbill,
|
||||
};
|
||||
use core::ops::Sub;
|
||||
|
||||
/// Piecewise Linear function in [0, 1] -> [0, 1].
|
||||
@@ -29,14 +32,15 @@ pub struct PiecewiseLinear<'a> {
|
||||
pub maximum: Perbill,
|
||||
}
|
||||
|
||||
fn abs_sub<N: Ord + Sub<Output=N> + Clone>(a: N, b: N) -> N where {
|
||||
fn abs_sub<N: Ord + Sub<Output = N> + Clone>(a: N, b: N) -> N where {
|
||||
a.clone().max(b.clone()) - a.min(b)
|
||||
}
|
||||
|
||||
impl<'a> PiecewiseLinear<'a> {
|
||||
/// Compute `f(n/d)*d` with `n <= d`. This is useful to avoid loss of precision.
|
||||
pub fn calculate_for_fraction_times_denominator<N>(&self, n: N, d: N) -> N where
|
||||
N: AtLeast32BitUnsigned + Clone
|
||||
pub fn calculate_for_fraction_times_denominator<N>(&self, n: N, d: N) -> N
|
||||
where
|
||||
N: AtLeast32BitUnsigned + Clone,
|
||||
{
|
||||
let n = n.min(d.clone());
|
||||
|
||||
@@ -44,8 +48,7 @@ impl<'a> PiecewiseLinear<'a> {
|
||||
return N::zero()
|
||||
}
|
||||
|
||||
let next_point_index = self.points.iter()
|
||||
.position(|p| n < p.0 * d.clone());
|
||||
let next_point_index = self.points.iter().position(|p| n < p.0 * d.clone());
|
||||
|
||||
let (prev, next) = if let Some(next_point_index) = next_point_index {
|
||||
if let Some(previous_point_index) = next_point_index.checked_sub(1) {
|
||||
@@ -80,7 +83,8 @@ impl<'a> PiecewiseLinear<'a> {
|
||||
// This is guaranteed not to overflow on whatever values nor lose precision.
|
||||
// `q` must be superior to zero.
|
||||
fn multiply_by_rational_saturating<N>(value: N, p: u32, q: u32) -> N
|
||||
where N: AtLeast32BitUnsigned + Clone
|
||||
where
|
||||
N: AtLeast32BitUnsigned + Clone,
|
||||
{
|
||||
let q = q.max(1);
|
||||
|
||||
@@ -112,17 +116,14 @@ fn test_multiply_by_rational_saturating() {
|
||||
for value in 0..=div {
|
||||
for p in 0..=div {
|
||||
for q in 1..=div {
|
||||
let value: u64 = (value as u128 * u64::MAX as u128 / div as u128)
|
||||
.try_into().unwrap();
|
||||
let p = (p as u64 * u32::MAX as u64 / div as u64)
|
||||
.try_into().unwrap();
|
||||
let q = (q as u64 * u32::MAX as u64 / div as u64)
|
||||
.try_into().unwrap();
|
||||
let value: u64 =
|
||||
(value as u128 * u64::MAX as u128 / div as u128).try_into().unwrap();
|
||||
let p = (p as u64 * u32::MAX as u64 / div as u64).try_into().unwrap();
|
||||
let q = (q as u64 * u32::MAX as u64 / div as u64).try_into().unwrap();
|
||||
|
||||
assert_eq!(
|
||||
multiply_by_rational_saturating(value, p, q),
|
||||
(value as u128 * p as u128 / q as u128)
|
||||
.try_into().unwrap_or(u64::MAX)
|
||||
(value as u128 * p as u128 / q as u128).try_into().unwrap_or(u64::MAX)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -153,10 +154,8 @@ fn test_calculate_for_fraction_times_denominator() {
|
||||
let div = 100u32;
|
||||
for d in 0..=div {
|
||||
for n in 0..=d {
|
||||
let d: u64 = (d as u128 * u64::MAX as u128 / div as u128)
|
||||
.try_into().unwrap();
|
||||
let n: u64 = (n as u128 * u64::MAX as u128 / div as u128)
|
||||
.try_into().unwrap();
|
||||
let d: u64 = (d as u128 * u64::MAX as u128 / div as u128).try_into().unwrap();
|
||||
let n: u64 = (n as u128 * u64::MAX as u128 / div as u128).try_into().unwrap();
|
||||
|
||||
let res = curve.calculate_for_fraction_times_denominator(n, d);
|
||||
let expected = formal_calculate_for_fraction_times_denominator(n, d);
|
||||
|
||||
Reference in New Issue
Block a user