microstack/
iterators.rs

1// Copyright (c) 2023 Yegor Bugayenko
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included
11// in all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21use crate::{IntoIter, Iter, Stack};
22use std::marker::PhantomData;
23
24impl<V: Copy, const N: usize> Iterator for IntoIter<V, N> {
25    type Item = V;
26
27    #[inline]
28    #[must_use]
29    fn next(&mut self) -> Option<Self::Item> {
30        if self.pos >= self.next {
31            None
32        } else {
33            let v = unsafe { self.items.add(self.pos).read() };
34            self.pos += 1;
35            Some(v)
36        }
37    }
38}
39
40impl<'a, V: Copy + 'a, const N: usize> Stack<V, N> {
41    /// Into-iterate them.
42    #[inline]
43    pub const fn into_iter(&self) -> IntoIter<V, N> {
44        IntoIter {
45            pos: 0,
46            next: self.next,
47            items: self.items.as_ptr(),
48        }
49    }
50}
51
52impl<'a, V: Copy, const N: usize> Iterator for Iter<'a, V, N> {
53    type Item = &'a V;
54
55    #[inline]
56    #[must_use]
57    fn next(&mut self) -> Option<Self::Item> {
58        if self.pos >= self.next {
59            None
60        } else {
61            unsafe {
62                let v = self.items.add(self.pos);
63                self.pos += 1;
64                v.as_ref()
65            }
66        }
67    }
68}
69
70impl<'a, V: Copy + 'a, const N: usize> Stack<V, N> {
71    /// Iterate them.
72    #[inline]
73    pub const fn iter(&self) -> Iter<V, N> {
74        Iter {
75            pos: 0,
76            next: self.next,
77            items: self.items.as_ptr(),
78            _marker: PhantomData,
79        }
80    }
81}
82
83#[test]
84fn push_and_iterate() {
85    let mut p: Stack<u64, 16> = Stack::new();
86    unsafe { p.push_unchecked(1) };
87    unsafe { p.push_unchecked(2) };
88    unsafe { p.push_unchecked(3) };
89    let mut sum = 0;
90    for x in p.iter() {
91        sum += x;
92    }
93    assert_eq!(6, sum);
94}
95
96#[test]
97fn push_and_into_iterate() {
98    let mut p: Stack<u64, 16> = Stack::new();
99    unsafe { p.push_unchecked(1) };
100    unsafe { p.push_unchecked(2) };
101    let mut sum = 0;
102    for x in p.into_iter() {
103        sum += x;
104    }
105    assert_eq!(3, sum);
106}