byte_array_ops/core/
iter.rs

1use super::model::ByteArray;
2
3// TODO document this in my md notes
4
5/// Iterator over immutable references to bytes in a [`ByteArray`].
6///
7/// Created by `ByteArray::iter()` in hardened mode, or via `IntoIterator` for `&ByteArray`.
8pub struct ByteArrayIter<'a> {
9    inner: core::slice::Iter<'a, u8>,
10}
11
12/// Iterator over mutable references to bytes in a [`ByteArray`].
13///
14/// Created by `ByteArray::iter_mut()` in hardened mode, or via `IntoIterator` for `&mut ByteArray`.
15pub struct ByteArrayIterMut<'a> {
16    inner: core::slice::IterMut<'a, u8>,
17}
18
19impl<'a> Iterator for ByteArrayIter<'a> {
20    type Item = &'a u8;
21
22    fn next(&mut self) -> Option<Self::Item> {
23        self.inner.next()
24    }
25}
26
27impl<'a> Iterator for ByteArrayIterMut<'a> {
28    type Item = &'a mut u8;
29
30    fn next(&mut self) -> Option<Self::Item> {
31        self.inner.next()
32    }
33}
34
35impl<'a> ExactSizeIterator for ByteArrayIter<'a> {
36    fn len(&self) -> usize {
37        self.inner.len()
38    }
39}
40
41impl<'a> ExactSizeIterator for ByteArrayIterMut<'a> {
42    fn len(&self) -> usize {
43        self.inner.len()
44    }
45}
46
47impl<'a> IntoIterator for &'a ByteArray {
48    type Item = &'a u8;
49    type IntoIter = ByteArrayIter<'a>;
50
51    fn into_iter(self) -> Self::IntoIter {
52        ByteArrayIter {
53            inner: self.bytes.iter(),
54        }
55    }
56}
57
58impl<'a> IntoIterator for &'a mut ByteArray {
59    type Item = &'a mut u8;
60    type IntoIter = ByteArrayIterMut<'a>;
61
62    fn into_iter(self) -> Self::IntoIter {
63        ByteArrayIterMut {
64            inner: self.bytes.iter_mut(),
65        }
66    }
67}