diff --git a/core/src/upgrade/select.rs b/core/src/upgrade/select.rs index 1d5ab9abe53..c823d791cf7 100644 --- a/core/src/upgrade/select.rs +++ b/core/src/upgrade/select.rs @@ -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. @@ -48,16 +49,24 @@ where B: UpgradeInfo, { type Info = EitherName; - type InfoIter = InfoIterChain< - ::IntoIter, - ::IntoIter, + type InfoIter = Chain< + Map<::IntoIter, fn(A::Info) -> Self::Info>, + Map<::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) } } @@ -94,32 +103,3 @@ where } } } - -/// Iterator that combines the protocol names of twp upgrades. -#[derive(Debug, Clone)] -pub struct InfoIterChain(A, B); - -impl Iterator for InfoIterChain -where - A: Iterator, - B: Iterator, -{ - type Item = EitherName; - - fn next(&mut self) -> Option { - 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) { - 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) - } -}