Skip to content

Commit c18939f

Browse files
refactor(core)!: remove EitherTransport (#3338)
We don't need to define our own type here, we can simply implement `Transport` on `either::Either`.
1 parent f4fed38 commit c18939f

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414

1515
- Remove `EitherError` in favor of `either::Either`. See [PR 3337].
1616

17+
- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338].
18+
1719
[PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031
1820
[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058
1921
[PR 3097]: https://github.com/libp2p/rust-libp2p/pull/3097
2022
[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090
2123
[PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972
2224
[PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337
25+
[PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338
2326

2427
# 0.37.0
2528

core/src/either.rs

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,8 @@ impl<A: ProtocolName, B: ProtocolName> ProtocolName for EitherName<A, B> {
293293
}
294294
}
295295
}
296-
#[pin_project(project = EitherTransportProj)]
297-
#[derive(Debug)]
298-
#[must_use = "transports do nothing unless polled"]
299-
pub enum EitherTransport<A, B> {
300-
Left(#[pin] A),
301-
Right(#[pin] B),
302-
}
303296

304-
impl<A, B> Transport for EitherTransport<A, B>
297+
impl<A, B> Transport for Either<A, B>
305298
where
306299
B: Transport,
307300
A: Transport,
@@ -315,14 +308,14 @@ where
315308
self: Pin<&mut Self>,
316309
cx: &mut Context<'_>,
317310
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
318-
match self.project() {
319-
EitherTransportProj::Left(a) => match a.poll(cx) {
311+
match self.as_pin_mut() {
312+
Either::Left(a) => match a.poll(cx) {
320313
Poll::Pending => Poll::Pending,
321314
Poll::Ready(event) => {
322315
Poll::Ready(event.map_upgrade(EitherFuture::First).map_err(Either::Left))
323316
}
324317
},
325-
EitherTransportProj::Right(b) => match b.poll(cx) {
318+
Either::Right(b) => match b.poll(cx) {
326319
Poll::Pending => Poll::Pending,
327320
Poll::Ready(event) => Poll::Ready(
328321
event
@@ -335,19 +328,19 @@ where
335328

336329
fn remove_listener(&mut self, id: ListenerId) -> bool {
337330
match self {
338-
EitherTransport::Left(t) => t.remove_listener(id),
339-
EitherTransport::Right(t) => t.remove_listener(id),
331+
Either::Left(t) => t.remove_listener(id),
332+
Either::Right(t) => t.remove_listener(id),
340333
}
341334
}
342335

343336
fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<Self::Error>> {
344337
use TransportError::*;
345338
match self {
346-
EitherTransport::Left(a) => a.listen_on(addr).map_err(|e| match e {
339+
Either::Left(a) => a.listen_on(addr).map_err(|e| match e {
347340
MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr),
348341
Other(err) => Other(Either::Left(err)),
349342
}),
350-
EitherTransport::Right(b) => b.listen_on(addr).map_err(|e| match e {
343+
Either::Right(b) => b.listen_on(addr).map_err(|e| match e {
351344
MultiaddrNotSupported(addr) => MultiaddrNotSupported(addr),
352345
Other(err) => Other(Either::Right(err)),
353346
}),
@@ -357,12 +350,12 @@ where
357350
fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
358351
use TransportError::*;
359352
match self {
360-
EitherTransport::Left(a) => match a.dial(addr) {
353+
Either::Left(a) => match a.dial(addr) {
361354
Ok(connec) => Ok(EitherFuture::First(connec)),
362355
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
363356
Err(Other(err)) => Err(Other(Either::Left(err))),
364357
},
365-
EitherTransport::Right(b) => match b.dial(addr) {
358+
Either::Right(b) => match b.dial(addr) {
366359
Ok(connec) => Ok(EitherFuture::Second(connec)),
367360
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
368361
Err(Other(err)) => Err(Other(Either::Right(err))),
@@ -379,12 +372,12 @@ where
379372
{
380373
use TransportError::*;
381374
match self {
382-
EitherTransport::Left(a) => match a.dial_as_listener(addr) {
375+
Either::Left(a) => match a.dial_as_listener(addr) {
383376
Ok(connec) => Ok(EitherFuture::First(connec)),
384377
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
385378
Err(Other(err)) => Err(Other(Either::Left(err))),
386379
},
387-
EitherTransport::Right(b) => match b.dial_as_listener(addr) {
380+
Either::Right(b) => match b.dial_as_listener(addr) {
388381
Ok(connec) => Ok(EitherFuture::Second(connec)),
389382
Err(MultiaddrNotSupported(addr)) => Err(MultiaddrNotSupported(addr)),
390383
Err(Other(err)) => Err(Other(Either::Right(err))),
@@ -394,8 +387,8 @@ where
394387

395388
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
396389
match self {
397-
EitherTransport::Left(a) => a.address_translation(server, observed),
398-
EitherTransport::Right(b) => b.address_translation(server, observed),
390+
Either::Left(a) => a.address_translation(server, observed),
391+
Either::Right(b) => b.address_translation(server, observed),
399392
}
400393
}
401394
}

examples/ipfs-private.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
//! You can ping this node, or use pubsub (gossipsub) on the topic "chat". For this
3333
//! to work, the ipfs node needs to be configured to use gossipsub.
3434
use async_std::io;
35+
use either::Either;
3536
use futures::{prelude::*, select};
3637
use libp2p::{
37-
core::{
38-
either::EitherTransport, muxing::StreamMuxerBox, transport, transport::upgrade::Version,
39-
},
38+
core::{muxing::StreamMuxerBox, transport, transport::upgrade::Version},
4039
gossipsub::{self, Gossipsub, GossipsubConfigBuilder, GossipsubEvent, MessageAuthenticity},
4140
identify, identity,
4241
multiaddr::Protocol,
@@ -59,10 +58,10 @@ pub fn build_transport(
5958

6059
let base_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true));
6160
let maybe_encrypted = match psk {
62-
Some(psk) => EitherTransport::Left(
61+
Some(psk) => Either::Left(
6362
base_transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
6463
),
65-
None => EitherTransport::Right(base_transport),
64+
None => Either::Right(base_transport),
6665
};
6766
maybe_encrypted
6867
.upgrade(Version::V1)

0 commit comments

Comments
 (0)