1use crate::Enum;
2use core::iter::FusedIterator;
3
4pub fn iter_each<T: Enum>() -> IterEachFrom<T> {
6 IterEachFrom(T::first())
7}
8
9pub fn iter_each_from<T: Enum>(x: T) -> IterEachFrom<T> {
11 IterEachFrom(Some(x))
12}
13
14#[must_use = "iterators are lazy and do nothing unless consumed"]
34#[derive(Debug, Clone)]
35pub struct IterEachFrom<T>(Option<T>);
36
37impl<T: Enum> Iterator for IterEachFrom<T> {
38 type Item = T;
39
40 #[inline]
41 fn next(&mut self) -> Option<Self::Item> {
42 let this = self.0.take()?;
43 self.0 = this.succ();
44 Some(this)
45 }
46
47 #[inline]
48 fn size_hint(&self) -> (usize, Option<usize>) {
49 let Some(this) = &self.0 else {
50 return (0, Some(0));
51 };
52 match T::count_from(this) {
53 Some(c) => (c.get(), Some(c.get())),
54 None => (usize::MAX, None),
55 }
56 }
57
58 #[inline]
59 fn count(self) -> usize
60 where
61 Self: Sized,
62 {
63 self.0.map_or(0, |this| {
64 T::count_from(&this).expect("count overflow").get()
65 })
66 }
67
68 #[inline]
69 fn last(self) -> Option<Self::Item> {
70 self.0?;
71 T::last()
72 }
73
74 fn fold<B, F>(self, init: B, f: F) -> B
75 where
76 F: FnMut(B, Self::Item) -> B,
77 {
78 match self.0 {
79 Some(this) => T::fold_each_from(&this, init, f),
80 None => init,
81 }
82 }
83}
84
85impl<T: Enum> FusedIterator for IterEachFrom<T> {}