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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{TiRangeBounds, TiSlice};
use core::ops;

mod private {
    use core::ops;

    pub trait Sealed<K> {}

    impl<K> Sealed<K> for K {}
    impl<K> Sealed<K> for ops::Range<K> {}
    impl<K> Sealed<K> for ops::RangeTo<K> {}
    impl<K> Sealed<K> for ops::RangeFrom<K> {}
    impl<K> Sealed<K> for ops::RangeInclusive<K> {}
    impl<K> Sealed<K> for ops::RangeToInclusive<K> {}
}

/// A helper trait used for indexing operations.
///
/// This trait is implemented for `K`, [`Range<K>`], [`RangeTo<K>`], [`RangeFrom<K>`],
/// [`RangeInclusive<K>`] and [`RangeToInclusive<K>`].
/// The [`RangeFull<K>`] trait is not currently supported.
///
/// Trait implementations are only forwards to standard Rust [`slice`] operations.
///
/// [`slice`]: https://doc.rust-lang.org/std/primitive.slice.html
/// [`Range<K>`]: https://doc.rust-lang.org/std/ops/struct.Range.html
/// [`RangeTo<K>`]: https://doc.rust-lang.org/std/ops/struct.RangeTo.html
/// [`RangeFrom<K>`]: https://doc.rust-lang.org/std/ops/struct.RangeFrom.html
/// [`RangeInclusive<K>`]: https://doc.rust-lang.org/std/ops/struct.RangeInclusive.html
/// [`RangeToInclusive<K>`]: https://doc.rust-lang.org/std/ops/struct.RangeToInclusive.html
/// [`RangeFull<K>`]: https://doc.rust-lang.org/std/ops/struct.RangeFull.html
pub trait TiSliceIndex<K, V>: private::Sealed<K> {
    /// The output type returned by methods.
    type Output: ?Sized;

    /// Returns a shared reference to the output at this location, if in
    /// bounds.
    fn get(self, slice: &TiSlice<K, V>) -> Option<&Self::Output>;

    /// Returns a mutable reference to the output at this location, if in
    /// bounds.
    fn get_mut(self, slice: &mut TiSlice<K, V>) -> Option<&mut Self::Output>;

    /// Returns a shared reference to the output at this location, without
    /// performing any bounds checking.
    /// Calling this method with an out-of-bounds index is *[undefined behavior]*
    /// even if the resulting reference is not used.
    ///
    /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
    #[allow(clippy::missing_safety_doc)]
    unsafe fn get_unchecked(self, slice: &TiSlice<K, V>) -> &Self::Output;

    /// Returns a mutable reference to the output at this location, without
    /// performing any bounds checking.
    /// Calling this method with an out-of-bounds index is *[undefined behavior]*
    /// even if the resulting reference is not used.
    ///
    /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
    #[allow(clippy::missing_safety_doc)]
    unsafe fn get_unchecked_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output;

    /// Returns a shared reference to the output at this location, panicking
    /// if out of bounds.
    fn index(self, slice: &TiSlice<K, V>) -> &Self::Output;

    /// Returns a mutable reference to the output at this location, panicking
    /// if out of bounds.
    fn index_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output;
}

impl<K, V> TiSliceIndex<K, V> for K
where
    usize: From<K>,
{
    type Output = V;

    #[inline]
    fn get(self, slice: &TiSlice<K, V>) -> Option<&Self::Output> {
        slice.raw.get(usize::from(self))
    }

    #[inline]
    fn get_mut(self, slice: &mut TiSlice<K, V>) -> Option<&mut Self::Output> {
        slice.raw.get_mut(usize::from(self))
    }

    #[inline]
    unsafe fn get_unchecked(self, slice: &TiSlice<K, V>) -> &Self::Output {
        slice.raw.get_unchecked(usize::from(self))
    }

    #[inline]
    unsafe fn get_unchecked_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output {
        slice.raw.get_unchecked_mut(usize::from(self))
    }

    #[inline]
    fn index(self, slice: &TiSlice<K, V>) -> &Self::Output {
        &slice.raw[usize::from(self)]
    }

    fn index_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output {
        &mut slice.raw[usize::from(self)]
    }
}

macro_rules! impl_ti_slice_range {
    ($ty:ty) => {
        impl<K, V> TiSliceIndex<K, V> for $ty
        where
            usize: From<K>,
        {
            type Output = TiSlice<K, V>;

            #[inline]
            fn get(self, slice: &TiSlice<K, V>) -> Option<&Self::Output> {
                slice.raw.get(self.into_range()).map(TiSlice::from_ref)
            }

            #[inline]
            fn get_mut(self, slice: &mut TiSlice<K, V>) -> Option<&mut Self::Output> {
                slice.raw.get_mut(self.into_range()).map(TiSlice::from_mut)
            }

            #[inline]
            unsafe fn get_unchecked(self, slice: &TiSlice<K, V>) -> &Self::Output {
                TiSlice::from_ref(slice.raw.get_unchecked(self.into_range()))
            }

            #[inline]
            unsafe fn get_unchecked_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output {
                TiSlice::from_mut(slice.raw.get_unchecked_mut(self.into_range()))
            }

            #[inline]
            fn index(self, slice: &TiSlice<K, V>) -> &Self::Output {
                TiSlice::from_ref(&slice.raw[self.into_range()])
            }

            #[inline]
            fn index_mut(self, slice: &mut TiSlice<K, V>) -> &mut Self::Output {
                TiSlice::from_mut(&mut slice.raw[self.into_range()])
            }
        }
    };
}

impl_ti_slice_range!(ops::Range<K>);
impl_ti_slice_range!(ops::RangeFrom<K>);
impl_ti_slice_range!(ops::RangeInclusive<K>);
impl_ti_slice_range!(ops::RangeTo<K>);
impl_ti_slice_range!(ops::RangeToInclusive<K>);