Skip to main content

libutils_constrangeiter/
range.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    },
19    range::Range,
20    marker::Destruct
21};
22
23
24//^
25//^ ITERATOR
26//^
27
28//> ITERATOR -> ITERABLE
29#[derive(Debug)]
30pub struct Iterable<Type: const Target> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
31    start: Type,
32    end: Type
33}
34
35//> ITERATOR -> IMPLEMENTATION
36const impl<Type: const Target> Iterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
37    type Item = Type;
38    fn next(&mut self) -> Option<Self::Item> {return if self.start < self.end {
39        let now = self.start;
40        self.start.add_assign(Type::ONE);
41        Some(now)
42    } else {None}}
43    fn size_hint(&self) -> (usize, Option<usize>) {
44        return if self.start < self.end {self.end.hint(self.start)} else {(0, Some(0))};
45    }
46}
47
48//> ITERATOR -> DOUBLE ENDED
49const impl<Type: const Target> DoubleEndedIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
50    fn next_back(&mut self) -> Option<Self::Item> {return if self.start < self.end {
51        self.end.sub_assign(Type::ONE);
52        return Some(self.end)
53    } else {None}}
54}
55
56//> ITERATOR -> EXACT SIZE
57impl<Type: const Target> ExactSizeIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
58
59//> ITERATOR -> FUSED
60impl<Type: const Target> FusedIterator for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
61
62//> ITERATOR -> TRUSTED LEN
63unsafe impl<Type: const Target> TrustedLen for Iterable<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {}
64
65
66//^
67//^ RANGE
68//^
69
70//> RANGE -> IMPLEMENTATION
71const impl<Type: const Target> ConstIntoIterator for Range<Type> where usize: const TryFrom<Type>, <usize as TryFrom<Type>>::Error: const Destruct {
72    type Item = Type;
73    type IntoIter = Iterable<Type>;
74    fn const_into_iter(self) -> Self::IntoIter {return Iterable {
75        start: self.start,
76        end: self.end
77    }}
78}