refactor: expose and use THandlerOutEvent type alias (#3368)

Previously, we used the full reference to the `OutEvent` of the `ConnectionHandler` in all implementations of `NetworkBehaviour`. Not only is this very verbose, it is also more brittle to changes. With the current implementation plan for #2824, we will be removing the `IntoConnectionHandler` abstraction. Using a type-alias to refer to the `OutEvent` makes the migration much easier.
This commit is contained in:
Thomas Eizinger
2023-01-26 22:55:02 +11:00
committed by GitHub
parent 1c596af1cf
commit 4de54f00f9
22 changed files with 75 additions and 68 deletions

View File

@ -37,8 +37,8 @@ use libp2p_swarm::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr, AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm, ExpiredListenAddr, FromSwarm,
}, },
ConnectionHandler, ConnectionId, ExternalAddresses, IntoConnectionHandler, ListenAddresses, ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
NetworkBehaviour, NetworkBehaviourAction, PollParameters, PollParameters, THandlerOutEvent,
}; };
use std::{ use std::{
collections::{HashMap, VecDeque}, collections::{HashMap, VecDeque},
@ -553,8 +553,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: ConnectionId, connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
) { ) {
self.inner self.inner
.on_connection_handler_event(peer_id, connection_id, event) .on_connection_handler_event(peer_id, connection_id, event)

View File

@ -27,11 +27,11 @@ use libp2p_core::multiaddr::Protocol;
use libp2p_core::{Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::dial_opts::{self, DialOpts}; use libp2p_swarm::dial_opts::{self, DialOpts};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, IntoConnectionHandler, ConnectionHandlerUpgrErr, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters, NotifyHandler, PollParameters,
}; };
use libp2p_swarm::{ConnectionId, THandlerOutEvent};
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use thiserror::Error; use thiserror::Error;
@ -222,8 +222,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
event_source: PeerId, event_source: PeerId,
connection: ConnectionId, connection: ConnectionId,
handler_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as handler_event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
) { ) {
match handler_event { match handler_event {
Either::Left(handler::relayed::Event::InboundConnectRequest { Either::Left(handler::relayed::Event::InboundConnectRequest {

View File

@ -30,9 +30,8 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, dial_opts::DialOpts, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
OneShotHandler, PollParameters, OneShotHandler, PollParameters, THandlerOutEvent,
}; };
use libp2p_swarm::{ConnectionHandler, IntoConnectionHandler};
use log::warn; use log::warn;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::collections::hash_map::{DefaultHasher, HashMap}; use std::collections::hash_map::{DefaultHasher, HashMap};
@ -352,8 +351,7 @@ impl NetworkBehaviour for Floodsub {
&mut self, &mut self,
propagation_source: PeerId, propagation_source: PeerId,
_connection_id: ConnectionId, _connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
) { ) {
// We ignore successful sends or timeouts. // We ignore successful sends or timeouts.
let event = match event { let event = match event {

View File

@ -41,8 +41,8 @@ use libp2p_core::{
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm}, behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, FromSwarm},
dial_opts::DialOpts, dial_opts::DialOpts,
ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
}; };
use wasm_timer::Instant; use wasm_timer::Instant;
@ -3313,8 +3313,7 @@ where
&mut self, &mut self,
propagation_source: PeerId, propagation_source: PeerId,
_connection_id: ConnectionId, _connection_id: ConnectionId,
handler_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as handler_event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
) { ) {
match handler_event { match handler_event {
HandlerEvent::PeerKind(kind) => { HandlerEvent::PeerKind(kind) => {

View File

@ -21,12 +21,12 @@
use crate::handler::{self, InEvent, Proto}; use crate::handler::{self, InEvent, Proto};
use crate::protocol::{Info, Protocol, UpgradeError}; use crate::protocol::{Info, Protocol, UpgradeError};
use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey}; use libp2p_core::{multiaddr, ConnectedPoint, Multiaddr, PeerId, PublicKey};
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::ConnectionId;
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError, behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
ExternalAddresses, IntoConnectionHandler, ListenAddresses, NetworkBehaviour, dial_opts::DialOpts,
NetworkBehaviourAction, NotifyHandler, PollParameters, AddressScore, ConnectionHandlerUpgrErr, ConnectionId, DialError, ExternalAddresses,
ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
THandlerOutEvent,
}; };
use lru::LruCache; use lru::LruCache;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
@ -253,7 +253,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: ConnectionId, connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent, event: THandlerOutEvent<Self>,
) { ) {
match event { match event {
handler::Event::Identified(mut info) => { handler::Event::Identified(mut info) => {

View File

@ -46,7 +46,7 @@ use libp2p_swarm::behaviour::{
use libp2p_swarm::{ use libp2p_swarm::{
dial_opts::{self, DialOpts}, dial_opts::{self, DialOpts},
ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour, ConnectionId, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
}; };
use log::{debug, info, warn}; use log::{debug, info, warn};
use smallvec::SmallVec; use smallvec::SmallVec;
@ -2025,7 +2025,7 @@ where
&mut self, &mut self,
source: PeerId, source: PeerId,
connection: ConnectionId, connection: ConnectionId,
event: KademliaHandlerEvent<QueryId>, event: THandlerOutEvent<Self>,
) { ) {
match event { match event {
KademliaHandlerEvent::ProtocolConfirmed { endpoint } => { KademliaHandlerEvent::ProtocolConfirmed { endpoint } => {

View File

@ -30,8 +30,8 @@ use if_watch::IfEvent;
use libp2p_core::{Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{ use libp2p_swarm::{
dummy, ConnectionHandler, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, dummy, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
PollParameters, THandlerOutEvent,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
use std::collections::hash_map::{Entry, HashMap}; use std::collections::hash_map::{Entry, HashMap};
@ -190,7 +190,7 @@ where
&mut self, &mut self,
_: PeerId, _: PeerId,
_: libp2p_swarm::ConnectionId, _: libp2p_swarm::ConnectionId,
ev: <Self::ConnectionHandler as ConnectionHandler>::OutEvent, ev: THandlerOutEvent<Self>,
) { ) {
void::unreachable(ev) void::unreachable(ev)
} }

View File

@ -50,6 +50,7 @@ pub use handler::{Config, Failure, Success};
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters, behaviour::FromSwarm, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
THandlerOutEvent,
}; };
use std::{ use std::{
collections::VecDeque, collections::VecDeque,
@ -123,7 +124,12 @@ impl NetworkBehaviour for Behaviour {
Handler::new(self.config.clone()) 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>,
) {
self.events.push_front(Event { peer, result }) self.events.push_front(Event { peer, result })
} }

View File

@ -32,7 +32,7 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm}; use libp2p_swarm::behaviour::{ConnectionClosed, FromSwarm};
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour, ConnectionHandlerUpgrErr, ConnectionId, ExternalAddresses, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
}; };
use std::collections::{hash_map, HashMap, HashSet, VecDeque}; use std::collections::{hash_map, HashMap, HashSet, VecDeque};
use std::num::NonZeroU32; use std::num::NonZeroU32;
@ -287,7 +287,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
event_source: PeerId, event_source: PeerId,
connection: ConnectionId, connection: ConnectionId,
event: Either<handler::Event, void::Void>, event: THandlerOutEvent<Self>,
) { ) {
let event = match event { let event = match event {
Either::Left(e) => e, Either::Left(e) => e,

View File

@ -37,7 +37,7 @@ use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm
use libp2p_swarm::dial_opts::DialOpts; use libp2p_swarm::dial_opts::DialOpts;
use libp2p_swarm::{ use libp2p_swarm::{
ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour, ConnectionHandlerUpgrErr, ConnectionId, NegotiatedSubstream, NetworkBehaviour,
NetworkBehaviourAction, NotifyHandler, PollParameters, NetworkBehaviourAction, NotifyHandler, PollParameters, THandlerOutEvent,
}; };
use std::collections::{hash_map, HashMap, VecDeque}; use std::collections::{hash_map, HashMap, VecDeque};
use std::io::{Error, ErrorKind, IoSlice}; use std::io::{Error, ErrorKind, IoSlice};
@ -194,7 +194,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
event_source: PeerId, event_source: PeerId,
_connection: ConnectionId, _connection: ConnectionId,
handler_event: Either<handler::Event, void::Void>, handler_event: THandlerOutEvent<Self>,
) { ) {
let handler_event = match handler_event { let handler_event = match handler_event {
Either::Left(e) => e, Either::Left(e) => e,

View File

@ -34,7 +34,7 @@ use libp2p_core::{Multiaddr, PeerId, PeerRecord};
use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{ use libp2p_swarm::{
CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction, CloseConnection, ConnectionId, ExternalAddresses, NetworkBehaviour, NetworkBehaviourAction,
NotifyHandler, PollParameters, NotifyHandler, PollParameters, THandlerOutEvent,
}; };
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::iter::FromIterator; use std::iter::FromIterator;
@ -191,7 +191,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: ConnectionId, connection_id: ConnectionId,
event: handler::OutboundOutEvent, event: THandlerOutEvent<Self>,
) { ) {
let new_events = match event { let new_events = match event {
handler::OutboundOutEvent::InboundEvent { message, .. } => void::unreachable(message), handler::OutboundOutEvent::InboundEvent { message, .. } => void::unreachable(message),

View File

@ -31,7 +31,7 @@ use libp2p_core::PeerId;
use libp2p_swarm::behaviour::FromSwarm; use libp2p_swarm::behaviour::FromSwarm;
use libp2p_swarm::{ use libp2p_swarm::{
CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, CloseConnection, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler,
PollParameters, PollParameters, THandlerOutEvent,
}; };
use std::collections::{HashMap, HashSet, VecDeque}; use std::collections::{HashMap, HashSet, VecDeque};
use std::iter::FromIterator; use std::iter::FromIterator;
@ -123,7 +123,7 @@ impl NetworkBehaviour for Behaviour {
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection: ConnectionId, connection: ConnectionId,
event: handler::InboundOutEvent, event: THandlerOutEvent<Self>,
) { ) {
let new_events = match event { let new_events = match event {
handler::InboundOutEvent::InboundEvent { id, message } => { handler::InboundOutEvent::InboundEvent { id, message } => {

View File

@ -74,8 +74,8 @@ use libp2p_core::{ConnectedPoint, Multiaddr, PeerId};
use libp2p_swarm::{ use libp2p_swarm::{
behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm}, behaviour::{AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm},
dial_opts::DialOpts, dial_opts::DialOpts,
ConnectionId, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, ConnectionId, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
PollParameters, THandlerOutEvent,
}; };
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{ use std::{
@ -776,8 +776,7 @@ where
&mut self, &mut self,
peer: PeerId, peer: PeerId,
connection: ConnectionId, connection: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as event: THandlerOutEvent<Self>,
libp2p_swarm::ConnectionHandler>::OutEvent,
) { ) {
match event { match event {
handler::Event::Response { handler::Event::Response {

View File

@ -29,7 +29,7 @@ pub use listen_addresses::ListenAddresses;
use crate::connection::ConnectionId; use crate::connection::ConnectionId;
use crate::dial_opts::DialOpts; use crate::dial_opts::DialOpts;
use crate::handler::{ConnectionHandler, IntoConnectionHandler}; 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 libp2p_core::{transport::ListenerId, ConnectedPoint, Multiaddr, PeerId};
use std::{task::Context, task::Poll}; use std::{task::Context, task::Poll};
@ -37,9 +37,6 @@ use std::{task::Context, task::Poll};
pub(crate) type THandlerInEvent<THandler> = pub(crate) type THandlerInEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent; <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
pub(crate) type THandlerOutEvent<THandler> =
<<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
/// A [`NetworkBehaviour`] defines the behaviour of the local node on the network. /// 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 /// In contrast to [`Transport`](libp2p_core::Transport) which defines **how** to send bytes on the
@ -170,8 +167,7 @@ pub trait NetworkBehaviour: 'static {
&mut self, &mut self,
_peer_id: PeerId, _peer_id: PeerId,
_connection_id: ConnectionId, _connection_id: ConnectionId,
_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as _event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
); );
/// Polls for things that swarm should do. /// Polls for things that swarm should do.
@ -264,7 +260,7 @@ pub enum NetworkBehaviourAction<
/// # use libp2p_plaintext::PlainText2Config; /// # use libp2p_plaintext::PlainText2Config;
/// # use libp2p_swarm::{ /// # use libp2p_swarm::{
/// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream, /// # ConnectionId, DialError, IntoConnectionHandler, KeepAlive, NegotiatedSubstream,
/// # FromSwarm, DialFailure, /// # FromSwarm, DialFailure, THandlerOutEvent,
/// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler, /// # NetworkBehaviour, NetworkBehaviourAction, PollParameters, ConnectionHandler,
/// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent, /// # ConnectionHandlerEvent, ConnectionHandlerUpgrErr, SubstreamProtocol, Swarm, SwarmEvent,
/// # }; /// # };
@ -334,7 +330,7 @@ pub enum NetworkBehaviourAction<
/// # &mut self, /// # &mut self,
/// # _: PeerId, /// # _: PeerId,
/// # _: ConnectionId, /// # _: ConnectionId,
/// # _: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent, /// # _: THandlerOutEvent<Self>,
/// # ) { /// # ) {
/// # unreachable!(); /// # unreachable!();
/// # } /// # }

View File

@ -21,6 +21,7 @@
use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::behaviour::{self, NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use crate::connection::ConnectionId; use crate::connection::ConnectionId;
use crate::handler::either::IntoEitherHandler; use crate::handler::either::IntoEitherHandler;
use crate::THandlerOutEvent;
use either::Either; use either::Either;
use libp2p_core::{Multiaddr, PeerId}; use libp2p_core::{Multiaddr, PeerId};
use std::{task::Context, task::Poll}; use std::{task::Context, task::Poll};
@ -71,7 +72,7 @@ where
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: ConnectionId, connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>, event: THandlerOutEvent<Self>,
) { ) {
match (self, event) { match (self, event) {
(Either::Left(left), Either::Left(event)) => { (Either::Left(left), Either::Left(event)) => {

View File

@ -26,7 +26,7 @@ use crate::handler::{
IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol, IntoConnectionHandler, KeepAlive, ListenUpgradeError, SubstreamProtocol,
}; };
use crate::upgrade::SendWrapper; use crate::upgrade::SendWrapper;
use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters}; use crate::{NetworkBehaviour, NetworkBehaviourAction, PollParameters, THandlerOutEvent};
use either::Either; use either::Either;
use futures::future; use futures::future;
use libp2p_core::{upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId}; use libp2p_core::{upgrade::DeniedUpgrade, ConnectedPoint, Multiaddr, PeerId};
@ -94,7 +94,7 @@ where
&mut self, &mut self,
peer_id: PeerId, peer_id: PeerId,
connection_id: ConnectionId, connection_id: ConnectionId,
event: crate::THandlerOutEvent<Self>, event: THandlerOutEvent<Self>,
) { ) {
if let Some(behaviour) = &mut self.inner { if let Some(behaviour) = &mut self.inner {
behaviour.on_connection_handler_event(peer_id, connection_id, event) behaviour.on_connection_handler_event(peer_id, connection_id, event)

View File

@ -21,7 +21,7 @@
use crate::connection::{Connection, ConnectionId, PendingPoint}; use crate::connection::{Connection, ConnectionId, PendingPoint};
use crate::{ use crate::{
behaviour::{THandlerInEvent, THandlerOutEvent}, behaviour::THandlerInEvent,
connection::{ connection::{
Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError, Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError,
PendingInboundConnectionError, PendingOutboundConnectionError, PendingInboundConnectionError, PendingOutboundConnectionError,
@ -294,7 +294,7 @@ pub enum PoolEvent<THandler: IntoConnectionHandler> {
id: ConnectionId, id: ConnectionId,
peer_id: PeerId, peer_id: PeerId,
/// The produced event. /// The produced event.
event: THandlerOutEvent<THandler>, event: <<THandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent,
}, },
/// The connection to a node has changed its address. /// The connection to a node has changed its address.

View File

@ -3,7 +3,10 @@ use crate::connection::ConnectionId;
use crate::handler::{ use crate::handler::{
ConnectionEvent, DialUpgradeError, FullyNegotiatedInbound, FullyNegotiatedOutbound, 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::upgrade::DeniedUpgrade;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_core::UpgradeError; use libp2p_core::UpgradeError;
@ -21,7 +24,12 @@ impl NetworkBehaviour for Behaviour {
ConnectionHandler ConnectionHandler
} }
fn on_connection_handler_event(&mut self, _: PeerId, _: ConnectionId, event: Void) { fn on_connection_handler_event(
&mut self,
_: PeerId,
_: ConnectionId,
event: THandlerOutEvent<Self>,
) {
void::unreachable(event) void::unreachable(event)
} }

View File

@ -4,6 +4,7 @@ use crate::handler::{
ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound, ConnectionEvent, ConnectionHandlerEvent, FullyNegotiatedInbound, FullyNegotiatedOutbound,
KeepAlive, SubstreamProtocol, KeepAlive, SubstreamProtocol,
}; };
use crate::THandlerOutEvent;
use libp2p_core::upgrade::DeniedUpgrade; use libp2p_core::upgrade::DeniedUpgrade;
use libp2p_core::PeerId; use libp2p_core::PeerId;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
@ -26,7 +27,12 @@ impl NetworkBehaviour for Behaviour {
ConnectionHandler ConnectionHandler
} }
fn on_connection_handler_event(&mut self, _: PeerId, _: ConnectionId, event: Void) { fn on_connection_handler_event(
&mut self,
_: PeerId,
_: ConnectionId,
event: THandlerOutEvent<Self>,
) {
void::unreachable(event) void::unreachable(event)
} }

View File

@ -166,7 +166,7 @@ type THandlerInEvent<TBehaviour> =
<<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent; <<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::InEvent;
/// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].
type THandlerOutEvent<TBehaviour> = pub type THandlerOutEvent<TBehaviour> =
<<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent; <<THandler<TBehaviour> as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent;
/// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. /// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].

View File

@ -24,7 +24,7 @@ use crate::behaviour::{
}; };
use crate::{ use crate::{
ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour, ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviour,
NetworkBehaviourAction, PollParameters, NetworkBehaviourAction, PollParameters, THandlerOutEvent,
}; };
use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId}; use libp2p_core::{multiaddr::Multiaddr, transport::ListenerId, ConnectedPoint, PeerId};
use std::collections::HashMap; use std::collections::HashMap;
@ -107,8 +107,7 @@ where
&mut self, &mut self,
_peer_id: PeerId, _peer_id: PeerId,
_connection_id: ConnectionId, _connection_id: ConnectionId,
_event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as _event: THandlerOutEvent<Self>,
ConnectionHandler>::OutEvent,
) { ) {
} }
} }
@ -455,7 +454,7 @@ where
&mut self, &mut self,
p: PeerId, p: PeerId,
c: ConnectionId, c: ConnectionId,
e: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent, e: THandlerOutEvent<Self>,
) { ) {
assert!( assert!(
self.on_connection_established self.on_connection_established

View File

@ -21,7 +21,7 @@
use futures::StreamExt; use futures::StreamExt;
use libp2p_identify as identify; use libp2p_identify as identify;
use libp2p_ping as ping; 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; use std::fmt::Debug;
/// Small utility to check that a type implements `NetworkBehaviour`. /// Small utility to check that a type implements `NetworkBehaviour`.
@ -369,10 +369,7 @@ fn generated_out_event_derive_debug() {
#[test] #[test]
fn custom_out_event_no_type_parameters() { fn custom_out_event_no_type_parameters() {
use libp2p_core::PeerId; use libp2p_core::PeerId;
use libp2p_swarm::{ use libp2p_swarm::{ConnectionId, NetworkBehaviourAction, PollParameters};
ConnectionHandler, ConnectionId, IntoConnectionHandler, NetworkBehaviourAction,
PollParameters,
};
use std::task::Context; use std::task::Context;
use std::task::Poll; use std::task::Poll;
@ -392,7 +389,7 @@ fn custom_out_event_no_type_parameters() {
&mut self, &mut self,
_peer: PeerId, _peer: PeerId,
_connection: ConnectionId, _connection: ConnectionId,
message: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::OutEvent, message: THandlerOutEvent<Self>,
) { ) {
void::unreachable(message); void::unreachable(message);
} }