refactor(core): remove InfoIterChain

This type can be replaced with std-lib components.

Related: https://github.com/libp2p/rust-libp2p/pull/3746.

Pull-Request: #3842.
This commit is contained in:
Thomas Eizinger 2023-04-27 14:50:06 +02:00 committed by GitHub
parent 31dbcabe7c
commit fadcc43e69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,6 +25,7 @@ use crate::{
}; };
use either::Either; use either::Either;
use futures::future; use futures::future;
use std::iter::{Chain, Map};
/// Upgrade that combines two upgrades into one. Supports all the protocols supported by either /// Upgrade that combines two upgrades into one. Supports all the protocols supported by either
/// sub-upgrade. /// sub-upgrade.
@ -48,16 +49,24 @@ where
B: UpgradeInfo, B: UpgradeInfo,
{ {
type Info = EitherName<A::Info, B::Info>; type Info = EitherName<A::Info, B::Info>;
type InfoIter = InfoIterChain< type InfoIter = Chain<
<A::InfoIter as IntoIterator>::IntoIter, Map<<A::InfoIter as IntoIterator>::IntoIter, fn(A::Info) -> Self::Info>,
<B::InfoIter as IntoIterator>::IntoIter, Map<<B::InfoIter as IntoIterator>::IntoIter, fn(B::Info) -> Self::Info>,
>; >;
fn protocol_info(&self) -> Self::InfoIter { fn protocol_info(&self) -> Self::InfoIter {
InfoIterChain( let a = self
self.0.protocol_info().into_iter(), .0
self.1.protocol_info().into_iter(), .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>(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)
}
}