bounded_integer/types/
indexing.rs

1//! Indexing operations on [T; N], Vec<T> and VecDeque<T> for BoundedUsize
2
3use super::BoundedUsize;
4use core::ops::Index;
5use core::ops::IndexMut;
6
7impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>> for [T] {
8    type Output = T;
9
10    #[inline]
11    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
12        &self[index.get()]
13    }
14}
15
16impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>> for [T] {
17    #[inline]
18    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
19        &mut self[index.get()]
20    }
21}
22
23#[cfg(feature = "alloc")]
24impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>> for alloc::vec::Vec<T> {
25    type Output = T;
26
27    #[inline]
28    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
29        &self[index.get()]
30    }
31}
32
33#[cfg(feature = "alloc")]
34impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>>
35    for alloc::vec::Vec<T>
36{
37    #[inline]
38    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
39        &mut self[index.get()]
40    }
41}
42
43#[cfg(feature = "alloc")]
44impl<const MIN: usize, const MAX: usize, T> Index<BoundedUsize<MIN, MAX>>
45    for alloc::collections::VecDeque<T>
46{
47    type Output = T;
48
49    #[inline]
50    fn index(&self, index: BoundedUsize<MIN, MAX>) -> &Self::Output {
51        &self[index.get()]
52    }
53}
54
55#[cfg(feature = "alloc")]
56impl<const MIN: usize, const MAX: usize, T> IndexMut<BoundedUsize<MIN, MAX>>
57    for alloc::collections::VecDeque<T>
58{
59    #[inline]
60    fn index_mut(&mut self, index: BoundedUsize<MIN, MAX>) -> &mut Self::Output {
61        &mut self[index.get()]
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use crate::types::BoundedUsize;
68
69    #[test]
70    fn indexing() {
71        let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
72
73        for i in 0..arr.len() {
74            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();
75            assert_eq!(arr[b_u], i);
76        }
77    }
78
79    #[test]
80    #[cfg(feature = "alloc")]
81    fn indexing_alloc() {
82        let vec = (0..20).collect::<alloc::vec::Vec<usize>>();
83        let deq = vec
84            .clone()
85            .into_iter()
86            .rev()
87            .collect::<alloc::collections::VecDeque<_>>();
88
89        for i in 0..vec.len() {
90            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();
91
92            assert_eq!(vec[b_u], i);
93            assert_eq!(deq[b_u], 19 - i);
94        }
95    }
96
97    #[test]
98    fn indexing_mut() {
99        let mut arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
100
101        for i in 0..arr.len() {
102            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();
103
104            arr[b_u] += 5;
105
106            assert_eq!(arr[b_u], i + 5);
107        }
108    }
109
110    #[test]
111    #[cfg(feature = "alloc")]
112    fn indexing_mut_alloc() {
113        let mut vec = (0..20).collect::<alloc::vec::Vec<usize>>();
114        let mut deq = vec
115            .clone()
116            .into_iter()
117            .rev()
118            .collect::<alloc::collections::VecDeque<_>>();
119
120        for i in 0..vec.len() {
121            let b_u = BoundedUsize::<0, 30>::new(i).unwrap();
122
123            vec[b_u] += 5;
124            deq[b_u] += 10;
125
126            assert_eq!(vec[b_u], i + 5);
127            assert_eq!(deq[b_u], (19 - i) + 10);
128        }
129    }
130}