From 25ff4ecc8ba67eb1bc6c01c5afea84d7e4b56dae Mon Sep 17 00:00:00 2001 From: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Date: Tue, 13 Feb 2024 12:06:28 +0200 Subject: [PATCH] subxt: Derive `std::cmp` traits for subxt payloads and addresses (#1429) * subxt/tx: Derive std::cmp traits Signed-off-by: Alexandru Vasile * subxt/runtime_api: Derive std::cmp traits Signed-off-by: Alexandru Vasile * subxt/constants: Derive std::cmp traits Signed-off-by: Alexandru Vasile * subxt/custom_values: Derive std::cmp traits Signed-off-by: Alexandru Vasile * subxt/storage: Derive std::cmp traits Signed-off-by: Alexandru Vasile * subxt: Fix non_canonical_partial_ord_impl clippy introduced in 1.73 Signed-off-by: Alexandru Vasile * subxt: Add comment wrt derivative issue that triggers clippy warning Signed-off-by: Alexandru Vasile * Update subxt/src/backend/mod.rs * Update subxt/src/constants/constant_address.rs --------- Signed-off-by: Alexandru Vasile Co-authored-by: Niklas Adolfsson --- subxt/src/backend/mod.rs | 1 + subxt/src/constants/constant_address.rs | 15 ++++++++++++++- subxt/src/custom_values/custom_value_address.rs | 14 +++++++++++++- subxt/src/runtime_api/runtime_payload.rs | 6 +++++- subxt/src/storage/storage_address.rs | 6 +++++- subxt/src/tx/tx_payload.rs | 11 ++++++++++- 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/subxt/src/backend/mod.rs b/subxt/src/backend/mod.rs index 3db06818b3..e816bc904c 100644 --- a/subxt/src/backend/mod.rs +++ b/subxt/src/backend/mod.rs @@ -184,6 +184,7 @@ impl PartialEq for BlockRef { } impl Eq for BlockRef {} +// Manual implementation to work around https://github.com/mcarton/rust-derivative/issues/115. impl PartialOrd for BlockRef { fn partial_cmp(&self, other: &Self) -> Option { self.hash.partial_cmp(&other.hash) diff --git a/subxt/src/constants/constant_address.rs b/subxt/src/constants/constant_address.rs index e9a0eb37b0..3765d4c84b 100644 --- a/subxt/src/constants/constant_address.rs +++ b/subxt/src/constants/constant_address.rs @@ -28,7 +28,13 @@ pub trait ConstantAddress { /// This represents the address of a constant. #[derive(Derivative)] -#[derivative(Clone(bound = ""), Debug(bound = ""))] +#[derivative( + Clone(bound = ""), + Debug(bound = ""), + Eq(bound = ""), + Ord(bound = ""), + PartialEq(bound = "") +)] pub struct Address { pallet_name: Cow<'static, str>, constant_name: Cow<'static, str>, @@ -36,6 +42,13 @@ pub struct Address { _marker: std::marker::PhantomData, } +// Manual implementation to work around https://github.com/mcarton/rust-derivative/issues/115. +impl PartialOrd for Address { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + /// The type of address typically used to return dynamic constant values. pub type DynamicAddress = Address; diff --git a/subxt/src/custom_values/custom_value_address.rs b/subxt/src/custom_values/custom_value_address.rs index f8034a161b..6e2f7f77ea 100644 --- a/subxt/src/custom_values/custom_value_address.rs +++ b/subxt/src/custom_values/custom_value_address.rs @@ -38,13 +38,25 @@ pub struct Yes; /// A static address to a custom value. #[derive(Derivative)] -#[derivative(Clone(bound = ""), Debug(bound = ""))] +#[derivative( + Clone(bound = ""), + Debug(bound = ""), + Eq(bound = ""), + Ord(bound = ""), + PartialEq(bound = "") +)] pub struct StaticAddress { name: &'static str, hash: Option<[u8; 32]>, phantom: PhantomData<(ReturnTy, IsDecodable)>, } +impl PartialOrd for StaticAddress { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + impl StaticAddress { #[doc(hidden)] /// Creates a new StaticAddress. diff --git a/subxt/src/runtime_api/runtime_payload.rs b/subxt/src/runtime_api/runtime_payload.rs index cd5a3355b8..ff776eb14f 100644 --- a/subxt/src/runtime_api/runtime_payload.rs +++ b/subxt/src/runtime_api/runtime_payload.rs @@ -69,7 +69,11 @@ pub trait RuntimeApiPayload { #[derive(Derivative)] #[derivative( Clone(bound = "ArgsData: Clone"), - Debug(bound = "ArgsData: std::fmt::Debug") + Debug(bound = "ArgsData: std::fmt::Debug"), + Eq(bound = "ArgsData: std::cmp::Eq"), + Ord(bound = "ArgsData: std::cmp::Ord"), + PartialEq(bound = "ArgsData: std::cmp::PartialEq"), + PartialOrd(bound = "ArgsData: std::cmp::PartialOrd") )] pub struct Payload { trait_name: Cow<'static, str>, diff --git a/subxt/src/storage/storage_address.rs b/subxt/src/storage/storage_address.rs index 893474b518..2211b20def 100644 --- a/subxt/src/storage/storage_address.rs +++ b/subxt/src/storage/storage_address.rs @@ -55,7 +55,11 @@ pub struct Yes; #[derive(Derivative)] #[derivative( Clone(bound = "StorageKey: Clone"), - Debug(bound = "StorageKey: std::fmt::Debug") + Debug(bound = "StorageKey: std::fmt::Debug"), + Eq(bound = "StorageKey: std::cmp::Eq"), + Ord(bound = "StorageKey: std::cmp::Ord"), + PartialEq(bound = "StorageKey: std::cmp::PartialEq"), + PartialOrd(bound = "StorageKey: std::cmp::PartialOrd") )] pub struct Address { pallet_name: Cow<'static, str>, diff --git a/subxt/src/tx/tx_payload.rs b/subxt/src/tx/tx_payload.rs index 508a6a99e6..ff9c71fd21 100644 --- a/subxt/src/tx/tx_payload.rs +++ b/subxt/src/tx/tx_payload.rs @@ -11,6 +11,7 @@ use crate::{ metadata::Metadata, }; use codec::Encode; +use derivative::Derivative; use scale_encode::EncodeAsFields; use scale_value::{Composite, ValueDef, Variant}; use std::{borrow::Cow, sync::Arc}; @@ -48,7 +49,15 @@ pub struct ValidationDetails<'a> { } /// A transaction payload containing some generic `CallData`. -#[derive(Clone, Debug)] +#[derive(Derivative)] +#[derivative( + Clone(bound = "CallData: Clone"), + Debug(bound = "CallData: std::fmt::Debug"), + Eq(bound = "CallData: std::cmp::Eq"), + Ord(bound = "CallData: std::cmp::Ord"), + PartialEq(bound = "CallData: std::cmp::PartialEq"), + PartialOrd(bound = "CallData: std::cmp::PartialOrd") +)] pub struct Payload { pallet_name: Cow<'static, str>, call_name: Cow<'static, str>,