1 use super::{for_both, Either, Left, Right}; 2 use core::iter; 3 4 macro_rules! wrap_either { 5 ($value:expr => $( $tail:tt )*) => { 6 match $value { 7 Left(inner) => inner.map(Left) $($tail)*, 8 Right(inner) => inner.map(Right) $($tail)*, 9 } 10 }; 11 } 12 13 /// Iterator that maps left or right iterators to corresponding `Either`-wrapped items. 14 /// 15 /// This struct is created by the [`Either::factor_into_iter`], 16 /// [`factor_iter`][Either::factor_iter], 17 /// and [`factor_iter_mut`][Either::factor_iter_mut] methods. 18 #[derive(Clone, Debug)] 19 pub struct IterEither<L, R> { 20 inner: Either<L, R>, 21 } 22 23 impl<L, R> IterEither<L, R> { new(inner: Either<L, R>) -> Self24 pub(crate) fn new(inner: Either<L, R>) -> Self { 25 IterEither { inner } 26 } 27 } 28 29 impl<L, R, A> Extend<A> for Either<L, R> 30 where 31 L: Extend<A>, 32 R: Extend<A>, 33 { extend<T>(&mut self, iter: T) where T: IntoIterator<Item = A>,34 fn extend<T>(&mut self, iter: T) 35 where 36 T: IntoIterator<Item = A>, 37 { 38 for_both!(*self, ref mut inner => inner.extend(iter)) 39 } 40 } 41 42 /// `Either<L, R>` is an iterator if both `L` and `R` are iterators. 43 impl<L, R> Iterator for Either<L, R> 44 where 45 L: Iterator, 46 R: Iterator<Item = L::Item>, 47 { 48 type Item = L::Item; 49 next(&mut self) -> Option<Self::Item>50 fn next(&mut self) -> Option<Self::Item> { 51 for_both!(*self, ref mut inner => inner.next()) 52 } 53 size_hint(&self) -> (usize, Option<usize>)54 fn size_hint(&self) -> (usize, Option<usize>) { 55 for_both!(*self, ref inner => inner.size_hint()) 56 } 57 fold<Acc, G>(self, init: Acc, f: G) -> Acc where G: FnMut(Acc, Self::Item) -> Acc,58 fn fold<Acc, G>(self, init: Acc, f: G) -> Acc 59 where 60 G: FnMut(Acc, Self::Item) -> Acc, 61 { 62 for_both!(self, inner => inner.fold(init, f)) 63 } 64 for_each<F>(self, f: F) where F: FnMut(Self::Item),65 fn for_each<F>(self, f: F) 66 where 67 F: FnMut(Self::Item), 68 { 69 for_both!(self, inner => inner.for_each(f)) 70 } 71 count(self) -> usize72 fn count(self) -> usize { 73 for_both!(self, inner => inner.count()) 74 } 75 last(self) -> Option<Self::Item>76 fn last(self) -> Option<Self::Item> { 77 for_both!(self, inner => inner.last()) 78 } 79 nth(&mut self, n: usize) -> Option<Self::Item>80 fn nth(&mut self, n: usize) -> Option<Self::Item> { 81 for_both!(*self, ref mut inner => inner.nth(n)) 82 } 83 collect<B>(self) -> B where B: iter::FromIterator<Self::Item>,84 fn collect<B>(self) -> B 85 where 86 B: iter::FromIterator<Self::Item>, 87 { 88 for_both!(self, inner => inner.collect()) 89 } 90 partition<B, F>(self, f: F) -> (B, B) where B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool,91 fn partition<B, F>(self, f: F) -> (B, B) 92 where 93 B: Default + Extend<Self::Item>, 94 F: FnMut(&Self::Item) -> bool, 95 { 96 for_both!(self, inner => inner.partition(f)) 97 } 98 all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool,99 fn all<F>(&mut self, f: F) -> bool 100 where 101 F: FnMut(Self::Item) -> bool, 102 { 103 for_both!(*self, ref mut inner => inner.all(f)) 104 } 105 any<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool,106 fn any<F>(&mut self, f: F) -> bool 107 where 108 F: FnMut(Self::Item) -> bool, 109 { 110 for_both!(*self, ref mut inner => inner.any(f)) 111 } 112 find<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> bool,113 fn find<P>(&mut self, predicate: P) -> Option<Self::Item> 114 where 115 P: FnMut(&Self::Item) -> bool, 116 { 117 for_both!(*self, ref mut inner => inner.find(predicate)) 118 } 119 find_map<B, F>(&mut self, f: F) -> Option<B> where F: FnMut(Self::Item) -> Option<B>,120 fn find_map<B, F>(&mut self, f: F) -> Option<B> 121 where 122 F: FnMut(Self::Item) -> Option<B>, 123 { 124 for_both!(*self, ref mut inner => inner.find_map(f)) 125 } 126 position<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool,127 fn position<P>(&mut self, predicate: P) -> Option<usize> 128 where 129 P: FnMut(Self::Item) -> bool, 130 { 131 for_both!(*self, ref mut inner => inner.position(predicate)) 132 } 133 } 134 135 impl<L, R> DoubleEndedIterator for Either<L, R> 136 where 137 L: DoubleEndedIterator, 138 R: DoubleEndedIterator<Item = L::Item>, 139 { next_back(&mut self) -> Option<Self::Item>140 fn next_back(&mut self) -> Option<Self::Item> { 141 for_both!(*self, ref mut inner => inner.next_back()) 142 } 143 nth_back(&mut self, n: usize) -> Option<Self::Item>144 fn nth_back(&mut self, n: usize) -> Option<Self::Item> { 145 for_both!(*self, ref mut inner => inner.nth_back(n)) 146 } 147 rfold<Acc, G>(self, init: Acc, f: G) -> Acc where G: FnMut(Acc, Self::Item) -> Acc,148 fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc 149 where 150 G: FnMut(Acc, Self::Item) -> Acc, 151 { 152 for_both!(self, inner => inner.rfold(init, f)) 153 } 154 rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> bool,155 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> 156 where 157 P: FnMut(&Self::Item) -> bool, 158 { 159 for_both!(*self, ref mut inner => inner.rfind(predicate)) 160 } 161 } 162 163 impl<L, R> ExactSizeIterator for Either<L, R> 164 where 165 L: ExactSizeIterator, 166 R: ExactSizeIterator<Item = L::Item>, 167 { len(&self) -> usize168 fn len(&self) -> usize { 169 for_both!(*self, ref inner => inner.len()) 170 } 171 } 172 173 impl<L, R> iter::FusedIterator for Either<L, R> 174 where 175 L: iter::FusedIterator, 176 R: iter::FusedIterator<Item = L::Item>, 177 { 178 } 179 180 impl<L, R> Iterator for IterEither<L, R> 181 where 182 L: Iterator, 183 R: Iterator, 184 { 185 type Item = Either<L::Item, R::Item>; 186 next(&mut self) -> Option<Self::Item>187 fn next(&mut self) -> Option<Self::Item> { 188 Some(map_either!(self.inner, ref mut inner => inner.next()?)) 189 } 190 size_hint(&self) -> (usize, Option<usize>)191 fn size_hint(&self) -> (usize, Option<usize>) { 192 for_both!(self.inner, ref inner => inner.size_hint()) 193 } 194 fold<Acc, G>(self, init: Acc, f: G) -> Acc where G: FnMut(Acc, Self::Item) -> Acc,195 fn fold<Acc, G>(self, init: Acc, f: G) -> Acc 196 where 197 G: FnMut(Acc, Self::Item) -> Acc, 198 { 199 wrap_either!(self.inner => .fold(init, f)) 200 } 201 for_each<F>(self, f: F) where F: FnMut(Self::Item),202 fn for_each<F>(self, f: F) 203 where 204 F: FnMut(Self::Item), 205 { 206 wrap_either!(self.inner => .for_each(f)) 207 } 208 count(self) -> usize209 fn count(self) -> usize { 210 for_both!(self.inner, inner => inner.count()) 211 } 212 last(self) -> Option<Self::Item>213 fn last(self) -> Option<Self::Item> { 214 Some(map_either!(self.inner, inner => inner.last()?)) 215 } 216 nth(&mut self, n: usize) -> Option<Self::Item>217 fn nth(&mut self, n: usize) -> Option<Self::Item> { 218 Some(map_either!(self.inner, ref mut inner => inner.nth(n)?)) 219 } 220 collect<B>(self) -> B where B: iter::FromIterator<Self::Item>,221 fn collect<B>(self) -> B 222 where 223 B: iter::FromIterator<Self::Item>, 224 { 225 wrap_either!(self.inner => .collect()) 226 } 227 partition<B, F>(self, f: F) -> (B, B) where B: Default + Extend<Self::Item>, F: FnMut(&Self::Item) -> bool,228 fn partition<B, F>(self, f: F) -> (B, B) 229 where 230 B: Default + Extend<Self::Item>, 231 F: FnMut(&Self::Item) -> bool, 232 { 233 wrap_either!(self.inner => .partition(f)) 234 } 235 all<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool,236 fn all<F>(&mut self, f: F) -> bool 237 where 238 F: FnMut(Self::Item) -> bool, 239 { 240 wrap_either!(&mut self.inner => .all(f)) 241 } 242 any<F>(&mut self, f: F) -> bool where F: FnMut(Self::Item) -> bool,243 fn any<F>(&mut self, f: F) -> bool 244 where 245 F: FnMut(Self::Item) -> bool, 246 { 247 wrap_either!(&mut self.inner => .any(f)) 248 } 249 find<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> bool,250 fn find<P>(&mut self, predicate: P) -> Option<Self::Item> 251 where 252 P: FnMut(&Self::Item) -> bool, 253 { 254 wrap_either!(&mut self.inner => .find(predicate)) 255 } 256 find_map<B, F>(&mut self, f: F) -> Option<B> where F: FnMut(Self::Item) -> Option<B>,257 fn find_map<B, F>(&mut self, f: F) -> Option<B> 258 where 259 F: FnMut(Self::Item) -> Option<B>, 260 { 261 wrap_either!(&mut self.inner => .find_map(f)) 262 } 263 position<P>(&mut self, predicate: P) -> Option<usize> where P: FnMut(Self::Item) -> bool,264 fn position<P>(&mut self, predicate: P) -> Option<usize> 265 where 266 P: FnMut(Self::Item) -> bool, 267 { 268 wrap_either!(&mut self.inner => .position(predicate)) 269 } 270 } 271 272 impl<L, R> DoubleEndedIterator for IterEither<L, R> 273 where 274 L: DoubleEndedIterator, 275 R: DoubleEndedIterator, 276 { next_back(&mut self) -> Option<Self::Item>277 fn next_back(&mut self) -> Option<Self::Item> { 278 Some(map_either!(self.inner, ref mut inner => inner.next_back()?)) 279 } 280 nth_back(&mut self, n: usize) -> Option<Self::Item>281 fn nth_back(&mut self, n: usize) -> Option<Self::Item> { 282 Some(map_either!(self.inner, ref mut inner => inner.nth_back(n)?)) 283 } 284 rfold<Acc, G>(self, init: Acc, f: G) -> Acc where G: FnMut(Acc, Self::Item) -> Acc,285 fn rfold<Acc, G>(self, init: Acc, f: G) -> Acc 286 where 287 G: FnMut(Acc, Self::Item) -> Acc, 288 { 289 wrap_either!(self.inner => .rfold(init, f)) 290 } 291 rfind<P>(&mut self, predicate: P) -> Option<Self::Item> where P: FnMut(&Self::Item) -> bool,292 fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item> 293 where 294 P: FnMut(&Self::Item) -> bool, 295 { 296 wrap_either!(&mut self.inner => .rfind(predicate)) 297 } 298 } 299 300 impl<L, R> ExactSizeIterator for IterEither<L, R> 301 where 302 L: ExactSizeIterator, 303 R: ExactSizeIterator, 304 { len(&self) -> usize305 fn len(&self) -> usize { 306 for_both!(self.inner, ref inner => inner.len()) 307 } 308 } 309 310 impl<L, R> iter::FusedIterator for IterEither<L, R> 311 where 312 L: iter::FusedIterator, 313 R: iter::FusedIterator, 314 { 315 } 316