inter_val/
std_range.rs

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