Ordering Has Rust’s Methods: then, then_with, reverse and the Six TestsStatus: Accepted
Date: November 29, 2026 (supervisor’s decision under the owner’s rule of
2026-10-31, docs/supervisor.md, Language decisions: an obvious std
counterpart of a Rust call is the supervisor’s)
Answers: whether Ordering offers then, reverse and is_eq, which a
comparator over two keys needs and which are NK1171 today
(#1873)
Supersedes / Related: ADR-341 (Ordering is the enum
Less | Equal | Greater that compare answers, and is the standard library’s
std::cmp::Ordering in the Rust a program lowers to; its §6 left the methods as
“library entries, asked when a program needs them”),
ADR-384 (the precedent: a missing std counterpart of a Rust call
is added as that call), ADR-collections-v3
(or_insert_with runs its lambda only where it is needed, and pauses where the
lambda does)
A comparator over two keys is written b.1.compare(a.1).then(a.0.compare(b.0)):
the second key decides where the first is equal. Ordering has no method, so the
line is NK1171, and a program sorts twice with two stable sort_by calls
instead (benches/game/k-nucleotide/speed.nika). Does Ordering have Rust’s
methods, and which?
| Language / tool | Its answer | What it costs there | Source |
|---|---|---|---|
| Rust | std::cmp::Ordering has then, then_with, reverse, is_eq, is_ne, is_lt, is_gt, is_le, is_ge |
nothing: methods on a one-byte enum, inlined | core::cmp::Ordering, stable since 1.17 (then) and 1.53 (is_*) |
| Haskell | Ordering is a Monoid: compare a b <> compare c d is Rust’s then; Down reverses |
nothing beyond the instance | Data.Ord, from memory, not checked |
| Java | Comparator.thenComparing, reversed() on the comparator, not on the result |
an object per comparator | java.util.Comparator, from memory, not checked |
What transfers is Rust’s: Ordering is Rust’s own type in the generated file
(ADR-341), so each method lowers to the method of the same name with nothing in
between. Java’s form chains comparators, not results; Nikaia’s sort_by takes a
lambda that returns an Ordering, so the result is where the method belongs.
Orderingrows.sort_by fn (a, b) { b.1.compare(a.1).then(a.0.compare(b.0)) }
match, or sorts twicerows.sort_by fn (a, b) {
match b.1.compare(a.1) {
Ordering::Equal => a.0.compare(b.0)
else => b.1.compare(a.1)
}
}
| D1 the special case pays, not the common one | D2 the compiler works, not the developer | D3 no cost for the common case | D4 consistent with itself | If it is wrong | |
|---|---|---|---|---|---|
| A | only a program that calls one sees it | the comparator is one line | none: Rust’s methods, inlined | Ordering is Rust’s Ordering everywhere else (ADR-341) |
a method that is never used costs a contract entry |
| B | every comparator over two keys pays a match or a second sort |
the developer writes what the library could | a second sort costs a second pass | a Rust type whose methods are cut off | none, but the detour stays |
Option A wins on every weight; B only keeps the detour that CLAUDE.md (A
missing form stops the work) counts as debt.
Ordering has then, then_with and reverse, as in Rust.x.then(y) is x where x is not Equal, and y where it is. x.then_with(f)
is the same with f() in place of y, and runs f only where x is Equal; it
pauses and throws where f does, as or_insert_with does
(ADR-collections-v3). x.reverse() is Greater for Less, Less for Greater,
and Equal for Equal. None of them panics; then and reverse never pause.
Ordering has the six tests is_eq, is_ne, is_lt, is_gt, is_le and is_ge.Each is bool: is_eq is true for Equal, is_lt for Less, is_le for
Less and Equal, and the others likewise. None of them panics or pauses.
std entries of Ordering and lower to Rust’s methods of the same name.Each is a fn."Ordering::…" entry in std.contracts, emitted as the method of
std::cmp::Ordering with the same name; nothing else is written for them. A name
Ordering declared by a program keeps its own methods (Part I 4.7: a program that
declares a type of the same name keeps its own).
A comparator over several keys is one expression, and a program that sorts twice
to get the same order can sort once. The specification’s sentence on Ordering
in Part I 4.7 names the methods. The build is
#1873.