irox_tools/iterators/
looping_forever.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2023 IROX Contributors
3
4extern crate alloc;
5use alloc::vec::Vec;
6
7#[must_use]
8pub struct LoopingForever<T>
9where
10    T: Clone,
11{
12    pub(crate) items: Vec<T>,
13    pub(crate) index: usize,
14}
15
16impl<I> Iterator for LoopingForever<I>
17where
18    I: Clone,
19{
20    type Item = I;
21
22    fn next(&mut self) -> Option<Self::Item> {
23        let size = self.items.len();
24        if size == 0 {
25            return None;
26        }
27        let item = self.items.get(self.index)?.clone();
28        self.index = (self.index + 1) % size;
29
30        Some(item)
31    }
32}