array_iter_tools/
mut_ref.rs1use crate::{ArrayIterator, IntoArrayIterator};
4use core::marker::PhantomData;
5
6pub struct ArrayIter<'a, T: 'a, const N: usize> {
8 ptr: *mut T,
9 i: usize,
10 _marker: PhantomData<&'a mut T>,
11}
12
13impl<'a, T, const N: usize> IntoArrayIterator<N> for &'a mut [T; N] {
14 type Item = &'a mut T;
15 type ArrayIter = ArrayIter<'a, T, N>;
16 fn into_array_iter(self) -> Self::ArrayIter {
17 ArrayIter {
18 ptr: self.as_mut_ptr(),
19 i: 0,
20 _marker: PhantomData,
21 }
22 }
23}
24
25impl<'a, T, const N: usize> ArrayIterator<N> for ArrayIter<'a, T, N> {
26 type Item = &'a mut T;
27 unsafe fn next(&mut self) -> Self::Item {
28 debug_assert_ne!(self.i, N, "Called next too many times");
29 let n = self.i + 1;
30 &mut *self.ptr.add(core::mem::replace(&mut self.i, n))
31 }
32}