From 11f251881c326a3ef6568791e2d01d0d9c6f1ca3 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 23 Jan 2023 16:44:03 +1100 Subject: [PATCH 1/4] Use `THandlerOutEvent` everywhere --- protocols/autonat/src/behaviour.rs | 7 +++---- protocols/dcutr/src/behaviour_impl.rs | 9 ++++----- protocols/floodsub/src/layer.rs | 6 ++---- protocols/gossipsub/src/behaviour.rs | 7 +++---- protocols/identify/src/behaviour.rs | 9 ++++----- protocols/kad/src/behaviour.rs | 4 ++-- protocols/mdns/src/behaviour.rs | 6 +++--- protocols/ping/src/lib.rs | 8 +++++++- protocols/relay/src/behaviour.rs | 4 ++-- protocols/relay/src/priv_client.rs | 4 ++-- protocols/rendezvous/src/client.rs | 4 ++-- protocols/rendezvous/src/server.rs | 4 ++-- protocols/request-response/src/lib.rs | 7 +++---- swarm/src/behaviour.rs | 12 ++++-------- swarm/src/behaviour/either.rs | 3 ++- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/connection/pool.rs | 4 ++-- swarm/src/dummy.rs | 12 ++++++++++-- swarm/src/keep_alive.rs | 8 +++++++- swarm/src/lib.rs | 2 +- swarm/src/test.rs | 7 +++---- swarm/tests/swarm_derive.rs | 9 +++------ 22 files changed, 73 insertions(+), 67 deletions(-) diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index dd88663df66..aae4beb5811 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -37,8 +37,8 @@ use libp2p_swarm::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, ExpiredListenAddr, FromSwarm, }, - ConnectionHandler, ConnectionId, ExternalAddresses, IntoConnectionHandler, ListenAddresses, - NetworkBehaviour, NetworkBehaviourAction, PollParameters, + ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, + PollParameters, THandlerOutEvent, }; use std::{ collections::{HashMap, VecDeque}, @@ -553,8 +553,7 @@ impl NetworkBehaviour for Behaviour { &mut self, peer_id: PeerId, connection_id: ConnectionId, - event: <::Handler as - ConnectionHandler>::OutEvent, + event: THandlerOutEvent, ) { self.inner .on_connection_handler_event(peer_id, connection_id, event) diff --git a/protocols/dcutr/src/behaviour_impl.rs b/protocols/dcutr/src/behaviour_impl.rs index 7d77a032150..acfc4cb2985 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -27,11 +27,11 @@ use libp2p_core::multiaddr::Protocol; use libp2p_core::{Multiaddr, PeerId}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::dial_opts::{self, DialOpts}; -use libp2p_swarm::ConnectionId; use libp2p_swarm::{ - ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler, - NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, + NotifyHandler, PollParameters, }; +use libp2p_swarm::{ConnectionId, THandlerOutEvent}; use std::collections::{HashMap, HashSet, VecDeque}; use std::task::{Context, Poll}; use thiserror::Error; @@ -222,8 +222,7 @@ impl NetworkBehaviour for Behaviour { &mut self, event_source: PeerId, connection: ConnectionId, - handler_event: <::Handler as - ConnectionHandler>::OutEvent, + handler_event: THandlerOutEvent, ) { match handler_event { Either::Left(handler::relayed::Event::InboundConnectRequest { diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 87dd5a9237a..a82fd0cbf4b 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -30,9 +30,8 @@ use libp2p_core::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::{ dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, - OneShotHandler, PollParameters, + OneShotHandler, PollParameters, THandlerOutEvent, }; -use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler}; use log::warn; use smallvec::SmallVec; use std::collections::hash_map::{DefaultHasher, HashMap}; @@ -352,8 +351,7 @@ impl NetworkBehaviour for Floodsub { &mut self, propagation_source: PeerId, _connection_id: ConnectionId, - event: <::Handler as - ConnectionHandler>::OutEvent, + event: THandlerOutEvent, ) { // We ignore successful sends or timeouts. let event = match event { diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index af199fcd214..c55acbcc1f4 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -41,8 +41,8 @@ use libp2p_core::{ use libp2p_swarm::{ behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm}, dial_opts::DialOpts, - ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, - NetworkBehaviourAction, NotifyHandler, PollParameters, + ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + THandlerOutEvent, }; use wasm_timer::Instant; @@ -3313,8 +3313,7 @@ where &mut self, propagation_source: PeerId, _connection_id: ConnectionId, - handler_event: <::Handler as - ConnectionHandler>::OutEvent, + handler_event: THandlerOutEvent, ) { match handler_event { HandlerEvent::PeerKind(kind) => { diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 22363ebbe10..3b285b53e8c 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -22,12 +22,11 @@ use crate::handler::{self, InEvent, Proto}; use crate::protocol::{Info, Protocol, UpgradeError}; use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; -use libp2p_swarm::ConnectionId; use libp2p_swarm::{ - dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError, - ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, - NetworkBehaviourAction, NotifyHandler, PollParameters, + dial_opts::DialOpts, AddressScore, ConnectionHandlerUpgrErr, DialError, ExternalAddresses, + ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, }; +use libp2p_swarm::{ConnectionId, THandlerOutEvent}; use lru::LruCache; use std::num::NonZeroUsize; use std::{ @@ -253,7 +252,7 @@ impl NetworkBehaviour for Behaviour { &mut self, peer_id: PeerId, connection_id: ConnectionId, - event: <::Handler as ConnectionHandler>::OutEvent, + event: THandlerOutEvent, ) { match event { handler::Event::Identified(mut info) => { diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 66e1b426ad7..149b11afd4a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -46,7 +46,7 @@ use libp2p_swarm::behaviour::{ use libp2p_swarm::{ dial_opts::{self, DialOpts}, ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, - NetworkBehaviourAction, NotifyHandler, PollParameters, + NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent, }; use log::{debug, info, warn}; use smallvec::SmallVec; @@ -2026,7 +2026,7 @@ where &mut self, source: PeerId, connection: ConnectionId, - event: KademliaHandlerEvent, + event: THandlerOutEvent, ) { match event { KademliaHandlerEvent::ProtocolConfirmed { endpoint } => { diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 9c280980e35..d1c496e5d41 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -30,8 +30,8 @@ use if_watch::IfEvent; use libp2p_core::{Multiaddr, PeerId}; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::{ - dummy, ConnectionHandler, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, - PollParameters, + dummy, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, PollParameters, + THandlerOutEvent, }; use smallvec::SmallVec; use std::collections::hash_map::{Entry, HashMap}; @@ -190,7 +190,7 @@ where &mut self, _: PeerId, _: libp2p_swarm::ConnectionId, - ev: ::OutEvent, + ev: THandlerOutEvent, ) { void::unreachable(ev) } diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index 492e6edd86b..da865f22f12 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -50,6 +50,7 @@ pub use handler::{Config, Failure, Success}; use libp2p_core::PeerId; use libp2p_swarm::{ behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters, + THandlerOutEvent, }; use std::{ collections::VecDeque, @@ -123,7 +124,12 @@ impl NetworkBehaviour for Behaviour { Handler::new(self.config.clone()) } - fn on_connection_handler_event(&mut self, peer: PeerId, _: ConnectionId, result: Result) { + fn on_connection_handler_event( + &mut self, + peer: PeerId, + _: ConnectionId, + result: THandlerOutEvent, + ) { self.events.push_front(Event { peer, result }) } diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 5cb269f6986..6919626d978 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -32,7 +32,7 @@ use libp2p_core::PeerId; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::{ ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour, - NetworkBehaviourAction, NotifyHandler, PollParameters, + NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent, }; use std::collections::{hash_map, HashMap, HashSet, VecDeque}; use std::num::NonZeroU32; @@ -287,7 +287,7 @@ impl NetworkBehaviour for Behaviour { &mut self, event_source: PeerId, connection: ConnectionId, - event: Either, + event: THandlerOutEvent, ) { let event = match event { Either::Left(e) => e, diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index 4399cceaa3c..3bf7819a4ee 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -37,7 +37,7 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm use libp2p_swarm::dial_opts::DialOpts; use libp2p_swarm::{ ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour, - NetworkBehaviourAction, NotifyHandler, PollParameters, + NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent, }; use std::collections::{hash_map, HashMap, VecDeque}; use std::io::{Error, ErrorKind, IoSlice}; @@ -194,7 +194,7 @@ impl NetworkBehaviour for Behaviour { &mut self, event_source: PeerId, _connection: ConnectionId, - handler_event: Either, + handler_event: THandlerOutEvent, ) { let handler_event = match handler_event { Either::Left(e) => e, diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 60cb0e6590c..23229cd12dd 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -34,7 +34,7 @@ use libp2p_core::{Multiaddr, PeerId, PeerRecord}; use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::{ CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, - NotifyHandler, PollParameters, + NotifyHandler, PollParameters, THandlerOutEvent, }; use std::collections::{HashMap, VecDeque}; use std::iter::FromIterator; @@ -191,7 +191,7 @@ impl NetworkBehaviour for Behaviour { &mut self, peer_id: PeerId, connection_id: ConnectionId, - event: handler::OutboundOutEvent, + event: THandlerOutEvent, ) { let new_events = match event { handler::OutboundOutEvent::InboundEvent { message, .. } => void::unreachable(message), diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 5f5fe0a8964..d7e42652a2d 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -31,7 +31,7 @@ use libp2p_core::PeerId; use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::{ CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, - PollParameters, + PollParameters, THandlerOutEvent, }; use std::collections::{HashMap, HashSet, VecDeque}; use std::iter::FromIterator; @@ -123,7 +123,7 @@ impl NetworkBehaviour for Behaviour { &mut self, peer_id: PeerId, connection: ConnectionId, - event: handler::InboundOutEvent, + event: THandlerOutEvent, ) { let new_events = match event { handler::InboundOutEvent::InboundEvent { id, message } => { diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 89732eec450..760a17120c9 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -74,8 +74,8 @@ use libp2p_core::{ConnectedPoint, Multiaddr, PeerId}; use libp2p_swarm::{ behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, dial_opts::DialOpts, - ConnectionId, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, - PollParameters, + ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + THandlerOutEvent, }; use smallvec::SmallVec; use std::{ @@ -776,8 +776,7 @@ where &mut self, peer: PeerId, connection: ConnectionId, - event: <::Handler as - libp2p_swarm::ConnectionHandler>::OutEvent, + event: THandlerOutEvent, ) { match event { handler::Event::Response { diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 896b7aa38ad..2607e380e17 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -29,7 +29,7 @@ pub use listen_addresses::ListenAddresses; use crate::connection::ConnectionId; use crate::dial_opts::DialOpts; use crate::handler::{ConnectionHandler, IntoConnectionHandler}; -use crate::{AddressRecord, AddressScore, DialError}; +use crate::{AddressRecord, AddressScore, DialError, THandlerOutEvent}; use libp2p_core::{transport::ListenerId, ConnectedPoint, Multiaddr, PeerId}; use std::{task::Context, task::Poll}; @@ -37,9 +37,6 @@ use std::{task::Context, task::Poll}; pub(crate) type THandlerInEvent = <::Handler as ConnectionHandler>::InEvent; -pub(crate) type THandlerOutEvent = - <::Handler as ConnectionHandler>::OutEvent; - /// A [`NetworkBehaviour`] defines the behaviour of the local node on the network. /// /// In contrast to [`Transport`](libp2p_core::Transport) which defines **how** to send bytes on the @@ -122,7 +119,7 @@ pub(crate) type THandlerOutEvent = /// } /// } /// ``` -pub trait NetworkBehaviour: 'static { +pub trait NetworkBehaviour: Sized + 'static { /// Handler for all the protocols the network behaviour supports. type ConnectionHandler: IntoConnectionHandler; @@ -170,8 +167,7 @@ pub trait NetworkBehaviour: 'static { &mut self, _peer_id: PeerId, _connection_id: ConnectionId, - _event: <::Handler as - ConnectionHandler>::OutEvent, + _event: THandlerOutEvent, ) { } @@ -335,7 +331,7 @@ pub enum NetworkBehaviourAction< /// # &mut self, /// # _: PeerId, /// # _: ConnectionId, - /// # _: <::Handler as ConnectionHandler>::OutEvent, + /// # _: THandlerOutEvent, /// # ) { /// # unreachable!(); /// # } diff --git a/swarm/src/behaviour/either.rs b/swarm/src/behaviour/either.rs index ce94070cbd0..fb463661d74 100644 --- a/swarm/src/behaviour/either.rs +++ b/swarm/src/behaviour/either.rs @@ -21,6 +21,7 @@ use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::connection::ConnectionId; use crate::handler::either::IntoEitherHandler; +use crate::THandlerOutEvent; use either::Either; use libp2p_core::{Multiaddr, PeerId}; use std::{task::Context, task::Poll}; @@ -71,7 +72,7 @@ where &mut self, peer_id: PeerId, connection_id: ConnectionId, - event: crate::THandlerOutEvent, + event: THandlerOutEvent, ) { match (self, event) { (Either::Left(left), Either::Left(event)) => { diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 897d515cd28..90dd86dc72b 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -26,7 +26,7 @@ use crate::handler::{ IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol, }; use crate::upgrade::SendWrapper; -use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; +use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, THandlerOutEvent}; use either::Either; use libp2p_core::{ either::EitherOutput, upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId, @@ -95,7 +95,7 @@ where &mut self, peer_id: PeerId, connection_id: ConnectionId, - event: crate::THandlerOutEvent, + event: THandlerOutEvent, ) { if let Some(behaviour) = &mut self.inner { behaviour.on_connection_handler_event(peer_id, connection_id, event) diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index a23edadd640..de52d292413 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -21,7 +21,7 @@ use crate::connection::{Connection, ConnectionId, PendingPoint}; use crate::{ - behaviour::{THandlerInEvent, THandlerOutEvent}, + behaviour::THandlerInEvent, connection::{ Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError, @@ -297,7 +297,7 @@ pub enum PoolEvent { id: ConnectionId, peer_id: PeerId, /// The produced event. - event: THandlerOutEvent, + event: <::Handler as ConnectionHandler>::OutEvent, }, /// The connection to a node has changed its address. diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index b7ecde6de13..017d3afb687 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -3,7 +3,10 @@ use crate::connection::ConnectionId; use crate::handler::{ ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, }; -use crate::{ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol}; +use crate::{ + ConnectionHandlerEvent, ConnectionHandlerUpgrErr, KeepAlive, SubstreamProtocol, + THandlerOutEvent, +}; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::PeerId; use libp2p_core::UpgradeError; @@ -21,7 +24,12 @@ impl NetworkBehaviour for Behaviour { ConnectionHandler } - fn on_connection_handler_event(&mut self, _: PeerId, _: ConnectionId, event: Void) { + fn on_connection_handler_event( + &mut self, + _: PeerId, + _: ConnectionId, + event: THandlerOutEvent, + ) { void::unreachable(event) } diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index b6085b7db9e..448e63638c6 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -4,6 +4,7 @@ use crate::handler::{ ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound, KeepAlive, SubstreamProtocol, }; +use crate::THandlerOutEvent; use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::PeerId; use std::task::{Context, Poll}; @@ -26,7 +27,12 @@ impl NetworkBehaviour for Behaviour { ConnectionHandler } - fn on_connection_handler_event(&mut self, _: PeerId, _: ConnectionId, event: Void) { + fn on_connection_handler_event( + &mut self, + _: PeerId, + _: ConnectionId, + event: THandlerOutEvent, + ) { void::unreachable(event) } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 0e2b0abc9ec..4a39bdf5a02 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -166,7 +166,7 @@ type THandlerInEvent = < as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent; /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. -type THandlerOutEvent = +pub type THandlerOutEvent = < as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent; /// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 5536d50acb5..104f505afd2 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -24,7 +24,7 @@ use crate::behaviour::{ }; use crate::{ ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, - NetworkBehaviourAction, PollParameters, + NetworkBehaviourAction, PollParameters, THandlerOutEvent, }; use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId}; use std::collections::HashMap; @@ -107,8 +107,7 @@ where &mut self, _peer_id: PeerId, _connection_id: ConnectionId, - _event: <::Handler as - ConnectionHandler>::OutEvent, + _event: THandlerOutEvent, ) { } } @@ -455,7 +454,7 @@ where &mut self, p: PeerId, c: ConnectionId, - e: <::Handler as ConnectionHandler>::OutEvent, + e: THandlerOutEvent, ) { assert!( self.on_connection_established diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 38d0f388b10..0d3b116f82f 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -21,7 +21,7 @@ use futures::StreamExt; use libp2p_identify as identify; use libp2p_ping as ping; -use libp2p_swarm::{behaviour::FromSwarm, dummy, NetworkBehaviour, SwarmEvent}; +use libp2p_swarm::{behaviour::FromSwarm, dummy, NetworkBehaviour, SwarmEvent, THandlerOutEvent}; use std::fmt::Debug; /// Small utility to check that a type implements `NetworkBehaviour`. @@ -369,10 +369,7 @@ fn generated_out_event_derive_debug() { #[test] fn custom_out_event_no_type_parameters() { use libp2p_core::PeerId; - use libp2p_swarm::{ - ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviourAction, - PollParameters, - }; + use libp2p_swarm::{ConnectionId, NetworkBehaviourAction, PollParameters}; use std::task::Context; use std::task::Poll; @@ -392,7 +389,7 @@ fn custom_out_event_no_type_parameters() { &mut self, _peer: PeerId, _connection: ConnectionId, - message: <::Handler as ConnectionHandler>::OutEvent, + message: THandlerOutEvent, ) { void::unreachable(message); } From 385b9513b83344e08c4887d29026e1c72c9ead9d Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 23 Jan 2023 16:48:41 +1100 Subject: [PATCH 2/4] Nest use statements --- protocols/identify/src/behaviour.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 3b285b53e8c..4e96d175de2 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -21,12 +21,13 @@ use crate::handler::{self, InEvent, Proto}; use crate::protocol::{Info, Protocol, UpgradeError}; use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey}; -use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::{ - dial_opts::DialOpts, AddressScore, ConnectionHandlerUpgrErr, DialError, ExternalAddresses, + behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, + dial_opts::DialOpts, + AddressScore, ConnectionHandlerUpgrErr, ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, + THandlerOutEvent, }; -use libp2p_swarm::{ConnectionId, THandlerOutEvent}; use lru::LruCache; use std::num::NonZeroUsize; use std::{ From c8be16a0ee60894d5839baae9d7a9dfaf25fe186 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 24 Jan 2023 16:08:43 +1100 Subject: [PATCH 3/4] Fix doc test --- swarm/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index df424ecc1cb..932c92549b2 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -260,7 +260,7 @@ pub enum NetworkBehaviourAction< /// # use libp2p_plaintext::PlainText2Config; /// # use libp2p_swarm::{ /// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, - /// # FromSwarm, DialFailure, + /// # FromSwarm, DialFailure, THandlerOutEvent, /// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler, /// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent, /// # }; From 3fa93a8268d3dbd2b93f51300ae32b264954f1ae Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 26 Jan 2023 16:11:32 +1100 Subject: [PATCH 4/4] Remove Sized bound --- swarm/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 932c92549b2..584800a096a 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -119,7 +119,7 @@ pub(crate) type THandlerInEvent = /// } /// } /// ``` -pub trait NetworkBehaviour: Sized + 'static { +pub trait NetworkBehaviour: 'static { /// Handler for all the protocols the network behaviour supports. type ConnectionHandler: IntoConnectionHandler;