const_primes/cache/
primes_into_iter.rs

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