1use crate::async_iter::all::All;
2use crate::async_iter::all_async::AllAsync;
3use crate::async_iter::and_then::AndThen;
4use crate::async_iter::and_then_async::AndThenAsync;
5use crate::async_iter::any::Any;
6use crate::async_iter::any_async::AnyAsync;
7use crate::async_iter::filter::Filter;
8use crate::async_iter::filter_async::FilterAsync;
9use crate::async_iter::filter_map::FilterMap;
10use crate::async_iter::filter_map_async::FilterMapAsync;
11use crate::async_iter::find::Find;
12use crate::async_iter::find_async::FindAsync;
13use crate::async_iter::find_map::FindMap;
14use crate::async_iter::find_map_async::FindMapAsync;
15use crate::async_iter::flat_map::FlatMap;
16use crate::async_iter::flat_map_async::FlatMapAsync;
17use crate::async_iter::flatten::Flatten;
18use crate::async_iter::fold::Fold;
19use crate::async_iter::fold_async::FoldAsync;
20use crate::async_iter::for_each::ForEach;
21use crate::async_iter::for_each_async::ForEachAsync;
22use crate::async_iter::fuse::Fuse;
23use crate::async_iter::inspect::Inspect;
24use crate::async_iter::map::Map;
25use crate::async_iter::map_async::MapAsync;
26use crate::async_iter::map_err::MapErr;
27use crate::async_iter::map_err_async::MapErrAsync;
28use crate::async_iter::map_ok::MapOk;
29use crate::async_iter::map_ok_async::MapOkAsync;
30use crate::async_iter::map_while::MapWhile;
31use crate::async_iter::map_while_async::MapWhileAsync;
32use crate::async_iter::or_else::OrElse;
33use crate::async_iter::or_else_async::OrElseAsync;
34use crate::async_iter::reduce::Reduce;
35use crate::async_iter::reduce_async::ReduceAsync;
36use crate::async_iter::scan::Scan;
37use crate::async_iter::scan_async::ScanAsync;
38use crate::async_iter::skip_while::SkipWhile;
39use crate::async_iter::skip_while_async::SkipWhileAsync;
40use crate::async_iter::take_while::TakeWhile;
41use crate::async_iter::take_while_async::TakeWhileAsync;
42use crate::async_iter::try_fold::TryFold;
43use crate::async_iter::try_fold_async::TryFoldAsync;
44use crate::async_iter::try_for_each::TryForEach;
45use crate::async_iter::try_for_each_async::TryForEachAsync;
46use crate::async_iter::zip::Zip;
47use crate::support::{AsyncIterator, FromResidual, IntoAsyncIterator, Residual, ResultAsyncIterator, Try};
48use core::future::IntoFuture;
49use fn_traits::fns::{CloneFn, CopyFn, MemTakeFn};
50
51pub trait AsyncIteratorExt: AsyncIterator {
52 fn slim_all<P>(self, predicate: P) -> All<Self, P>
53 where
54 Self: Sized,
55 P: FnMut(Self::Item) -> bool,
56 {
57 crate::support::assert_future::<_, bool>(All::new(self, predicate))
58 }
59
60 fn slim_all_async<P, Fut>(self, predicate: P) -> AllAsync<Self, P>
61 where
62 Self: Sized,
63 P: FnMut(Self::Item) -> Fut,
64 Fut: IntoFuture<Output = bool>,
65 {
66 crate::support::assert_future::<_, bool>(AllAsync::new(self, predicate))
67 }
68
69 fn slim_and_then<F, R>(self, f: F) -> AndThen<Self, F>
70 where
71 Self: Sized,
72 Self::Item: Try,
73 F: FnMut(<Self::Item as Try>::Output) -> R,
74 R: FromResidual<<Self::Item as Try>::Residual>,
75 {
76 crate::support::assert_async_iter::<_, R>(AndThen::new(self, f))
77 }
78
79 fn slim_and_then_async<F, Fut>(self, f: F) -> AndThenAsync<Self, F>
80 where
81 Self: Sized,
82 Self::Item: Try,
83 F: FnMut(<Self::Item as Try>::Output) -> Fut,
84 Fut: IntoFuture,
85 Fut::Output: FromResidual<<Self::Item as Try>::Residual>,
86 {
87 crate::support::assert_async_iter::<_, Fut::Output>(AndThenAsync::new(self, f))
88 }
89
90 fn slim_any<P>(self, predicate: P) -> Any<Self, P>
91 where
92 Self: Sized,
93 P: FnMut(Self::Item) -> bool,
94 {
95 crate::support::assert_future::<_, bool>(Any::new(self, predicate))
96 }
97
98 fn slim_any_async<P, Fut>(self, predicate: P) -> AnyAsync<Self, P>
99 where
100 Self: Sized,
101 P: FnMut(Self::Item) -> Fut,
102 Fut: IntoFuture<Output = bool>,
103 {
104 crate::support::assert_future::<_, bool>(AnyAsync::new(self, predicate))
105 }
106
107 fn slim_filter<P>(self, predicate: P) -> Filter<Self, P>
108 where
109 Self: Sized,
110 P: FnMut(&Self::Item) -> bool,
111 {
112 crate::support::assert_async_iter::<_, Self::Item>(Filter::new(self, predicate))
113 }
114
115 fn slim_filter_async<P, Fut>(self, predicate: P) -> FilterAsync<Self, P>
116 where
117 Self: Sized,
118 P: FnMut(&Self::Item) -> Fut,
119 Fut: IntoFuture<Output = bool>,
120 {
121 crate::support::assert_async_iter::<_, Self::Item>(FilterAsync::new(self, predicate))
122 }
123
124 fn slim_filter_map<F, T>(self, f: F) -> FilterMap<Self, F>
125 where
126 Self: Sized,
127 F: FnMut(Self::Item) -> Option<T>,
128 {
129 crate::support::assert_async_iter::<_, T>(FilterMap::new(self, f))
130 }
131
132 fn slim_filter_map_async<F, Fut, T>(self, f: F) -> FilterMapAsync<Self, F>
133 where
134 Self: Sized,
135 F: FnMut(Self::Item) -> Fut,
136 Fut: IntoFuture<Output = Option<T>>,
137 {
138 crate::support::assert_async_iter::<_, T>(FilterMapAsync::new(self, f))
139 }
140
141 fn slim_find<P>(self, predicate: P) -> Find<Self, P>
142 where
143 Self: Sized,
144 P: FnMut(&Self::Item) -> bool,
145 {
146 crate::support::assert_future::<_, Option<Self::Item>>(Find::new(self, predicate))
147 }
148
149 fn slim_find_async<P, Fut>(self, predicate: P) -> FindAsync<Self, P>
150 where
151 Self: Sized,
152 P: FnMut(&Self::Item) -> Fut,
153 Fut: IntoFuture<Output = bool>,
154 {
155 crate::support::assert_future::<_, Option<Self::Item>>(FindAsync::new(self, predicate))
156 }
157
158 fn slim_find_map<F, T>(self, f: F) -> FindMap<Self, F>
159 where
160 Self: Sized,
161 F: FnMut(Self::Item) -> Option<T>,
162 {
163 crate::support::assert_future::<_, Option<T>>(FindMap::new(self, f))
164 }
165
166 fn slim_find_map_async<F, Fut, T>(self, f: F) -> FindMapAsync<Self, F>
167 where
168 Self: Sized,
169 F: FnMut(Self::Item) -> Fut,
170 Fut: IntoFuture<Output = Option<T>>,
171 {
172 crate::support::assert_future::<_, Option<T>>(FindMapAsync::new(self, f))
173 }
174
175 fn slim_flat_map<F, I>(self, f: F) -> FlatMap<Self, F>
176 where
177 Self: Sized,
178 F: FnMut(Self::Item) -> I,
179 I: IntoAsyncIterator,
180 {
181 crate::support::assert_async_iter::<_, I::Item>(FlatMap::new(self, f))
182 }
183
184 fn slim_flat_map_async<F, Fut>(self, f: F) -> FlatMapAsync<Self, F>
185 where
186 Self: Sized,
187 F: FnMut(Self::Item) -> Fut,
188 Fut: IntoFuture,
189 Fut::Output: IntoAsyncIterator,
190 {
191 crate::support::assert_async_iter::<_, <<F::Output as IntoFuture>::Output as IntoAsyncIterator>::Item>(
192 FlatMapAsync::new(self, f),
193 )
194 }
195
196 fn slim_flatten(self) -> Flatten<Self>
197 where
198 Self: Sized,
199 Self::Item: IntoAsyncIterator,
200 {
201 crate::support::assert_async_iter::<_, <Self::Item as IntoAsyncIterator>::Item>(Flatten::new(self))
202 }
203
204 fn slim_fold_by<T, G, F>(self, init: T, getter: G, f: F) -> Fold<Self, T, G, F>
205 where
206 Self: Sized,
207 G: FnMut(&mut T) -> T,
208 F: FnMut(T, Self::Item) -> T,
209 {
210 crate::support::assert_future::<_, T>(Fold::new(self, init, getter, f))
211 }
212
213 fn slim_fold_by_copy<T, F>(self, init: T, f: F) -> Fold<Self, T, CopyFn, F>
214 where
215 Self: Sized,
216 T: Copy,
217 F: FnMut(T, Self::Item) -> T,
218 {
219 crate::support::assert_future::<_, T>(Fold::new(self, init, CopyFn::default(), f))
220 }
221
222 fn slim_fold_by_clone<T, F>(self, init: T, f: F) -> Fold<Self, T, CloneFn, F>
223 where
224 Self: Sized,
225 T: Clone,
226 F: FnMut(T, Self::Item) -> T,
227 {
228 crate::support::assert_future::<_, T>(Fold::new(self, init, CloneFn::default(), f))
229 }
230
231 fn slim_fold_by_take<T, F>(self, init: T, f: F) -> Fold<Self, T, MemTakeFn, F>
232 where
233 Self: Sized,
234 T: Default,
235 F: FnMut(T, Self::Item) -> T,
236 {
237 crate::support::assert_future::<_, T>(Fold::new(self, init, MemTakeFn::default(), f))
238 }
239
240 fn slim_fold_async_by<T, G, F, Fut>(self, init: T, getter: G, f: F) -> FoldAsync<Self, T, G, F>
241 where
242 Self: Sized,
243 G: FnMut(&mut T) -> T,
244 F: FnMut(T, Self::Item) -> Fut,
245 Fut: IntoFuture<Output = T>,
246 {
247 crate::support::assert_future::<_, T>(FoldAsync::new(self, init, getter, f))
248 }
249
250 fn slim_fold_async_by_copy<T, F, Fut>(self, init: T, f: F) -> FoldAsync<Self, T, CopyFn, F>
251 where
252 Self: Sized,
253 T: Copy,
254 F: FnMut(T, Self::Item) -> Fut,
255 Fut: IntoFuture<Output = T>,
256 {
257 crate::support::assert_future::<_, T>(FoldAsync::new(self, init, CopyFn::default(), f))
258 }
259
260 fn slim_fold_async_by_clone<T, F, Fut>(self, init: T, f: F) -> FoldAsync<Self, T, CloneFn, F>
261 where
262 Self: Sized,
263 T: Clone,
264 F: FnMut(T, Self::Item) -> Fut,
265 Fut: IntoFuture<Output = T>,
266 {
267 crate::support::assert_future::<_, T>(FoldAsync::new(self, init, CloneFn::default(), f))
268 }
269
270 fn slim_fold_async_by_take<T, F, Fut>(self, init: T, f: F) -> FoldAsync<Self, T, MemTakeFn, F>
271 where
272 Self: Sized,
273 T: Default,
274 F: FnMut(T, Self::Item) -> Fut,
275 Fut: IntoFuture<Output = T>,
276 {
277 crate::support::assert_future::<_, T>(FoldAsync::new(self, init, MemTakeFn::default(), f))
278 }
279
280 fn slim_for_each<F>(self, f: F) -> ForEach<Self, F>
281 where
282 Self: Sized,
283 F: FnMut(Self::Item),
284 {
285 crate::support::assert_future::<_, ()>(ForEach::new(self, f))
286 }
287
288 fn slim_for_each_async<F, Fut>(self, f: F) -> ForEachAsync<Self, F>
289 where
290 Self: Sized,
291 F: FnMut(Self::Item) -> Fut,
292 Fut: IntoFuture<Output = ()>,
293 {
294 crate::support::assert_future::<_, ()>(ForEachAsync::new(self, f))
295 }
296
297 fn slim_fuse(self) -> Fuse<Self>
298 where
299 Self: Sized,
300 {
301 crate::support::assert_async_iter::<_, Self::Item>(Fuse::new(self))
302 }
303
304 fn slim_inspect<F>(self, f: F) -> Inspect<Self, F>
305 where
306 Self: Sized,
307 F: FnMut(&Self::Item),
308 {
309 crate::support::assert_async_iter::<_, Self::Item>(Inspect::new(self, f))
310 }
311
312 fn slim_map<F, T>(self, f: F) -> Map<Self, F>
313 where
314 Self: Sized,
315 F: FnMut(Self::Item) -> T,
316 {
317 crate::support::assert_async_iter::<_, T>(Map::new(self, f))
318 }
319
320 fn slim_map_async<F, Fut>(self, f: F) -> MapAsync<Self, F>
321 where
322 Self: Sized,
323 F: FnMut(Self::Item) -> Fut,
324 Fut: IntoFuture,
325 {
326 crate::support::assert_async_iter::<_, Fut::Output>(MapAsync::new(self, f))
327 }
328
329 fn slim_map_err<F, E>(self, f: F) -> MapErr<Self, F>
330 where
331 Self: ResultAsyncIterator + Sized,
332 F: FnMut(Self::Error) -> E,
333 {
334 crate::support::assert_async_iter::<_, Result<Self::Ok, E>>(MapErr::new(self, f))
335 }
336
337 fn slim_map_err_async<F, Fut>(self, f: F) -> MapErrAsync<Self, F>
338 where
339 Self: ResultAsyncIterator + Sized,
340 F: FnMut(Self::Error) -> Fut,
341 Fut: IntoFuture,
342 {
343 crate::support::assert_async_iter::<_, Result<Self::Ok, Fut::Output>>(MapErrAsync::new(self, f))
344 }
345
346 fn slim_map_ok<F, T>(self, f: F) -> MapOk<Self, F>
347 where
348 Self: Sized,
349 Self::Item: Try,
350 <Self::Item as Try>::Residual: Residual<T>,
351 F: FnMut(<Self::Item as Try>::Output) -> T,
352 {
353 crate::support::assert_async_iter::<_, <<Self::Item as Try>::Residual as Residual<T>>::TryType>(MapOk::new(
354 self, f,
355 ))
356 }
357
358 fn slim_map_ok_async<F, Fut>(self, f: F) -> MapOkAsync<Self, F>
359 where
360 Self: Sized,
361 Self::Item: Try,
362 <Self::Item as Try>::Residual: Residual<Fut::Output>,
363 F: FnMut(<Self::Item as Try>::Output) -> Fut,
364 Fut: IntoFuture,
365 {
366 crate::support::assert_async_iter::<_, <<Self::Item as Try>::Residual as Residual<Fut::Output>>::TryType>(
367 MapOkAsync::new(self, f),
368 )
369 }
370
371 fn slim_map_while<F, T>(self, f: F) -> MapWhile<Self, F>
372 where
373 Self: Sized,
374 F: FnMut(Self::Item) -> Option<T>,
375 {
376 crate::support::assert_async_iter::<_, T>(MapWhile::new(self, f))
377 }
378
379 fn slim_map_while_async<F, Fut, T>(self, f: F) -> MapWhileAsync<Self, F>
380 where
381 Self: Sized,
382 F: FnMut(Self::Item) -> Fut,
383 Fut: IntoFuture<Output = Option<T>>,
384 {
385 crate::support::assert_async_iter::<_, T>(MapWhileAsync::new(self, f))
386 }
387
388 fn slim_or_else<F, R>(self, f: F) -> OrElse<Self, F>
389 where
390 Self: ResultAsyncIterator + Sized,
391 F: FnMut(Self::Error) -> R,
392 R: Try<Output = Self::Ok>,
393 {
394 crate::support::assert_async_iter::<_, R>(OrElse::new(self, f))
395 }
396
397 fn slim_or_else_async<F, Fut>(self, f: F) -> OrElseAsync<Self, F>
398 where
399 Self: ResultAsyncIterator + Sized,
400 F: FnMut(Self::Error) -> Fut,
401 Fut: IntoFuture,
402 Fut::Output: Try<Output = Self::Ok>,
403 {
404 crate::support::assert_async_iter::<_, Fut::Output>(OrElseAsync::new(self, f))
405 }
406
407 fn slim_reduce<F>(self, f: F) -> Reduce<Self, F>
408 where
409 Self: Sized,
410 F: FnMut(Self::Item, Self::Item) -> Self::Item,
411 {
412 crate::support::assert_future::<_, Option<Self::Item>>(Reduce::new(self, f))
413 }
414
415 fn slim_reduce_async<F, Fut>(self, f: F) -> ReduceAsync<Self, F>
416 where
417 Self: Sized,
418 F: FnMut(Self::Item, Self::Item) -> Fut,
419 Fut: IntoFuture<Output = Self::Item>,
420 {
421 crate::support::assert_future::<_, Option<Self::Item>>(ReduceAsync::new(self, f))
422 }
423
424 fn slim_scan<S, F, T>(self, state: S, f: F) -> Scan<Self, S, F>
425 where
426 Self: Sized,
427 F: FnMut(&mut S, Self::Item) -> Option<T>,
428 {
429 crate::support::assert_async_iter::<_, T>(Scan::new(self, state, f))
430 }
431
432 fn slim_scan_async<S, F, Fut, T>(self, state: S, f: F) -> ScanAsync<Self, S, F>
433 where
434 Self: Sized,
435 F: FnMut(&mut S, Self::Item) -> Fut,
436 Fut: IntoFuture<Output = Option<T>>,
437 {
438 crate::support::assert_async_iter::<_, T>(ScanAsync::new(self, state, f))
439 }
440
441 fn slim_skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
442 where
443 Self: Sized,
444 P: FnMut(&Self::Item) -> bool,
445 {
446 crate::support::assert_async_iter::<_, Self::Item>(SkipWhile::new(self, predicate))
447 }
448
449 fn slim_skip_while_async<P, Fut>(self, predicate: P) -> SkipWhileAsync<Self, P>
450 where
451 Self: Sized,
452 P: FnMut(&Self::Item) -> Fut,
453 Fut: IntoFuture<Output = bool>,
454 {
455 crate::support::assert_async_iter::<_, Self::Item>(SkipWhileAsync::new(self, predicate))
456 }
457
458 fn slim_take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
459 where
460 Self: Sized,
461 P: FnMut(&Self::Item) -> bool,
462 {
463 crate::support::assert_async_iter::<_, Self::Item>(TakeWhile::new(self, predicate))
464 }
465
466 fn slim_take_while_async<P, Fut>(self, predicate: P) -> TakeWhileAsync<Self, P>
467 where
468 Self: Sized,
469 P: FnMut(&Self::Item) -> Fut,
470 Fut: IntoFuture<Output = bool>,
471 {
472 crate::support::assert_async_iter::<_, Self::Item>(TakeWhileAsync::new(self, predicate))
473 }
474
475 fn slim_try_fold_by<T, G, F, R>(self, init: T, getter: G, f: F) -> TryFold<Self, T, G, F>
476 where
477 Self: Sized,
478 G: FnMut(&mut T) -> T,
479 F: FnMut(T, Self::Item) -> R,
480 R: Try<Output = T>,
481 {
482 crate::support::assert_future::<_, R>(TryFold::new(self, init, getter, f))
483 }
484
485 fn slim_try_fold_by_copy<T, F, R>(self, init: T, f: F) -> TryFold<Self, T, CopyFn, F>
486 where
487 Self: Sized,
488 T: Copy,
489 F: FnMut(T, Self::Item) -> R,
490 R: Try<Output = T>,
491 {
492 crate::support::assert_future::<_, R>(TryFold::new(self, init, CopyFn::default(), f))
493 }
494
495 fn slim_try_fold_by_clone<T, F, R>(self, init: T, f: F) -> TryFold<Self, T, CloneFn, F>
496 where
497 Self: Sized,
498 T: Clone,
499 F: FnMut(T, Self::Item) -> R,
500 R: Try<Output = T>,
501 {
502 crate::support::assert_future::<_, R>(TryFold::new(self, init, CloneFn::default(), f))
503 }
504
505 fn slim_try_fold_by_take<T, F, R>(self, init: T, f: F) -> TryFold<Self, T, MemTakeFn, F>
506 where
507 Self: Sized,
508 T: Default,
509 F: FnMut(T, Self::Item) -> R,
510 R: Try<Output = T>,
511 {
512 crate::support::assert_future::<_, R>(TryFold::new(self, init, MemTakeFn::default(), f))
513 }
514
515 fn slim_try_fold_async_by<T, G, F, Fut>(self, init: T, getter: G, f: F) -> TryFoldAsync<Self, T, G, F>
516 where
517 Self: Sized,
518 G: FnMut(&mut T) -> T,
519 F: FnMut(T, Self::Item) -> Fut,
520 Fut: IntoFuture,
521 Fut::Output: Try<Output = T>,
522 {
523 crate::support::assert_future::<_, Fut::Output>(TryFoldAsync::new(self, init, getter, f))
524 }
525
526 fn slim_try_fold_async_by_copy<T, F, Fut>(self, init: T, f: F) -> TryFoldAsync<Self, T, CopyFn, F>
527 where
528 Self: Sized,
529 T: Copy,
530 F: FnMut(T, Self::Item) -> Fut,
531 Fut: IntoFuture,
532 Fut::Output: Try<Output = T>,
533 {
534 crate::support::assert_future::<_, Fut::Output>(TryFoldAsync::new(self, init, CopyFn::default(), f))
535 }
536
537 fn slim_try_fold_async_by_clone<T, F, Fut>(self, init: T, f: F) -> TryFoldAsync<Self, T, CloneFn, F>
538 where
539 Self: Sized,
540 T: Clone,
541 F: FnMut(T, Self::Item) -> Fut,
542 Fut: IntoFuture,
543 Fut::Output: Try<Output = T>,
544 {
545 crate::support::assert_future::<_, Fut::Output>(TryFoldAsync::new(self, init, CloneFn::default(), f))
546 }
547
548 fn slim_try_fold_async_by_take<T, F, Fut>(self, init: T, f: F) -> TryFoldAsync<Self, T, MemTakeFn, F>
549 where
550 Self: Sized,
551 T: Default,
552 F: FnMut(T, Self::Item) -> Fut,
553 Fut: IntoFuture,
554 Fut::Output: Try<Output = T>,
555 {
556 crate::support::assert_future::<_, Fut::Output>(TryFoldAsync::new(self, init, MemTakeFn::default(), f))
557 }
558
559 fn slim_try_for_each<F, R>(self, f: F) -> TryForEach<Self, F>
560 where
561 Self: Sized,
562 F: FnMut(Self::Item) -> R,
563 R: Try<Output = ()>,
564 {
565 crate::support::assert_future::<_, R>(TryForEach::new(self, f))
566 }
567
568 fn slim_try_for_each_async<F, Fut>(self, f: F) -> TryForEachAsync<Self, F>
569 where
570 Self: Sized,
571 F: FnMut(Self::Item) -> Fut,
572 Fut: IntoFuture,
573 Fut::Output: Try<Output = ()>,
574 {
575 crate::support::assert_future::<_, Fut::Output>(TryForEachAsync::new(self, f))
576 }
577
578 fn slim_zip<I>(self, other: I) -> Zip<Self, I::IntoAsyncIter>
579 where
580 Self: Sized,
581 I: IntoAsyncIterator,
582 {
583 crate::support::assert_async_iter::<_, (Self::Item, I::Item)>(Zip::new(self, other.into_async_iter()))
584 }
585}
586
587impl<I> AsyncIteratorExt for I where I: AsyncIterator {}