Add (Partial)OrdNoBound derive macros (#2256)

This PR is related to
[this](https://github.com/paritytech/polkadot-sdk/issues/154) issue.

The idea is to add `OrdNoBound` and `PartialOrdNoBound` macros to the
substrate `*NoBound` macros.

closes #2198 
✄
-----------------------------------------------------------------------------

Thank you for your Pull Request! 🙏 Please make sure it follows the
contribution guidelines outlined in
[this
document](https://github.com/paritytech/polkadot-sdk/blob/master/docs/CONTRIBUTING.md)
and fill
out the sections below. Once you're ready to submit your PR for review,
please
delete this section and leave only the text under the "Description"
heading.

# Description

*Please include a summary of the changes and the related issue. Please
also include relevant motivation and context,
including:*

- What does this PR do?
- Why are these changes needed?
- How were these changes implemented and what do they affect?

*Use [Github semantic

linking](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword)
to address any open issues this PR relates to or closes.*

Fixes # (issue number, *if applicable*)

Closes # (issue number, *if applicable*)

# Checklist

- [ ] My PR includes a detailed description as outlined in the
"Description" section above
- [ ] My PR follows the [labeling requirements](CONTRIBUTING.md#Process)
of this project (at minimum one label for `T`
  required)
- [ ] I have made corresponding changes to the documentation (if
applicable)
- [ ] I have added tests that prove my fix is effective or that my
feature works (if applicable)

You can remove the "Checklist" section once all have been checked. Thank
you for your contribution!

✄
-----------------------------------------------------------------------------

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: command-bot <>
This commit is contained in:
Adel Arja
2024-01-27 10:03:48 -03:00
committed by GitHub
parent 30b30bee59
commit 25eaa95fbf
13 changed files with 412 additions and 10 deletions
@@ -15,11 +15,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//! Tests for DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound, and
//! RuntimeDebugNoBound
//! Tests for DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound,
//! RuntimeDebugNoBound, PartialOrdNoBound and OrdNoBound
use frame_support::{
CloneNoBound, DebugNoBound, DefaultNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound,
CloneNoBound, DebugNoBound, DefaultNoBound, EqNoBound, OrdNoBound, PartialEqNoBound,
PartialOrdNoBound, RuntimeDebugNoBound,
};
#[derive(RuntimeDebugNoBound)]
@@ -32,7 +33,7 @@ fn runtime_debug_no_bound_display_correctly() {
}
trait Config {
type C: std::fmt::Debug + Clone + Eq + PartialEq + Default;
type C: std::fmt::Debug + Clone + Eq + PartialEq + Default + PartialOrd + Ord;
}
struct Runtime;
@@ -42,7 +43,15 @@ impl Config for Runtime {
type C = u32;
}
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
#[derive(
DebugNoBound,
CloneNoBound,
EqNoBound,
PartialEqNoBound,
DefaultNoBound,
PartialOrdNoBound,
OrdNoBound,
)]
struct StructNamed<T: Config, U, V> {
a: u32,
b: u64,
@@ -96,9 +105,18 @@ fn test_struct_named() {
};
assert!(b != a_1);
assert!(b > a_1);
}
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
#[derive(
DebugNoBound,
CloneNoBound,
EqNoBound,
PartialEqNoBound,
DefaultNoBound,
PartialOrdNoBound,
OrdNoBound,
)]
struct StructUnnamed<T: Config, U, V>(u32, u64, T::C, core::marker::PhantomData<(U, V)>);
#[rustversion::attr(not(stable), ignore)]
@@ -128,9 +146,18 @@ fn test_struct_unnamed() {
let b = StructUnnamed::<Runtime, ImplNone, ImplNone>(1, 2, 4, Default::default());
assert!(b != a_1);
assert!(b > a_1);
}
#[derive(DebugNoBound, CloneNoBound, EqNoBound, PartialEqNoBound, DefaultNoBound)]
#[derive(
DebugNoBound,
CloneNoBound,
EqNoBound,
PartialEqNoBound,
DefaultNoBound,
PartialOrdNoBound,
OrdNoBound,
)]
struct StructNoGenerics {
field1: u32,
field2: u64,
@@ -254,3 +281,63 @@ fn test_enum() {
assert!(variant_2.clone() == variant_2);
assert!(variant_3.clone() == variant_3);
}
// Return all the combinations of (0, 0, 0), (0, 0, 1), (0, 1, 0), ...
// Used to test all the possible struct orderings
fn combinations() -> Vec<(u32, u64, u32)> {
let mut v = vec![];
for a in 0..=1 {
for b in 0..=1 {
for c in 0..=1 {
v.push((a, b, c));
}
}
}
v
}
// Ensure that the PartialOrdNoBound follows the same rules as the native PartialOrd
#[derive(Debug, Clone, Eq, PartialEq, Default, PartialOrd, Ord)]
struct StructNamedRust {
a: u32,
b: u64,
c: u32,
phantom: core::marker::PhantomData<(ImplNone, ImplNone)>,
}
#[derive(Debug, Clone, Eq, PartialEq, Default, PartialOrd, Ord)]
struct StructUnnamedRust(u32, u64, u32, core::marker::PhantomData<(ImplNone, ImplNone)>);
#[test]
fn struct_named_same_as_native_rust() {
for (a, b, c) in combinations() {
let a_1 =
StructNamed::<Runtime, ImplNone, ImplNone> { a, b, c, phantom: Default::default() };
let b_1 = StructNamedRust { a, b, c, phantom: Default::default() };
for (a, b, c) in combinations() {
let a_2 =
StructNamed::<Runtime, ImplNone, ImplNone> { a, b, c, phantom: Default::default() };
let b_2 = StructNamedRust { a, b, c, phantom: Default::default() };
assert_eq!(a_1.partial_cmp(&a_2), b_1.partial_cmp(&b_2));
assert_eq!(a_1.cmp(&a_2), b_1.cmp(&b_2));
assert_eq!(a_1.eq(&a_2), b_1.eq(&b_2));
}
}
}
#[test]
fn struct_unnamed_same_as_native_rust() {
for (a, b, c) in combinations() {
let a_1 = StructUnnamed::<Runtime, ImplNone, ImplNone>(a, b, c, Default::default());
let b_1 = StructUnnamedRust(a, b, c, Default::default());
for (a, b, c) in combinations() {
let a_2 = StructUnnamed::<Runtime, ImplNone, ImplNone>(a, b, c, Default::default());
let b_2 = StructUnnamedRust(a, b, c, Default::default());
assert_eq!(a_1.partial_cmp(&a_2), b_1.partial_cmp(&b_2));
assert_eq!(a_1.cmp(&a_2), b_1.cmp(&b_2));
assert_eq!(a_1.eq(&a_2), b_1.eq(&b_2));
}
}
}