use core::ops;
pub trait TiRangeBounds<K> {
type Range: ops::RangeBounds<usize>;
fn into_range(self) -> Self::Range;
}
impl<K> TiRangeBounds<K> for ops::Range<K>
where
K: Into<usize>,
{
type Range = ops::Range<usize>;
#[inline]
fn into_range(self) -> Self::Range {
self.start.into()..self.end.into()
}
}
impl<K> TiRangeBounds<K> for ops::RangeFrom<K>
where
K: Into<usize>,
{
type Range = ops::RangeFrom<usize>;
#[inline]
fn into_range(self) -> Self::Range {
self.start.into()..
}
}
impl<K> TiRangeBounds<K> for ops::RangeFull
where
K: Into<usize>,
{
type Range = Self;
#[inline]
fn into_range(self) -> Self::Range {
Self
}
}
impl<K> TiRangeBounds<K> for ops::RangeInclusive<K>
where
K: Into<usize>,
{
type Range = ops::RangeInclusive<usize>;
#[inline]
fn into_range(self) -> Self::Range {
let (start, end) = self.into_inner();
start.into()..=end.into()
}
}
impl<K> TiRangeBounds<K> for ops::RangeTo<K>
where
K: Into<usize>,
{
type Range = ops::RangeTo<usize>;
#[inline]
fn into_range(self) -> Self::Range {
..self.end.into()
}
}
impl<K> TiRangeBounds<K> for ops::RangeToInclusive<K>
where
K: Into<usize>,
{
type Range = ops::RangeToInclusive<usize>;
#[inline]
fn into_range(self) -> Self::Range {
..=self.end.into()
}
}
impl<K> TiRangeBounds<K> for (ops::Bound<K>, ops::Bound<K>)
where
K: Into<usize>,
{
type Range = (ops::Bound<usize>, ops::Bound<usize>);
#[inline]
fn into_range(self) -> Self::Range {
(map_bound(self.0), map_bound(self.1))
}
}
#[inline]
fn map_bound<K>(bound: ops::Bound<K>) -> ops::Bound<usize>
where
K: Into<usize>,
{
match bound {
ops::Bound::Included(index) => ops::Bound::Included(index.into()),
ops::Bound::Excluded(index) => ops::Bound::Excluded(index.into()),
ops::Bound::Unbounded => ops::Bound::Unbounded,
}
}