#[derive(PartialCmp)]
{
// Attributes available to this derive:
#[ord]
}
Expand description
Derives comparison traits with customizable field behavior.
By default, this macro generates implementations for PartialEq, Eq,
PartialOrd, and Ord. All traits respect the same field configuration,
ensuring consistent behavior.
§Struct-Level Attributes
#[ord(reverse)]— Reverses the final comparison result#[ord(by = [field1(asc), field2(desc)])]— Explicit field comparison order#[ord(skip_partial_eq)]— Don’t generatePartialEq(disables all other traits too)#[ord(skip_eq)]— Don’t generateEq(also disablesOrd)#[ord(skip_partial_ord)]— Don’t generatePartialOrd(also disablesOrd)#[ord(skip_ord)]— Don’t generateOrd
§Field-Level Attributes
#[ord(skip)]— Exclude this field from all comparisons (both eq and ord)#[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 for ordering (signature:fn(&T, &T) -> Ordering)#[ord(eq_with = "path::to::fn")]— Custom equality function (signature:fn(&T, &T) -> bool)#[ord(none_order = "first")]or#[ord(none_order = "last")]— Option handling
§Variant-Level Attributes (Enums)
#[ord(rank = N)]— Explicit variant ranking (lower = less than)
§Custom Comparison Functions
When using compare_with, you can optionally provide eq_with for a custom
equality check. If eq_with is not provided, equality is derived from the
comparison function (equal when compare_with returns Ordering::Equal).
use partial_cmp_derive::PartialCmp;
use std::cmp::Ordering;
fn cmp_abs(a: &i32, b: &i32) -> Ordering {
a.abs().cmp(&b.abs())
}
fn eq_abs(a: &i32, b: &i32) -> bool {
a.abs() == b.abs()
}
#[derive(PartialCmp)]
struct AbsValue {
#[ord(compare_with = "cmp_abs", eq_with = "eq_abs")]
value: i32,
}§Example
use partial_cmp_derive::PartialCmp;
#[derive(Debug, PartialCmp)]
#[ord(by = [score(desc), name(asc)])]
struct Player {
id: u64, // Not compared (not in `by` list)
name: String,
score: u32,
}