PartialCmp

Derive Macro PartialCmp 

Source
#[derive(PartialCmp)]
{
    // Attributes available to this derive:
    #[ord]
}
Expand description

Derives PartialOrd and Ord with customizable field comparison behavior.

§Struct-Level Attributes

  • #[ord(reverse)] — Reverses the final comparison result
  • #[ord(by = [field1(asc), field2(desc)])] — Explicit field comparison order

§Field-Level Attributes

  • #[ord(skip)] — Exclude this field from comparison
  • #[ord(order = "asc")] or #[ord(order = "desc")] — Sort direction
  • #[ord(priority = N)] — Comparison priority (lower = compared first)
  • #[ord(compare_with = "path::to::fn")] — Custom comparison function
  • #[ord(none_order = "first")] or #[ord(none_order = "last")] — Option handling

§Variant-Level Attributes (Enums)

  • #[ord(rank = N)] — Explicit variant ranking (lower = less than)

§Requirements

  • The type must also derive or implement PartialEq and Eq
  • All compared fields must implement Ord (or provide compare_with)

§Example

use partial_cmp_derive::PartialCmpDerive;

#[derive(PartialEq, Eq, PartialCmpDerive)]
#[ord(by = [score(desc), name(asc)])]
struct Player {
    id: u64,  // Not compared (not in `by` list)
    name: String,
    score: u32,
}