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

Implement PartialEq for DispatchError. #8407

Merged
merged 1 commit into from
Mar 21, 2021
Merged
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
55 changes: 54 additions & 1 deletion primitives/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ pub type DispatchResult = sp_std::result::Result<(), DispatchError>;
pub type DispatchResultWithInfo<T> = sp_std::result::Result<T, DispatchErrorWithPostInfo<T>>;

/// Reason why a dispatch call failed.
#[derive(Eq, PartialEq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[derive(Eq, Clone, Copy, Encode, Decode, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum DispatchError {
/// Some error occurred.
Expand Down Expand Up @@ -589,6 +589,27 @@ impl<T> traits::Printable for DispatchErrorWithPostInfo<T> where
}
}

impl PartialEq for DispatchError {
fn eq(&self, other: &Self) -> bool {
use DispatchError::*;

match (self, other) {
(CannotLookup, CannotLookup) |
(BadOrigin, BadOrigin) |
(ConsumerRemaining, ConsumerRemaining) |
(NoProviders, NoProviders) => true,

(Other(l), Other(r)) => l == r,
(
Module { index: index_l, error: error_l, .. },
Module { index: index_r, error: error_r, .. },
) => (index_l == index_r) && (error_l == error_r),

_ => false,
}
}
}

/// This type specifies the outcome of dispatching a call to a module.
///
/// In case of failure an error specific to the module is returned.
Expand Down Expand Up @@ -826,6 +847,38 @@ mod tests {
);
}

#[test]
fn dispatch_error_equality() {
use DispatchError::*;

let variants = vec![
Other("foo"),
Other("bar"),
CannotLookup,
BadOrigin,
Module { index: 1, error: 1, message: None },
Module { index: 1, error: 2, message: None },
Module { index: 2, error: 1, message: None },
ConsumerRemaining,
NoProviders,
];
for (i, variant) in variants.iter().enumerate() {
for (j, other_variant) in variants.iter().enumerate() {
if i == j {
assert_eq!(variant, other_variant);
} else {
assert_ne!(variant, other_variant);
}
}
}

// Ignores `message` field in `Module` variant.
assert_eq!(
Module { index: 1, error: 1, message: Some("foo") },
Module { index: 1, error: 1, message: None},
);
}

#[test]
fn multi_signature_ecdsa_verify_works() {
let msg = &b"test-message"[..];
Expand Down