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
//! Indexing operations on [T; N], Vec<T> and VecDeque<T> for BoundedUsize

use super::BoundedUsize;
use core::ops::Index;
use core::ops::IndexMut;

impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>> for [T] {
    type Output = T;

    #[inline]
    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
        &self[index.get()]
    }
}

impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>> for [T] {
    #[inline]
    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
        &mut self[index.get()]
    }
}

#[cfg(feature = "alloc")]
impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>> for alloc::vec::Vec<T> {
    type Output = T;

    #[inline]
    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
        &self[index.get()]
    }
}

#[cfg(feature = "alloc")]
impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>>
    for alloc::vec::Vec<T>
{
    #[inline]
    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
        &mut self[index.get()]
    }
}

#[cfg(feature = "alloc")]
impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>>
    for alloc::collections::VecDeque<T>
{
    type Output = T;

    #[inline]
    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
        &self[index.get()]
    }
}

#[cfg(feature = "alloc")]
impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>>
    for alloc::collections::VecDeque<T>
{
    #[inline]
    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
        &mut self[index.get()]
    }
}

#[cfg(test)]
mod tests {
    use crate::types::BoundedUsize;

    #[test]
    fn indexing() {
        let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        for i in 0..arr.len() {
            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();
            assert_eq!(arr[b_u], i);
        }
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn indexing_alloc() {
        let vec = (0..20).collect::<alloc::vec::Vec<usize>>();
        let deq = vec
            .clone()
            .into_iter()
            .rev()
            .collect::<alloc::collections::VecDeque<_>>();

        for i in 0..vec.len() {
            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();

            assert_eq!(vec[b_u], i);
            assert_eq!(deq[b_u], 19 - i);
        }
    }

    #[test]
    fn indexing_mut() {
        let mut arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        for i in 0..arr.len() {
            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();

            arr[b_u] += 5;

            assert_eq!(arr[b_u], i + 5);
        }
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn indexing_mut_alloc() {
        let mut vec = (0..20).collect::<alloc::vec::Vec<usize>>();
        let mut deq = vec
            .clone()
            .into_iter()
            .rev()
            .collect::<alloc::collections::VecDeque<_>>();

        for i in 0..vec.len() {
            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();

            vec[b_u] += 5;
            deq[b_u] += 10;

            assert_eq!(vec[b_u], i + 5);
            assert_eq!(deq[b_u], (19 - i) + 10);
        }
    }
}