const_primes/cache/
primes_iter.rs

1use super::Underlying;
2use core::iter::FusedIterator;
3
4/// A borrowing iterator over prime numbers.
5///
6/// Created by the [`iter`](super::Primes::iter) function on [`Primes`](super::Primes),
7/// see it for more information.
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 PrimesIter<'a>(core::slice::Iter<'a, Underlying>);
20
21impl<'a> PrimesIter<'a> {
22    pub(crate) const fn new(iter: core::slice::Iter<'a, Underlying>) -> Self {
23        Self(iter)
24    }
25
26    /// Returns an immutable slice of all the primes that have not been yielded yet.
27    pub fn as_slice(&self) -> &[Underlying] {
28        self.0.as_slice()
29    }
30}
31
32impl<'a> Iterator for PrimesIter<'a> {
33    type Item = &'a Underlying;
34
35    #[inline]
36    fn next(&mut self) -> Option<Self::Item> {
37        self.0.next()
38    }
39
40    #[inline]
41    fn size_hint(&self) -> (usize, Option<usize>) {
42        self.0.size_hint()
43    }
44
45    #[inline]
46    fn nth(&mut self, n: usize) -> Option<Self::Item> {
47        self.0.nth(n)
48    }
49
50    #[inline]
51    fn count(self) -> usize {
52        self.0.count()
53    }
54
55    #[inline]
56    fn last(self) -> Option<Self::Item> {
57        self.0.last()
58    }
59}
60
61impl ExactSizeIterator for PrimesIter<'_> {
62    #[inline]
63    fn len(&self) -> usize {
64        self.0.len()
65    }
66}
67
68impl FusedIterator for PrimesIter<'_> {}
69
70impl DoubleEndedIterator for PrimesIter<'_> {
71    #[inline]
72    fn next_back(&mut self) -> Option<Self::Item> {
73        self.0.next_back()
74    }
75
76    #[inline]
77    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
78        self.0.nth_back(n)
79    }
80}