silk/order
Compile-time ordering witnesses and a three-way result derived from strict comparison.
When to use
Add an Order bound when a generic algorithm needs <, as sorting and binary search do. Use
compare when all three outcomes matter, or less and equal for predicates.
Details
An interface witness is selected during specialization; it creates no service requirement or
runtime dispatch. Equal means neither operand orders before the other, so equality cannot
disagree with the witness's own strict ordering.
Gotchas
A witness must define a strict total order. The module cannot verify this law. If two different
values are incomparable, compare and equal report them as equal.
Examples
Classify each comparison result
import silk.order as Order
pub fn main() -> i32 {
let low = Order.compare<i32>(2, 7)
let same = Order.compare<i32>(7, 7)
let high = Order.compare<i32>(9, 7)
if !Order.isLess(&low) {
return 1
}
if !Order.isEqual(&same) {
return 2
}
if !Order.isGreater(&high) {
return 3
}
return 42
}Import as Order with import silk.order.
Public declarations: 11.
Less
pub struct LessThe left value orders before the right value.
Equal
pub struct EqualNeither value orders before the other.
Greater
pub struct GreaterThe left value orders after the right value.
Ordering
pub struct OrderingA three-way comparison result that contains exactly one of Less, Equal, or Greater.
Field value
pub value: silk/order.Equal | silk/order.Greater | silk/order.LessThe comparison result, which a match expression narrows to one outcome type.
Order
pub interface OrderStatic strict-ordering contract for primitive comparable values.
When to use
Use this interface when a generic algorithm needs strict ordering through <.
Details
A witness supplies the strict lessThan the operator < spells. Equality is derived from it
rather than declared, so a witness cannot disagree with itself about which values are equal.
Gotchas
An implementation must define a strict total order. If two different values are incomparable,
compare and equal treat them as equal.
Operation lessThan
operator < fn lessThan(left: &Self, right: &Self) -> boolReports whether left orders strictly before right. An implementation must define a strict
total order so different values are not incomparable.
Implementation Order for u8
impl Order for u8Operation lessThan
lessThan = Intrinsic.u8LessThanImplementation Order for u16
impl Order for u16Operation lessThan
lessThan = Intrinsic.u16LessThanImplementation Order for u32
impl Order for u32Operation lessThan
lessThan = Intrinsic.u32LessThanImplementation Order for u64
impl Order for u64Operation lessThan
lessThan = Intrinsic.u64LessThanImplementation Order for usize
impl Order for usizeOperation lessThan
lessThan = Intrinsic.usizeLessThanImplementation Order for i8
impl Order for i8Operation lessThan
lessThan = Intrinsic.i8LessThanImplementation Order for i16
impl Order for i16Operation lessThan
lessThan = Intrinsic.i16LessThanImplementation Order for i32
impl Order for i32Operation lessThan
lessThan = Intrinsic.i32LessThanImplementation Order for i64
impl Order for i64Operation lessThan
lessThan = Intrinsic.i64LessThanImplementation Order for isize
impl Order for isizeOperation lessThan
lessThan = Intrinsic.isizeLessThancompare
pub fn compare<T>(left: T, right: T) -> OrderingCompares two values three ways using the concrete comparison selected during specialization.
When to use
Use this function when the caller needs to distinguish less, equal, and greater outcomes.
Details
Calls the selected strict comparison with left, right. If that result is false, it calls the
comparison with right, left. The function then consumes both owned arguments.
Gotchas
If neither comparison is true, this function returns Equal. The witness must not leave two
different values incomparable.
less
pub fn less<T>(left: T, right: T) -> boolReports whether left orders strictly before right under the selected Order witness. The
function calls the witness once and then consumes both owned arguments.
equal
pub fn equal<T>(left: T, right: T) -> boolReports whether neither value orders before the other under the selected Order witness. The
function calls the witness once or twice and then consumes both owned arguments.
Gotchas
If the witness leaves different values incomparable, this function reports them as equal.
isLess
pub fn isLess(ordering: &silk/order.Ordering) -> boolReports whether an ordering is Less.
isEqual
pub fn isEqual(ordering: &silk/order.Ordering) -> boolReports whether an ordering is Equal.
isGreater
pub fn isGreater(ordering: &silk/order.Ordering) -> boolReports whether an ordering is Greater.