time/iter/
weekday_iter.rs1use core::iter::FusedIterator;
4
5use super::Rev;
6use crate::Weekday::{self, *};
7
8const ALL_WEEKDAYS: [Weekday; 7] = [
9 Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday,
10];
11
12#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct WeekdayIter {
17 current: Weekday,
18}
19
20impl WeekdayIter {
21 #[inline]
31 pub const fn new(start: Weekday) -> Self {
32 Self { current: start }
33 }
34
35 #[inline]
45 pub const fn rev(self) -> Rev<Self> {
46 Rev { iter: self }
47 }
48}
49
50impl Iterator for WeekdayIter {
51 type Item = Weekday;
52
53 #[inline]
54 fn next(&mut self) -> Option<Self::Item> {
55 let current = self.current;
56 self.current = current.next();
57 Some(current)
58 }
59
60 #[inline]
61 fn size_hint(&self) -> (usize, Option<usize>) {
62 (usize::MAX, None)
63 }
64
65 #[inline]
66 fn nth(&mut self, n: usize) -> Option<Self::Item> {
67 let result = self.current.nth_next((n % 7) as u8);
68 self.current = result.next();
69 Some(result)
70 }
71
72 fn count(self) -> usize {
73 panic!("`WeekdayIter` is infinite and cannot be counted")
74 }
75
76 fn last(self) -> Option<Self::Item> {
77 panic!("`WeekdayIter` is infinite and has no last element")
78 }
79
80 #[inline]
81 fn all<F>(&mut self, f: F) -> bool
82 where
83 F: FnMut(Self::Item) -> bool,
84 {
85 ALL_WEEKDAYS.into_iter().all(f)
86 }
87
88 #[inline]
89 fn any<F>(&mut self, f: F) -> bool
90 where
91 F: FnMut(Self::Item) -> bool,
92 {
93 ALL_WEEKDAYS.into_iter().any(f)
94 }
95}
96
97impl Iterator for Rev<WeekdayIter> {
98 type Item = Weekday;
99
100 #[inline]
101 fn next(&mut self) -> Option<Self::Item> {
102 let current = self.iter.current;
103 self.iter.current = current.previous();
104 Some(current)
105 }
106
107 #[inline]
108 fn size_hint(&self) -> (usize, Option<usize>) {
109 (usize::MAX, None)
110 }
111
112 #[inline]
113 fn nth(&mut self, n: usize) -> Option<Self::Item> {
114 let result = self.iter.current.nth_prev((n % 7) as u8);
115 self.iter.current = result.previous();
116 Some(result)
117 }
118
119 fn count(self) -> usize {
120 panic!("`Rev<WeekdayIter>` is infinite and cannot be counted")
121 }
122
123 fn last(self) -> Option<Self::Item> {
124 panic!("`Rev<WeekdayIter>` is infinite and has no last element")
125 }
126
127 #[inline]
128 fn all<F>(&mut self, f: F) -> bool
129 where
130 F: FnMut(Self::Item) -> bool,
131 {
132 ALL_WEEKDAYS.into_iter().all(f)
133 }
134
135 #[inline]
136 fn any<F>(&mut self, f: F) -> bool
137 where
138 F: FnMut(Self::Item) -> bool,
139 {
140 ALL_WEEKDAYS.into_iter().any(f)
141 }
142}
143
144impl FusedIterator for WeekdayIter {}
145impl FusedIterator for Rev<WeekdayIter> {}