#![forbid(unsafe_code)]
use std::ops::Range;
mod compose;
mod cut;
trait RangeContainsExt<T> {
fn contains_or_ends_at(&self, index: &T) -> bool;
}
impl<T> RangeContainsExt<T> for Range<T>
where
T: PartialOrd,
{
fn contains_or_ends_at(&self, index: &T) -> bool {
index <= &self.end
}
}
pub trait RangeExt {
fn concat(&self, other: &Self) -> Self
where
Self: Clone;
fn remove_prefix(self, prefix: Self) -> Self;
fn remove_suffix(self, suffix: Self) -> Self;
fn cut<C>(&self, middle: &C) -> (Self, Self)
where
Self: Sized,
Self: cut::RangeCut<C>,
{
cut::RangeCut::cut(self, middle)
}
fn compose<Rhs, Output>(&self, rhs: &Rhs) -> Output
where
Self: compose::RangeCompose<Rhs, Output = Output>,
{
compose::RangeCompose::compose(self, rhs)
}
}
impl<T> RangeExt for Range<T>
where
T: std::cmp::PartialOrd + std::cmp::PartialEq + Clone,
T: std::fmt::Debug,
{
fn concat(&self, other: &Self) -> Self {
assert!(self.end == other.start);
self.start.clone()..other.end.clone()
}
fn remove_prefix(self, prefix: Self) -> Self {
assert!(prefix.start == self.start);
assert!(self.contains_or_ends_at(&prefix.end));
prefix.end..self.end
}
fn remove_suffix(self, suffix: Self) -> Self {
assert!(self.contains(&suffix.start));
assert!(self.end == suffix.end);
self.start..suffix.start
}
}