]>
git.proxmox.com Git - rustc.git/blob - vendor/itertools/src/zip_longest.rs
1 use std
::cmp
::Ordering
::{Equal, Greater, Less}
;
5 use either_or_both
::EitherOrBoth
;
7 // ZipLongest originally written by SimonSapin,
8 // and dedicated to itertools https://github.com/rust-lang/rust/pull/19283
10 /// An iterator which iterates two other iterators simultaneously
12 /// This iterator is *fused*.
14 /// See [`.zip_longest()`](../trait.Itertools.html#method.zip_longest) for more information.
15 #[derive(Clone, Debug)]
16 #[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
17 pub struct ZipLongest
<T
, U
> {
22 /// Create a new `ZipLongest` iterator.
23 pub fn zip_longest
<T
, U
>(a
: T
, b
: U
) -> ZipLongest
<T
, U
>
33 impl<T
, U
> Iterator
for ZipLongest
<T
, U
>
37 type Item
= EitherOrBoth
<T
::Item
, U
::Item
>;
40 fn next(&mut self) -> Option
<Self::Item
> {
41 match (self.a
.next(), self.b
.next()) {
43 (Some(a
), None
) => Some(EitherOrBoth
::Left(a
)),
44 (None
, Some(b
)) => Some(EitherOrBoth
::Right(b
)),
45 (Some(a
), Some(b
)) => Some(EitherOrBoth
::Both(a
, b
)),
50 fn size_hint(&self) -> (usize, Option
<usize>) {
51 size_hint
::max(self.a
.size_hint(), self.b
.size_hint())
55 impl<T
, U
> DoubleEndedIterator
for ZipLongest
<T
, U
>
56 where T
: DoubleEndedIterator
+ ExactSizeIterator
,
57 U
: DoubleEndedIterator
+ ExactSizeIterator
60 fn next_back(&mut self) -> Option
<Self::Item
> {
61 match self.a
.len().cmp(&self.b
.len()) {
62 Equal
=> match (self.a
.next_back(), self.b
.next_back()) {
64 (Some(a
), Some(b
)) => Some(EitherOrBoth
::Both(a
, b
)),
65 // These can only happen if .len() is inconsistent with .next_back()
66 (Some(a
), None
) => Some(EitherOrBoth
::Left(a
)),
67 (None
, Some(b
)) => Some(EitherOrBoth
::Right(b
)),
69 Greater
=> self.a
.next_back().map(EitherOrBoth
::Left
),
70 Less
=> self.b
.next_back().map(EitherOrBoth
::Right
),
75 impl<T
, U
> ExactSizeIterator
for ZipLongest
<T
, U
>
76 where T
: ExactSizeIterator
,