rust__/
views.rs

1use std::{
2    iter::{Enumerate, Filter, Flatten, Map, Rev, Skip, SkipWhile, Take, TakeWhile, Zip},
3    ops::{BitOr, Deref},
4};
5
6pub struct range<I>(pub I);
7
8impl<I> Iterator for range<I>
9where
10    I: Iterator,
11{
12    type Item = I::Item;
13
14    fn next(&mut self) -> Option<Self::Item> {
15        self.0.next()
16    }
17}
18
19impl<I> Deref for range<I> {
20    type Target = I;
21    fn deref(&self) -> &Self::Target {
22        &self.0
23    }
24}
25
26impl<I> range<I>
27where
28    I: Iterator,
29{
30    pub fn begin(&mut self) -> I::Item {
31        self.0.next().unwrap()
32    }
33}
34
35pub trait adaptor<I> {
36    type Output;
37    fn pipe(self, i: I) -> Self::Output;
38}
39
40impl<I, V> BitOr<V> for range<I>
41where
42    V: adaptor<I>,
43{
44    type Output = range<V::Output>;
45    fn bitor(self, rhs: V) -> Self::Output {
46        range(rhs.pipe(self.0))
47    }
48}
49
50#[macro_export]
51macro_rules! iota {
52    ($value:expr) => {
53        $crate::views::iter(($value)..)
54    };
55    ($first:expr, $last:expr) => {
56        $crate::views::iter(($first)..($last))
57    };
58}
59
60#[macro_export]
61macro_rules! closed_iota {
62    ($value:expr) => {
63        $crate::views::iter(($value)..)
64    };
65    ($first:expr, $last:expr) => {
66        $crate::views::iter(($first)..=($last))
67    };
68}
69
70pub struct repeat(pub usize);
71impl<I> adaptor<I> for repeat
72where
73    I: Iterator + Clone,
74{
75    type Output = Flatten<Take<std::iter::Repeat<I>>>;
76    fn pipe(self, i: I) -> Self::Output {
77        std::iter::repeat(i).take(self.0).flatten()
78    }
79}
80
81pub struct filter<F>(pub F);
82impl<I, F> adaptor<I> for filter<F>
83where
84    I: Iterator,
85    F: FnMut(&I::Item) -> bool,
86{
87    type Output = Filter<I, F>;
88    fn pipe(self, i: I) -> Self::Output {
89        i.filter(self.0)
90    }
91}
92
93pub struct transform<F>(pub F);
94impl<I, F, O> adaptor<I> for transform<F>
95where
96    I: Iterator,
97    F: FnMut(I::Item) -> O,
98{
99    type Output = Map<I, F>;
100    fn pipe(self, i: I) -> Self::Output {
101        i.map(self.0)
102    }
103}
104
105pub struct take(pub usize);
106impl<I> adaptor<I> for take
107where
108    I: Iterator,
109{
110    type Output = Take<I>;
111    fn pipe(self, i: I) -> Self::Output {
112        i.take(self.0)
113    }
114}
115
116pub struct take_while<F>(pub F);
117impl<I, F> adaptor<I> for take_while<F>
118where
119    I: Iterator,
120    F: FnMut(&I::Item) -> bool,
121{
122    type Output = TakeWhile<I, F>;
123    fn pipe(self, i: I) -> Self::Output {
124        i.take_while(self.0)
125    }
126}
127
128pub struct drop(pub usize);
129impl<I> adaptor<I> for drop
130where
131    I: Iterator,
132{
133    type Output = Skip<I>;
134    fn pipe(self, i: I) -> Self::Output {
135        i.skip(self.0)
136    }
137}
138
139pub struct drop_while<F>(pub F);
140impl<I, F> adaptor<I> for drop_while<F>
141where
142    I: Iterator,
143    F: FnMut(&I::Item) -> bool,
144{
145    type Output = SkipWhile<I, F>;
146    fn pipe(self, i: I) -> Self::Output {
147        i.skip_while(self.0)
148    }
149}
150
151pub struct join;
152impl<I> adaptor<I> for join
153where
154    I: Iterator,
155    I::Item: Iterator,
156{
157    type Output = Flatten<I>;
158    fn pipe(self, i: I) -> Self::Output {
159        i.flatten()
160    }
161}
162
163pub struct reverse;
164impl<I> adaptor<I> for reverse
165where
166    I: DoubleEndedIterator,
167{
168    type Output = Rev<I>;
169    fn pipe(self, i: I) -> Self::Output {
170        i.rev()
171    }
172}
173
174pub struct enumerate;
175impl<I> adaptor<I> for enumerate
176where
177    I: Iterator,
178{
179    type Output = Enumerate<I>;
180    fn pipe(self, i: I) -> Self::Output {
181        i.enumerate()
182    }
183}
184
185pub struct zip<J>(J);
186impl<I, J> adaptor<I> for zip<J>
187where
188    I: Iterator,
189    J: IntoIterator,
190{
191    type Output = Zip<I, J::IntoIter>;
192    fn pipe(self, i: I) -> Self::Output {
193        i.zip(self.0)
194    }
195}