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, onlyPartialOrdis implemented (notOrd), allowing skipped fields to contain types that don’t implementOrdorEq(likef32). - 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 implementPartialOrdwithoutOrd
§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§
- Partial
Cmp - Derives
PartialOrdandOrdwith customizable field comparison behavior.