Skip to content

Commit db2cd43

Browse files
refactor(core)!: remove EitherUpgrade (#3339)
We don't need to define our own type here, we can simply implement `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`.
1 parent 8cd14e6 commit db2cd43

File tree

14 files changed

+64
-82
lines changed

14 files changed

+64
-82
lines changed

core/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@
1616

1717
- Remove `EitherTransport` in favor of implementing `Transport` on `either::Either`. See [PR 3338].
1818

19+
- Remove `EitherUpgrade` in favor of implementing `UpgradeInfo`, `InboundUpgrade` and `OutboundUpgrade` on `either::Either`. See [PR 3339].
20+
1921
[PR 3031]: https://github.com/libp2p/rust-libp2p/pull/3031
2022
[PR 3058]: https://github.com/libp2p/rust-libp2p/pull/3058
2123
[PR 3097]: https://github.com/libp2p/rust-libp2p/pull/3097
2224
[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090
2325
[PR 2972]: https://github.com/libp2p/rust-libp2p/pull/2972
2426
[PR 3337]: https://github.com/libp2p/rust-libp2p/pull/3337
2527
[PR 3338]: https://github.com/libp2p/rust-libp2p/pull/3338
28+
[PR 3339]: https://github.com/libp2p/rust-libp2p/pull/3339
2629

2730
# 0.37.0
2831

core/src/upgrade.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ use futures::future::Future;
7474
pub use self::{
7575
apply::{apply, apply_inbound, apply_outbound, InboundUpgradeApply, OutboundUpgradeApply},
7676
denied::DeniedUpgrade,
77-
either::EitherUpgrade,
7877
error::UpgradeError,
7978
from_fn::{from_fn, FromFnUpgrade},
8079
map::{MapInboundUpgrade, MapInboundUpgradeErr, MapOutboundUpgrade, MapOutboundUpgradeErr},

core/src/upgrade/either.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,7 @@ use crate::{
2424
};
2525
use either::Either;
2626

27-
/// A type to represent two possible upgrade types (inbound or outbound).
28-
#[derive(Debug, Clone)]
29-
pub enum EitherUpgrade<A, B> {
30-
A(A),
31-
B(B),
32-
}
33-
34-
impl<A, B> UpgradeInfo for EitherUpgrade<A, B>
27+
impl<A, B> UpgradeInfo for Either<A, B>
3528
where
3629
A: UpgradeInfo,
3730
B: UpgradeInfo,
@@ -44,13 +37,13 @@ where
4437

4538
fn protocol_info(&self) -> Self::InfoIter {
4639
match self {
47-
EitherUpgrade::A(a) => EitherIter::A(a.protocol_info().into_iter()),
48-
EitherUpgrade::B(b) => EitherIter::B(b.protocol_info().into_iter()),
40+
Either::Left(a) => EitherIter::A(a.protocol_info().into_iter()),
41+
Either::Right(b) => EitherIter::B(b.protocol_info().into_iter()),
4942
}
5043
}
5144
}
5245

53-
impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for EitherUpgrade<A, B>
46+
impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for Either<A, B>
5447
where
5548
A: InboundUpgrade<C, Output = TA, Error = EA>,
5649
B: InboundUpgrade<C, Output = TB, Error = EB>,
@@ -61,18 +54,18 @@ where
6154

6255
fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
6356
match (self, info) {
64-
(EitherUpgrade::A(a), EitherName::A(info)) => {
57+
(Either::Left(a), EitherName::A(info)) => {
6558
EitherFuture2::A(a.upgrade_inbound(sock, info))
6659
}
67-
(EitherUpgrade::B(b), EitherName::B(info)) => {
60+
(Either::Right(b), EitherName::B(info)) => {
6861
EitherFuture2::B(b.upgrade_inbound(sock, info))
6962
}
7063
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_inbound"),
7164
}
7265
}
7366
}
7467

75-
impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for EitherUpgrade<A, B>
68+
impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for Either<A, B>
7669
where
7770
A: OutboundUpgrade<C, Output = TA, Error = EA>,
7871
B: OutboundUpgrade<C, Output = TB, Error = EB>,
@@ -83,10 +76,10 @@ where
8376

8477
fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
8578
match (self, info) {
86-
(EitherUpgrade::A(a), EitherName::A(info)) => {
79+
(Either::Left(a), EitherName::A(info)) => {
8780
EitherFuture2::A(a.upgrade_outbound(sock, info))
8881
}
89-
(EitherUpgrade::B(b), EitherName::B(info)) => {
82+
(Either::Right(b), EitherName::B(info)) => {
9083
EitherFuture2::B(b.upgrade_outbound(sock, info))
9184
}
9285
_ => panic!("Invalid invocation of EitherUpgrade::upgrade_outbound"),

protocols/dcutr/src/handler.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use crate::protocol;
2222
use either::Either;
2323
use libp2p_core::connection::ConnectionId;
24-
use libp2p_core::upgrade::{self, DeniedUpgrade};
24+
use libp2p_core::upgrade::DeniedUpgrade;
2525
use libp2p_core::{ConnectedPoint, PeerId};
2626
use libp2p_swarm::dummy;
2727
use libp2p_swarm::handler::SendWrapper;
@@ -70,11 +70,11 @@ impl IntoConnectionHandler for Prototype {
7070

7171
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
7272
match self {
73-
Prototype::UnknownConnection => upgrade::EitherUpgrade::A(SendWrapper(
74-
upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}),
75-
)),
73+
Prototype::UnknownConnection => {
74+
Either::Left(SendWrapper(Either::Left(protocol::inbound::Upgrade {})))
75+
}
7676
Prototype::DirectConnection { .. } => {
77-
upgrade::EitherUpgrade::A(SendWrapper(upgrade::EitherUpgrade::B(DeniedUpgrade)))
77+
Either::Left(SendWrapper(Either::Right(DeniedUpgrade)))
7878
}
7979
}
8080
}

protocols/dcutr/src/handler/relayed.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use futures::future::{BoxFuture, FutureExt};
2626
use instant::Instant;
2727
use libp2p_core::either::EitherOutput;
2828
use libp2p_core::multiaddr::Multiaddr;
29-
use libp2p_core::upgrade::{self, DeniedUpgrade, NegotiationError, UpgradeError};
29+
use libp2p_core::upgrade::{DeniedUpgrade, NegotiationError, UpgradeError};
3030
use libp2p_core::ConnectedPoint;
3131
use libp2p_swarm::handler::{
3232
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
@@ -305,23 +305,23 @@ impl ConnectionHandler for Handler {
305305
type Error = ConnectionHandlerUpgrErr<
306306
Either<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
307307
>;
308-
type InboundProtocol = upgrade::EitherUpgrade<protocol::inbound::Upgrade, DeniedUpgrade>;
308+
type InboundProtocol = Either<protocol::inbound::Upgrade, DeniedUpgrade>;
309309
type OutboundProtocol = protocol::outbound::Upgrade;
310310
type OutboundOpenInfo = u8; // Number of upgrade attempts.
311311
type InboundOpenInfo = ();
312312

313313
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
314314
match self.endpoint {
315315
ConnectedPoint::Dialer { .. } => {
316-
SubstreamProtocol::new(upgrade::EitherUpgrade::A(protocol::inbound::Upgrade {}), ())
316+
SubstreamProtocol::new(Either::Left(protocol::inbound::Upgrade {}), ())
317317
}
318318
ConnectedPoint::Listener { .. } => {
319319
// By the protocol specification the listening side of a relayed connection
320320
// initiates the _direct connection upgrade_. In other words the listening side of
321321
// the relayed connection opens a substream to the dialing side. (Connection roles
322322
// and substream roles are reversed.) The listening side on a relayed connection
323323
// never expects incoming substreams, hence the denied upgrade below.
324-
SubstreamProtocol::new(upgrade::EitherUpgrade::B(DeniedUpgrade), ())
324+
SubstreamProtocol::new(Either::Right(DeniedUpgrade), ())
325325
}
326326
}
327327
}

protocols/identify/src/handler.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use futures::prelude::*;
2727
use futures::stream::FuturesUnordered;
2828
use futures_timer::Delay;
2929
use libp2p_core::either::EitherOutput;
30-
use libp2p_core::upgrade::{EitherUpgrade, SelectUpgrade};
30+
use libp2p_core::upgrade::SelectUpgrade;
3131
use libp2p_core::{ConnectedPoint, Multiaddr, PeerId, PublicKey};
3232
use libp2p_swarm::handler::{
3333
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound,
@@ -102,8 +102,7 @@ pub struct Handler {
102102
inbound_identify_push: Option<BoxFuture<'static, Result<Info, UpgradeError>>>,
103103
/// Pending events to yield.
104104
events: SmallVec<
105-
[ConnectionHandlerEvent<EitherUpgrade<Identify, Push<OutboundPush>>, (), Event, io::Error>;
106-
4],
105+
[ConnectionHandlerEvent<Either<Identify, Push<OutboundPush>>, (), Event, io::Error>; 4],
107106
>,
108107

109108
/// Streams awaiting `BehaviourInfo` to then send identify requests.
@@ -277,7 +276,7 @@ impl ConnectionHandler for Handler {
277276
type OutEvent = Event;
278277
type Error = io::Error;
279278
type InboundProtocol = SelectUpgrade<Identify, Push<InboundPush>>;
280-
type OutboundProtocol = EitherUpgrade<Identify, Push<OutboundPush>>;
279+
type OutboundProtocol = Either<Identify, Push<OutboundPush>>;
281280
type OutboundOpenInfo = ();
282281
type InboundOpenInfo = ();
283282

@@ -306,10 +305,7 @@ impl ConnectionHandler for Handler {
306305
Protocol::Push => {
307306
self.events
308307
.push(ConnectionHandlerEvent::OutboundSubstreamRequest {
309-
protocol: SubstreamProtocol::new(
310-
EitherUpgrade::B(Push::outbound(info)),
311-
(),
312-
),
308+
protocol: SubstreamProtocol::new(Either::Right(Push::outbound(info)), ()),
313309
});
314310
}
315311
Protocol::Identify(_) => {
@@ -347,7 +343,7 @@ impl ConnectionHandler for Handler {
347343
Poll::Ready(()) => {
348344
self.trigger_next_identify.reset(self.interval);
349345
let ev = ConnectionHandlerEvent::OutboundSubstreamRequest {
350-
protocol: SubstreamProtocol::new(EitherUpgrade::A(Identify), ()),
346+
protocol: SubstreamProtocol::new(Either::Left(Identify), ()),
351347
};
352348
return Poll::Ready(ev);
353349
}

protocols/kad/src/handler.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::protocol::{
2323
KademliaProtocolConfig,
2424
};
2525
use crate::record::{self, Record};
26+
use either::Either;
2627
use futures::prelude::*;
2728
use futures::stream::SelectAll;
2829
use instant::Instant;
@@ -68,9 +69,9 @@ impl<T: Clone + fmt::Debug + Send + 'static + Unpin> IntoConnectionHandler
6869

6970
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
7071
if self.config.allow_listening {
71-
upgrade::EitherUpgrade::A(self.config.protocol_config.clone())
72+
Either::Left(self.config.protocol_config.clone())
7273
} else {
73-
upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade)
74+
Either::Right(upgrade::DeniedUpgrade)
7475
}
7576
}
7677
}
@@ -633,7 +634,7 @@ where
633634
type InEvent = KademliaHandlerIn<TUserData>;
634635
type OutEvent = KademliaHandlerEvent<TUserData>;
635636
type Error = io::Error; // TODO: better error type?
636-
type InboundProtocol = upgrade::EitherUpgrade<KademliaProtocolConfig, upgrade::DeniedUpgrade>;
637+
type InboundProtocol = Either<KademliaProtocolConfig, upgrade::DeniedUpgrade>;
637638
type OutboundProtocol = KademliaProtocolConfig;
638639
// Message of the request to send to the remote, and user data if we expect an answer.
639640
type OutboundOpenInfo = (KadRequestMsg, Option<TUserData>);
@@ -642,9 +643,9 @@ where
642643
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
643644
if self.config.allow_listening {
644645
SubstreamProtocol::new(self.config.protocol_config.clone(), ())
645-
.map_upgrade(upgrade::EitherUpgrade::A)
646+
.map_upgrade(Either::Left)
646647
} else {
647-
SubstreamProtocol::new(upgrade::EitherUpgrade::B(upgrade::DeniedUpgrade), ())
648+
SubstreamProtocol::new(Either::Right(upgrade::DeniedUpgrade), ())
648649
}
649650
}
650651

protocols/ping/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
1111
categories = ["network-programming", "asynchronous"]
1212

1313
[dependencies]
14+
either = "1.8.0"
1415
futures = "0.3.1"
1516
futures-timer = "3.0.2"
1617
instant = "0.1.11"

protocols/ping/tests/ping.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
//! Integration tests for the `Ping` network behaviour.
2222
23+
use either::Either;
2324
use futures::{channel::mpsc, prelude::*};
2425
use libp2p_core::{
2526
identity,
@@ -251,8 +252,8 @@ fn mk_transport(muxer: MuxerChoice) -> (PeerId, transport::Boxed<(PeerId, Stream
251252
.upgrade(upgrade::Version::V1)
252253
.authenticate(noise::NoiseAuthenticated::xx(&id_keys).unwrap())
253254
.multiplex(match muxer {
254-
MuxerChoice::Yamux => upgrade::EitherUpgrade::A(yamux::YamuxConfig::default()),
255-
MuxerChoice::Mplex => upgrade::EitherUpgrade::B(mplex::MplexConfig::default()),
255+
MuxerChoice::Yamux => Either::Left(yamux::YamuxConfig::default()),
256+
MuxerChoice::Mplex => Either::Right(mplex::MplexConfig::default()),
256257
})
257258
.boxed(),
258259
)

protocols/relay/src/behaviour/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ impl IntoConnectionHandler for Prototype {
355355
}
356356

357357
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
358-
upgrade::EitherUpgrade::A(SendWrapper(inbound_hop::Upgrade {
358+
Either::Left(SendWrapper(inbound_hop::Upgrade {
359359
reservation_duration: self.config.reservation_duration,
360360
max_circuit_duration: self.config.max_circuit_duration,
361361
max_circuit_bytes: self.config.max_circuit_bytes,

0 commit comments

Comments
 (0)