1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
mod impl_range_bounds {
use crate::{Exclusive, Inclusive, LeftBounded, RightBounded};
use std::ops::{Bound, RangeBounds};
impl<T: PartialOrd> RangeBounds<T> for LeftBounded<T, Inclusive> {
fn start_bound(&self) -> Bound<&T> {
Bound::Included(&self.limit)
}
fn end_bound(&self) -> Bound<&T> {
Bound::Unbounded
}
}
impl<T: PartialOrd> RangeBounds<T> for LeftBounded<T, Exclusive> {
fn start_bound(&self) -> Bound<&T> {
Bound::Excluded(&self.limit)
}
fn end_bound(&self) -> Bound<&T> {
Bound::Unbounded
}
}
impl<T: PartialOrd> RangeBounds<T> for RightBounded<T, Inclusive> {
fn start_bound(&self) -> Bound<&T> {
Bound::Unbounded
}
fn end_bound(&self) -> Bound<&T> {
Bound::Included(&self.limit)
}
}
impl<T: PartialOrd> RangeBounds<T> for RightBounded<T, Exclusive> {
fn start_bound(&self) -> Bound<&T> {
Bound::Unbounded
}
fn end_bound(&self) -> Bound<&T> {
Bound::Excluded(&self.limit)
}
}
}
mod converters {
use crate::{Exclusive, Inclusive, Interval, IntervalIsEmpty};
/// ```
/// use std::any::{Any, TypeId};
/// use inter_val::{Interval, Inclusive, Exclusive};
/// let a: Interval<_, _, _> = (2..4).try_into().unwrap();
/// assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Exclusive>>());
/// assert_eq!(a.left().limit, 2);
/// assert_eq!(a.right().limit, 4);
/// ```
impl<T: PartialOrd> TryFrom<std::ops::Range<T>> for Interval<T, Inclusive, Exclusive> {
type Error = IntervalIsEmpty;
fn try_from(r: std::ops::Range<T>) -> Result<Self, Self::Error> {
Self::try_new(r.start.into(), r.end.into()).ok_or(IntervalIsEmpty)
}
}
/// ```
/// use std::any::{Any, TypeId};
/// use inter_val::{Interval, Inclusive};
/// let a: Interval<_, _, _> = (2..=4).try_into().unwrap();
/// assert_eq!(a.type_id(), TypeId::of::<Interval<i32, Inclusive, Inclusive>>());
/// assert_eq!(a.left().limit, 2);
/// assert_eq!(a.right().limit, 4);
/// ```
impl<T: PartialOrd> TryFrom<std::ops::RangeInclusive<T>> for Interval<T, Inclusive> {
type Error = IntervalIsEmpty;
fn try_from(r: std::ops::RangeInclusive<T>) -> Result<Self, Self::Error> {
let (left, right) = r.into_inner();
Self::try_new(left.into(), right.into()).ok_or(IntervalIsEmpty)
}
}
/// ```
/// use inter_val::{Interval, Inclusive, Exclusive};
/// let src = Inclusive.at(0).to(Exclusive.at(10));
/// let dst: std::ops::Range<i32> = src.into();
/// assert_eq!(dst.start, 0);
/// assert_eq!(dst.end, 10);
/// ```
impl<T> From<Interval<T, Inclusive, Exclusive>> for std::ops::Range<T> {
fn from(i: Interval<T, Inclusive, Exclusive>) -> Self {
i.left.0.limit..i.right.0.limit
}
}
/// ```
/// use inter_val::{Interval, Inclusive};
/// let src = Inclusive.at(0).to(Inclusive.at(10));
/// let dst: std::ops::RangeInclusive<i32> = src.into();
/// assert_eq!(dst.start(), &0);
/// assert_eq!(dst.end(), &10);
/// ```
impl<T> From<Interval<T, Inclusive, Inclusive>> for std::ops::RangeInclusive<T> {
fn from(i: Interval<T, Inclusive, Inclusive>) -> Self {
i.left.0.limit..=i.right.0.limit
}
}
}