@@ -2063,137 +2063,65 @@ impl<I: ExactSizeIterator, F> ExactSizeIterator for Inspect<I, F>
2063
2063
impl < I : FusedIterator , F > FusedIterator for Inspect < I , F >
2064
2064
where F : FnMut ( & I :: Item ) { }
2065
2065
2066
- /// An iterator adapter that produces output as long as the underlying
2067
- /// iterator produces `Option::Some` values.
2068
- pub ( crate ) struct OptionShunt < I > {
2069
- iter : I ,
2070
- exited_early : bool ,
2071
- }
2072
-
2073
- impl < I , T > OptionShunt < I >
2074
- where
2075
- I : Iterator < Item = Option < T > > ,
2076
- {
2077
- /// Process the given iterator as if it yielded a `T` instead of a
2078
- /// `Option<T>`. Any `None` value will stop the inner iterator and
2079
- /// the overall result will be a `None`.
2080
- pub fn process < F , U > ( iter : I , mut f : F ) -> Option < U >
2081
- where
2082
- F : FnMut ( & mut Self ) -> U ,
2083
- {
2084
- let mut shunt = OptionShunt :: new ( iter) ;
2085
- let value = f ( shunt. by_ref ( ) ) ;
2086
- shunt. reconstruct ( value)
2087
- }
2088
-
2089
- fn new ( iter : I ) -> Self {
2090
- OptionShunt {
2091
- iter,
2092
- exited_early : false ,
2093
- }
2094
- }
2095
-
2096
- /// Consume the adapter and rebuild a `Option` value.
2097
- fn reconstruct < U > ( self , val : U ) -> Option < U > {
2098
- if self . exited_early {
2099
- None
2100
- } else {
2101
- Some ( val)
2102
- }
2103
- }
2104
- }
2105
-
2106
- impl < I , T > Iterator for OptionShunt < I >
2107
- where
2108
- I : Iterator < Item = Option < T > > ,
2109
- {
2110
- type Item = T ;
2111
-
2112
- fn next ( & mut self ) -> Option < Self :: Item > {
2113
- match self . iter . next ( ) {
2114
- Some ( Some ( v) ) => Some ( v) ,
2115
- Some ( None ) => {
2116
- self . exited_early = true ;
2117
- None
2118
- }
2119
- None => None ,
2120
- }
2121
- }
2122
-
2123
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2124
- if self . exited_early {
2125
- ( 0 , Some ( 0 ) )
2126
- } else {
2127
- let ( _, upper) = self . iter . size_hint ( ) ;
2128
- ( 0 , upper)
2129
- }
2130
- }
2131
- }
2132
-
2133
2066
/// An iterator adapter that produces output as long as the underlying
2134
2067
/// iterator produces `Result::Ok` values.
2135
2068
///
2136
2069
/// If an error is encountered, the iterator stops and the error is
2137
- /// stored. The error may be recovered later via `reconstruct`.
2138
- pub ( crate ) struct ResultShunt < I , E > {
2070
+ /// stored.
2071
+ pub ( crate ) struct ResultShunt < ' a , I , E > {
2139
2072
iter : I ,
2140
- error : Option < E > ,
2073
+ error : & ' a mut Result < ( ) , E > ,
2141
2074
}
2142
2075
2143
- impl < I , T , E > ResultShunt < I , E >
2144
- where I : Iterator < Item = Result < T , E > >
2076
+ /// Process the given iterator as if it yielded a `T` instead of a
2077
+ /// `Result<T, _>`. Any errors will stop the inner iterator and
2078
+ /// the overall result will be an error.
2079
+ pub ( crate ) fn process_results < I , T , E , F , U > ( iter : I , mut f : F ) -> Result < U , E >
2080
+ where
2081
+ I : Iterator < Item = Result < T , E > > ,
2082
+ for < ' a > F : FnMut ( ResultShunt < ' a , I , E > ) -> U ,
2145
2083
{
2146
- /// Process the given iterator as if it yielded a `T` instead of a
2147
- /// `Result<T, _>`. Any errors will stop the inner iterator and
2148
- /// the overall result will be an error.
2149
- pub fn process < F , U > ( iter : I , mut f : F ) -> Result < U , E >
2150
- where F : FnMut ( & mut Self ) -> U
2151
- {
2152
- let mut shunt = ResultShunt :: new ( iter) ;
2153
- let value = f ( shunt. by_ref ( ) ) ;
2154
- shunt. reconstruct ( value)
2155
- }
2156
-
2157
- fn new ( iter : I ) -> Self {
2158
- ResultShunt {
2159
- iter,
2160
- error : None ,
2161
- }
2162
- }
2163
-
2164
- /// Consume the adapter and rebuild a `Result` value. This should
2165
- /// *always* be called, otherwise any potential error would be
2166
- /// lost.
2167
- fn reconstruct < U > ( self , val : U ) -> Result < U , E > {
2168
- match self . error {
2169
- None => Ok ( val) ,
2170
- Some ( e) => Err ( e) ,
2171
- }
2172
- }
2084
+ let mut error = Ok ( ( ) ) ;
2085
+ let shunt = ResultShunt {
2086
+ iter,
2087
+ error : & mut error,
2088
+ } ;
2089
+ let value = f ( shunt) ;
2090
+ error. map ( |( ) | value)
2173
2091
}
2174
2092
2175
- impl < I , T , E > Iterator for ResultShunt < I , E >
2093
+ impl < I , T , E > Iterator for ResultShunt < ' _ , I , E >
2176
2094
where I : Iterator < Item = Result < T , E > >
2177
2095
{
2178
2096
type Item = T ;
2179
2097
2180
2098
fn next ( & mut self ) -> Option < Self :: Item > {
2181
- match self . iter . next ( ) {
2182
- Some ( Ok ( v) ) => Some ( v) ,
2183
- Some ( Err ( e) ) => {
2184
- self . error = Some ( e) ;
2185
- None
2186
- }
2187
- None => None ,
2188
- }
2099
+ self . find ( |_| true )
2189
2100
}
2190
2101
2191
2102
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2192
- if self . error . is_some ( ) {
2103
+ if self . error . is_err ( ) {
2193
2104
( 0 , Some ( 0 ) )
2194
2105
} else {
2195
2106
let ( _, upper) = self . iter . size_hint ( ) ;
2196
2107
( 0 , upper)
2197
2108
}
2198
2109
}
2110
+
2111
+ fn try_fold < B , F , R > ( & mut self , init : B , mut f : F ) -> R
2112
+ where
2113
+ F : FnMut ( B , Self :: Item ) -> R ,
2114
+ R : Try < Ok = B > ,
2115
+ {
2116
+ let error = & mut * self . error ;
2117
+ self . iter
2118
+ . try_fold ( init, |acc, x| match x {
2119
+ Ok ( x) => LoopState :: from_try ( f ( acc, x) ) ,
2120
+ Err ( e) => {
2121
+ * error = Err ( e) ;
2122
+ LoopState :: Break ( Try :: from_ok ( acc) )
2123
+ }
2124
+ } )
2125
+ . into_try ( )
2126
+ }
2199
2127
}
0 commit comments