iterator_ext/
trait_.rs

1use crate::{
2    and_then::AndThen, map_err::MapErr, try_filter::TryFilter, try_filter_map::TryFilterMap,
3    try_flat_map::TryFlatMap, try_flat_map_results::TryFlatMapResults, try_flatten::TryFlatten,
4    try_flatten_results::TryFlattenResults, try_scan::TryScan,
5};
6
7/// An extension trait to [Iterator]
8pub trait IteratorExt {
9    /// A fallible iterator adaptor analogous to [scan](Iterator::scan) from [Iterator](Iterator).
10    fn try_scan<St, F, T, U, E>(self, init: St, f: F) -> TryScan<Self, St, F>
11    where
12        Self: Sized + Iterator<Item = Result<T, E>>,
13        F: FnMut(&mut St, T) -> Result<Option<U>, E>,
14    {
15        TryScan {
16            iter: Some(self),
17            state: init,
18            f,
19        }
20    }
21
22    /// Creates a fallible iterator that works like map, but flattens nested structure.
23    fn try_flat_map<F, T, U, V, E>(self, f: F) -> TryFlatMap<Self, F, U::IntoIter>
24    where
25        Self: Sized + Iterator<Item = Result<T, E>>,
26        F: FnMut(T) -> Result<U, E>,
27        U: IntoIterator<Item = V>,
28    {
29        TryFlatMap {
30            iter: Some(self),
31            sub_iter: None,
32            f,
33        }
34    }
35
36    /// Creates a fallible iterator that flattens nested structure.
37    fn try_flatten<T, U, E>(self) -> TryFlatten<Self, T::IntoIter>
38    where
39        Self: Sized + Iterator<Item = Result<T, E>>,
40        T: IntoIterator<Item = U>,
41    {
42        TryFlatten {
43            iter: Some(self),
44            sub_iter: None,
45        }
46    }
47
48    /// Similar to [try_flat_map](IteratorExt::try_flat_map), but flattens nested results.
49    fn try_flat_map_results<F, T, U, V, E>(self, f: F) -> TryFlatMapResults<Self, F, U::IntoIter>
50    where
51        Self: Sized + Iterator<Item = Result<T, E>>,
52        F: FnMut(T) -> Result<U, E>,
53        U: IntoIterator<Item = Result<V, E>>,
54    {
55        TryFlatMapResults {
56            iter: Some(self),
57            sub_iter: None,
58            f,
59        }
60    }
61
62    /// Similar to [try_flatten](IteratorExt::try_flatten), but flattens nested results.
63    fn try_flatten_results<T, U, E>(self) -> TryFlattenResults<Self, T::IntoIter>
64    where
65        Self: Sized + Iterator<Item = Result<T, E>>,
66        T: IntoIterator<Item = Result<U, E>>,
67    {
68        TryFlattenResults {
69            iter: Some(self),
70            sub_iter: None,
71        }
72    }
73
74    /// Creates a fallible iterator that filters items.
75    fn try_filter<F, T, E>(self, f: F) -> TryFilter<Self, F>
76    where
77        Self: Sized + Iterator<Item = Result<T, E>>,
78        F: FnMut(&T) -> Result<bool, E>,
79    {
80        TryFilter {
81            iter: Some(self),
82            f,
83        }
84    }
85
86    /// Creates a fallible iterator that both filters and maps.
87    fn try_filter_map<F, T, U, E>(self, f: F) -> TryFilterMap<Self, F>
88    where
89        Self: Sized + Iterator<Item = Result<T, E>>,
90        F: FnMut(T) -> Result<Option<U>, E>,
91    {
92        TryFilterMap {
93            iter: Some(self),
94            f,
95        }
96    }
97
98    /// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained Err value, leaving an Ok value untouched.
99    fn map_err<F, Ein, Eout>(self, f: F) -> MapErr<Self, F>
100    where
101        Self: Sized,
102        F: FnMut(Ein) -> Eout,
103    {
104        MapErr { iter: self, f }
105    }
106
107    /// Takes a closure and creates a fallible iterator which calls that closure on each element.
108    fn and_then<F, T, U, E>(self, f: F) -> AndThen<Self, F>
109    where
110        Self: Sized + Iterator<Item = Result<T, E>>,
111        F: FnMut(T) -> Result<U, E>,
112    {
113        AndThen {
114            iter: Some(self),
115            f,
116        }
117    }
118}
119
120impl<I> IteratorExt for I where I: Iterator {}