Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m44s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Fix: https://jumble.social/notes/nevent1qvzqqqqqqypzqwlsccluhy6xxsr6l9a9uhhxf75g85g8a709tprjcn4e42h053vaqyvhwumn8ghj7un9d3shjtnjv4ukztnnw5hkjmnzdauqzrnhwden5te0dehhxtnvdakz7qpqpj4awhj4ul6tztlne0v7efvqhthygt0myrlxslpsjh7t6x4esapq3lf5c0 Reviewed-on: #15
46 lines
1.3 KiB
Rust
46 lines
1.3 KiB
Rust
use std::cmp::{self};
|
|
use std::ops::{Range, RangeInclusive};
|
|
|
|
pub trait RangeExt<T> {
|
|
fn sorted(&self) -> Self;
|
|
fn to_inclusive(&self) -> RangeInclusive<T>;
|
|
fn overlaps(&self, other: &Range<T>) -> bool;
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool;
|
|
}
|
|
|
|
impl<T: Ord + Clone> RangeExt<T> for Range<T> {
|
|
fn sorted(&self) -> Self {
|
|
cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone()
|
|
}
|
|
|
|
fn to_inclusive(&self) -> RangeInclusive<T> {
|
|
self.start.clone()..=self.end.clone()
|
|
}
|
|
|
|
fn overlaps(&self, other: &Range<T>) -> bool {
|
|
self.start < other.end && other.start < self.end
|
|
}
|
|
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool {
|
|
self.start <= other.start && other.end <= self.end
|
|
}
|
|
}
|
|
|
|
impl<T: Ord + Clone> RangeExt<T> for RangeInclusive<T> {
|
|
fn sorted(&self) -> Self {
|
|
cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone()
|
|
}
|
|
|
|
fn to_inclusive(&self) -> RangeInclusive<T> {
|
|
self.clone()
|
|
}
|
|
|
|
fn overlaps(&self, other: &Range<T>) -> bool {
|
|
self.start() < &other.end && &other.start <= self.end()
|
|
}
|
|
|
|
fn contains_inclusive(&self, other: &Range<T>) -> bool {
|
|
self.start() <= &other.start && &other.end <= self.end()
|
|
}
|
|
}
|