Skip to content

Commit 5313f6a

Browse files
lexnvniklasad1
authored andcommitted
subxt: Derive std::cmp traits for subxt payloads and addresses (#1429)
* subxt/tx: Derive std::cmp traits Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/runtime_api: Derive std::cmp traits Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/constants: Derive std::cmp traits Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/custom_values: Derive std::cmp traits Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt/storage: Derive std::cmp traits Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Fix non_canonical_partial_ord_impl clippy introduced in 1.73 Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * subxt: Add comment wrt derivative issue that triggers clippy warning Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> * Update subxt/src/backend/mod.rs * Update subxt/src/constants/constant_address.rs --------- Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io> Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
1 parent db12bd7 commit 5313f6a

File tree

6 files changed

+51
-5
lines changed

6 files changed

+51
-5
lines changed

subxt/src/backend/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ impl<H: PartialEq> PartialEq for BlockRef<H> {
184184
}
185185
impl<H: Eq> Eq for BlockRef<H> {}
186186

187+
// Manual implementation to work around https://github.com/mcarton/rust-derivative/issues/115.
187188
impl<H: PartialOrd> PartialOrd for BlockRef<H> {
188189
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
189190
self.hash.partial_cmp(&other.hash)

subxt/src/constants/constant_address.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,27 @@ pub trait ConstantAddress {
2828

2929
/// This represents the address of a constant.
3030
#[derive(Derivative)]
31-
#[derivative(Clone(bound = ""), Debug(bound = ""))]
31+
#[derivative(
32+
Clone(bound = ""),
33+
Debug(bound = ""),
34+
Eq(bound = ""),
35+
Ord(bound = ""),
36+
PartialEq(bound = "")
37+
)]
3238
pub struct Address<ReturnTy> {
3339
pallet_name: Cow<'static, str>,
3440
constant_name: Cow<'static, str>,
3541
constant_hash: Option<[u8; 32]>,
3642
_marker: std::marker::PhantomData<ReturnTy>,
3743
}
3844

45+
// Manual implementation to work around https://github.com/mcarton/rust-derivative/issues/115.
46+
impl<ReturnTy> PartialOrd for Address<ReturnTy> {
47+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
48+
Some(self.cmp(other))
49+
}
50+
}
51+
3952
/// The type of address typically used to return dynamic constant values.
4053
pub type DynamicAddress = Address<DecodedValueThunk>;
4154

subxt/src/custom_values/custom_value_address.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,25 @@ pub struct Yes;
3838

3939
/// A static address to a custom value.
4040
#[derive(Derivative)]
41-
#[derivative(Clone(bound = ""), Debug(bound = ""))]
41+
#[derivative(
42+
Clone(bound = ""),
43+
Debug(bound = ""),
44+
Eq(bound = ""),
45+
Ord(bound = ""),
46+
PartialEq(bound = "")
47+
)]
4248
pub struct StaticAddress<ReturnTy, IsDecodable> {
4349
name: &'static str,
4450
hash: Option<[u8; 32]>,
4551
phantom: PhantomData<(ReturnTy, IsDecodable)>,
4652
}
4753

54+
impl<ReturnTy, IsDecodable> PartialOrd for StaticAddress<ReturnTy, IsDecodable> {
55+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
56+
Some(self.cmp(other))
57+
}
58+
}
59+
4860
impl<ReturnTy, IsDecodable> StaticAddress<ReturnTy, IsDecodable> {
4961
#[doc(hidden)]
5062
/// Creates a new StaticAddress.

subxt/src/runtime_api/runtime_payload.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ pub trait RuntimeApiPayload {
6969
#[derive(Derivative)]
7070
#[derivative(
7171
Clone(bound = "ArgsData: Clone"),
72-
Debug(bound = "ArgsData: std::fmt::Debug")
72+
Debug(bound = "ArgsData: std::fmt::Debug"),
73+
Eq(bound = "ArgsData: std::cmp::Eq"),
74+
Ord(bound = "ArgsData: std::cmp::Ord"),
75+
PartialEq(bound = "ArgsData: std::cmp::PartialEq"),
76+
PartialOrd(bound = "ArgsData: std::cmp::PartialOrd")
7377
)]
7478
pub struct Payload<ArgsData, ReturnTy> {
7579
trait_name: Cow<'static, str>,

subxt/src/storage/storage_address.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ pub struct Yes;
5757
/// A concrete storage address. This can be created from static values (ie those generated
5858
/// via the `subxt` macro) or dynamic values via [`dynamic`].
5959
#[derive(Derivative)]
60-
#[derivative(Clone(bound = "Keys: Clone"), Debug(bound = "Keys: std::fmt::Debug"))]
60+
#[derivative(
61+
Clone(bound = "Keys: Clone"),
62+
Debug(bound = "Keys: std::fmt::Debug"),
63+
Eq(bound = "Keys: std::cmp::Eq"),
64+
Ord(bound = "Keys: std::cmp::Ord"),
65+
PartialEq(bound = "Keys: std::cmp::PartialEq"),
66+
PartialOrd(bound = "Keys: std::cmp::PartialOrd")
67+
)]
6168
pub struct Address<Keys: StorageMultiKey, ReturnTy, Fetchable, Defaultable, Iterable> {
6269
pallet_name: Cow<'static, str>,
6370
entry_name: Cow<'static, str>,

subxt/src/tx/tx_payload.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{
1111
metadata::Metadata,
1212
};
1313
use codec::Encode;
14+
use derivative::Derivative;
1415
use scale_encode::EncodeAsFields;
1516
use scale_value::{Composite, ValueDef, Variant};
1617
use std::{borrow::Cow, sync::Arc};
@@ -48,7 +49,15 @@ pub struct ValidationDetails<'a> {
4849
}
4950

5051
/// A transaction payload containing some generic `CallData`.
51-
#[derive(Clone, Debug)]
52+
#[derive(Derivative)]
53+
#[derivative(
54+
Clone(bound = "CallData: Clone"),
55+
Debug(bound = "CallData: std::fmt::Debug"),
56+
Eq(bound = "CallData: std::cmp::Eq"),
57+
Ord(bound = "CallData: std::cmp::Ord"),
58+
PartialEq(bound = "CallData: std::cmp::PartialEq"),
59+
PartialOrd(bound = "CallData: std::cmp::PartialOrd")
60+
)]
5261
pub struct Payload<CallData> {
5362
pallet_name: Cow<'static, str>,
5463
call_name: Cow<'static, str>,

0 commit comments

Comments
 (0)