#[non_exhaustive]pub struct RangeSet {
ranges: Vec<RangeInclusive<u64>>,
}Expand description
A sorted, coalesced set of inclusive u64 ranges.
Built once from a configuration and then queried per unit, so
construction sorts and coalesces (including adjacent ranges: 1..=3
and 4..=6 become 1..=6) and RangeSet::contains is a binary
search over the result.
Ranges whose start exceeds their end are empty and are discarded at construction rather than stored as a range that can never match.
#[non_exhaustive] with no Default: the fields are private and there
are two constructors, so there is no meaningful zero value to derive.
§The written form is a plain list
Under the serde feature a range set is written as the list of ranges
it was built from — [{"start": 1, "end": 3}, {"start": 4, "end": 6}] —
and read back through RangeSet::new, which is what
#[serde(from = ...)] buys. A derived Deserialize would fill the private
ranges field straight from the file, and the invariant every method here
relies on — sorted, disjoint, non-adjacent — would then hold only for files
that happened to be written in order. RangeSet::contains is a binary
search, so on an unsorted set it does not fail: it answers false for
values that are in the set, and the class quietly stops claiming half its
traffic.
Two consequences of routing through the constructor are worth knowing
before reading a file back. The written form is normalised, so the two
ranges above are one range when they are read and the file that comes back
out says [{"start": 1, "end": 6}]. And an inverted range is dropped
rather than stored, so a file whose only range is {"start": 5, "end": 1}
produces an empty set — which
ShapeProfile::try_new then refuses as
ShapeError::InertMatcher rather than
arming a class that can never claim anything.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.ranges: Vec<RangeInclusive<u64>>Disjoint, non-adjacent, ascending by start. The invariant every method below relies on.
Implementations§
Source§impl RangeSet
impl RangeSet
Sourcepub fn new(ranges: impl IntoIterator<Item = RangeInclusive<u64>>) -> Self
pub fn new(ranges: impl IntoIterator<Item = RangeInclusive<u64>>) -> Self
Build a range set from any iterator of inclusive ranges.
The input needs no ordering: overlapping, adjacent and duplicated
ranges are merged, and empty ranges (start > end) are dropped.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the set holds no values at all. Such a set matches nothing.
Sourcepub fn ranges(&self) -> &[RangeInclusive<u64>]
pub fn ranges(&self) -> &[RangeInclusive<u64>]
The coalesced ranges, ascending and disjoint.
Exposed because coalescing is a claim — that 1..=3 plus 4..=6
is one range — and RangeSet::contains cannot falsify it: both
shapes answer every contains query identically. A test that can
only see contains cannot tell a working coalescer from none.