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
use std::ops::{
    Bound,
    Range,
    RangeInclusive,
    RangeFrom,
};

/// A type that consists of a start (inclusive) and end bound.
pub trait IntoBounds<S, E> {
    /// Returns the start (inclusive) and end bounds of `self`.
    fn into_bounds(self) -> (S, Bound<E>);
}

impl<S, E> IntoBounds<S, E> for (S, Bound<E>) {
    #[inline]
    fn into_bounds(self) -> (S, Bound<E>) {
        self
    }
}

impl<S, E> IntoBounds<S, E> for (S, E) {
    #[inline]
    fn into_bounds(self) -> (S, Bound<E>) {
        (self.0, Bound::Excluded(self.1))
    }
}

impl<A> IntoBounds<A, A> for Range<A> {
    #[inline]
    fn into_bounds(self) -> (A, Bound<A>) {
        (self.start, Bound::Excluded(self.end))
    }
}

impl<A> IntoBounds<A, A> for RangeInclusive<A> {
    #[inline]
    fn into_bounds(self) -> (A, Bound<A>) {
        let (start, end) = self.into_inner();
        (start, Bound::Included(end))
    }
}

impl<A> IntoBounds<A, A> for RangeFrom<A> {
    #[inline]
    fn into_bounds(self) -> (A, Bound<A>) {
        (self.start, Bound::Unbounded)
    }
}