| 1 |
//! An infinite iterator over [`Weekday`]s. |
| 2 |
|
| 3 |
use core::iter::FusedIterator; |
| 4 |
|
| 5 |
use super::Rev; |
| 6 |
use crate::Weekday::{self, *}; |
| 7 |
|
| 8 |
const ALL_WEEKDAYS: [Weekday; 7] = [ |
| 9 |
Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, |
| 10 |
]; |
| 11 |
|
| 12 |
/// An infinite iterator over [`Weekday`]s. |
| 13 |
/// |
| 14 |
/// This struct is created by [`Weekday::iter_from`] or [`WeekdayIter::new`]. |
| 15 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 16 |
pub struct WeekdayIter { |
| 17 |
current: Weekday, |
| 18 |
} |
| 19 |
|
| 20 |
impl WeekdayIter { |
| 21 |
/// Create a new `WeekdayIter` starting at `start`. |
| 22 |
/// |
| 23 |
/// ```rust |
| 24 |
/// # use time::{Weekday, iter::WeekdayIter}; |
| 25 |
/// let mut iter = WeekdayIter::new(Weekday::Monday); |
| 26 |
/// assert_eq!(iter.next(), Some(Weekday::Monday)); |
| 27 |
/// assert_eq!(iter.next(), Some(Weekday::Tuesday)); |
| 28 |
/// assert_eq!(iter.next(), Some(Weekday::Wednesday)); |
| 29 |
/// ``` |
| 30 |
#[inline] |
| 31 |
pub const fn new(start: Weekday) -> Self { |
| 32 |
Self { current: start } |
| 33 |
} |
| 34 |
|
| 35 |
/// Make the iterator go in reverse order. |
| 36 |
/// |
| 37 |
/// ```rust |
| 38 |
/// # use time::{Weekday, iter::WeekdayIter}; |
| 39 |
/// let mut iter = WeekdayIter::new(Weekday::Monday).rev(); |
| 40 |
/// assert_eq!(iter.next(), Some(Weekday::Monday)); |
| 41 |
/// assert_eq!(iter.next(), Some(Weekday::Sunday)); |
| 42 |
/// assert_eq!(iter.next(), Some(Weekday::Saturday)); |
| 43 |
/// ``` |
| 44 |
#[inline] |
| 45 |
pub const fn rev(self) -> Rev<Self> { |
| 46 |
Rev { iter: self } |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl 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 |
|
| 97 |
impl 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 |
|
| 144 |
impl FusedIterator for WeekdayIter {} |
| 145 |
impl FusedIterator for Rev<WeekdayIter> {} |