diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 85330faf704..bbb812dd87b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -16,6 +16,8 @@ - Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338]. +- Remove `EitherUpgrade` in favor of implementing `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`. See [PR 3339]. + [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 @@ -23,6 +25,7 @@ [PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972 [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 # 0.37.0 diff --git a/core/src/upgrade.rs b/core/src/upgrade.rs index de9ef765e16..62b3e278cf1 100644 --- a/core/src/upgrade.rs +++ b/core/src/upgrade.rs @@ -74,7 +74,6 @@ use futures::future::Future; pub use self::{ apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply}, denied::DeniedUpgrade, - either::EitherUpgrade, error::UpgradeError, from_fn::{from_fn, FromFnUpgrade}, map::{MapInboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgrade, MapOutboundUpgradeErr}, diff --git a/core/src/upgrade/either.rs b/core/src/upgrade/either.rs index 99a2f557e5f..9f4c3f7a41b 100644 --- a/core/src/upgrade/either.rs +++ b/core/src/upgrade/either.rs @@ -24,14 +24,7 @@ use crate::{ }; use either::Either; -/// A type to represent two possible upgrade types (inbound or outbound). -#[derive(Debug, Clone)] -pub enum EitherUpgrade { - A(A), - B(B), -} - -impl UpgradeInfo for EitherUpgrade +impl UpgradeInfo for Either where A: UpgradeInfo, B: UpgradeInfo, @@ -44,13 +37,13 @@ where fn protocol_info(&self) -> Self::InfoIter { match self { - EitherUpgrade::A(a) => EitherIter::A(a.protocol_info().into_iter()), - EitherUpgrade::B(b) => EitherIter::B(b.protocol_info().into_iter()), + Either::Left(a) => EitherIter::A(a.protocol_info().into_iter()), + Either::Right(b) => EitherIter::B(b.protocol_info().into_iter()), } } } -impl InboundUpgrade for EitherUpgrade +impl InboundUpgrade for Either where A: InboundUpgrade, B: InboundUpgrade, @@ -61,10 +54,10 @@ where fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { - (EitherUpgrade::A(a), EitherName::A(info)) => { + (Either::Left(a), EitherName::A(info)) => { EitherFuture2::A(a.upgrade_inbound(sock, info)) } - (EitherUpgrade::B(b), EitherName::B(info)) => { + (Either::Right(b), EitherName::B(info)) => { EitherFuture2::B(b.upgrade_inbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"), @@ -72,7 +65,7 @@ where } } -impl OutboundUpgrade for EitherUpgrade +impl OutboundUpgrade for Either where A: OutboundUpgrade, B: OutboundUpgrade, @@ -83,10 +76,10 @@ where fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { match (self, info) { - (EitherUpgrade::A(a), EitherName::A(info)) => { + (Either::Left(a), EitherName::A(info)) => { EitherFuture2::A(a.upgrade_outbound(sock, info)) } - (EitherUpgrade::B(b), EitherName::B(info)) => { + (Either::Right(b), EitherName::B(info)) => { EitherFuture2::B(b.upgrade_outbound(sock, info)) } _ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"), diff --git a/protocols/dcutr/src/handler.rs b/protocols/dcutr/src/handler.rs index e854b395308..aca0809ece4 100644 --- a/protocols/dcutr/src/handler.rs +++ b/protocols/dcutr/src/handler.rs @@ -21,7 +21,7 @@ use crate::protocol; use either::Either; use libp2p_core::connection::ConnectionId; -use libp2p_core::upgrade::{self, DeniedUpgrade}; +use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::{ConnectedPoint, PeerId}; use libp2p_swarm::dummy; use libp2p_swarm::handler::SendWrapper; @@ -70,11 +70,11 @@ impl IntoConnectionHandler for Prototype { fn inbound_protocol(&self) -> ::InboundProtocol { match self { - Prototype::UnknownConnection => upgrade::EitherUpgrade::A(SendWrapper( - upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}), - )), + Prototype::UnknownConnection => { + Either::Left(SendWrapper(Either::Left(protocol::inbound::Upgrade {}))) + } Prototype::DirectConnection { .. } => { - upgrade::EitherUpgrade::A(SendWrapper(upgrade::EitherUpgrade::B(DeniedUpgrade))) + Either::Left(SendWrapper(Either::Right(DeniedUpgrade))) } } } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 7e8270629ed..23a458414c8 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -26,7 +26,7 @@ use futures::future::{BoxFuture, FutureExt}; use instant::Instant; use libp2p_core::either::EitherOutput; use libp2p_core::multiaddr::Multiaddr; -use libp2p_core::upgrade::{self, DeniedUpgrade, NegotiationError, UpgradeError}; +use libp2p_core::upgrade::{DeniedUpgrade, NegotiationError, UpgradeError}; use libp2p_core::ConnectedPoint; use libp2p_swarm::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, @@ -305,7 +305,7 @@ impl ConnectionHandler for Handler { type Error = ConnectionHandlerUpgrErr< Either, >; - type InboundProtocol = upgrade::EitherUpgrade; + type InboundProtocol = Either; type OutboundProtocol = protocol::outbound::Upgrade; type OutboundOpenInfo = u8; // Number of upgrade attempts. type InboundOpenInfo = (); @@ -313,7 +313,7 @@ impl ConnectionHandler for Handler { fn listen_protocol(&self) -> SubstreamProtocol { match self.endpoint { ConnectedPoint::Dialer { .. } => { - SubstreamProtocol::new(upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}), ()) + SubstreamProtocol::new(Either::Left(protocol::inbound::Upgrade {}), ()) } ConnectedPoint::Listener { .. } => { // By the protocol specification the listening side of a relayed connection @@ -321,7 +321,7 @@ impl ConnectionHandler for Handler { // the relayed connection opens a substream to the dialing side. (Connection roles // and substream roles are reversed.) The listening side on a relayed connection // never expects incoming substreams, hence the denied upgrade below. - SubstreamProtocol::new(upgrade::EitherUpgrade::B(DeniedUpgrade), ()) + SubstreamProtocol::new(Either::Right(DeniedUpgrade), ()) } } } diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 0dce0e49940..e251d552cf0 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -27,7 +27,7 @@ use futures::prelude::*; use futures::stream::FuturesUnordered; use futures_timer::Delay; use libp2p_core::either::EitherOutput; -use libp2p_core::upgrade::{EitherUpgrade, SelectUpgrade}; +use libp2p_core::upgrade::SelectUpgrade; use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PublicKey}; use libp2p_swarm::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, @@ -102,8 +102,7 @@ pub struct Handler { inbound_identify_push: Option>>, /// Pending events to yield. events: SmallVec< - [ConnectionHandlerEvent>, (), Event, io::Error>; - 4], + [ConnectionHandlerEvent>, (), Event, io::Error>; 4], >, /// Streams awaiting `BehaviourInfo` to then send identify requests. @@ -277,7 +276,7 @@ impl ConnectionHandler for Handler { type OutEvent = Event; type Error = io::Error; type InboundProtocol = SelectUpgrade>; - type OutboundProtocol = EitherUpgrade>; + type OutboundProtocol = Either>; type OutboundOpenInfo = (); type InboundOpenInfo = (); @@ -306,10 +305,7 @@ impl ConnectionHandler for Handler { Protocol::Push => { self.events .push(ConnectionHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new( - EitherUpgrade::B(Push::outbound(info)), - (), - ), + protocol: SubstreamProtocol::new(Either::Right(Push::outbound(info)), ()), }); } Protocol::Identify(_) => { @@ -347,7 +343,7 @@ impl ConnectionHandler for Handler { Poll::Ready(()) => { self.trigger_next_identify.reset(self.interval); let ev = ConnectionHandlerEvent::OutboundSubstreamRequest { - protocol: SubstreamProtocol::new(EitherUpgrade::A(Identify), ()), + protocol: SubstreamProtocol::new(Either::Left(Identify), ()), }; return Poll::Ready(ev); } diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 37f41a8cd8a..a475c045e78 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -23,6 +23,7 @@ use crate::protocol::{ KademliaProtocolConfig, }; use crate::record::{self, Record}; +use either::Either; use futures::prelude::*; use futures::stream::SelectAll; use instant::Instant; @@ -68,9 +69,9 @@ impl IntoConnectionHandler fn inbound_protocol(&self) -> ::InboundProtocol { if self.config.allow_listening { - upgrade::EitherUpgrade::A(self.config.protocol_config.clone()) + Either::Left(self.config.protocol_config.clone()) } else { - upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade) + Either::Right(upgrade::DeniedUpgrade) } } } @@ -633,7 +634,7 @@ where type InEvent = KademliaHandlerIn; type OutEvent = KademliaHandlerEvent; type Error = io::Error; // TODO: better error type? - type InboundProtocol = upgrade::EitherUpgrade; + type InboundProtocol = Either; type OutboundProtocol = KademliaProtocolConfig; // Message of the request to send to the remote, and user data if we expect an answer. type OutboundOpenInfo = (KadRequestMsg, Option); @@ -642,9 +643,9 @@ where fn listen_protocol(&self) -> SubstreamProtocol { if self.config.allow_listening { SubstreamProtocol::new(self.config.protocol_config.clone(), ()) - .map_upgrade(upgrade::EitherUpgrade::A) + .map_upgrade(Either::Left) } else { - SubstreamProtocol::new(upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade), ()) + SubstreamProtocol::new(Either::Right(upgrade::DeniedUpgrade), ()) } } diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index b9b693b924e..a23d7eb4e66 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -11,6 +11,7 @@ keywords = ["peer-to-peer", "libp2p", "networking"] categories = ["network-programming", "asynchronous"] [dependencies] +either = "1.8.0" futures = "0.3.1" futures-timer = "3.0.2" instant = "0.1.11" diff --git a/protocols/ping/tests/ping.rs b/protocols/ping/tests/ping.rs index c4eaea4ecba..cb1b0405691 100644 --- a/protocols/ping/tests/ping.rs +++ b/protocols/ping/tests/ping.rs @@ -20,6 +20,7 @@ //! Integration tests for the `Ping` network behaviour. +use either::Either; use futures::{channel::mpsc, prelude::*}; use libp2p_core::{ identity, @@ -251,8 +252,8 @@ fn mk_transport(muxer: MuxerChoice) -> (PeerId, transport::Boxed<(PeerId, Stream .upgrade(upgrade::Version::V1) .authenticate(noise::NoiseAuthenticated::xx(&id_keys).unwrap()) .multiplex(match muxer { - MuxerChoice::Yamux => upgrade::EitherUpgrade::A(yamux::YamuxConfig::default()), - MuxerChoice::Mplex => upgrade::EitherUpgrade::B(mplex::MplexConfig::default()), + MuxerChoice::Yamux => Either::Left(yamux::YamuxConfig::default()), + MuxerChoice::Mplex => Either::Right(mplex::MplexConfig::default()), }) .boxed(), ) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 8b28428ea84..a35d3fbbe8b 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -355,7 +355,7 @@ impl IntoConnectionHandler for Prototype { } fn inbound_protocol(&self) -> ::InboundProtocol { - upgrade::EitherUpgrade::A(SendWrapper(inbound_hop::Upgrade { + Either::Left(SendWrapper(inbound_hop::Upgrade { reservation_duration: self.config.reservation_duration, max_circuit_duration: self.config.max_circuit_duration, max_circuit_bytes: self.config.max_circuit_bytes, diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index ff34be496c4..9bf09e3d178 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -155,7 +155,7 @@ impl IntoConnectionHandler for Prototype { } fn inbound_protocol(&self) -> ::InboundProtocol { - upgrade::EitherUpgrade::A(SendWrapper(inbound_stop::Upgrade {})) + Either::Left(SendWrapper(inbound_stop::Upgrade {})) } } diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 1606e5e53a3..75dff7a697a 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -28,9 +28,7 @@ use crate::upgrade::SendWrapper; use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use either::Either; use libp2p_core::{ - either::EitherOutput, - upgrade::{DeniedUpgrade, EitherUpgrade}, - ConnectedPoint, Multiaddr, PeerId, + either::EitherOutput, upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId, }; use std::{task::Context, task::Poll}; @@ -143,9 +141,9 @@ where fn inbound_protocol(&self) -> ::InboundProtocol { if let Some(inner) = self.inner.as_ref() { - EitherUpgrade::A(SendWrapper(inner.inbound_protocol())) + Either::Left(SendWrapper(inner.inbound_protocol())) } else { - EitherUpgrade::B(SendWrapper(DeniedUpgrade)) + Either::Right(SendWrapper(DeniedUpgrade)) } } } @@ -235,8 +233,7 @@ where type InEvent = TInner::InEvent; type OutEvent = TInner::OutEvent; type Error = TInner::Error; - type InboundProtocol = - EitherUpgrade, SendWrapper>; + type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = TInner::OutboundProtocol; type OutboundOpenInfo = TInner::OutboundOpenInfo; type InboundOpenInfo = Either; @@ -245,13 +242,10 @@ where if let Some(inner) = self.inner.as_ref() { inner .listen_protocol() - .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) + .map_upgrade(|u| Either::Left(SendWrapper(u))) .map_info(Either::Left) } else { - SubstreamProtocol::new( - EitherUpgrade::B(SendWrapper(DeniedUpgrade)), - Either::Right(()), - ) + SubstreamProtocol::new(Either::Right(SendWrapper(DeniedUpgrade)), Either::Right(())) } } diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index f03afb20499..2345830e5b3 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -26,7 +26,7 @@ use crate::handler::{ use crate::upgrade::SendWrapper; use either::Either; use libp2p_core::either::EitherOutput; -use libp2p_core::upgrade::{EitherUpgrade, UpgradeError}; +use libp2p_core::upgrade::UpgradeError; use libp2p_core::{ConnectedPoint, PeerId}; use std::task::{Context, Poll}; @@ -58,10 +58,10 @@ where fn inbound_protocol(&self) -> ::InboundProtocol { match self { IntoEitherHandler::Left(into_handler) => { - EitherUpgrade::A(SendWrapper(into_handler.inbound_protocol())) + Either::Left(SendWrapper(into_handler.inbound_protocol())) } IntoEitherHandler::Right(into_handler) => { - EitherUpgrade::B(SendWrapper(into_handler.inbound_protocol())) + Either::Right(SendWrapper(into_handler.inbound_protocol())) } } } @@ -91,7 +91,7 @@ impl IntoEitherHandler { } impl - FullyNegotiatedInbound, SendWrapper>, Either> + FullyNegotiatedInbound, SendWrapper>, Either> where RIP: InboundUpgradeSend, LIP: InboundUpgradeSend, @@ -114,7 +114,7 @@ where } impl - FullyNegotiatedOutbound, SendWrapper>, Either> + FullyNegotiatedOutbound, SendWrapper>, Either> where LOP: OutboundUpgradeSend, ROP: OutboundUpgradeSend, @@ -137,7 +137,7 @@ where } impl - DialUpgradeError, EitherUpgrade, SendWrapper>> + DialUpgradeError, Either, SendWrapper>> where LOP: OutboundUpgradeSend, ROP: OutboundUpgradeSend, @@ -206,7 +206,7 @@ where } impl - ListenUpgradeError, EitherUpgrade, SendWrapper>> + ListenUpgradeError, Either, SendWrapper>> where RIP: InboundUpgradeSend, LIP: InboundUpgradeSend, @@ -284,10 +284,9 @@ where type InEvent = Either; type OutEvent = Either; type Error = Either; - type InboundProtocol = - EitherUpgrade, SendWrapper>; + type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = - EitherUpgrade, SendWrapper>; + Either, SendWrapper>; type InboundOpenInfo = Either; type OutboundOpenInfo = Either; @@ -295,11 +294,11 @@ where match self { Either::Left(a) => a .listen_protocol() - .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) + .map_upgrade(|u| Either::Left(SendWrapper(u))) .map_info(Either::Left), Either::Right(b) => b .listen_protocol() - .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) + .map_upgrade(|u| Either::Right(SendWrapper(u))) .map_info(Either::Right), } } @@ -334,12 +333,12 @@ where Either::Left(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Left) .map_close(Either::Left) - .map_protocol(|p| EitherUpgrade::A(SendWrapper(p))) + .map_protocol(|p| Either::Left(SendWrapper(p))) .map_outbound_open_info(Either::Left), Either::Right(handler) => futures::ready!(handler.poll(cx)) .map_custom(Either::Right) .map_close(Either::Right) - .map_protocol(|p| EitherUpgrade::B(SendWrapper(p))) + .map_protocol(|p| Either::Right(SendWrapper(p))) .map_outbound_open_info(Either::Right), }; diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 8d052d637d7..535b06bee41 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -29,7 +29,7 @@ use crate::upgrade::SendWrapper; use either::Either; use libp2p_core::{ either::EitherOutput, - upgrade::{EitherUpgrade, NegotiationError, ProtocolError, SelectUpgrade, UpgradeError}, + upgrade::{NegotiationError, ProtocolError, SelectUpgrade, UpgradeError}, ConnectedPoint, PeerId, }; use std::{cmp, task::Context, task::Poll}; @@ -102,7 +102,7 @@ impl ConnectionHandlerSelect { impl FullyNegotiatedOutbound< - EitherUpgrade, SendWrapper>, + Either, SendWrapper>, EitherOutput, > where @@ -151,10 +151,7 @@ where } impl - DialUpgradeError< - EitherOutput, - EitherUpgrade, SendWrapper>, - > + DialUpgradeError, Either, SendWrapper>> where S1OP: OutboundUpgradeSend, S2OP: OutboundUpgradeSend, @@ -348,10 +345,8 @@ where SendWrapper<::InboundProtocol>, SendWrapper<::InboundProtocol>, >; - type OutboundProtocol = EitherUpgrade< - SendWrapper, - SendWrapper, - >; + type OutboundProtocol = + Either, SendWrapper>; type OutboundOpenInfo = EitherOutput; type InboundOpenInfo = (TProto1::InboundOpenInfo, TProto2::InboundOpenInfo); @@ -400,7 +395,7 @@ where Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: protocol - .map_upgrade(|u| EitherUpgrade::A(SendWrapper(u))) + .map_upgrade(|u| Either::Left(SendWrapper(u))) .map_info(EitherOutput::First), }); } @@ -417,7 +412,7 @@ where Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: protocol - .map_upgrade(|u| EitherUpgrade::B(SendWrapper(u))) + .map_upgrade(|u| Either::Right(SendWrapper(u))) .map_info(EitherOutput::Second), }); }