Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

frame-support: RuntimeDebug\Eq\PartialEq impls for Imbalance #1717

Merged
merged 4 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
38 changes: 36 additions & 2 deletions substrate/frame/support/src/traits/tokens/fungible/imbalance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::traits::{
misc::{SameOrOther, TryDrop},
tokens::Balance,
};
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_runtime::traits::Zero;
use sp_std::marker::PhantomData;

/// Handler for when an imbalance gets dropped. This could handle either a credit (negative) or
Expand All @@ -43,7 +43,6 @@ impl<Balance> HandleImbalanceDrop<Balance> for () {
///
/// Importantly, it has a special `Drop` impl, and cannot be created outside of this module.
#[must_use]
#[derive(RuntimeDebug, Eq, PartialEq)]
pub struct Imbalance<
B: Balance,
OnDrop: HandleImbalanceDrop<B>,
Expand Down Expand Up @@ -141,6 +140,41 @@ impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalance
}
}

impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalanceDrop<B>> PartialEq
for Imbalance<B, OnDrop, OppositeOnDrop>
{
fn eq(&self, other: &Self) -> bool {
self.amount.eq(&other.amount)
}
}

impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalanceDrop<B>> Eq
for Imbalance<B, OnDrop, OppositeOnDrop>
{
}

#[cfg(any(feature = "std", feature = "force-debug"))]
impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalanceDrop<B>>
sp_std::fmt::Debug for Imbalance<B, OnDrop, OppositeOnDrop>
{
fn fmt(&self, fmt: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
fmt.debug_struct("Imbalance")
.field("amount", &self.amount)
.field("OnDrop", &sp_std::any::type_name::<OnDrop>())
.field("OppositeOnDrop", &sp_std::any::type_name::<OppositeOnDrop>())
.finish()
}
}

#[cfg(all(not(feature = "std"), not(feature = "force-debug")))]
muharem marked this conversation as resolved.
Show resolved Hide resolved
impl<B: Balance, OnDrop: HandleImbalanceDrop<B>, OppositeOnDrop: HandleImbalanceDrop<B>>
sp_std::fmt::Debug for Imbalance<B, OnDrop, OppositeOnDrop>
{
fn fmt(&self, fmt: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
fmt.write_str("<wasm:stripped>")
}
}

/// Imbalance implying that the total_issuance value is less than the sum of all account balances.
pub type Debt<AccountId, B> = Imbalance<
<B as Inspect<AccountId>>::Balance,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::traits::{
misc::{SameOrOther, TryDrop},
tokens::{AssetId, Balance},
};
use sp_runtime::{traits::Zero, RuntimeDebug};
use sp_runtime::traits::Zero;
use sp_std::marker::PhantomData;

/// Handler for when an imbalance gets dropped. This could handle either a credit (negative) or
Expand All @@ -38,7 +38,6 @@ pub trait HandleImbalanceDrop<AssetId, Balance> {
///
/// Importantly, it has a special `Drop` impl, and cannot be created outside of this module.
#[must_use]
#[derive(RuntimeDebug, Eq, PartialEq)]
pub struct Imbalance<
A: AssetId,
B: Balance,
Expand Down Expand Up @@ -158,6 +157,58 @@ impl<
}
}

impl<
A: AssetId,
B: Balance,
OnDrop: HandleImbalanceDrop<A, B>,
OppositeOnDrop: HandleImbalanceDrop<A, B>,
> PartialEq for Imbalance<A, B, OnDrop, OppositeOnDrop>
{
fn eq(&self, other: &Self) -> bool {
self.amount.eq(&other.amount) && self.asset.eq(&other.asset)
}
}

impl<
A: AssetId,
B: Balance,
OnDrop: HandleImbalanceDrop<A, B>,
OppositeOnDrop: HandleImbalanceDrop<A, B>,
> Eq for Imbalance<A, B, OnDrop, OppositeOnDrop>
{
}
muharem marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(any(feature = "std", feature = "force-debug"))]
impl<
A: AssetId,
B: Balance,
OnDrop: HandleImbalanceDrop<A, B>,
OppositeOnDrop: HandleImbalanceDrop<A, B>,
> sp_std::fmt::Debug for Imbalance<A, B, OnDrop, OppositeOnDrop>
{
fn fmt(&self, fmt: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
fmt.debug_struct("Imbalance")
.field("asset", &self.asset)
.field("amount", &self.amount)
.field("OnDrop", &sp_std::any::type_name::<OnDrop>())
.field("OppositeOnDrop", &sp_std::any::type_name::<OppositeOnDrop>())
.finish()
}
}

#[cfg(all(not(feature = "std"), not(feature = "force-debug")))]
impl<
A: AssetId,
B: Balance,
OnDrop: HandleImbalanceDrop<A, B>,
OppositeOnDrop: HandleImbalanceDrop<A, B>,
> sp_std::fmt::Debug for Imbalance<A, B, OnDrop, OppositeOnDrop>
{
fn fmt(&self, fmt: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
fmt.write_str("<wasm:stripped>")
}
}

/// Imbalance implying that the total_issuance value is less than the sum of all account balances.
pub type Debt<AccountId, B> = Imbalance<
<B as Inspect<AccountId>>::AssetId,
Expand Down
Loading