From 3a84c141787d2206db1ace365fa1b19082d9f00a Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 11 Jan 2023 16:13:16 +1100 Subject: [PATCH 01/12] Use single quotes to avoid running commands --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9ce4fc5fdea..a25fdf3c4e0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -299,10 +299,10 @@ jobs: - name: Check PR title length run: | - title="${{ github.event.pull_request.title }}" + title='${{ github.event.pull_request.title }}' title_length=${#title} if [ $title_length -gt 72 ] then echo "PR title is too long (greater than 72 characters)" exit 1 - fi \ No newline at end of file + fi From 458aa533d9489aa374658bdba808f45446d4925d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 13 Jan 2023 11:47:28 +1100 Subject: [PATCH 02/12] Use intermediary variable --- .github/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a25fdf3c4e0..c8725c7a332 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -298,9 +298,10 @@ jobs: require_scope: false - name: Check PR title length + env: + TITLE: ${{ github.event.pull_request.title }} run: | - title='${{ github.event.pull_request.title }}' - title_length=${#title} + title_length=${#TITLE} if [ $title_length -gt 72 ] then echo "PR title is too long (greater than 72 characters)" From aed5363135b06237befeccca95fef1e10ffc232c Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 00:04:34 +1100 Subject: [PATCH 03/12] refactor(relay): introduce `Handler::new` functions (#3326) --- protocols/relay/src/behaviour/handler.rs | 30 +++++++++++--------- protocols/relay/src/priv_client/handler.rs | 32 ++++++++++++++-------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 120e980c001..20b7a01af14 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -351,19 +351,7 @@ impl IntoConnectionHandler for Prototype { // Deny all substreams on relayed connection. Either::Right(dummy::ConnectionHandler) } else { - Either::Left(Handler { - endpoint: endpoint.clone(), - config: self.config, - queued_events: Default::default(), - pending_error: Default::default(), - reservation_request_future: Default::default(), - circuit_accept_futures: Default::default(), - circuit_deny_futures: Default::default(), - alive_lend_out_substreams: Default::default(), - circuits: Default::default(), - active_reservation: Default::default(), - keep_alive: KeepAlive::Yes, - }) + Either::Left(Handler::new(self.config, endpoint.clone())) } } @@ -432,6 +420,22 @@ pub struct Handler { } impl Handler { + fn new(config: Config, endpoint: ConnectedPoint) -> Handler { + Handler { + endpoint, + config, + queued_events: Default::default(), + pending_error: Default::default(), + reservation_request_future: Default::default(), + circuit_accept_futures: Default::default(), + circuit_deny_futures: Default::default(), + alive_lend_out_substreams: Default::default(), + circuits: Default::default(), + active_reservation: Default::default(), + keep_alive: KeepAlive::Yes, + } + } + fn on_fully_negotiated_inbound( &mut self, FullyNegotiatedInbound { diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index c9f7c18adbb..922a767cf45 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -141,18 +141,11 @@ impl IntoConnectionHandler for Prototype { // Deny all substreams on relayed connection. Either::Right(dummy::ConnectionHandler) } else { - let mut handler = Handler { - remote_peer_id: *remote_peer_id, - remote_addr: endpoint.get_remote_address().clone(), - local_peer_id: self.local_peer_id, - queued_events: Default::default(), - pending_error: Default::default(), - reservation: Reservation::None, - alive_lend_out_substreams: Default::default(), - circuit_deny_futs: Default::default(), - send_error_futs: Default::default(), - keep_alive: KeepAlive::Yes, - }; + let mut handler = Handler::new( + self.local_peer_id, + *remote_peer_id, + endpoint.get_remote_address().clone(), + ); if let Some(event) = self.initial_in { handler.on_behaviour_event(event) @@ -213,6 +206,21 @@ pub struct Handler { } impl Handler { + fn new(local_peer_id: PeerId, remote_peer_id: PeerId, remote_addr: Multiaddr) -> Self { + Self { + local_peer_id, + remote_peer_id, + remote_addr, + queued_events: Default::default(), + pending_error: Default::default(), + reservation: Reservation::None, + alive_lend_out_substreams: Default::default(), + circuit_deny_futs: Default::default(), + send_error_futs: Default::default(), + keep_alive: KeepAlive::Yes, + } + } + fn on_fully_negotiated_inbound( &mut self, FullyNegotiatedInbound { From 5e97d0e734ba4ce2c46200a75938345a52b1feca Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 14:21:53 +1100 Subject: [PATCH 04/12] fix: revert "refactor(relay): introduce `Handler::new` functions" (#3333) --- protocols/relay/src/behaviour/handler.rs | 30 +++++++++----------- protocols/relay/src/priv_client/handler.rs | 32 ++++++++-------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 20b7a01af14..120e980c001 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -351,7 +351,19 @@ impl IntoConnectionHandler for Prototype { // Deny all substreams on relayed connection. Either::Right(dummy::ConnectionHandler) } else { - Either::Left(Handler::new(self.config, endpoint.clone())) + Either::Left(Handler { + endpoint: endpoint.clone(), + config: self.config, + queued_events: Default::default(), + pending_error: Default::default(), + reservation_request_future: Default::default(), + circuit_accept_futures: Default::default(), + circuit_deny_futures: Default::default(), + alive_lend_out_substreams: Default::default(), + circuits: Default::default(), + active_reservation: Default::default(), + keep_alive: KeepAlive::Yes, + }) } } @@ -420,22 +432,6 @@ pub struct Handler { } impl Handler { - fn new(config: Config, endpoint: ConnectedPoint) -> Handler { - Handler { - endpoint, - config, - queued_events: Default::default(), - pending_error: Default::default(), - reservation_request_future: Default::default(), - circuit_accept_futures: Default::default(), - circuit_deny_futures: Default::default(), - alive_lend_out_substreams: Default::default(), - circuits: Default::default(), - active_reservation: Default::default(), - keep_alive: KeepAlive::Yes, - } - } - fn on_fully_negotiated_inbound( &mut self, FullyNegotiatedInbound { diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 922a767cf45..c9f7c18adbb 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -141,11 +141,18 @@ impl IntoConnectionHandler for Prototype { // Deny all substreams on relayed connection. Either::Right(dummy::ConnectionHandler) } else { - let mut handler = Handler::new( - self.local_peer_id, - *remote_peer_id, - endpoint.get_remote_address().clone(), - ); + let mut handler = Handler { + remote_peer_id: *remote_peer_id, + remote_addr: endpoint.get_remote_address().clone(), + local_peer_id: self.local_peer_id, + queued_events: Default::default(), + pending_error: Default::default(), + reservation: Reservation::None, + alive_lend_out_substreams: Default::default(), + circuit_deny_futs: Default::default(), + send_error_futs: Default::default(), + keep_alive: KeepAlive::Yes, + }; if let Some(event) = self.initial_in { handler.on_behaviour_event(event) @@ -206,21 +213,6 @@ pub struct Handler { } impl Handler { - fn new(local_peer_id: PeerId, remote_peer_id: PeerId, remote_addr: Multiaddr) -> Self { - Self { - local_peer_id, - remote_peer_id, - remote_addr, - queued_events: Default::default(), - pending_error: Default::default(), - reservation: Reservation::None, - alive_lend_out_substreams: Default::default(), - circuit_deny_futs: Default::default(), - send_error_futs: Default::default(), - keep_alive: KeepAlive::Yes, - } - } - fn on_fully_negotiated_inbound( &mut self, FullyNegotiatedInbound { From da97e8fba0b49af81db875c9d33c0045354b0226 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:00:58 +1100 Subject: [PATCH 05/12] Replace `EitherError` with `Either` --- Cargo.toml | 1 + core/src/either.rs | 113 ++++++++------------- core/src/transport/and_then.rs | 20 ++-- core/src/transport/choice.rs | 21 ++-- core/src/upgrade/either.rs | 7 +- core/src/upgrade/select.rs | 7 +- examples/file-sharing.rs | 4 +- protocols/dcutr/src/handler/relayed.rs | 15 +-- protocols/identify/Cargo.toml | 1 + protocols/identify/src/handler.rs | 7 +- protocols/relay/src/behaviour/handler.rs | 9 +- protocols/relay/src/priv_client/handler.rs | 11 +- swarm/src/behaviour/toggle.rs | 6 +- swarm/src/handler/either.rs | 10 +- swarm/src/handler/select.rs | 16 +-- swarm/src/lib.rs | 11 +- 16 files changed, 115 insertions(+), 144 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5103c7802b8..c0f97e48109 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -132,6 +132,7 @@ libp2p-gossipsub = { version = "0.44.0", path = "protocols/gossipsub", optional [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } async-trait = "0.1" +either = "1.8.0" env_logger = "0.10.0" clap = { version = "4.0.13", features = ["derive"] } tokio = { version = "1.15", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] } diff --git a/core/src/either.rs b/core/src/either.rs index a34552bf28f..411251cbd84 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -24,44 +24,13 @@ use crate::{ transport::{ListenerId, Transport, TransportError, TransportEvent}, Multiaddr, ProtocolName, }; +use either::Either; use futures::{ io::{IoSlice, IoSliceMut}, prelude::*, }; use pin_project::pin_project; -use std::{fmt, io, pin::Pin, task::Context, task::Poll}; - -#[derive(Debug, Copy, Clone)] -pub enum EitherError { - A(A), - B(B), -} - -impl fmt::Display for EitherError -where - A: fmt::Display, - B: fmt::Display, -{ - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self { - EitherError::A(a) => a.fmt(f), - EitherError::B(b) => b.fmt(f), - } - } -} - -impl std::error::Error for EitherError -where - A: std::error::Error, - B: std::error::Error, -{ - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - EitherError::A(a) => a.source(), - EitherError::B(b) => b.source(), - } - } -} +use std::{io, pin::Pin, task::Context, task::Poll}; /// Implements `AsyncRead` and `AsyncWrite` and dispatches all method calls to /// either `First` or `Second`. @@ -147,15 +116,15 @@ where A: TryStream, B: TryStream, { - type Item = Result>; + type Item = Result>; fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.project() { EitherOutputProj::First(a) => { - TryStream::try_poll_next(a, cx).map(|v| v.map(|r| r.map_err(EitherError::A))) + TryStream::try_poll_next(a, cx).map(|v| v.map(|r| r.map_err(Either::Left))) } EitherOutputProj::Second(b) => { - TryStream::try_poll_next(b, cx).map(|v| v.map(|r| r.map_err(EitherError::B))) + TryStream::try_poll_next(b, cx).map(|v| v.map(|r| r.map_err(Either::Right))) } } } @@ -166,33 +135,33 @@ where A: Sink, B: Sink, { - type Error = EitherError; + type Error = Either; fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.project() { - EitherOutputProj::First(a) => Sink::poll_ready(a, cx).map_err(EitherError::A), - EitherOutputProj::Second(b) => Sink::poll_ready(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_ready(a, cx).map_err(Either::Left), + EitherOutputProj::Second(b) => Sink::poll_ready(b, cx).map_err(Either::Right), } } fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> { match self.project() { - EitherOutputProj::First(a) => Sink::start_send(a, item).map_err(EitherError::A), - EitherOutputProj::Second(b) => Sink::start_send(b, item).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::start_send(a, item).map_err(Either::Left), + EitherOutputProj::Second(b) => Sink::start_send(b, item).map_err(Either::Right), } } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.project() { - EitherOutputProj::First(a) => Sink::poll_flush(a, cx).map_err(EitherError::A), - EitherOutputProj::Second(b) => Sink::poll_flush(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_flush(a, cx).map_err(Either::Left), + EitherOutputProj::Second(b) => Sink::poll_flush(b, cx).map_err(Either::Right), } } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.project() { - EitherOutputProj::First(a) => Sink::poll_close(a, cx).map_err(EitherError::A), - EitherOutputProj::Second(b) => Sink::poll_close(b, cx).map_err(EitherError::B), + EitherOutputProj::First(a) => Sink::poll_close(a, cx).map_err(Either::Left), + EitherOutputProj::Second(b) => Sink::poll_close(b, cx).map_err(Either::Right), } } } @@ -203,7 +172,7 @@ where B: StreamMuxer, { type Substream = EitherOutput; - type Error = EitherError; + type Error = Either; fn poll_inbound( self: Pin<&mut Self>, @@ -213,11 +182,11 @@ where EitherOutputProj::First(inner) => inner .poll_inbound(cx) .map_ok(EitherOutput::First) - .map_err(EitherError::A), + .map_err(Either::Left), EitherOutputProj::Second(inner) => inner .poll_inbound(cx) .map_ok(EitherOutput::Second) - .map_err(EitherError::B), + .map_err(Either::Right), } } @@ -229,18 +198,18 @@ where EitherOutputProj::First(inner) => inner .poll_outbound(cx) .map_ok(EitherOutput::First) - .map_err(EitherError::A), + .map_err(Either::Left), EitherOutputProj::Second(inner) => inner .poll_outbound(cx) .map_ok(EitherOutput::Second) - .map_err(EitherError::B), + .map_err(Either::Right), } } fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { match self.project() { - EitherOutputProj::First(inner) => inner.poll_close(cx).map_err(EitherError::A), - EitherOutputProj::Second(inner) => inner.poll_close(cx).map_err(EitherError::B), + EitherOutputProj::First(inner) => inner.poll_close(cx).map_err(Either::Left), + EitherOutputProj::Second(inner) => inner.poll_close(cx).map_err(Either::Right), } } @@ -249,8 +218,8 @@ where cx: &mut Context<'_>, ) -> Poll> { match self.project() { - EitherOutputProj::First(inner) => inner.poll(cx).map_err(EitherError::A), - EitherOutputProj::Second(inner) => inner.poll(cx).map_err(EitherError::B), + EitherOutputProj::First(inner) => inner.poll(cx).map_err(Either::Left), + EitherOutputProj::Second(inner) => inner.poll(cx).map_err(Either::Right), } } } @@ -269,16 +238,16 @@ where AFuture: TryFuture, BFuture: TryFuture, { - type Output = Result, EitherError>; + type Output = Result, Either>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { match self.project() { EitherFutureProj::First(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::First) - .map_err(EitherError::A), + .map_err(Either::Left), EitherFutureProj::Second(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::Second) - .map_err(EitherError::B), + .map_err(Either::Right), } } } @@ -296,16 +265,16 @@ where AFut: TryFuture, BFut: TryFuture, { - type Output = Result, EitherError>; + 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(EitherError::A), + .map_err(Either::Left), EitherFuture2Proj::B(a) => TryFuture::try_poll(a, cx) .map_ok(EitherOutput::Second) - .map_err(EitherError::B), + .map_err(Either::Right), } } } @@ -338,7 +307,7 @@ where A: Transport, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type ListenerUpgrade = EitherFuture; type Dial = EitherFuture; @@ -349,18 +318,16 @@ where match self.project() { EitherTransportProj::Left(a) => match a.poll(cx) { Poll::Pending => Poll::Pending, - Poll::Ready(event) => Poll::Ready( - event - .map_upgrade(EitherFuture::First) - .map_err(EitherError::A), - ), + Poll::Ready(event) => { + Poll::Ready(event.map_upgrade(EitherFuture::First).map_err(Either::Left)) + } }, EitherTransportProj::Right(b) => match b.poll(cx) { Poll::Pending => Poll::Pending, Poll::Ready(event) => Poll::Ready( event .map_upgrade(EitherFuture::Second) - .map_err(EitherError::B), + .map_err(Either::Right), ), }, } @@ -378,11 +345,11 @@ where match self { EitherTransport::Left(a) => a.listen_on(addr).map_err(|e| match e { MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr), - Other(err) => Other(EitherError::A(err)), + Other(err) => Other(Either::Left(err)), }), EitherTransport::Right(b) => b.listen_on(addr).map_err(|e| match e { MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr), - Other(err) => Other(EitherError::B(err)), + Other(err) => Other(Either::Right(err)), }), } } @@ -393,12 +360,12 @@ where EitherTransport::Left(a) => match a.dial(addr) { Ok(connec) => Ok(EitherFuture::First(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(EitherError::A(err))), + Err(Other(err)) => Err(Other(Either::Left(err))), }, EitherTransport::Right(b) => match b.dial(addr) { Ok(connec) => Ok(EitherFuture::Second(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(EitherError::B(err))), + Err(Other(err)) => Err(Other(Either::Right(err))), }, } } @@ -415,12 +382,12 @@ where EitherTransport::Left(a) => match a.dial_as_listener(addr) { Ok(connec) => Ok(EitherFuture::First(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(EitherError::A(err))), + Err(Other(err)) => Err(Other(Either::Left(err))), }, EitherTransport::Right(b) => match b.dial_as_listener(addr) { Ok(connec) => Ok(EitherFuture::Second(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), - Err(Other(err)) => Err(Other(EitherError::B(err))), + Err(Other(err)) => Err(Other(Either::Right(err))), }, } } diff --git a/core/src/transport/and_then.rs b/core/src/transport/and_then.rs index 561a2f281ff..fb5280568ea 100644 --- a/core/src/transport/and_then.rs +++ b/core/src/transport/and_then.rs @@ -20,10 +20,10 @@ use crate::{ connection::{ConnectedPoint, Endpoint}, - either::EitherError, transport::{ListenerId, Transport, TransportError, TransportEvent}, }; -use futures::{future::Either, prelude::*}; +use either::Either; +use futures::prelude::*; use multiaddr::Multiaddr; use std::{error, marker::PhantomPinned, pin::Pin, task::Context, task::Poll}; @@ -50,14 +50,14 @@ where F::Error: error::Error, { type Output = O; - type Error = EitherError; + type Error = Either; type ListenerUpgrade = AndThenFuture; type Dial = AndThenFuture; fn listen_on(&mut self, addr: Multiaddr) -> Result> { self.transport .listen_on(addr) - .map_err(|err| err.map(EitherError::A)) + .map_err(|err| err.map(Either::Left)) } fn remove_listener(&mut self, id: ListenerId) -> bool { @@ -68,7 +68,7 @@ where let dialed_fut = self .transport .dial(addr.clone()) - .map_err(|err| err.map(EitherError::A))?; + .map_err(|err| err.map(Either::Left))?; let future = AndThenFuture { inner: Either::Left(Box::pin(dialed_fut)), args: Some(( @@ -90,7 +90,7 @@ where let dialed_fut = self .transport .dial_as_listener(addr.clone()) - .map_err(|err| err.map(EitherError::A))?; + .map_err(|err| err.map(Either::Left))?; let future = AndThenFuture { inner: Either::Left(Box::pin(dialed_fut)), args: Some(( @@ -139,7 +139,7 @@ where Poll::Ready(other) => { let mapped = other .map_upgrade(|_upgrade| unreachable!("case already matched")) - .map_err(EitherError::A); + .map_err(Either::Left); Poll::Ready(mapped) } Poll::Pending => Poll::Pending, @@ -163,7 +163,7 @@ where TMap: FnOnce(TFut::Ok, ConnectedPoint) -> TMapOut, TMapOut: TryFuture, { - type Output = Result>; + type Output = Result>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { loop { @@ -171,7 +171,7 @@ where Either::Left(future) => { let item = match TryFuture::try_poll(future.as_mut(), cx) { Poll::Ready(Ok(v)) => v, - Poll::Ready(Err(err)) => return Poll::Ready(Err(EitherError::A(err))), + Poll::Ready(Err(err)) => return Poll::Ready(Err(Either::Left(err))), Poll::Pending => return Poll::Pending, }; let (f, a) = self @@ -183,7 +183,7 @@ where Either::Right(future) => { return match TryFuture::try_poll(future.as_mut(), cx) { Poll::Ready(Ok(v)) => Poll::Ready(Ok(v)), - Poll::Ready(Err(err)) => return Poll::Ready(Err(EitherError::B(err))), + Poll::Ready(Err(err)) => return Poll::Ready(Err(Either::Right(err))), Poll::Pending => Poll::Pending, } } diff --git a/core/src/transport/choice.rs b/core/src/transport/choice.rs index 17528c1d4a8..2405f013039 100644 --- a/core/src/transport/choice.rs +++ b/core/src/transport/choice.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::{EitherError, EitherFuture, EitherOutput}; +use crate::either::{EitherFuture, EitherOutput}; use crate::transport::{ListenerId, Transport, TransportError, TransportEvent}; +use either::Either; use multiaddr::Multiaddr; use std::{pin::Pin, task::Context, task::Poll}; @@ -40,19 +41,19 @@ where A: Transport, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type ListenerUpgrade = EitherFuture; type Dial = EitherFuture; fn listen_on(&mut self, addr: Multiaddr) -> Result> { let addr = match self.0.listen_on(addr) { Err(TransportError::MultiaddrNotSupported(addr)) => addr, - res => return res.map_err(|err| err.map(EitherError::A)), + res => return res.map_err(|err| err.map(Either::Left)), }; let addr = match self.1.listen_on(addr) { Err(TransportError::MultiaddrNotSupported(addr)) => addr, - res => return res.map_err(|err| err.map(EitherError::B)), + res => return res.map_err(|err| err.map(Either::Right)), }; Err(TransportError::MultiaddrNotSupported(addr)) @@ -67,7 +68,7 @@ where Ok(connec) => return Ok(EitherFuture::First(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => addr, Err(TransportError::Other(err)) => { - return Err(TransportError::Other(EitherError::A(err))) + return Err(TransportError::Other(Either::Left(err))) } }; @@ -75,7 +76,7 @@ where Ok(connec) => return Ok(EitherFuture::Second(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => addr, Err(TransportError::Other(err)) => { - return Err(TransportError::Other(EitherError::B(err))) + return Err(TransportError::Other(Either::Right(err))) } }; @@ -90,7 +91,7 @@ where Ok(connec) => return Ok(EitherFuture::First(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => addr, Err(TransportError::Other(err)) => { - return Err(TransportError::Other(EitherError::A(err))) + return Err(TransportError::Other(Either::Left(err))) } }; @@ -98,7 +99,7 @@ where Ok(connec) => return Ok(EitherFuture::Second(connec)), Err(TransportError::MultiaddrNotSupported(addr)) => addr, Err(TransportError::Other(err)) => { - return Err(TransportError::Other(EitherError::B(err))) + return Err(TransportError::Other(Either::Right(err))) } }; @@ -120,13 +121,13 @@ where let this = self.project(); match this.0.poll(cx) { Poll::Ready(ev) => { - return Poll::Ready(ev.map_upgrade(EitherFuture::First).map_err(EitherError::A)) + return Poll::Ready(ev.map_upgrade(EitherFuture::First).map_err(Either::Left)) } Poll::Pending => {} } match this.1.poll(cx) { Poll::Ready(ev) => { - return Poll::Ready(ev.map_upgrade(EitherFuture::Second).map_err(EitherError::B)) + return Poll::Ready(ev.map_upgrade(EitherFuture::Second).map_err(Either::Right)) } Poll::Pending => {} } diff --git a/core/src/upgrade/either.rs b/core/src/upgrade/either.rs index 8b5c7f71422..99a2f557e5f 100644 --- a/core/src/upgrade/either.rs +++ b/core/src/upgrade/either.rs @@ -19,9 +19,10 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherError, EitherFuture2, EitherName, EitherOutput}, + either::{EitherFuture2, EitherName, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; +use either::Either; /// A type to represent two possible upgrade types (inbound or outbound). #[derive(Debug, Clone)] @@ -55,7 +56,7 @@ where B: InboundUpgrade, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type Future = EitherFuture2; fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { @@ -77,7 +78,7 @@ where B: OutboundUpgrade, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type Future = EitherFuture2; fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { diff --git a/core/src/upgrade/select.rs b/core/src/upgrade/select.rs index d1a8cabca2f..fed93bc0430 100644 --- a/core/src/upgrade/select.rs +++ b/core/src/upgrade/select.rs @@ -19,9 +19,10 @@ // DEALINGS IN THE SOFTWARE. use crate::{ - either::{EitherError, EitherFuture2, EitherName, EitherOutput}, + either::{EitherFuture2, EitherName, EitherOutput}, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo}, }; +use either::Either; /// Upgrade that combines two upgrades into one. Supports all the protocols supported by either /// sub-upgrade. @@ -64,7 +65,7 @@ where B: InboundUpgrade, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type Future = EitherFuture2; fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future { @@ -81,7 +82,7 @@ where B: OutboundUpgrade, { type Output = EitherOutput; - type Error = EitherError; + type Error = Either; type Future = EitherFuture2; fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future { diff --git a/examples/file-sharing.rs b/examples/file-sharing.rs index b38f456972b..6cae1f492a5 100644 --- a/examples/file-sharing.rs +++ b/examples/file-sharing.rs @@ -207,8 +207,8 @@ enum CliArgument { mod network { use super::*; use async_trait::async_trait; + use either::Either; use futures::channel::{mpsc, oneshot}; - use libp2p::core::either::EitherError; use libp2p::core::upgrade::{read_length_prefixed, write_length_prefixed, ProtocolName}; use libp2p::identity; use libp2p::identity::ed25519; @@ -405,7 +405,7 @@ mod network { &mut self, event: SwarmEvent< ComposedEvent, - EitherError, io::Error>, + Either, io::Error>, >, ) { match event { diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 301f2ee3d82..7e8270629ed 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -21,9 +21,10 @@ //! [`ConnectionHandler`] handling relayed connection potentially upgraded to a direct connection. use crate::protocol; +use either::Either; use futures::future::{BoxFuture, FutureExt}; use instant::Instant; -use libp2p_core::either::{EitherError, EitherOutput}; +use libp2p_core::either::EitherOutput; use libp2p_core::multiaddr::Multiaddr; use libp2p_core::upgrade::{self, DeniedUpgrade, NegotiationError, UpgradeError}; use libp2p_core::ConnectedPoint; @@ -134,7 +135,7 @@ pub struct Handler { /// A pending fatal error that results in the connection being closed. pending_error: Option< ConnectionHandlerUpgrErr< - EitherError, + Either, >, >, /// Queue of events to return when polled. @@ -252,8 +253,8 @@ impl Handler { // the remote peer and results in closing the connection. self.pending_error = Some(error.map_upgrade_err(|e| { e.map_err(|e| match e { - EitherError::A(e) => EitherError::A(e), - EitherError::B(v) => void::unreachable(v), + Either::Left(e) => Either::Left(e), + Either::Right(v) => void::unreachable(v), }) })); } @@ -292,7 +293,7 @@ impl Handler { _ => { // Anything else is considered a fatal error or misbehaviour of // the remote peer and results in closing the connection. - self.pending_error = Some(error.map_upgrade_err(|e| e.map_err(EitherError::B))); + self.pending_error = Some(error.map_upgrade_err(|e| e.map_err(Either::Right))); } } } @@ -302,7 +303,7 @@ impl ConnectionHandler for Handler { type InEvent = Command; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< - EitherError, + Either, >; type InboundProtocol = upgrade::EitherUpgrade; type OutboundProtocol = protocol::outbound::Upgrade; @@ -393,7 +394,7 @@ impl ConnectionHandler for Handler { } Err(e) => { return Poll::Ready(ConnectionHandlerEvent::Close( - ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))), + ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(e))), )) } } diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index bc3316d9bb0..3e844a2c8d0 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -23,6 +23,7 @@ prost = "0.11" smallvec = "1.6.1" thiserror = "1.0" void = "1.0" +either = "1.8.0" [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 21063acc661..0dce0e49940 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -21,11 +21,12 @@ use crate::protocol::{ self, Identify, InboundPush, Info, OutboundPush, Protocol, Push, UpgradeError, }; +use either::Either; use futures::future::BoxFuture; use futures::prelude::*; use futures::stream::FuturesUnordered; use futures_timer::Delay; -use libp2p_core::either::{EitherError, EitherOutput}; +use libp2p_core::either::EitherOutput; use libp2p_core::upgrade::{EitherUpgrade, SelectUpgrade}; use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PublicKey}; use libp2p_swarm::handler::{ @@ -259,8 +260,8 @@ impl Handler { let err = err.map_upgrade_err(|e| match e { UpgradeError::Select(e) => UpgradeError::Select(e), - UpgradeError::Apply(EitherError::A(ioe)) => UpgradeError::Apply(ioe), - UpgradeError::Apply(EitherError::B(ioe)) => UpgradeError::Apply(ioe), + UpgradeError::Apply(Either::Left(ioe)) => UpgradeError::Apply(ioe), + UpgradeError::Apply(Either::Right(ioe)) => UpgradeError::Apply(ioe), }); self.events .push(ConnectionHandlerEvent::Custom(Event::IdentificationError( diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 120e980c001..1aaa47b6345 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -31,7 +31,6 @@ use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; use instant::Instant; use libp2p_core::connection::ConnectionId; -use libp2p_core::either::EitherError; use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, @@ -397,7 +396,7 @@ pub struct Handler { /// A pending fatal error that results in the connection being closed. pending_error: Option< ConnectionHandlerUpgrErr< - EitherError, + Either, >, >, @@ -521,7 +520,7 @@ impl Handler { inbound_hop::UpgradeError::Fatal(error), )) => { self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade( - upgrade::UpgradeError::Apply(EitherError::A(error)), + upgrade::UpgradeError::Apply(Either::Left(error)), )); return; } @@ -572,7 +571,7 @@ impl Handler { ConnectionHandlerUpgrErr::Upgrade(upgrade::UpgradeError::Apply(error)) => match error { outbound_stop::UpgradeError::Fatal(error) => { self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade( - upgrade::UpgradeError::Apply(EitherError::B(error)), + upgrade::UpgradeError::Apply(Either::Right(error)), )); return; } @@ -624,7 +623,7 @@ impl ConnectionHandler for Handler { type InEvent = In; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< - EitherError, + Either, >; type InboundProtocol = inbound_hop::Upgrade; type OutboundProtocol = outbound_stop::Upgrade; diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index c9f7c18adbb..d64732766b1 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -28,7 +28,6 @@ use futures::sink::SinkExt; use futures::stream::{FuturesUnordered, StreamExt}; use futures_timer::Delay; use instant::Instant; -use libp2p_core::either::EitherError; use libp2p_core::multiaddr::Protocol; use libp2p_core::{upgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::handler::{ @@ -174,7 +173,7 @@ pub struct Handler { /// A pending fatal error that results in the connection being closed. pending_error: Option< ConnectionHandlerUpgrErr< - EitherError, + Either, >, >, /// Until when to keep the connection alive. @@ -366,7 +365,7 @@ impl Handler { inbound_stop::UpgradeError::Fatal(error), )) => { self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade( - upgrade::UpgradeError::Apply(EitherError::A(error)), + upgrade::UpgradeError::Apply(Either::Left(error)), )); return; } @@ -413,7 +412,7 @@ impl Handler { match error { outbound_hop::UpgradeError::Fatal(error) => { self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade( - upgrade::UpgradeError::Apply(EitherError::B(error)), + upgrade::UpgradeError::Apply(Either::Right(error)), )); return; } @@ -476,7 +475,7 @@ impl Handler { match error { outbound_hop::UpgradeError::Fatal(error) => { self.pending_error = Some(ConnectionHandlerUpgrErr::Upgrade( - upgrade::UpgradeError::Apply(EitherError::B(error)), + upgrade::UpgradeError::Apply(Either::Right(error)), )); return; } @@ -510,7 +509,7 @@ impl ConnectionHandler for Handler { type InEvent = In; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< - EitherError, + Either, >; type InboundProtocol = inbound_stop::Upgrade; type OutboundProtocol = outbound_hop::Upgrade; diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index ae198dc2bd3..1606e5e53a3 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -28,7 +28,7 @@ use crate::upgrade::SendWrapper; use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use either::Either; use libp2p_core::{ - either::{EitherError, EitherOutput}, + either::EitherOutput, upgrade::{DeniedUpgrade, EitherUpgrade}, ConnectedPoint, Multiaddr, PeerId, }; @@ -215,8 +215,8 @@ where ConnectionHandlerUpgrErr::Timer => ConnectionHandlerUpgrErr::Timer, ConnectionHandlerUpgrErr::Upgrade(err) => { ConnectionHandlerUpgrErr::Upgrade(err.map_err(|err| match err { - EitherError::A(e) => e, - EitherError::B(v) => void::unreachable(v), + Either::Left(e) => e, + Either::Right(v) => void::unreachable(v), })) } }; diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index 4be677a55f6..f03afb20499 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -25,7 +25,7 @@ use crate::handler::{ }; use crate::upgrade::SendWrapper; use either::Either; -use libp2p_core::either::{EitherError, EitherOutput}; +use libp2p_core::either::EitherOutput; use libp2p_core::upgrade::{EitherUpgrade, UpgradeError}; use libp2p_core::{ConnectedPoint, PeerId}; use std::task::{Context, Poll}; @@ -145,14 +145,14 @@ where fn transpose(self) -> Either, DialUpgradeError> { match self { DialUpgradeError { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(error))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(error))), info: Either::Left(info), } => Either::Left(DialUpgradeError { error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)), info, }), DialUpgradeError { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(error))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(error))), info: Either::Right(info), } => Either::Right(DialUpgradeError { error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)), @@ -214,14 +214,14 @@ where fn transpose(self) -> Either, ListenUpgradeError> { match self { ListenUpgradeError { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(error))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(error))), info: Either::Left(info), } => Either::Left(ListenUpgradeError { error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)), info, }), ListenUpgradeError { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(error))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(error))), info: Either::Right(info), } => Either::Right(ListenUpgradeError { error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(error)), diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index c979d91c71e..8d052d637d7 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -28,7 +28,7 @@ use crate::upgrade::SendWrapper; use either::Either; use libp2p_core::{ - either::{EitherError, EitherOutput}, + either::EitherOutput, upgrade::{EitherUpgrade, NegotiationError, ProtocolError, SelectUpgrade, UpgradeError}, ConnectedPoint, PeerId, }; @@ -186,7 +186,7 @@ where }), DialUpgradeError { info: EitherOutput::First(info), - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(err))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(err))), } => Either::Left(DialUpgradeError { info, error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)), @@ -214,7 +214,7 @@ where }), DialUpgradeError { info: EitherOutput::Second(info), - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(err))), + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(err))), } => Either::Right(DialUpgradeError { info, error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(err)), @@ -318,14 +318,14 @@ where error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(e2)), })); } - ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::A(e))) => { + ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Left(e))) => { self.proto1 .on_connection_event(ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: i1, error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(e)), })); } - ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(EitherError::B(e))) => { + ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Apply(Either::Right(e))) => { self.proto2 .on_connection_event(ConnectionEvent::ListenUpgradeError(ListenUpgradeError { info: i2, @@ -343,7 +343,7 @@ where { type InEvent = EitherOutput; type OutEvent = EitherOutput; - type Error = EitherError; + type Error = Either; type InboundProtocol = SelectUpgrade< SendWrapper<::InboundProtocol>, SendWrapper<::InboundProtocol>, @@ -395,7 +395,7 @@ where return Poll::Ready(ConnectionHandlerEvent::Custom(EitherOutput::First(event))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Close(EitherError::A(event))); + return Poll::Ready(ConnectionHandlerEvent::Close(Either::Left(event))); } Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { @@ -412,7 +412,7 @@ where return Poll::Ready(ConnectionHandlerEvent::Custom(EitherOutput::Second(event))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Close(EitherError::B(event))); + return Poll::Ready(ConnectionHandlerEvent::Close(Either::Right(event))); } Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol }) => { return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest { diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index cf0b4c3b08a..47fc410a356 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1750,12 +1750,11 @@ fn p2p_addr(peer: Option, addr: Multiaddr) -> Result { + future::Either::Left((TransportEvent::Incoming { .. }, _)) => { break; } - Either::Left(_) => { + future::Either::Left(_) => { panic!("Unexpected transport event.") } - Either::Right((e, _)) => { + future::Either::Right((e, _)) => { panic!("Expect swarm to not emit any event {e:?}") } } @@ -2633,7 +2632,7 @@ mod tests { "/ip4/127.0.0.1/tcp/80".parse().unwrap(), TransportError::Other(io::Error::new( io::ErrorKind::Other, - EitherError::<_, Void>::A(EitherError::::B(UpgradeError::Apply( + Either::<_, Void>::Left(Either::::Right(UpgradeError::Apply( MemoryTransportError::Unreachable, ))), )), From 2ce805d77770a592175d6af45d12245105504762 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:02:25 +1100 Subject: [PATCH 06/12] Add changelog entry --- core/CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index a7e24f5d616..8c639dbbc0d 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -12,11 +12,14 @@ - Improve error messages in case keys cannot be decoded because of missing feature flags. See [PR 2972]. +- Remove `EitherError` in favor of `either::Either`. See [PR XXXX]. + [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 [PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090 [PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972 +[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX # 0.37.0 From 0a2729de32375e93eeea59e5bf2434c89d064134 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:16:51 +1100 Subject: [PATCH 07/12] Remove `EitherTransport` in favor of `Either` --- Cargo.toml | 1 + core/src/either.rs | 36 +++++++++++++++--------------------- examples/ipfs-private.rs | 9 ++++----- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5103c7802b8..c0f97e48109 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -132,6 +132,7 @@ libp2p-gossipsub = { version = "0.44.0", path = "protocols/gossipsub", optional [dev-dependencies] async-std = { version = "1.6.2", features = ["attributes"] } async-trait = "0.1" +either = "1.8.0" env_logger = "0.10.0" clap = { version = "4.0.13", features = ["derive"] } tokio = { version = "1.15", features = ["io-util", "io-std", "macros", "rt", "rt-multi-thread"] } diff --git a/core/src/either.rs b/core/src/either.rs index a34552bf28f..8ade73a3da4 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -24,6 +24,7 @@ use crate::{ transport::{ListenerId, Transport, TransportError, TransportEvent}, Multiaddr, ProtocolName, }; +use either::Either; use futures::{ io::{IoSlice, IoSliceMut}, prelude::*, @@ -324,15 +325,8 @@ impl ProtocolName for EitherName { } } } -#[pin_project(project = EitherTransportProj)] -#[derive(Debug)] -#[must_use = "transports do nothing unless polled"] -pub enum EitherTransport { - Left(#[pin] A), - Right(#[pin] B), -} -impl Transport for EitherTransport +impl Transport for Either where B: Transport, A: Transport, @@ -346,8 +340,8 @@ where self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll> { - match self.project() { - EitherTransportProj::Left(a) => match a.poll(cx) { + match self.as_pin_mut() { + Either::Left(a) => match a.poll(cx) { Poll::Pending => Poll::Pending, Poll::Ready(event) => Poll::Ready( event @@ -355,7 +349,7 @@ where .map_err(EitherError::A), ), }, - EitherTransportProj::Right(b) => match b.poll(cx) { + Either::Right(b) => match b.poll(cx) { Poll::Pending => Poll::Pending, Poll::Ready(event) => Poll::Ready( event @@ -368,19 +362,19 @@ where fn remove_listener(&mut self, id: ListenerId) -> bool { match self { - EitherTransport::Left(t) => t.remove_listener(id), - EitherTransport::Right(t) => t.remove_listener(id), + Either::Left(t) => t.remove_listener(id), + Either::Right(t) => t.remove_listener(id), } } fn listen_on(&mut self, addr: Multiaddr) -> Result> { use TransportError::*; match self { - EitherTransport::Left(a) => a.listen_on(addr).map_err(|e| match e { + Either::Left(a) => a.listen_on(addr).map_err(|e| match e { MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr), Other(err) => Other(EitherError::A(err)), }), - EitherTransport::Right(b) => b.listen_on(addr).map_err(|e| match e { + Either::Right(b) => b.listen_on(addr).map_err(|e| match e { MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr), Other(err) => Other(EitherError::B(err)), }), @@ -390,12 +384,12 @@ where fn dial(&mut self, addr: Multiaddr) -> Result> { use TransportError::*; match self { - EitherTransport::Left(a) => match a.dial(addr) { + Either::Left(a) => match a.dial(addr) { Ok(connec) => Ok(EitherFuture::First(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(EitherError::A(err))), }, - EitherTransport::Right(b) => match b.dial(addr) { + Either::Right(b) => match b.dial(addr) { Ok(connec) => Ok(EitherFuture::Second(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(EitherError::B(err))), @@ -412,12 +406,12 @@ where { use TransportError::*; match self { - EitherTransport::Left(a) => match a.dial_as_listener(addr) { + Either::Left(a) => match a.dial_as_listener(addr) { Ok(connec) => Ok(EitherFuture::First(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(EitherError::A(err))), }, - EitherTransport::Right(b) => match b.dial_as_listener(addr) { + Either::Right(b) => match b.dial_as_listener(addr) { Ok(connec) => Ok(EitherFuture::Second(connec)), Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)), Err(Other(err)) => Err(Other(EitherError::B(err))), @@ -427,8 +421,8 @@ where fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option { match self { - EitherTransport::Left(a) => a.address_translation(server, observed), - EitherTransport::Right(b) => b.address_translation(server, observed), + Either::Left(a) => a.address_translation(server, observed), + Either::Right(b) => b.address_translation(server, observed), } } } diff --git a/examples/ipfs-private.rs b/examples/ipfs-private.rs index db3907f4496..af0eab7aedf 100644 --- a/examples/ipfs-private.rs +++ b/examples/ipfs-private.rs @@ -32,11 +32,10 @@ //! You can ping this node, or use pubsub (gossipsub) on the topic "chat". For this //! to work, the ipfs node needs to be configured to use gossipsub. use async_std::io; +use either::Either; use futures::{prelude::*, select}; use libp2p::{ - core::{ - either::EitherTransport, muxing::StreamMuxerBox, transport, transport::upgrade::Version, - }, + core::{muxing::StreamMuxerBox, transport, transport::upgrade::Version}, gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity}, identify, identity, multiaddr::Protocol, @@ -59,10 +58,10 @@ pub fn build_transport( let base_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true)); let maybe_encrypted = match psk { - Some(psk) => EitherTransport::Left( + Some(psk) => Either::Left( base_transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)), ), - None => EitherTransport::Right(base_transport), + None => Either::Right(base_transport), }; maybe_encrypted .upgrade(Version::V1) From 3397266195e15a9071573e7e213ebc7548f743e3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:17:26 +1100 Subject: [PATCH 08/12] Update core/CHANGELOG.md --- core/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 8c639dbbc0d..f9f293e6e38 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -12,14 +12,14 @@ - Improve error messages in case keys cannot be decoded because of missing feature flags. See [PR 2972]. -- Remove `EitherError` in favor of `either::Either`. See [PR XXXX]. +- Remove `EitherError` in favor of `either::Either`. See [PR 3337]. [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 [PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090 [PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972 -[PR XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX +[PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337 # 0.37.0 From 7060f4bdffa06c7b89539ca1384d9e6f3596ff31 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:18:19 +1100 Subject: [PATCH 09/12] Add changelog entry --- core/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 8c639dbbc0d..620b85fc4f1 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -14,6 +14,8 @@ - Remove `EitherError` in favor of `either::Either`. See [PR XXXX]. +- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR XXXX]. + [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 From 59f6465c6dbf23eecc9076f431d62a666d637c8f Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:22:07 +1100 Subject: [PATCH 10/12] Update changelog entry --- core/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 14a78516f38..85330faf704 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -14,7 +14,7 @@ - Remove `EitherError` in favor of `either::Either`. See [PR 3337]. -- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR XXXX]. +- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338]. [PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031 [PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058 @@ -22,6 +22,7 @@ [PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090 [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 # 0.37.0 From 8872039b187e4e1a2c1c77cc70f0f1dc8008663d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:28:03 +1100 Subject: [PATCH 11/12] Remove `EitherUpgrade` --- core/CHANGELOG.md | 3 +++ core/src/upgrade.rs | 1 - core/src/upgrade/either.rs | 25 ++++++++------------ protocols/dcutr/src/handler.rs | 10 ++++---- protocols/dcutr/src/handler/relayed.rs | 8 +++---- protocols/identify/src/handler.rs | 14 ++++------- protocols/kad/src/handler.rs | 11 +++++---- protocols/ping/Cargo.toml | 1 + protocols/ping/tests/ping.rs | 5 ++-- protocols/relay/src/behaviour/handler.rs | 2 +- protocols/relay/src/priv_client/handler.rs | 2 +- swarm/src/behaviour/toggle.rs | 18 +++++---------- swarm/src/handler/either.rs | 27 +++++++++++----------- swarm/src/handler/select.rs | 19 ++++++--------- 14 files changed, 64 insertions(+), 82 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 85330faf704..2b0ca83b439 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 XXXX]. + [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 XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX # 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 1aaa47b6345..1535fc05ff6 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -367,7 +367,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 d64732766b1..7c7a6c28d25 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -162,7 +162,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), }); } From d3e79cf3432ea7cf6c9d8a6e4a89d8b884bed250 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 17 Jan 2023 23:30:38 +1100 Subject: [PATCH 12/12] Update core/CHANGELOG.md --- core/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 2b0ca83b439..bbb812dd87b 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -16,7 +16,7 @@ - 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 XXXX]. +- 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 @@ -25,7 +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 XXXX]: https://github.com/libp2p/rust-libp2p/pull/XXXX +[PR 3339]: https://github.com/libp2p/rust-libp2p/pull/3339 # 0.37.0