Skip to main content

Interval

Struct Interval 

Source
pub struct Interval<T>
where T: Copy + PartialOrd,
{ pub start: T, pub end: T, }
Expand description

Half-open time interval [start, end).

Half-open intervals are convenient for period arithmetic because adjacent intervals such as [a, b) and [b, c) touch without overlapping.

Fields§

§start: T

Inclusive lower bound.

§end: T

Exclusive upper bound.

Implementations§

Source§

impl<T> Interval<T>
where T: Copy + PartialOrd,

Source

pub fn new<S, E>(start: S, end: E) -> Interval<T>
where S: Into<T>, E: Into<T>,

Construct without validation.

This accepts any start/end pair, including reversed or NaN-like endpoints. Prefer try_new when the bounds come from computation or external input.

Source

pub fn try_new<S, E>( start: S, end: E, ) -> Result<Interval<T>, InvalidIntervalError>
where S: Into<T>, E: Into<T>,

Validating constructor: rejects start > end and NaN endpoints.

Zero-length intervals where start == end are allowed.

Source

pub fn intersection(&self, other: &Interval<T>) -> Option<Interval<T>>

Overlap as a half-open range. Returns None if the intervals touch only at a point or do not overlap.

Source

pub fn union(&self, other: &Interval<T>) -> Vec<Interval<T>>

Smallest interval covering both self and other, together with any gap between them.

Returns one interval when they overlap or touch, two when they are strictly disjoint (sorted by start time).

This pairwise API preserves disjointness instead of forcing a merged interval that would invent coverage through the gap.

Source

pub fn complement(&self, periods: &[Interval<T>]) -> Vec<Interval<T>>

Gaps inside self that are not covered by any interval in periods.

periods must be sorted and non-overlapping (see Interval::validate). Intervals outside self are effectively ignored except for how they advance the internal cursor.

Source

pub fn try_complement( &self, periods: &[Interval<T>], ) -> Result<Vec<Interval<T>>, PeriodListError>

Checked variant of complement.

Validates that self is well ordered and that periods is sorted, non-overlapping, and internally valid before computing the complement.

Source

pub fn validate(periods: &[Interval<T>]) -> Result<(), PeriodListError>

Check that a list is sorted, non-overlapping, and each start <= end.

Touching intervals such as [a, b) followed by [b, c) are valid.

Source

pub fn intersect_many(a: &[Interval<T>], b: &[Interval<T>]) -> Vec<Interval<T>>

Intersection of two sorted, non-overlapping period lists. O(n + m).

Source

pub fn try_intersect_many( a: &[Interval<T>], b: &[Interval<T>], ) -> Result<Vec<Interval<T>>, PeriodListError>

Checked variant of intersect_many.

Validates both input lists before computing their intersection.

Source

pub fn union_many(a: &[Interval<T>], b: &[Interval<T>]) -> Vec<Interval<T>>

Union of two sorted period lists.

Overlapping and adjacent intervals are merged. The result is sorted and non-overlapping.

Source

pub fn normalize(periods: &[Interval<T>]) -> Vec<Interval<T>>

Sort and merge overlapping or adjacent intervals.

This is useful for normalizing hand-built or concatenated period lists before later set operations.

Trait Implementations§

Source§

impl<T> Clone for Interval<T>
where T: Clone + Copy + PartialOrd,

Source§

fn clone(&self) -> Interval<T>

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<T> Debug for Interval<T>
where T: Debug + Copy + PartialOrd,

Source§

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

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

impl<T> Display for Interval<T>
where T: Copy + PartialOrd + Display,

Source§

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

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

impl<T> PartialEq for Interval<T>
where T: PartialEq + Copy + PartialOrd,

Source§

fn eq(&self, other: &Interval<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Copy for Interval<T>
where T: Copy + PartialOrd,

Source§

impl<T> StructuralPartialEq for Interval<T>
where T: Copy + PartialOrd,

Auto Trait Implementations§

§

impl<T> Freeze for Interval<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Interval<T>
where T: RefUnwindSafe,

§

impl<T> Send for Interval<T>
where T: Send,

§

impl<T> Sync for Interval<T>
where T: Sync,

§

impl<T> Unpin for Interval<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Interval<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Interval<T>
where T: UnwindSafe,

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.

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,