rta_for_fps_lib/
iterators.rs1use alloc::boxed::Box;
4use core::fmt::Debug;
5use core::iter::{Empty, Fuse, TakeWhile};
6use core::marker::PhantomData;
7
8use crate::curve::curve_types::{CurveType, UnspecifiedCurve};
9use crate::iterators::curve::FromCurveIterator;
10use crate::iterators::join::JoinAdjacentIterator;
11use crate::window::window_types::WindowType;
12use crate::window::Window;
13use core::ops::DerefMut;
14
15pub mod curve;
16pub mod join;
17pub mod peek;
18pub mod server;
19pub mod task;
20
21pub trait CurveIterator: Debug {
29 type CurveKind: CurveType;
31
32 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>>;
35
36 #[must_use]
38 fn collect_curve<R: FromCurveIterator<Self::CurveKind>>(self) -> R
39 where
40 Self: Sized,
41 {
42 R::from_curve_iter(self)
43 }
44
45 #[must_use]
47 fn reclassify<O>(self) -> ReclassifyIterator<Self, O>
48 where
49 Self: Sized,
50 {
51 ReclassifyIterator {
52 iter: self,
53 phantom: PhantomData,
54 }
55 }
56
57 fn normalize(
59 self,
60 ) -> JoinAdjacentIterator<
61 CurveIteratorIterator<Self>,
62 <Self::CurveKind as CurveType>::WindowKind,
63 Self::CurveKind,
64 >
65 where
66 Self: Sized,
67 {
68 JoinAdjacentIterator::new_from_curve(self)
69 }
70
71 fn take_while_curve<F>(self, fun: F) -> TakeWhile<CurveIteratorIterator<Self>, F>
73 where
74 Self: Sized,
75 F: for<'a> FnMut(&'a Window<<Self::CurveKind as CurveType>::WindowKind>) -> bool,
76 {
77 self.into_iterator().take_while(fun)
78 }
79
80 fn fuse_curve(self) -> Fuse<CurveIteratorIterator<Self>>
82 where
83 Self: Sized,
84 {
85 self.into_iterator().fuse()
86 }
87
88 fn into_iterator(self) -> CurveIteratorIterator<Self>
90 where
91 Self: Sized,
92 {
93 CurveIteratorIterator { iter: self }
94 }
95}
96
97pub trait ClonableCurveIterator<'b>: CurveIterator + 'b {
98 fn clone_box(&self) -> Box<dyn ClonableCurveIterator<'b, CurveKind = Self::CurveKind> + 'b>;
99}
100
101impl<'b, T> ClonableCurveIterator<'b> for T
102where
103 T: 'b + CurveIterator + Clone,
104{
105 fn clone_box(&self) -> Box<dyn ClonableCurveIterator<'b, CurveKind = Self::CurveKind> + 'b> {
106 Box::new(self.clone())
107 }
108}
109
110impl<'a, C: CurveType> Clone for Box<dyn ClonableCurveIterator<'a, CurveKind = C> + 'a>
111where
112 C: 'a,
113{
114 fn clone(&self) -> Self {
115 self.clone_box()
116 }
117}
118
119#[derive(Debug)]
121pub struct ReclassifyIterator<I, O> {
122 iter: I,
124 phantom: PhantomData<O>,
126}
127
128impl<I: Clone, O> Clone for ReclassifyIterator<I, O> {
129 fn clone(&self) -> Self {
130 ReclassifyIterator {
131 iter: self.iter.clone(),
132 phantom: PhantomData,
133 }
134 }
135}
136
137impl<I, O> CurveIterator for ReclassifyIterator<I, O>
138where
139 I: CurveIterator,
140 O: CurveType,
141{
142 type CurveKind = O;
143
144 fn next_window(&mut self) -> Option<Window<O::WindowKind>> {
145 self.iter.next_window().map(Window::reclassify)
146 }
147}
148
149#[derive(Debug, Clone)]
151pub struct CurveIteratorIterator<I> {
152 iter: I,
154}
155
156impl<I> CurveIterator for CurveIteratorIterator<I>
157where
158 I: CurveIterator,
159{
160 type CurveKind = I::CurveKind;
161
162 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
163 self.iter.next_window()
164 }
165}
166
167impl<CI: CurveIterator> CurveIterator for &mut CI {
168 type CurveKind = CI::CurveKind;
169
170 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
171 CI::next_window(self)
172 }
173}
174
175impl<CI: CurveIterator> CurveIterator for Box<CI> {
176 type CurveKind = CI::CurveKind;
177
178 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
179 CI::next_window(self)
180 }
181}
182
183impl<'b, C> CurveIterator for Box<dyn ClonableCurveIterator<'b, CurveKind = C>>
184where
185 C: CurveType,
186{
187 type CurveKind = C;
188
189 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
190 self.deref_mut().next_window()
191 }
192}
193
194impl<I> Iterator for CurveIteratorIterator<I>
195where
196 I: CurveIterator,
197{
198 type Item = Window<<I::CurveKind as CurveType>::WindowKind>;
199
200 fn next(&mut self) -> Option<Self::Item> {
201 self.iter.next_window()
202 }
203}
204
205impl<W> CurveIterator for Empty<Window<W>>
206where
207 W: WindowType,
208{
209 type CurveKind = UnspecifiedCurve<W>;
210
211 fn next_window(&mut self) -> Option<Window<W>> {
212 None
213 }
214}
215
216impl<W: WindowType, CI> CurveIterator for Fuse<CI>
217where
218 CI: CurveIterator + Iterator<Item = Window<W>>,
219 CI::CurveKind: CurveType<WindowKind = W>,
220{
221 type CurveKind = CI::CurveKind;
222
223 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
224 self.next()
225 }
226}
227
228impl<W, P, CI> CurveIterator for TakeWhile<CI, P>
229where
230 W: WindowType,
231 P: for<'r> FnMut(&'r Window<W>) -> bool,
232 CI: CurveIterator + Iterator<Item = Window<W>>,
233 CI::CurveKind: CurveType<WindowKind = W>,
234{
235 type CurveKind = CI::CurveKind;
236
237 fn next_window(&mut self) -> Option<Window<<Self::CurveKind as CurveType>::WindowKind>> {
238 self.next()
239 }
240}