From 73cbbe29679f5d56d81d5477d3796e82712f9ac2 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 18 Jan 2023 21:17:18 +1100 Subject: [PATCH] refactor(core)!: remove `EitherFuture2` (#3340) We can completely replace `EitherFuture2` with `EitherFuture`. `EitherFuture` itself cannot be removed for now because the `Future` implementation on `future::Either` forces both `Future`s to evaluate to the same type. --- core/CHANGELOG.md | 3 +++ core/src/either.rs | 27 --------------------------- core/src/upgrade/either.rs | 14 +++++++------- core/src/upgrade/select.rs | 15 ++++++++------- 4 files changed, 18 insertions(+), 41 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 83588698..f9c45f5f 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -25,6 +25,8 @@ - Remove `EitherUpgrade` in favor of implementing `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`. See [PR 3339]. +- Remove `EitherFuture2` in favor of `EitherFuture`. See [PR 3340]. + [PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031 [PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058 [PR 3097]: https://github.com/libp2p/rust-libp2p/pull/3097 @@ -33,6 +35,7 @@ [PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337 [PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338 [PR 3339]: https://github.com/libp2p/rust-libp2p/pull/3339 +[PR 3340]: https://github.com/libp2p/rust-libp2p/pull/3340 # 0.37.0 diff --git a/core/src/either.rs b/core/src/either.rs index 5c2ca8f3..b25e4e12 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -252,33 +252,6 @@ where } } -#[pin_project(project = EitherFuture2Proj)] -#[derive(Debug, Copy, Clone)] -#[must_use = "futures do nothing unless polled"] -pub enum EitherFuture2 { - A(#[pin] A), - B(#[pin] B), -} - -impl Future for EitherFuture2 -where - AFut: TryFuture, - BFut: TryFuture, -{ - type Output = Result, Either>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - match self.project() { - EitherFuture2Proj::A(a) => TryFuture::try_poll(a, cx) - .map_ok(EitherOutput::First) - .map_err(Either::Left), - EitherFuture2Proj::B(a) => TryFuture::try_poll(a, cx) - .map_ok(EitherOutput::Second) - .map_err(Either::Right), - } - } -} - #[derive(Debug, Clone)] pub enum EitherName { A(A), diff --git a/core/src/upgrade/either.rs b/core/src/upgrade/either.rs index 9f4c3f7a..5785fda2 100644 --- a/core/src/upgrade/either.rs +++ b/core/src/upgrade/either.rs @@ -19,7 +19,7 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherFuture2, EitherName, EitherOutput}, + either::{EitherFuture, EitherName, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; use either::Either; @@ -50,15 +50,15 @@ where { type Output = EitherOutput; type Error = Either; - type Future = EitherFuture2; + type Future = EitherFuture; fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { (Either::Left(a), EitherName::A(info)) => { - EitherFuture2::A(a.upgrade_inbound(sock, info)) + EitherFuture::First(a.upgrade_inbound(sock, info)) } (Either::Right(b), EitherName::B(info)) => { - EitherFuture2::B(b.upgrade_inbound(sock, info)) + EitherFuture::Second(b.upgrade_inbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"), } @@ -72,15 +72,15 @@ where { type Output = EitherOutput; type Error = Either; - type Future = EitherFuture2; + type Future = EitherFuture; fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { (Either::Left(a), EitherName::A(info)) => { - EitherFuture2::A(a.upgrade_outbound(sock, info)) + EitherFuture::First(a.upgrade_outbound(sock, info)) } (Either::Right(b), EitherName::B(info)) => { - EitherFuture2::B(b.upgrade_outbound(sock, info)) + EitherFuture::Second(b.upgrade_outbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"), } diff --git a/core/src/upgrade/select.rs b/core/src/upgrade/select.rs index fed93bc0..cb3cdc23 100644 --- a/core/src/upgrade/select.rs +++ b/core/src/upgrade/select.rs @@ -18,8 +18,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +use crate::either::EitherFuture; use crate::{ - either::{EitherFuture2, EitherName, EitherOutput}, + either::{EitherName, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; use either::Either; @@ -66,12 +67,12 @@ where { type Output = EitherOutput; type Error = Either; - type Future = EitherFuture2; + type Future = EitherFuture; fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { match info { - EitherName::A(info) => EitherFuture2::A(self.0.upgrade_inbound(sock, info)), - EitherName::B(info) => EitherFuture2::B(self.1.upgrade_inbound(sock, info)), + EitherName::A(info) => EitherFuture::First(self.0.upgrade_inbound(sock, info)), + EitherName::B(info) => EitherFuture::Second(self.1.upgrade_inbound(sock, info)), } } } @@ -83,12 +84,12 @@ where { type Output = EitherOutput; type Error = Either; - type Future = EitherFuture2; + type Future = EitherFuture; fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { match info { - EitherName::A(info) => EitherFuture2::A(self.0.upgrade_outbound(sock, info)), - EitherName::B(info) => EitherFuture2::B(self.1.upgrade_outbound(sock, info)), + EitherName::A(info) => EitherFuture::First(self.0.upgrade_outbound(sock, info)), + EitherName::B(info) => EitherFuture::Second(self.1.upgrade_outbound(sock, info)), } } }