mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-20 15:05:40 +00:00
extract amount method for fungible/s Imbalance (#1847)
Introduces an `extract` amount method for `fungible/s` `Imbalance`.
This commit is contained in:
@@ -100,6 +100,11 @@ mod imbalances {
|
|||||||
mem::forget(self);
|
mem::forget(self);
|
||||||
(Self(first), Self(second))
|
(Self(first), Self(second))
|
||||||
}
|
}
|
||||||
|
fn extract(&mut self, amount: T::Balance) -> Self {
|
||||||
|
let new = self.0.min(amount);
|
||||||
|
self.0 = self.0 - new;
|
||||||
|
Self(new)
|
||||||
|
}
|
||||||
fn merge(mut self, other: Self) -> Self {
|
fn merge(mut self, other: Self) -> Self {
|
||||||
self.0 = self.0.saturating_add(other.0);
|
self.0 = self.0.saturating_add(other.0);
|
||||||
mem::forget(other);
|
mem::forget(other);
|
||||||
@@ -159,6 +164,11 @@ mod imbalances {
|
|||||||
mem::forget(self);
|
mem::forget(self);
|
||||||
(Self(first), Self(second))
|
(Self(first), Self(second))
|
||||||
}
|
}
|
||||||
|
fn extract(&mut self, amount: T::Balance) -> Self {
|
||||||
|
let new = self.0.min(amount);
|
||||||
|
self.0 = self.0 - new;
|
||||||
|
Self(new)
|
||||||
|
}
|
||||||
fn merge(mut self, other: Self) -> Self {
|
fn merge(mut self, other: Self) -> Self {
|
||||||
self.0 = self.0.saturating_add(other.0);
|
self.0 = self.0.saturating_add(other.0);
|
||||||
mem::forget(other);
|
mem::forget(other);
|
||||||
|
|||||||
@@ -113,6 +113,13 @@ impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalance
|
|||||||
sp_std::mem::forget(self);
|
sp_std::mem::forget(self);
|
||||||
(Imbalance::new(first), Imbalance::new(second))
|
(Imbalance::new(first), Imbalance::new(second))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn extract(&mut self, amount: B) -> Self {
|
||||||
|
let new = self.amount.min(amount);
|
||||||
|
self.amount = self.amount - new;
|
||||||
|
Imbalance::new(new)
|
||||||
|
}
|
||||||
|
|
||||||
fn merge(mut self, other: Self) -> Self {
|
fn merge(mut self, other: Self) -> Self {
|
||||||
self.amount = self.amount.saturating_add(other.amount);
|
self.amount = self.amount.saturating_add(other.amount);
|
||||||
sp_std::mem::forget(other);
|
sp_std::mem::forget(other);
|
||||||
|
|||||||
@@ -109,6 +109,15 @@ impl<
|
|||||||
sp_std::mem::forget(self);
|
sp_std::mem::forget(self);
|
||||||
(Imbalance::new(asset.clone(), first), Imbalance::new(asset, second))
|
(Imbalance::new(asset.clone(), first), Imbalance::new(asset, second))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mutate `self` by extracting a new instance with at most `amount` value, reducing `self`
|
||||||
|
/// accordingly.
|
||||||
|
pub fn extract(&mut self, amount: B) -> Self {
|
||||||
|
let new = self.amount.min(amount);
|
||||||
|
self.amount = self.amount - new;
|
||||||
|
Imbalance::new(self.asset.clone(), new)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn merge(mut self, other: Self) -> Result<Self, (Self, Self)> {
|
pub fn merge(mut self, other: Self) -> Result<Self, (Self, Self)> {
|
||||||
if self.asset == other.asset {
|
if self.asset == other.asset {
|
||||||
self.amount = self.amount.saturating_add(other.amount);
|
self.amount = self.amount.saturating_add(other.amount);
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ pub trait Imbalance<Balance>: Sized + TryDrop + Default {
|
|||||||
/// is guaranteed to be at most `amount` and the second will be the remainder.
|
/// is guaranteed to be at most `amount` and the second will be the remainder.
|
||||||
fn split(self, amount: Balance) -> (Self, Self);
|
fn split(self, amount: Balance) -> (Self, Self);
|
||||||
|
|
||||||
|
/// Mutate `self` by extracting a new instance with at most `amount` value, reducing `self`
|
||||||
|
/// accordingly.
|
||||||
|
fn extract(&mut self, amount: Balance) -> Self;
|
||||||
|
|
||||||
/// Consume `self` and return two independent instances; the amounts returned will be in
|
/// Consume `self` and return two independent instances; the amounts returned will be in
|
||||||
/// approximately the same ratio as `first`:`second`.
|
/// approximately the same ratio as `first`:`second`.
|
||||||
///
|
///
|
||||||
@@ -190,6 +194,9 @@ impl<Balance: Default> Imbalance<Balance> for () {
|
|||||||
fn split(self, _: Balance) -> (Self, Self) {
|
fn split(self, _: Balance) -> (Self, Self) {
|
||||||
((), ())
|
((), ())
|
||||||
}
|
}
|
||||||
|
fn extract(&mut self, _: Balance) -> Self {
|
||||||
|
()
|
||||||
|
}
|
||||||
fn ration(self, _: u32, _: u32) -> (Self, Self)
|
fn ration(self, _: u32, _: u32) -> (Self, Self)
|
||||||
where
|
where
|
||||||
Balance: From<u32> + Saturating + Div<Output = Balance>,
|
Balance: From<u32> + Saturating + Div<Output = Balance>,
|
||||||
|
|||||||
Reference in New Issue
Block a user