range-cmp
This Rust crate provides the RangeOrd trait on all types that
implement Ord. This trait exposes a rcmp associated method
that allows comparing a value with a range of values:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
The crate is #![no_std] and has zero dependencies, so it can be used in embedded
and other environments without the standard library. Its MSRV (minimum supported
Rust version) is 1.35.
Empty ranges handling
Empty ranges are handled explicitly, instead of returning an arbitrary,
representation-dependent answer. An empty range is reported as
RangeOrdering::Empty:
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Emptiness is judged from the bounds, not from the population of the type:
..0u32 is treated as a regular (non-empty) range even though no u32 is
below 0.
Partial orders
The crate also provides the PartialRangeOrd trait on all types that
implement PartialOrd. Because a partial order is a poset rather than a
line, a value may be incomparable with one or both bounds, so a single
Below/Inside/Above verdict is not always meaningful. partial_rcmp
therefore returns a RangePosition: the pair of the value's relationships
to the lower and upper bounds, which never loses information. Use
RangePosition::ordering to collapse it into a simple RangeOrdering when
the value is comparable with both bounds.
use ;
assert_eq!;
assert_eq!;
assert_eq!;
// `NaN` is incomparable with the bounds:
assert_eq!;