Skip to main content

libutils_constrangeiter/
rangeinclusive.rs

1//^
2//^ HEAD
3//^
4
5//> HEAD -> SUPER
6use super::{
7    ConstIntoIterator,
8    Target
9};
10
11//> HEAD -> CORE
12use core::{
13    iter::{
14        DoubleEndedIterator,
15        ExactSizeIterator,
16        FusedIterator,
17        TrustedLen
18    }, marker::Destruct, ops::AddAssign, range::RangeInclusive
19};
20
21
22//^
23//^ ITERATOR
24//^
25
26//> ITERATOR -> ITERABLE
27#[derive(Debug)]
28pub struct Iterable<Type: const Target> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
29    start: Type,
30    last: Type
31}
32
33//> ITERATOR -> IMPLEMENTATION
34const impl<Type: const Target> Iterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
35    type Item = Type;
36    fn next(&mut self) -> Option<Self::Item> {return if self.start <= self.last {
37        let now = self.start;
38        self.start.add_assign(Type::ONE);
39        Some(now)
40    } else {None}}
41    fn size_hint(&self) -> (usize, Option<usize>) {return if self.start <= self.last {
42        let mut value = self.last.hint(self.start);
43        value.0.add_assign(1);
44        value.1.as_mut().map(const |value| value.add_assign(1));
45        value
46    } else {(0, Some(0))}}
47}
48
49//> ITERATOR -> DOUBLE ENDED
50const impl<Type: const Target> DoubleEndedIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
51    fn next_back(&mut self) -> Option<Self::Item> {return if self.start <= self.last {
52        let now = self.last;
53        self.last.sub_assign(Type::ONE);
54        return Some(now)
55    } else {None}}
56}
57
58//> ITERATOR -> EXACT SIZE
59impl<Type: const Target> ExactSizeIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
60
61//> ITERATOR -> FUSED
62impl<Type: const Target> FusedIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
63
64//> ITERATOR -> TRUSTED LEN
65unsafe impl<Type: const Target> TrustedLen for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
66
67
68//^
69//^ RANGE
70//^
71
72//> RANGE -> IMPLEMENTATION
73const impl<Type: const Target> ConstIntoIterator for RangeInclusive<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
74    type Item = Type;
75    type IntoIter = Iterable<Type>;
76    fn const_into_iter(self) -> Self::IntoIter {return Iterable {
77        start: self.start,
78        last: self.last,
79    }}
80}