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
use crate::PartialArray;
use core::fmt::{self, Debug, Formatter};
use core::iter::FusedIterator;
use core::mem::{self, MaybeUninit};
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct IntoIter<T, const N: usize> {
array: [MaybeUninit<T>; N],
filled: usize,
read: usize,
}
impl<T, const N: usize> IntoIter<T, N> {
pub(crate) fn new(array: PartialArray<T, N>) -> Self {
Self {
array: array.array,
filled: array.filled,
read: 0,
}
}
}
impl<T: Debug, const N: usize> Debug for IntoIter<T, N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let slice = &self.array[self.read..self.filled];
let slice = unsafe { mem::transmute(slice) };
<[T] as Debug>::fmt(slice, f)
}
}
impl<T, const N: usize> Iterator for IntoIter<T, N> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.read != self.filled {
let value = mem::replace(&mut self.array[self.read], PartialArray::<_, N>::UNINIT);
self.read += 1;
Some(unsafe { value.assume_init() })
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.filled - self.read;
(len, Some(len))
}
}
impl<T, const N: usize> DoubleEndedIterator for IntoIter<T, N> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.read != self.filled && self.filled > 0 {
self.filled -= 1;
let value = mem::replace(&mut self.array[self.filled], PartialArray::<_, N>::UNINIT);
Some(unsafe { value.assume_init() })
} else {
None
}
}
}
impl<T, const N: usize> FusedIterator for IntoIter<T, N> {}
impl<T, const N: usize> ExactSizeIterator for IntoIter<T, N> {}