Trait RangeUtil

Source
pub trait RangeUtil<T: Ord + Clone + BasicNum>: Sized + Clone {
    // Required methods
    fn starts_at(&self) -> T;
    fn ends_at(&self) -> T;

    // Provided methods
    fn len(&self) -> Option<T>
       where T: Sub<Output = T> { ... }
    fn includes(&self, x: &T) -> bool { ... }
    fn intersects(&self, other: &impl RangeUtil<T>) -> bool { ... }
    fn intersection(
        &self,
        other: &impl RangeUtil<T>,
    ) -> Option<RangeInclusive<T>> { ... }
    fn setminus(
        &self,
        other: &impl RangeUtil<T>,
    ) -> (Option<RangeInclusive<T>>, Option<RangeInclusive<T>>) { ... }
}
Expand description

Note that this implementation is inefficient if cloning is extremely expensive.

Required Methods§

Source

fn starts_at(&self) -> T

Start bound inclusive

Source

fn ends_at(&self) -> T

End bound inclusive

Provided Methods§

Source

fn len(&self) -> Option<T>
where T: Sub<Output = T>,

The length of the range

Source

fn includes(&self, x: &T) -> bool

Using different name to prevent name clash, this does not require Self: RangeBound

Source

fn intersects(&self, other: &impl RangeUtil<T>) -> bool

Whether two ranges intersect, e.g. 0..=3 and 1..=4 intersect while 0..=3 and 4.. don’t

This also works for “different ranges”, e.g. 0..=3 and 2.. returns true

Source

fn intersection(&self, other: &impl RangeUtil<T>) -> Option<RangeInclusive<T>>

The intersection of two ranges, e.g. 0..=3 and 1..=4 is 1..=3

This also works for “different ranges”, e.g. 0..=3 and 2.. is 1..=3

Source

fn setminus( &self, other: &impl RangeUtil<T>, ) -> (Option<RangeInclusive<T>>, Option<RangeInclusive<T>>)

The result of substracting other from self, e.g. 0..=3`1..=4is(0..=0, None)`

If there are two sets representing the result, then the smaller range comes first. If only one range represents the result, then either result may be None (implementation detail, may change in the future).

This also works for “different ranges”, e.g. 0..=3`2..is0..=1`

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Ord + Clone + BasicNum, R: RangeBounds<T> + Clone> RangeUtil<T> for R