use std::cmp::{self}; use std::ops::{Range, RangeInclusive}; pub trait RangeExt { fn sorted(&self) -> Self; fn to_inclusive(&self) -> RangeInclusive; fn overlaps(&self, other: &Range) -> bool; fn contains_inclusive(&self, other: &Range) -> bool; } impl RangeExt for Range { fn sorted(&self) -> Self { cmp::min(&self.start, &self.end).clone()..cmp::max(&self.start, &self.end).clone() } fn to_inclusive(&self) -> RangeInclusive { self.start.clone()..=self.end.clone() } fn overlaps(&self, other: &Range) -> bool { self.start < other.end && other.start < self.end } fn contains_inclusive(&self, other: &Range) -> bool { self.start <= other.start && other.end <= self.end } } impl RangeExt for RangeInclusive { fn sorted(&self) -> Self { cmp::min(self.start(), self.end()).clone()..=cmp::max(self.start(), self.end()).clone() } fn to_inclusive(&self) -> RangeInclusive { self.clone() } fn overlaps(&self, other: &Range) -> bool { self.start() < &other.end && &other.start <= self.end() } fn contains_inclusive(&self, other: &Range) -> bool { self.start() <= &other.start && &other.end <= self.end() } }