Skip to content

Commit 4c65c7d

Browse files
authored
refactor(swarm): remove deprecated inject calls (#3264)
Finishes work first started with #2832
1 parent 3cc8247 commit 4c65c7d

File tree

23 files changed

+1147
-1346
lines changed

23 files changed

+1147
-1346
lines changed

protocols/dcutr/src/handler/direct.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
//! [`ConnectionHandler`] handling direct connection upgraded through a relayed connection.
2222
2323
use libp2p_core::connection::ConnectionId;
24-
use libp2p_core::upgrade::{DeniedUpgrade, InboundUpgrade, OutboundUpgrade};
24+
use libp2p_core::upgrade::DeniedUpgrade;
2525
use libp2p_swarm::handler::ConnectionEvent;
2626
use libp2p_swarm::{
2727
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
28-
NegotiatedSubstream, SubstreamProtocol,
28+
SubstreamProtocol,
2929
};
3030
use std::task::{Context, Poll};
3131
use void::Void;
@@ -62,31 +62,8 @@ impl ConnectionHandler for Handler {
6262
SubstreamProtocol::new(DeniedUpgrade, ())
6363
}
6464

65-
fn inject_fully_negotiated_inbound(
66-
&mut self,
67-
_: <Self::InboundProtocol as InboundUpgrade<NegotiatedSubstream>>::Output,
68-
_: Self::InboundOpenInfo,
69-
) {
70-
}
71-
72-
fn inject_fully_negotiated_outbound(
73-
&mut self,
74-
_: <Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Output,
75-
_: Self::OutboundOpenInfo,
76-
) {
77-
}
78-
7965
fn on_behaviour_event(&mut self, _: Self::InEvent) {}
8066

81-
fn inject_dial_upgrade_error(
82-
&mut self,
83-
_: Self::OutboundOpenInfo,
84-
_: ConnectionHandlerUpgrErr<
85-
<Self::OutboundProtocol as OutboundUpgrade<NegotiatedSubstream>>::Error,
86-
>,
87-
) {
88-
}
89-
9067
fn connection_keep_alive(&self) -> KeepAlive {
9168
KeepAlive::No
9269
}

protocols/relay/src/priv_client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl Behaviour {
145145
}
146146
}
147147
hash_map::Entry::Vacant(_) => {
148-
unreachable!("`inject_connection_closed` for unconnected peer.")
148+
unreachable!("`on_connection_closed` for unconnected peer.")
149149
}
150150
};
151151
}

protocols/rendezvous/src/handler/inbound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl SubstreamHandler for Stream {
9797
Stream::PendingRead(Framed::new(substream, RendezvousCodec::default()))
9898
}
9999

100-
fn inject_event(self, event: Self::InEvent) -> Self {
100+
fn on_event(self, event: Self::InEvent) -> Self {
101101
match (event, self) {
102102
(InEvent::RegisterResponse { ttl }, Stream::PendingBehaviour(substream)) => {
103103
Stream::PendingSend(substream, Message::RegisterResponse(Ok(ttl)))

protocols/rendezvous/src/handler/outbound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl SubstreamHandler for Stream {
9494
}))
9595
}
9696

97-
fn inject_event(self, event: Self::InEvent) -> Self {
97+
fn on_event(self, event: Self::InEvent) -> Self {
9898
void::unreachable(event)
9999
}
100100

protocols/rendezvous/src/substream_handler.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ use futures::future::{self, BoxFuture, Fuse, FusedFuture};
2929
use futures::FutureExt;
3030
use instant::Instant;
3131
use libp2p_core::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
32-
use libp2p_swarm::handler::{InboundUpgradeSend, OutboundUpgradeSend};
32+
use libp2p_swarm::handler::{ConnectionEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound};
3333
use libp2p_swarm::{
34-
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive,
35-
NegotiatedSubstream, SubstreamProtocol,
34+
ConnectionHandler, ConnectionHandlerEvent, KeepAlive, NegotiatedSubstream, SubstreamProtocol,
3635
};
3736
use std::collections::{HashMap, VecDeque};
3837
use std::fmt;
@@ -52,7 +51,7 @@ pub trait SubstreamHandler: Sized {
5251
fn upgrade(open_info: Self::OpenInfo)
5352
-> SubstreamProtocol<PassthroughProtocol, Self::OpenInfo>;
5453
fn new(substream: NegotiatedSubstream, info: Self::OpenInfo) -> Self;
55-
fn inject_event(self, event: Self::InEvent) -> Self;
54+
fn on_event(self, event: Self::InEvent) -> Self;
5655
fn advance(self, cx: &mut Context<'_>) -> Result<Next<Self, Self::OutEvent>, Self::Error>;
5756
}
5857

@@ -367,35 +366,47 @@ where
367366
TInboundSubstreamHandler::upgrade(())
368367
}
369368

370-
fn inject_fully_negotiated_inbound(
369+
fn on_connection_event(
371370
&mut self,
372-
protocol: <Self::InboundProtocol as InboundUpgradeSend>::Output,
373-
_: Self::InboundOpenInfo,
374-
) {
375-
self.inbound_substreams.insert(
376-
self.next_inbound_substream_id.fetch_and_increment(),
377-
TInboundSubstreamHandler::new(protocol, ()),
378-
);
379-
}
380-
381-
fn inject_fully_negotiated_outbound(
382-
&mut self,
383-
protocol: <Self::OutboundProtocol as OutboundUpgradeSend>::Output,
384-
info: Self::OutboundOpenInfo,
371+
event: ConnectionEvent<
372+
Self::InboundProtocol,
373+
Self::OutboundProtocol,
374+
Self::InboundOpenInfo,
375+
Self::OutboundOpenInfo,
376+
>,
385377
) {
386-
self.outbound_substreams.insert(
387-
self.next_outbound_substream_id.fetch_and_increment(),
388-
TOutboundSubstreamHandler::new(protocol, info),
389-
);
378+
match event {
379+
ConnectionEvent::FullyNegotiatedInbound(FullyNegotiatedInbound {
380+
protocol, ..
381+
}) => {
382+
self.inbound_substreams.insert(
383+
self.next_inbound_substream_id.fetch_and_increment(),
384+
TInboundSubstreamHandler::new(protocol, ()),
385+
);
386+
}
387+
ConnectionEvent::FullyNegotiatedOutbound(FullyNegotiatedOutbound {
388+
protocol,
389+
info,
390+
}) => {
391+
self.outbound_substreams.insert(
392+
self.next_outbound_substream_id.fetch_and_increment(),
393+
TOutboundSubstreamHandler::new(protocol, info),
394+
);
395+
}
396+
// TODO: Handle upgrade errors properly
397+
ConnectionEvent::AddressChange(_)
398+
| ConnectionEvent::ListenUpgradeError(_)
399+
| ConnectionEvent::DialUpgradeError(_) => {}
400+
}
390401
}
391402

392-
fn inject_event(&mut self, event: Self::InEvent) {
403+
fn on_behaviour_event(&mut self, event: Self::InEvent) {
393404
match event {
394405
InEvent::NewSubstream { open_info } => self.new_substreams.push_back(open_info),
395406
InEvent::NotifyInboundSubstream { id, message } => {
396407
match self.inbound_substreams.remove(&id) {
397408
Some(handler) => {
398-
let new_handler = handler.inject_event(message);
409+
let new_handler = handler.on_event(message);
399410

400411
self.inbound_substreams.insert(id, new_handler);
401412
}
@@ -407,7 +418,7 @@ where
407418
InEvent::NotifyOutboundSubstream { id, message } => {
408419
match self.outbound_substreams.remove(&id) {
409420
Some(handler) => {
410-
let new_handler = handler.inject_event(message);
421+
let new_handler = handler.on_event(message);
411422

412423
self.outbound_substreams.insert(id, new_handler);
413424
}
@@ -419,14 +430,6 @@ where
419430
}
420431
}
421432

422-
fn inject_dial_upgrade_error(
423-
&mut self,
424-
_: Self::OutboundOpenInfo,
425-
_: ConnectionHandlerUpgrErr<Void>,
426-
) {
427-
// TODO: Handle upgrade errors properly
428-
}
429-
430433
fn connection_keep_alive(&self) -> KeepAlive {
431434
// Rudimentary keep-alive handling, to be extended as needed as this abstraction is used more by other protocols.
432435

@@ -537,7 +540,7 @@ impl SubstreamHandler for void::Void {
537540
unreachable!("we should never yield a substream")
538541
}
539542

540-
fn inject_event(self, event: Self::InEvent) -> Self {
543+
fn on_event(self, event: Self::InEvent) -> Self {
541544
void::unreachable(event)
542545
}
543546

protocols/request-response/src/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ where
366366
Err(oneshot::Canceled) => {
367367
// The inbound upgrade has errored or timed out reading
368368
// or waiting for the request. The handler is informed
369-
// via `inject_listen_upgrade_error`.
369+
// via `on_connection_event` call with `ConnectionEvent::ListenUpgradeError`.
370370
}
371371
}
372372
}

swarm-derive/CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
# 0.31.0
1+
# 0.32.0 [unreleased]
22

33
- Replace `NetworkBehaviour` Derive macro deprecated `inject_*` method implementations
44
with the new `on_swarm_event` and `on_connection_handler_event`.
5-
See [PR 3011].
5+
See [PR 3011] and [PR 3264].
6+
7+
[PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011
8+
[PR 3264]: https://github.com/libp2p/rust-libp2p/pull/3264
9+
10+
# 0.31.0
611

712
- Add `prelude` configuration option.
813
The derive-macro generates code that needs to refer to various symbols. See [PR 3055].
914

1015
- Update `rust-version` to reflect the actual MSRV: 1.60.0. See [PR 3090].
1116

12-
[PR 3011]: https://github.com/libp2p/rust-libp2p/pull/3011
1317
[PR 3055]: https://github.com/libp2p/rust-libp2p/pull/3055
1418
[PR 3090]: https://github.com/libp2p/rust-libp2p/pull/3090
1519

swarm-derive/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-swarm-derive"
33
edition = "2021"
44
rust-version = "1.60.0"
55
description = "Procedural macros of libp2p-swarm"
6-
version = "0.31.0"
6+
version = "0.32.0"
77
authors = ["Parity Technologies <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"

0 commit comments

Comments
 (0)