typed_index_collections/
range.rs

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
99
100
101
102
103
104
105
use core::ops;

/// A helper trait used to convert typed index ranges to `usize` ranges.
/// The trait is implemented for Rust's built-in range types with `K where usize: `[`From<K>`] used as bound endpoints.
///
/// See [`core::ops::RangeBounds`] for more details.
///
/// [`From<K>`]: https://doc.rust-lang.org/std/convert/trait.From.html
/// [`core::ops::RangeBounds`]: https://doc.rust-lang.org/core/ops/trait.RangeBounds.html
pub trait TiRangeBounds<K> {
    /// Appropriate usize range
    type Range: ops::RangeBounds<usize>;
    /// Converts the `TiRangeBounds` into an appropriate usize range.
    fn into_range(self) -> Self::Range;
}

impl<K> TiRangeBounds<K> for ops::Range<K>
where
    usize: From<K>,
{
    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
    usize: From<K>,
{
    type Range = ops::RangeFrom<usize>;
    #[inline]
    fn into_range(self) -> Self::Range {
        self.start.into()..
    }
}

impl<K> TiRangeBounds<K> for ops::RangeFull
where
    usize: From<K>,
{
    type Range = Self;
    #[inline]
    fn into_range(self) -> Self::Range {
        Self
    }
}

impl<K> TiRangeBounds<K> for ops::RangeInclusive<K>
where
    usize: From<K>,
{
    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
    usize: From<K>,
{
    type Range = ops::RangeTo<usize>;
    #[inline]
    fn into_range(self) -> Self::Range {
        ..self.end.into()
    }
}

impl<K> TiRangeBounds<K> for ops::RangeToInclusive<K>
where
    usize: From<K>,
{
    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
    usize: From<K>,
{
    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
    usize: From<K>,
{
    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,
    }
}