Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 16 additions & 36 deletions core/src/upgrade/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
};
use either::Either;
use futures::future;
use std::iter::{Chain, Map};

/// Upgrade that combines two upgrades into one. Supports all the protocols supported by either
/// sub-upgrade.
Expand All @@ -48,16 +49,24 @@ where
B: UpgradeInfo,
{
type Info = EitherName<A::Info, B::Info>;
type InfoIter = InfoIterChain<
<A::InfoIter as IntoIterator>::IntoIter,
<B::InfoIter as IntoIterator>::IntoIter,
type InfoIter = Chain<
Map<<A::InfoIter as IntoIterator>::IntoIter, fn(A::Info) -> Self::Info>,
Map<<B::InfoIter as IntoIterator>::IntoIter, fn(B::Info) -> Self::Info>,
>;

fn protocol_info(&self) -> Self::InfoIter {
InfoIterChain(
self.0.protocol_info().into_iter(),
self.1.protocol_info().into_iter(),
)
let a = self
.0
.protocol_info()
.into_iter()
.map(EitherName::A as fn(A::Info) -> _);
let b = self
.1
.protocol_info()
.into_iter()
.map(EitherName::B as fn(B::Info) -> _);

a.chain(b)
}
}

Expand Down Expand Up @@ -94,32 +103,3 @@ where
}
}
}

/// Iterator that combines the protocol names of twp upgrades.
#[derive(Debug, Clone)]
pub struct InfoIterChain<A, B>(A, B);

impl<A, B> Iterator for InfoIterChain<A, B>
where
A: Iterator,
B: Iterator,
{
type Item = EitherName<A::Item, B::Item>;

fn next(&mut self) -> Option<Self::Item> {
if let Some(info) = self.0.next() {
return Some(EitherName::A(info));
}
if let Some(info) = self.1.next() {
return Some(EitherName::B(info));
}
None
}

fn size_hint(&self) -> (usize, Option<usize>) {
let (min1, max1) = self.0.size_hint();
let (min2, max2) = self.1.size_hint();
let max = max1.and_then(move |m1| max2.and_then(move |m2| m1.checked_add(m2)));
(min1.saturating_add(min2), max)
}
}