1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use crate::{
    and_then::AndThen, map_err::MapErr, try_filter::TryFilter, try_filter_map::TryFilterMap,
    try_flat_map::TryFlatMap, try_flat_map_results::TryFlatMapResults, try_flatten::TryFlatten,
    try_flatten_results::TryFlattenResults, try_scan::TryScan,
};

/// An extension trait to [Iterator]
pub trait IteratorExt {
    /// A fallible iterator adaptor analogous to [scan](Iterator::scan) from [Iterator](Iterator).
    fn try_scan<St, F, T, U, E>(self, init: St, f: F) -> TryScan<Self, St, F>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(&mut St, T) -> Result<Option<U>, E>,
    {
        TryScan {
            iter: Some(self),
            state: init,
            f,
        }
    }

    /// Creates a fallible iterator that works like map, but flattens nested structure.
    fn try_flat_map<F, T, U, V, E>(self, f: F) -> TryFlatMap<Self, F, U::IntoIter>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(T) -> Result<U, E>,
        U: IntoIterator<Item = V>,
    {
        TryFlatMap {
            iter: Some(self),
            sub_iter: None,
            f,
        }
    }

    /// Creates a fallible iterator that flattens nested structure.
    fn try_flatten<T, U, E>(self) -> TryFlatten<Self, T::IntoIter>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        T: IntoIterator<Item = U>,
    {
        TryFlatten {
            iter: Some(self),
            sub_iter: None,
        }
    }

    /// Similar to [try_flat_map](IteratorExt::try_flat_map), but flattens nested results.
    fn try_flat_map_results<F, T, U, V, E>(self, f: F) -> TryFlatMapResults<Self, F, U::IntoIter>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(T) -> Result<U, E>,
        U: IntoIterator<Item = Result<V, E>>,
    {
        TryFlatMapResults {
            iter: Some(self),
            sub_iter: None,
            f,
        }
    }

    /// Similar to [try_flatten](IteratorExt::try_flatten), but flattens nested results.
    fn try_flatten_results<T, U, E>(self) -> TryFlattenResults<Self, T::IntoIter>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        T: IntoIterator<Item = Result<U, E>>,
    {
        TryFlattenResults {
            iter: Some(self),
            sub_iter: None,
        }
    }

    /// Creates a fallible iterator that filters items.
    fn try_filter<F, T, E>(self, f: F) -> TryFilter<Self, F>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(&T) -> Result<bool, E>,
    {
        TryFilter {
            iter: Some(self),
            f,
        }
    }

    /// Creates a fallible iterator that both filters and maps.
    fn try_filter_map<F, T, U, E>(self, f: F) -> TryFilterMap<Self, F>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(T) -> Result<Option<U>, E>,
    {
        TryFilterMap {
            iter: Some(self),
            f,
        }
    }

    /// Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a contained Err value, leaving an Ok value untouched.
    fn map_err<F, Ein, Eout>(self, f: F) -> MapErr<Self, F>
    where
        Self: Sized,
        F: FnMut(Ein) -> Eout,
    {
        MapErr { iter: self, f }
    }

    /// Takes a closure and creates a fallible iterator which calls that closure on each element.
    fn and_then<F, T, U, E>(self, f: F) -> AndThen<Self, F>
    where
        Self: Sized + Iterator<Item = Result<T, E>>,
        F: FnMut(T) -> Result<U, E>,
    {
        AndThen {
            iter: Some(self),
            f,
        }
    }
}

impl<I> IteratorExt for I where I: Iterator {}