1use core::iter;
4
5use non_zero_size::Size;
6
7use crate::non_empty::NonEmptyIterator;
8
9pub const fn repeat<T: Clone>(item: T) -> Repeat<T> {
11 Repeat::new(item)
12}
13
14#[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 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
42pub const fn repeat_with<T, F: FnMut() -> T>(function: F) -> RepeatWith<F> {
45 RepeatWith::new(function)
46}
47
48#[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 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
76pub const fn repeat_n<T: Clone>(item: T, count: Size) -> RepeatN<T> {
78 RepeatN::new(item, count)
79}
80
81#[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 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> {}