Skip to content

Commit 0e36c7c

Browse files
feat(swarm): remove deprecated IntoConnectionHandler
This removes the deprecated `IntoConnectionHandler` trait and all its implementations. Consequently, `NetworkBehaviour::new_handler` and `NetworkBehaviour::addresses_of_peer` are now gone and the two `handle_` functions are now required to implement. Related: #3647. Pull-Request: #3884.
1 parent b4e724d commit 0e36c7c

File tree

15 files changed

+38
-371
lines changed

15 files changed

+38
-371
lines changed

protocols/identify/src/behaviour.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,6 @@ impl Config {
167167
}
168168

169169
/// Configures the size of the LRU cache, caching addresses of discovered peers.
170-
///
171-
/// The [`Swarm`](libp2p_swarm::Swarm) may extend the set of addresses of an outgoing connection attempt via
172-
/// [`Behaviour::addresses_of_peer`].
173170
pub fn with_cache_size(mut self, cache_size: usize) -> Self {
174171
self.cache_size = cache_size;
175172
self

protocols/rendezvous/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub struct Behaviour {
4747

4848
/// Hold addresses of all peers that we have discovered so far.
4949
///
50-
/// Storing these internally allows us to assist the [`libp2p_swarm::Swarm`] in dialing by returning addresses from [`NetworkBehaviour::addresses_of_peer`].
50+
/// Storing these internally allows us to assist the [`libp2p_swarm::Swarm`] in dialing by returning addresses from [`NetworkBehaviour::handle_pending_outbound_connection`].
5151
discovered_peers: HashMap<(PeerId, Namespace), Vec<Multiaddr>>,
5252

5353
/// Tracks the expiry of registrations that we have discovered and stored in `discovered_peers` otherwise we have a memory leak.

protocols/request-response/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ where
417417

418418
/// Adds a known address for a peer that can be used for
419419
/// dialing attempts by the `Swarm`, i.e. is returned
420-
/// by [`NetworkBehaviour::addresses_of_peer`].
420+
/// by [`NetworkBehaviour::handle_pending_outbound_connection`].
421421
///
422422
/// Addresses added in this way are only removed by `remove_address`.
423423
pub fn add_address(&mut self, peer: &PeerId, address: Multiaddr) {

swarm/CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
This variant was never constructed and thus dead code.
1919
See [PR 3605].
2020

21-
[PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605
22-
[PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746
21+
- Remove deprecated `IntoConnectionHandler` and all its implementations.
22+
This also removes the `NetworkBehaviour::new_handler` and `NetworkBehaviour::addresses_of_peer` methods.
23+
See changelog for `0.42` on how to migrate.
24+
See [PR 3884].
25+
2326
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
2427
[PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746
2528
[PR 3865]: https://github.com/libp2p/rust-libp2p/pull/3865
2629
[PR 3886]: https://github.com/libp2p/rust-libp2p/pull/3886
30+
[PR 3884]: https://github.com/libp2p/rust-libp2p/pull/3884
31+
[PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605
32+
[PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746
2733

2834
## 0.42.2
2935

swarm/src/behaviour.rs

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ pub use listen_addresses::ListenAddresses;
2828

2929
use crate::connection::ConnectionId;
3030
use crate::dial_opts::DialOpts;
31-
#[allow(deprecated)]
32-
use crate::handler::IntoConnectionHandler;
3331
use crate::{
34-
AddressRecord, AddressScore, ConnectionDenied, DialError, ListenError, THandler,
35-
THandlerInEvent, THandlerOutEvent,
32+
AddressRecord, AddressScore, ConnectionDenied, ConnectionHandler, DialError, ListenError,
33+
THandler, THandlerInEvent, THandlerOutEvent,
3634
};
3735
use libp2p_core::{transport::ListenerId, ConnectedPoint, Endpoint, Multiaddr};
3836
use libp2p_identity::PeerId;
@@ -77,9 +75,7 @@ use std::{task::Context, task::Poll};
7775
/// implementation for the custom `struct`. Each [`NetworkBehaviour`] trait method is simply
7876
/// delegated to each `struct` member in the order the `struct` is defined. For example for
7977
/// [`NetworkBehaviour::poll`] it will first poll the first `struct` member until it returns
80-
/// [`Poll::Pending`] before moving on to later members. For [`NetworkBehaviour::addresses_of_peer`]
81-
/// it will delegate to each `struct` member and return a concatenated array of all addresses
82-
/// returned by the struct members.
78+
/// [`Poll::Pending`] before moving on to later members.
8379
///
8480
/// Events ([`NetworkBehaviour::OutEvent`]) returned by each `struct` member are wrapped in a new
8581
/// `enum` event, with an `enum` variant for each `struct` member. Users can define this event
@@ -122,37 +118,11 @@ use std::{task::Context, task::Poll};
122118
/// ```
123119
pub trait NetworkBehaviour: 'static {
124120
/// Handler for all the protocols the network behaviour supports.
125-
#[allow(deprecated)]
126-
type ConnectionHandler: IntoConnectionHandler;
121+
type ConnectionHandler: ConnectionHandler;
127122

128123
/// Event generated by the `NetworkBehaviour` and that the swarm will report back.
129124
type OutEvent: Send + 'static;
130125

131-
/// Creates a new [`ConnectionHandler`](crate::ConnectionHandler) for a connection with a peer.
132-
///
133-
/// Every time an incoming connection is opened, and every time another [`NetworkBehaviour`]
134-
/// emitted a dial request, this method is called.
135-
///
136-
/// The returned object is a handler for that specific connection, and will be moved to a
137-
/// background task dedicated to that connection.
138-
///
139-
/// The network behaviour (ie. the implementation of this trait) and the handlers it has spawned
140-
/// (ie. the objects returned by `new_handler`) can communicate by passing messages. Messages
141-
/// sent from the handler to the behaviour are invoked with
142-
/// [`NetworkBehaviour::on_connection_handler_event`],
143-
/// and the behaviour can send a message to the handler by making [`NetworkBehaviour::poll`]
144-
/// return [`ToSwarm::NotifyHandler`].
145-
///
146-
/// Note that the handler is returned to the [`NetworkBehaviour`] on connection failure and
147-
/// connection closing.
148-
#[deprecated(
149-
since = "0.42.0",
150-
note = "Use one or more of `NetworkBehaviour::{handle_pending_inbound_connection,handle_established_inbound_connection,handle_pending_outbound_connection,handle_established_outbound_connection}` instead."
151-
)]
152-
fn new_handler(&mut self) -> Self::ConnectionHandler {
153-
panic!("You must implement `handle_established_inbound_connection` and `handle_established_outbound_connection`.")
154-
}
155-
156126
/// Callback that is invoked for every new inbound connection.
157127
///
158128
/// At this point in the connection lifecycle, only the remote's and our local address are known.
@@ -181,16 +151,7 @@ pub trait NetworkBehaviour: 'static {
181151
peer: PeerId,
182152
local_addr: &Multiaddr,
183153
remote_addr: &Multiaddr,
184-
) -> Result<THandler<Self>, ConnectionDenied> {
185-
#[allow(deprecated)]
186-
Ok(self.new_handler().into_handler(
187-
&peer,
188-
&ConnectedPoint::Listener {
189-
local_addr: local_addr.clone(),
190-
send_back_addr: remote_addr.clone(),
191-
},
192-
))
193-
}
154+
) -> Result<THandler<Self>, ConnectionDenied>;
194155

195156
/// Callback that is invoked for every outbound connection attempt.
196157
///
@@ -207,16 +168,11 @@ pub trait NetworkBehaviour: 'static {
207168
fn handle_pending_outbound_connection(
208169
&mut self,
209170
_connection_id: ConnectionId,
210-
maybe_peer: Option<PeerId>,
171+
_maybe_peer: Option<PeerId>,
211172
_addresses: &[Multiaddr],
212173
_effective_role: Endpoint,
213174
) -> Result<Vec<Multiaddr>, ConnectionDenied> {
214-
#[allow(deprecated)]
215-
if let Some(peer_id) = maybe_peer {
216-
Ok(self.addresses_of_peer(&peer_id))
217-
} else {
218-
Ok(vec![])
219-
}
175+
Ok(vec![])
220176
}
221177

222178
/// Callback that is invoked for every established outbound connection.
@@ -231,27 +187,7 @@ pub trait NetworkBehaviour: 'static {
231187
peer: PeerId,
232188
addr: &Multiaddr,
233189
role_override: Endpoint,
234-
) -> Result<THandler<Self>, ConnectionDenied> {
235-
#[allow(deprecated)]
236-
Ok(self.new_handler().into_handler(
237-
&peer,
238-
&ConnectedPoint::Dialer {
239-
address: addr.clone(),
240-
role_override,
241-
},
242-
))
243-
}
244-
245-
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
246-
/// reaching the peer.
247-
///
248-
/// The addresses will be tried in the order returned by this function, which means that they
249-
/// should be ordered by decreasing likelihood of reachability. In other words, the first
250-
/// address should be the most likely to be reachable.
251-
#[deprecated(note = "Use `NetworkBehaviour::handle_pending_outbound_connection` instead.")]
252-
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
253-
vec![]
254-
}
190+
) -> Result<THandler<Self>, ConnectionDenied>;
255191

256192
/// Informs the behaviour about an event from the [`Swarm`](crate::Swarm).
257193
fn on_swarm_event(&mut self, event: FromSwarm<Self::ConnectionHandler>);
@@ -480,7 +416,7 @@ pub enum CloseConnection {
480416
/// Enumeration with the list of the possible events
481417
/// to pass to [`on_swarm_event`](NetworkBehaviour::on_swarm_event).
482418
#[allow(deprecated)]
483-
pub enum FromSwarm<'a, Handler: IntoConnectionHandler> {
419+
pub enum FromSwarm<'a, Handler> {
484420
/// Informs the behaviour about a newly established connection to a peer.
485421
ConnectionEstablished(ConnectionEstablished<'a>),
486422
/// Informs the behaviour about a closed connection to a peer.
@@ -535,11 +471,11 @@ pub struct ConnectionEstablished<'a> {
535471
/// [`FromSwarm::ConnectionEstablished`] with the same peer ID, connection ID
536472
/// and endpoint.
537473
#[allow(deprecated)]
538-
pub struct ConnectionClosed<'a, Handler: IntoConnectionHandler> {
474+
pub struct ConnectionClosed<'a, Handler> {
539475
pub peer_id: PeerId,
540476
pub connection_id: ConnectionId,
541477
pub endpoint: &'a ConnectedPoint,
542-
pub handler: <Handler as IntoConnectionHandler>::Handler,
478+
pub handler: Handler,
543479
pub remaining_established: usize,
544480
}
545481

@@ -625,30 +561,19 @@ pub struct ExpiredExternalAddr<'a> {
625561
pub addr: &'a Multiaddr,
626562
}
627563

628-
#[allow(deprecated)]
629-
impl<'a, Handler: IntoConnectionHandler> FromSwarm<'a, Handler> {
564+
impl<'a, Handler> FromSwarm<'a, Handler> {
630565
fn map_handler<NewHandler>(
631566
self,
632-
map_handler: impl FnOnce(
633-
<Handler as IntoConnectionHandler>::Handler,
634-
) -> <NewHandler as IntoConnectionHandler>::Handler,
635-
) -> FromSwarm<'a, NewHandler>
636-
where
637-
NewHandler: IntoConnectionHandler,
638-
{
567+
map_handler: impl FnOnce(Handler) -> NewHandler,
568+
) -> FromSwarm<'a, NewHandler> {
639569
self.maybe_map_handler(|h| Some(map_handler(h)))
640570
.expect("To return Some as all closures return Some.")
641571
}
642572

643573
fn maybe_map_handler<NewHandler>(
644574
self,
645-
map_handler: impl FnOnce(
646-
<Handler as IntoConnectionHandler>::Handler,
647-
) -> Option<<NewHandler as IntoConnectionHandler>::Handler>,
648-
) -> Option<FromSwarm<'a, NewHandler>>
649-
where
650-
NewHandler: IntoConnectionHandler,
651-
{
575+
map_handler: impl FnOnce(Handler) -> Option<NewHandler>,
576+
) -> Option<FromSwarm<'a, NewHandler>> {
652577
match self {
653578
FromSwarm::ConnectionClosed(ConnectionClosed {
654579
peer_id,

swarm/src/behaviour/external_addresses.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::behaviour::{ExpiredExternalAddr, FromSwarm, NewExternalAddr};
2-
#[allow(deprecated)]
3-
use crate::IntoConnectionHandler;
42
use libp2p_core::Multiaddr;
53
use std::collections::HashSet;
64

@@ -34,11 +32,7 @@ impl ExternalAddresses {
3432
/// Feed a [`FromSwarm`] event to this struct.
3533
///
3634
/// Returns whether the event changed our set of external addresses.
37-
#[allow(deprecated)]
38-
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) -> bool
39-
where
40-
THandler: IntoConnectionHandler,
41-
{
35+
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) -> bool {
4236
match event {
4337
FromSwarm::NewExternalAddr(NewExternalAddr { addr, .. }) => {
4438
if self.addresses.len() < self.limit {

swarm/src/behaviour/listen_addresses.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::behaviour::{ExpiredListenAddr, FromSwarm, NewListenAddr};
2-
#[allow(deprecated)]
3-
use crate::IntoConnectionHandler;
42
use libp2p_core::Multiaddr;
53
use std::collections::HashSet;
64

@@ -19,11 +17,7 @@ impl ListenAddresses {
1917
/// Feed a [`FromSwarm`] event to this struct.
2018
///
2119
/// Returns whether the event changed our set of listen addresses.
22-
#[allow(deprecated)]
23-
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) -> bool
24-
where
25-
THandler: IntoConnectionHandler,
26-
{
20+
pub fn on_swarm_event<THandler>(&mut self, event: &FromSwarm<THandler>) -> bool {
2721
match event {
2822
FromSwarm::NewListenAddr(NewListenAddr { addr, .. }) => {
2923
self.addresses.insert((*addr).clone())

swarm/src/connection/pool.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
// DEALINGS IN THE SOFTWARE.
2121
#[allow(deprecated)]
2222
use crate::connection::{Connection, ConnectionId, ConnectionLimit, PendingPoint};
23-
#[allow(deprecated)]
24-
use crate::IntoConnectionHandler;
2523
use crate::{
2624
connection::{
2725
Connected, ConnectionError, IncomingInfo, PendingConnectionError,
@@ -512,7 +510,7 @@ where
512510
obtained_peer_id: PeerId,
513511
endpoint: &ConnectedPoint,
514512
connection: NewConnection,
515-
handler: <THandler as IntoConnectionHandler>::Handler,
513+
handler: THandler,
516514
) {
517515
let connection = connection.extract();
518516

swarm/src/dial_opts.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,6 @@ impl WithPeerId {
207207
}
208208

209209
/// Build the final [`DialOpts`].
210-
///
211-
/// Addresses to dial the peer are retrieved via
212-
/// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer).
213210
pub fn build(self) -> DialOpts {
214211
DialOpts {
215212
peer_id: Some(self.peer_id),
@@ -241,7 +238,7 @@ impl WithPeerIdWithAddresses {
241238
}
242239

243240
/// In addition to the provided addresses, extend the set via
244-
/// [`NetworkBehaviour::addresses_of_peer`](crate::behaviour::NetworkBehaviour::addresses_of_peer).
241+
/// [`NetworkBehaviour::handle_pending_outbound_connection`](crate::behaviour::NetworkBehaviour::handle_pending_outbound_connection).
245242
pub fn extend_addresses_through_behaviour(mut self) -> Self {
246243
self.extend_addresses_through_behaviour = true;
247244
self

swarm/src/handler.rs

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,14 @@ mod select;
4949
pub use crate::upgrade::{InboundUpgradeSend, OutboundUpgradeSend, SendWrapper, UpgradeInfoSend};
5050

5151
use instant::Instant;
52-
use libp2p_core::{upgrade::UpgradeError, ConnectedPoint, Multiaddr};
53-
use libp2p_identity::PeerId;
52+
use libp2p_core::{upgrade::UpgradeError, Multiaddr};
5453
use std::{cmp::Ordering, error, fmt, task::Context, task::Poll, time::Duration};
5554

5655
pub use map_in::MapInEvent;
5756
pub use map_out::MapOutEvent;
5857
pub use one_shot::{OneShotHandler, OneShotHandlerConfig};
5958
pub use pending::PendingConnectionHandler;
60-
pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect};
59+
pub use select::ConnectionHandlerSelect;
6160

6261
/// A handler for a set of protocols used on a connection with a remote.
6362
///
@@ -510,52 +509,6 @@ where
510509
}
511510
}
512511

513-
/// Prototype for a [`ConnectionHandler`].
514-
#[deprecated(
515-
note = "Implement `ConnectionHandler` directly and use `NetworkBehaviour::{handle_pending_inbound_connection,handle_pending_outbound_connection}` to handle pending connections."
516-
)]
517-
pub trait IntoConnectionHandler: Send + 'static {
518-
/// The protocols handler.
519-
type Handler: ConnectionHandler;
520-
521-
/// Builds the protocols handler.
522-
///
523-
/// The `PeerId` is the id of the node the handler is going to handle.
524-
fn into_handler(
525-
self,
526-
remote_peer_id: &PeerId,
527-
connected_point: &ConnectedPoint,
528-
) -> Self::Handler;
529-
530-
/// Return the handler's inbound protocol.
531-
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol;
532-
533-
/// Builds an implementation of [`IntoConnectionHandler`] that handles both this protocol and the
534-
/// other one together.
535-
fn select<TProto2>(self, other: TProto2) -> IntoConnectionHandlerSelect<Self, TProto2>
536-
where
537-
Self: Sized,
538-
{
539-
IntoConnectionHandlerSelect::new(self, other)
540-
}
541-
}
542-
543-
#[allow(deprecated)]
544-
impl<T> IntoConnectionHandler for T
545-
where
546-
T: ConnectionHandler,
547-
{
548-
type Handler = Self;
549-
550-
fn into_handler(self, _: &PeerId, _: &ConnectedPoint) -> Self {
551-
self
552-
}
553-
554-
fn inbound_protocol(&self) -> <Self::Handler as ConnectionHandler>::InboundProtocol {
555-
self.listen_protocol().into_upgrade().0
556-
}
557-
}
558-
559512
/// How long the connection should be kept alive.
560513
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
561514
pub enum KeepAlive {

0 commit comments

Comments
 (0)