serial-uint
Generic serial number arithmetic following RFC 1982 semantics.
A serial number is a fixed-width unsigned integer whose operations wrap around. Its defining
difference from an ordinary integer is comparison: in the wraparound space MAX is the
predecessor of 0, so Serial(MAX) < Serial(0).
no_std— depends only oncore.- Generic over
u8/u16/u32/u64/u128/usize. - Wraparound add / subtract with an offset.
- RFC 1982 wraparound comparison via
PartialOrd. When two values are exactly half a cycle apart their ordering is undefined andpartial_cmpreturnsNone(this is whyPartialOrdis used instead ofOrd). - Compare and test equality directly against the raw underlying value, in both directions.
Usage
use Serial;
let a = new;
let b = a + 10; // wraparound: 250 + 10 = 4
assert_eq!;
// wraparound comparison: a precedes b
assert!;
// MAX is the predecessor of 0
assert!;
// compare directly against a raw value (also wraparound)
assert!;
assert!;
// exactly half a cycle apart -> undefined ordering
assert_eq!;
Semantics
For two serial numbers, let d = (other - self) mod 2^BITS:
d |
result |
|---|---|
0 |
equal |
0 < d < HALF |
self < other |
d == HALF |
undefined (None) |
d > HALF |
self > other |
where HALF = 2^(BITS-1). Per RFC 1982 the largest valid addend is HALF - 1; larger addends are
undefined and trigger a debug_assert! in debug builds.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.