diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index f9f293e6e38..85330faf704 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -14,12 +14,15 @@ - Remove `EitherError` in favor of `either::Either`. See [PR 3337]. +- 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 [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 3337]: https://github.com/libp2p/rust-libp2p/pull/3337 +[PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338 # 0.37.0 diff --git a/core/src/either.rs b/core/src/either.rs index 411251cbd84..5c2ca8f377a 100644 --- a/core/src/either.rs +++ b/core/src/either.rs @@ -293,15 +293,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, @@ -315,14 +308,14 @@ 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.map_upgrade(EitherFuture::First).map_err(Either::Left)) } }, - EitherTransportProj::Right(b) => match b.poll(cx) { + Either::Right(b) => match b.poll(cx) { Poll::Pending => Poll::Pending, Poll::Ready(event) => Poll::Ready( event @@ -335,19 +328,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(Either::Left(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(Either::Right(err)), }), @@ -357,12 +350,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(Either::Left(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(Either::Right(err))), @@ -379,12 +372,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(Either::Left(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(Either::Right(err))), @@ -394,8 +387,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)