Crate partial_cmp_derive

Crate partial_cmp_derive 

Source
Expand description

§partial-cmp-derive

A procedural macro crate for deriving PartialOrd and Ord with fine-grained control over field comparison behavior.

§Features

  • Skip fields: Use #[ord(skip)] to exclude fields from comparison. When any field is skipped, only PartialOrd is implemented (not Ord), allowing skipped fields to contain types that don’t implement Ord or Eq (like f32).
  • Sort order: Use #[ord(order = "asc")] or #[ord(order = "desc")] per field
  • Explicit ordering: Use #[ord(by = [field1(desc), field2(asc)])] at struct level
  • Field priority: Use #[ord(priority = N)] for implicit ordering (lower = first)
  • Custom comparators: Use #[ord(compare_with = "path::to::fn")]
  • Reverse all: Use #[ord(reverse)] at struct level to reverse entire comparison
  • Enum ranking: Use #[ord(rank = N)] to control variant ordering
  • Option handling: Use #[ord(none_order = "first")] or "last"
  • Skip Ord: Use #[ord(skip_ord)] to only implement PartialOrd without Ord

§Example

use partial_cmp_derive::PartialCmp;

// When no fields are skipped, both PartialOrd and Ord are implemented
#[derive(PartialEq, Eq, PartialCmp)]
struct Point {
    x: i32,
    y: i32,
}

// When fields are skipped, only PartialOrd is implemented
// This allows using types like f32 that don't implement Ord
#[derive(Debug, PartialEq, PartialCmp)]
struct Measurement {
    #[ord(skip)]
    raw_value: f32,  // f32 doesn't implement Ord, but it's skipped
    timestamp: u64,
}

This generates PartialOrd (and Ord when no fields are skipped) implementations that compare fields in the specified order, ignoring skipped fields entirely.

Derive Macros§

PartialCmp
Derives PartialOrd and Ord with customizable field comparison behavior.