non_empty_iter/
repeat.rs

1//! Non-empty iterators that repeat items.
2
3use core::iter;
4
5use non_zero_size::Size;
6
7use crate::non_empty::NonEmptyIterator;
8
9/// Creates [`Repeat<T>`] non-empty iterator that repeats the given item endlessly.
10pub const fn repeat<T: Clone>(item: T) -> Repeat<T> {
11    Repeat::new(item)
12}
13
14/// Represents non-empty iterators that repeat the given item endlessly.
15///
16/// This `struct` is created by the [`repeat`] function. See its documentation for more.
17#[derive(Debug, Clone)]
18#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
19pub struct Repeat<T: Clone> {
20    item: T,
21}
22
23impl<T: Clone> Repeat<T> {
24    /// Constructs [`Self`].
25    pub const fn new(item: T) -> Self {
26        Self { item }
27    }
28}
29
30impl<T: Clone> IntoIterator for Repeat<T> {
31    type Item = T;
32
33    type IntoIter = iter::Repeat<T>;
34
35    fn into_iter(self) -> Self::IntoIter {
36        iter::repeat(self.item)
37    }
38}
39
40unsafe impl<T: Clone> NonEmptyIterator for Repeat<T> {}
41
42/// Creates [`RepeatWith<F>`] non-empty iterator that repeats items
43/// computed from the given function endlessly.
44pub const fn repeat_with<T, F: FnMut() -> T>(function: F) -> RepeatWith<F> {
45    RepeatWith::new(function)
46}
47
48/// Represents non-empty iterators that repeat items computed from the given function endlessly.
49///
50/// This `struct` is created by the [`repeat_with`] function. See its documentation for more.
51#[derive(Debug, Clone)]
52#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
53pub struct RepeatWith<F> {
54    function: F,
55}
56
57impl<F> RepeatWith<F> {
58    /// Constructs [`Self`].
59    pub const fn new(function: F) -> Self {
60        Self { function }
61    }
62}
63
64impl<T, F: FnMut() -> T> IntoIterator for RepeatWith<F> {
65    type Item = T;
66
67    type IntoIter = iter::RepeatWith<F>;
68
69    fn into_iter(self) -> Self::IntoIter {
70        iter::repeat_with(self.function)
71    }
72}
73
74unsafe impl<T, F: FnMut() -> T> NonEmptyIterator for RepeatWith<F> {}
75
76/// Creates [`RepeatN<T>`] non-empty iterator that repeats the given item the given number of times.
77pub const fn repeat_n<T: Clone>(item: T, count: Size) -> RepeatN<T> {
78    RepeatN::new(item, count)
79}
80
81/// Represents non-empty iterators that repeat the given item exactly the given number of times.
82///
83/// This `struct` is created by the [`repeat_n`] function. See its documentation for more.
84#[derive(Debug, Clone)]
85#[must_use = "non-empty iterators are lazy and do nothing unless consumed"]
86pub struct RepeatN<T: Clone> {
87    item: T,
88    count: Size,
89}
90
91impl<T: Clone> RepeatN<T> {
92    /// Constructs [`Self`].
93    pub const fn new(item: T, count: Size) -> Self {
94        Self { item, count }
95    }
96}
97
98impl<T: Clone> IntoIterator for RepeatN<T> {
99    type Item = T;
100
101    type IntoIter = iter::RepeatN<T>;
102
103    fn into_iter(self) -> Self::IntoIter {
104        iter::repeat_n(self.item, self.count.get())
105    }
106}
107
108unsafe impl<T: Clone> NonEmptyIterator for RepeatN<T> {}