1use core::iter::FusedIterator;
4
5use super::Rev;
6use crate::Month::{self, *};
7
8pub(super) const ALL_MONTHS: [Month; 12] = [
9 January, February, March, April, May, June, July, August, September, October, November,
10 December,
11];
12
13#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct MonthIter {
18 current: Month,
19}
20
21impl MonthIter {
22 #[inline]
32 pub const fn new(start: Month) -> Self {
33 Self { current: start }
34 }
35
36 #[inline]
46 pub const fn rev(self) -> Rev<Self> {
47 Rev { iter: self }
48 }
49}
50
51impl Iterator for MonthIter {
52 type Item = Month;
53
54 #[inline]
55 fn next(&mut self) -> Option<Self::Item> {
56 let current = self.current;
57 self.current = current.next();
58 Some(current)
59 }
60
61 #[inline]
62 fn size_hint(&self) -> (usize, Option<usize>) {
63 (usize::MAX, None)
64 }
65
66 #[inline]
67 fn nth(&mut self, n: usize) -> Option<Self::Item> {
68 let result = self.current.nth_next((n % 12) as u8);
69 self.current = result.next();
70 Some(result)
71 }
72
73 fn count(self) -> usize {
74 panic!("`MonthIter` is infinite and cannot be counted")
75 }
76
77 fn last(self) -> Option<Self::Item> {
78 panic!("`MonthIter` is infinite and has no last element")
79 }
80
81 #[inline]
82 fn all<F>(&mut self, f: F) -> bool
83 where
84 F: FnMut(Self::Item) -> bool,
85 {
86 ALL_MONTHS.into_iter().all(f)
87 }
88
89 #[inline]
90 fn any<F>(&mut self, f: F) -> bool
91 where
92 F: FnMut(Self::Item) -> bool,
93 {
94 ALL_MONTHS.into_iter().any(f)
95 }
96
97 #[inline]
98 fn max(self) -> Option<Self::Item> {
99 Some(December)
100 }
101
102 #[inline]
103 fn min(self) -> Option<Self::Item> {
104 Some(January)
105 }
106
107 #[inline]
108 fn is_sorted(self) -> bool {
109 false
111 }
112}
113
114impl Iterator for Rev<MonthIter> {
115 type Item = Month;
116
117 #[inline]
118 fn next(&mut self) -> Option<Self::Item> {
119 let current = self.iter.current;
120 self.iter.current = current.previous();
121 Some(current)
122 }
123
124 #[inline]
125 fn size_hint(&self) -> (usize, Option<usize>) {
126 (usize::MAX, None)
127 }
128
129 #[inline]
130 fn nth(&mut self, n: usize) -> Option<Self::Item> {
131 let result = self.iter.current.nth_prev((n % 12) as u8);
132 self.iter.current = result.previous();
133 Some(result)
134 }
135
136 fn count(self) -> usize {
137 panic!("`Rev<MonthIter>` is infinite and cannot be counted")
138 }
139
140 fn last(self) -> Option<Self::Item> {
141 panic!("`Rev<MonthIter>` is infinite and has no last element")
142 }
143
144 #[inline]
145 fn all<F>(&mut self, f: F) -> bool
146 where
147 F: FnMut(Self::Item) -> bool,
148 {
149 ALL_MONTHS.into_iter().all(f)
150 }
151
152 #[inline]
153 fn any<F>(&mut self, f: F) -> bool
154 where
155 F: FnMut(Self::Item) -> bool,
156 {
157 ALL_MONTHS.into_iter().any(f)
158 }
159
160 #[inline]
161 fn max(self) -> Option<Self::Item> {
162 Some(December)
163 }
164
165 #[inline]
166 fn min(self) -> Option<Self::Item> {
167 Some(January)
168 }
169
170 #[inline]
171 fn is_sorted(self) -> bool {
172 false
173 }
174}
175
176impl FusedIterator for MonthIter {}
177impl FusedIterator for Rev<MonthIter> {}