From 14b91f3b8eb97e627253d9e5bd4d07127f9c321e Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 13 Aug 2021 19:32:41 +0200 Subject: [PATCH 1/4] swarm/src/protocols_handler: Add EitherHandler Add implementation of a ProtocolsHandler that represents either of two ProtocolsHandler implementations. --- swarm/src/protocols_handler.rs | 3 +- swarm/src/protocols_handler/either.rs | 329 ++++++++++++++++++++++++++ 2 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 swarm/src/protocols_handler/either.rs diff --git a/swarm/src/protocols_handler.rs b/swarm/src/protocols_handler.rs index 911693f32af..208d3ce6e13 100644 --- a/swarm/src/protocols_handler.rs +++ b/swarm/src/protocols_handler.rs @@ -38,6 +38,7 @@ //! > the network as a whole, see the `NetworkBehaviour` trait. mod dummy; +pub mod either; mod map_in; mod map_out; pub mod multi; @@ -45,7 +46,7 @@ mod node_handler; mod one_shot; mod select; -pub use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, UpgradeInfoSend}; +pub use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper, UpgradeInfoSend}; use libp2p_core::{upgrade::UpgradeError, ConnectedPoint, Multiaddr, PeerId}; use std::{cmp::Ordering, error, fmt, task::Context, task::Poll, time::Duration}; diff --git a/swarm/src/protocols_handler/either.rs b/swarm/src/protocols_handler/either.rs new file mode 100644 index 00000000000..b876e8d5746 --- /dev/null +++ b/swarm/src/protocols_handler/either.rs @@ -0,0 +1,329 @@ +// Copyright 2021 Protocol Labs. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +use crate::protocols_handler::{ + IntoProtocolsHandler, KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, + ProtocolsHandlerUpgrErr, SubstreamProtocol, +}; +use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper}; +use either::Either; +use libp2p_core::either::{EitherError, EitherOutput}; +use libp2p_core::upgrade::{EitherUpgrade, UpgradeError}; +use libp2p_core::{ConnectedPoint, Multiaddr, PeerId}; +use std::task::{Context, Poll}; + +pub enum IntoEitherHandler { + A(A), + B(B), +} + +/// Implementation of a [`IntoProtocolsHandler`] that represents either of two [`IntoProtocolsHandler`] +/// implementations. +impl IntoProtocolsHandler for IntoEitherHandler +where + A: IntoProtocolsHandler, + B: IntoProtocolsHandler, +{ + type Handler = EitherHandler; + + fn into_handler(self, p: &PeerId, c: &ConnectedPoint) -> Self::Handler { + match self { + IntoEitherHandler::A(into_handler) => EitherHandler::A(into_handler.into_handler(p, c)), + IntoEitherHandler::B(into_handler) => EitherHandler::B(into_handler.into_handler(p, c)), + } + } + + fn inbound_protocol(&self) -> ::InboundProtocol { + match self { + IntoEitherHandler::A(into_handler) => { + EitherUpgrade::A(SendWrapper(into_handler.inbound_protocol())) + } + IntoEitherHandler::B(into_handler) => { + EitherUpgrade::B(SendWrapper(into_handler.inbound_protocol())) + } + } + } +} + +/// Implementation of a [`ProtocolsHandler`] that represents either of two [`ProtocolsHandler`] +/// implementations. +#[derive(Debug)] +pub enum EitherHandler { + A(A), + B(B), +} + +impl ProtocolsHandler for EitherHandler +where + A: ProtocolsHandler, + B: ProtocolsHandler, +{ + type InEvent = Either; + type OutEvent = Either; + type Error = Either; + type InboundProtocol = + EitherUpgrade, SendWrapper>; + type OutboundProtocol = + EitherUpgrade, SendWrapper>; + type InboundOpenInfo = Either; + type OutboundOpenInfo = Either; + + fn listen_protocol(&self) -> SubstreamProtocol { + match self { + EitherHandler::A(a) => a + .listen_protocol() + .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) + .map_info(Either::Left), + EitherHandler::B(b) => b + .listen_protocol() + .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) + .map_info(Either::Right), + } + } + + fn inject_fully_negotiated_outbound( + &mut self, + output: ::Output, + info: Self::OutboundOpenInfo, + ) { + match (self, output, info) { + (EitherHandler::A(handler), EitherOutput::First(output), Either::Left(info)) => { + handler.inject_fully_negotiated_outbound(output, info) + } + (EitherHandler::B(handler), EitherOutput::Second(output), Either::Right(info)) => { + handler.inject_fully_negotiated_outbound(output, info) + } + _ => unreachable!(), + } + } + + fn inject_fully_negotiated_inbound( + &mut self, + output: ::Output, + info: Self::InboundOpenInfo, + ) { + match (self, output, info) { + (EitherHandler::A(handler), EitherOutput::First(output), Either::Left(info)) => { + handler.inject_fully_negotiated_inbound(output, info) + } + (EitherHandler::B(handler), EitherOutput::Second(output), Either::Right(info)) => { + handler.inject_fully_negotiated_inbound(output, info) + } + _ => unreachable!(), + } + } + + fn inject_event(&mut self, event: Self::InEvent) { + match (self, event) { + (EitherHandler::A(handler), Either::Left(event)) => handler.inject_event(event), + (EitherHandler::B(handler), Either::Right(event)) => handler.inject_event(event), + _ => unreachable!(), + } + } + + fn inject_address_change(&mut self, addr: &Multiaddr) { + match self { + EitherHandler::A(handler) => handler.inject_address_change(addr), + EitherHandler::B(handler) => handler.inject_address_change(addr), + } + } + + fn inject_dial_upgrade_error( + &mut self, + info: Self::OutboundOpenInfo, + error: ProtocolsHandlerUpgrErr<::Error>, + ) { + match error { + ProtocolsHandlerUpgrErr::Timer => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Timeout => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_dial_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), + ); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_dial_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), + ); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => { + match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_dial_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), + ); + } + _ => unreachable!(), + } + } + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => { + match (self, info) { + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_dial_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), + ); + } + _ => unreachable!(), + } + } + } + } + + fn inject_listen_upgrade_error( + &mut self, + info: Self::InboundOpenInfo, + error: ProtocolsHandlerUpgrErr<::Error>, + ) { + match error { + ProtocolsHandlerUpgrErr::Timer => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Timeout => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_listen_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), + ); + } + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_listen_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), + ); + } + _ => unreachable!(), + }, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => { + match (self, info) { + (EitherHandler::A(handler), Either::Left(info)) => { + handler.inject_listen_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), + ); + } + _ => unreachable!(), + } + } + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => { + match (self, info) { + (EitherHandler::B(handler), Either::Right(info)) => { + handler.inject_listen_upgrade_error( + info, + ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), + ); + } + _ => unreachable!(), + } + } + } + } + + fn connection_keep_alive(&self) -> KeepAlive { + match self { + EitherHandler::A(handler) => handler.connection_keep_alive(), + EitherHandler::B(handler) => handler.connection_keep_alive(), + } + } + + fn poll( + &mut self, + cx: &mut Context<'_>, + ) -> Poll< + ProtocolsHandlerEvent< + Self::OutboundProtocol, + Self::OutboundOpenInfo, + Self::OutEvent, + Self::Error, + >, + > { + match self { + EitherHandler::A(handler) => match handler.poll(cx) { + Poll::Ready(ProtocolsHandlerEvent::Custom(event)) => { + return Poll::Ready(ProtocolsHandlerEvent::Custom(Either::Left(event))); + } + Poll::Ready(ProtocolsHandlerEvent::Close(event)) => { + return Poll::Ready(ProtocolsHandlerEvent::Close(Either::Left(event))); + } + Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }) => { + return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { + protocol: protocol + .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) + .map_info(Either::Left), + }); + } + Poll::Pending => Poll::Pending, + }, + EitherHandler::B(handler) => match handler.poll(cx) { + Poll::Ready(ProtocolsHandlerEvent::Custom(event)) => { + return Poll::Ready(ProtocolsHandlerEvent::Custom(Either::Right(event))); + } + Poll::Ready(ProtocolsHandlerEvent::Close(event)) => { + return Poll::Ready(ProtocolsHandlerEvent::Close(Either::Right(event))); + } + Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }) => { + return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { + protocol: protocol + .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) + .map_info(Either::Right), + }); + } + Poll::Pending => Poll::Pending, + }, + } + } +} From 1c83a5b215dac4d60090082bac5cdc1c2d0cc73d Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 17 Aug 2021 13:04:05 +0200 Subject: [PATCH 2/4] swarm/src/protocols_handler/either.rs: Use map fns Co-authored-by: Thomas Eizinger --- swarm/src/protocols_handler/either.rs | 48 ++++++++------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/swarm/src/protocols_handler/either.rs b/swarm/src/protocols_handler/either.rs index b876e8d5746..c068ce1b052 100644 --- a/swarm/src/protocols_handler/either.rs +++ b/swarm/src/protocols_handler/either.rs @@ -291,39 +291,19 @@ where Self::Error, >, > { - match self { - EitherHandler::A(handler) => match handler.poll(cx) { - Poll::Ready(ProtocolsHandlerEvent::Custom(event)) => { - return Poll::Ready(ProtocolsHandlerEvent::Custom(Either::Left(event))); - } - Poll::Ready(ProtocolsHandlerEvent::Close(event)) => { - return Poll::Ready(ProtocolsHandlerEvent::Close(Either::Left(event))); - } - Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }) => { - return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { - protocol: protocol - .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) - .map_info(Either::Left), - }); - } - Poll::Pending => Poll::Pending, - }, - EitherHandler::B(handler) => match handler.poll(cx) { - Poll::Ready(ProtocolsHandlerEvent::Custom(event)) => { - return Poll::Ready(ProtocolsHandlerEvent::Custom(Either::Right(event))); - } - Poll::Ready(ProtocolsHandlerEvent::Close(event)) => { - return Poll::Ready(ProtocolsHandlerEvent::Close(Either::Right(event))); - } - Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { protocol }) => { - return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest { - protocol: protocol - .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) - .map_info(Either::Right), - }); - } - Poll::Pending => Poll::Pending, - }, - } + let event = match self { + EitherHandler::A(handler) => futures::ready!(handler.poll(cx)) + .map_custom(Either::Left) + .map_close(Either::Left) + .map_protocol(|p| EitherUpgrade::A(SendWrapper(p))) + .map_outbound_open_info(Either::Left), + EitherHandler::B(handler) => futures::ready!(handler.poll(cx)) + .map_custom(Either::Right) + .map_close(Either::Right) + .map_protocol(|p| EitherUpgrade::B(SendWrapper(p))) + .map_outbound_open_info(Either::Right), + }; + + Poll::Ready(event) } } From 26063285effb784effa4da6d4631025a75027b10 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 17 Aug 2021 13:17:59 +0200 Subject: [PATCH 3/4] swarm/src/protocols_handler/either: Implement on either::Either --- swarm/src/protocols_handler/either.rs | 110 +++++++++++++------------- 1 file changed, 53 insertions(+), 57 deletions(-) diff --git a/swarm/src/protocols_handler/either.rs b/swarm/src/protocols_handler/either.rs index c068ce1b052..c51baadd2d7 100644 --- a/swarm/src/protocols_handler/either.rs +++ b/swarm/src/protocols_handler/either.rs @@ -29,33 +29,35 @@ use libp2p_core::upgrade::{EitherUpgrade, UpgradeError}; use libp2p_core::{ConnectedPoint, Multiaddr, PeerId}; use std::task::{Context, Poll}; -pub enum IntoEitherHandler { - A(A), - B(B), +pub enum IntoEitherHandler { + Left(L), + Right(R), } /// Implementation of a [`IntoProtocolsHandler`] that represents either of two [`IntoProtocolsHandler`] /// implementations. -impl IntoProtocolsHandler for IntoEitherHandler +impl IntoProtocolsHandler for IntoEitherHandler where - A: IntoProtocolsHandler, - B: IntoProtocolsHandler, + L: IntoProtocolsHandler, + R: IntoProtocolsHandler, { - type Handler = EitherHandler; + type Handler = Either; fn into_handler(self, p: &PeerId, c: &ConnectedPoint) -> Self::Handler { match self { - IntoEitherHandler::A(into_handler) => EitherHandler::A(into_handler.into_handler(p, c)), - IntoEitherHandler::B(into_handler) => EitherHandler::B(into_handler.into_handler(p, c)), + IntoEitherHandler::Left(into_handler) => Either::Left(into_handler.into_handler(p, c)), + IntoEitherHandler::Right(into_handler) => { + Either::Right(into_handler.into_handler(p, c)) + } } } fn inbound_protocol(&self) -> ::InboundProtocol { match self { - IntoEitherHandler::A(into_handler) => { + IntoEitherHandler::Left(into_handler) => { EitherUpgrade::A(SendWrapper(into_handler.inbound_protocol())) } - IntoEitherHandler::B(into_handler) => { + IntoEitherHandler::Right(into_handler) => { EitherUpgrade::B(SendWrapper(into_handler.inbound_protocol())) } } @@ -64,34 +66,28 @@ where /// Implementation of a [`ProtocolsHandler`] that represents either of two [`ProtocolsHandler`] /// implementations. -#[derive(Debug)] -pub enum EitherHandler { - A(A), - B(B), -} - -impl ProtocolsHandler for EitherHandler +impl ProtocolsHandler for Either where - A: ProtocolsHandler, - B: ProtocolsHandler, + L: ProtocolsHandler, + R: ProtocolsHandler, { - type InEvent = Either; - type OutEvent = Either; - type Error = Either; + type InEvent = Either; + type OutEvent = Either; + type Error = Either; type InboundProtocol = - EitherUpgrade, SendWrapper>; + EitherUpgrade, SendWrapper>; type OutboundProtocol = - EitherUpgrade, SendWrapper>; - type InboundOpenInfo = Either; - type OutboundOpenInfo = Either; + EitherUpgrade, SendWrapper>; + type InboundOpenInfo = Either; + type OutboundOpenInfo = Either; fn listen_protocol(&self) -> SubstreamProtocol { match self { - EitherHandler::A(a) => a + Either::Left(a) => a .listen_protocol() .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) .map_info(Either::Left), - EitherHandler::B(b) => b + Either::Right(b) => b .listen_protocol() .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) .map_info(Either::Right), @@ -104,10 +100,10 @@ where info: Self::OutboundOpenInfo, ) { match (self, output, info) { - (EitherHandler::A(handler), EitherOutput::First(output), Either::Left(info)) => { + (Either::Left(handler), EitherOutput::First(output), Either::Left(info)) => { handler.inject_fully_negotiated_outbound(output, info) } - (EitherHandler::B(handler), EitherOutput::Second(output), Either::Right(info)) => { + (Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) => { handler.inject_fully_negotiated_outbound(output, info) } _ => unreachable!(), @@ -120,10 +116,10 @@ where info: Self::InboundOpenInfo, ) { match (self, output, info) { - (EitherHandler::A(handler), EitherOutput::First(output), Either::Left(info)) => { + (Either::Left(handler), EitherOutput::First(output), Either::Left(info)) => { handler.inject_fully_negotiated_inbound(output, info) } - (EitherHandler::B(handler), EitherOutput::Second(output), Either::Right(info)) => { + (Either::Right(handler), EitherOutput::Second(output), Either::Right(info)) => { handler.inject_fully_negotiated_inbound(output, info) } _ => unreachable!(), @@ -132,16 +128,16 @@ where fn inject_event(&mut self, event: Self::InEvent) { match (self, event) { - (EitherHandler::A(handler), Either::Left(event)) => handler.inject_event(event), - (EitherHandler::B(handler), Either::Right(event)) => handler.inject_event(event), + (Either::Left(handler), Either::Left(event)) => handler.inject_event(event), + (Either::Right(handler), Either::Right(event)) => handler.inject_event(event), _ => unreachable!(), } } fn inject_address_change(&mut self, addr: &Multiaddr) { match self { - EitherHandler::A(handler) => handler.inject_address_change(addr), - EitherHandler::B(handler) => handler.inject_address_change(addr), + Either::Left(handler) => handler.inject_address_change(addr), + Either::Right(handler) => handler.inject_address_change(addr), } } @@ -152,31 +148,31 @@ where ) { match error { ProtocolsHandlerUpgrErr::Timer => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); } _ => unreachable!(), }, ProtocolsHandlerUpgrErr::Timeout => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_dial_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); } _ => unreachable!(), }, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_dial_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), ); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_dial_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), @@ -186,7 +182,7 @@ where }, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => { match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_dial_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), @@ -197,7 +193,7 @@ where } ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => { match (self, info) { - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_dial_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), @@ -216,31 +212,31 @@ where ) { match error { ProtocolsHandlerUpgrErr::Timer => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timer); } _ => unreachable!(), }, ProtocolsHandlerUpgrErr::Timeout => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_listen_upgrade_error(info, ProtocolsHandlerUpgrErr::Timeout); } _ => unreachable!(), }, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)) => match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_listen_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), ); } - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_listen_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(error)), @@ -250,7 +246,7 @@ where }, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => { match (self, info) { - (EitherHandler::A(handler), Either::Left(info)) => { + (Either::Left(handler), Either::Left(info)) => { handler.inject_listen_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), @@ -261,7 +257,7 @@ where } ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => { match (self, info) { - (EitherHandler::B(handler), Either::Right(info)) => { + (Either::Right(handler), Either::Right(info)) => { handler.inject_listen_upgrade_error( info, ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), @@ -275,8 +271,8 @@ where fn connection_keep_alive(&self) -> KeepAlive { match self { - EitherHandler::A(handler) => handler.connection_keep_alive(), - EitherHandler::B(handler) => handler.connection_keep_alive(), + Either::Left(handler) => handler.connection_keep_alive(), + Either::Right(handler) => handler.connection_keep_alive(), } } @@ -292,12 +288,12 @@ where >, > { let event = match self { - EitherHandler::A(handler) => futures::ready!(handler.poll(cx)) + Either::Left(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Left) .map_close(Either::Left) .map_protocol(|p| EitherUpgrade::A(SendWrapper(p))) .map_outbound_open_info(Either::Left), - EitherHandler::B(handler) => futures::ready!(handler.poll(cx)) + Either::Right(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Right) .map_close(Either::Right) .map_protocol(|p| EitherUpgrade::B(SendWrapper(p))) From b9f968e7b0218966933e877771f213f42db633d7 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 17 Aug 2021 16:47:49 +0200 Subject: [PATCH 4/4] swarm/CHANGELOG: Add entry --- swarm/CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 281f6ca3bc7..2afa04c91b7 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -13,9 +13,13 @@ - Require `ProtocolsHandler::{InEvent,OutEvent,Error}` to implement `Debug` (see [PR 2183]). -[PR 2150]: https://github.com/libp2p/rust-libp2p/pull/2150/ +- Implement `ProtocolsHandler` on `either::Either`representing either of two + `ProtocolsHandler` implementations (see [PR 2192]). + +[PR 2150]: https://github.com/libp2p/rust-libp2p/pull/2150 [PR 2182]: https://github.com/libp2p/rust-libp2p/pull/2182 [PR 2183]: https://github.com/libp2p/rust-libp2p/pull/2183 +[PR 2192]: https://github.com/libp2p/rust-libp2p/pull/2192 # 0.30.0 [2021-07-12]