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§
Provided Methods§
Sourcefn includes(&self, x: &T) -> bool
fn includes(&self, x: &T) -> bool
Using different name to prevent name clash, this does not require Self: RangeBound
Sourcefn intersects(&self, other: &impl RangeUtil<T>) -> bool
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
Sourcefn intersection(&self, other: &impl RangeUtil<T>) -> Option<RangeInclusive<T>>
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
Sourcefn setminus(
&self,
other: &impl RangeUtil<T>,
) -> (Option<RangeInclusive<T>>, Option<RangeInclusive<T>>)
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..is
0..=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.