Skip to main content

RangeSet

Struct RangeSet 

Source
#[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
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional 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

Source

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.

Source

pub fn single(v: u64) -> Self

A range set holding exactly one value.

Source

pub fn contains(&self, v: u64) -> bool

Whether v falls in any of the ranges. Binary search.

Source

pub fn is_empty(&self) -> bool

Whether the set holds no values at all. Such a set matches nothing.

Source

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.

Trait Implementations§

Source§

impl Clone for RangeSet

Source§

fn clone(&self) -> RangeSet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RangeSet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RangeSet

Source§

impl PartialEq for RangeSet

Source§

fn eq(&self, other: &RangeSet) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RangeSet

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more