const_primes/cache/
primes_into_iter.rs

1use core::iter::FusedIterator;
2
3use super::Underlying;
4
5/// An owning iterator over prime numbers.
6///
7/// Created by the [`IntoIterator`] implementation on [`Primes`](super::Primes).
8#[derive(Debug, Clone)]
9#[cfg_attr(
10    feature = "rkyv",
11    derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
12)]
13#[cfg_attr(
14    feature = "zerocopy",
15    derive(zerocopy::IntoBytes, zerocopy::Immutable, zerocopy::KnownLayout)
16)]
17#[must_use = "iterators are lazy and do nothing unless consumed"]
18#[cfg_attr(feature = "zerocopy", repr(transparent))]
19pub struct PrimesIntoIter<const N: usize>(core::array::IntoIter<Underlying, N>);
20
21impl<const N: usize> PrimesIntoIter<N> {
22    pub(crate) const fn new(iter: core::array::IntoIter<Underlying, N>) -> Self {
23        Self(iter)
24    }
25
26    /// Returns an immutable slice of all primes that have not been yielded yet.
27    pub fn as_slice(&self) -> &[Underlying] {
28        self.0.as_slice()
29    }
30}
31
32impl<const N: usize> Iterator for PrimesIntoIter<N> {
33    type Item = Underlying;
34
35    #[inline]
36    fn next(&mut self) -> Option<Self::Item> {
37        self.0.next()
38    }
39
40    #[inline]
41    fn nth(&mut self, n: usize) -> Option<Self::Item> {
42        self.0.nth(n)
43    }
44
45    #[inline]
46    fn count(self) -> usize {
47        self.0.count()
48    }
49
50    #[inline]
51    fn last(self) -> Option<Self::Item> {
52        self.0.last()
53    }
54
55    #[inline]
56    fn size_hint(&self) -> (usize, Option<usize>) {
57        self.0.size_hint()
58    }
59}
60
61impl<const N: usize> DoubleEndedIterator for PrimesIntoIter<N> {
62    #[inline]
63    fn next_back(&mut self) -> Option<Self::Item> {
64        self.0.next_back()
65    }
66
67    #[inline]
68    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
69        self.0.nth_back(n)
70    }
71}
72
73impl<const N: usize> FusedIterator for PrimesIntoIter<N> {}
74
75impl<const N: usize> ExactSizeIterator for PrimesIntoIter<N> {
76    #[inline]
77    fn len(&self) -> usize {
78        self.0.len()
79    }
80}