feat(swarm): rename associated types for message passing

Previously, the associated types on `NetworkBehaviour` and `ConnectionHandler` carried generic names like `InEvent` and `OutEvent`. These names are _correct_ in that `OutEvent`s are passed out and `InEvent`s are passed in but they don't help users understand how these types are used.

In theory, a `ConnectionHandler` could be used separately from `NetworkBehaviour`s but that is highly unlikely. Thus, we rename these associated types to indicate, where the message is going to be sent to:

- `NetworkBehaviour::OutEvent` is renamed to `ToSwarm`: It describes the message(s) a `NetworkBehaviour` can emit to the `Swarm`. The user is going to receive those in `SwarmEvent::Behaviour`.
- `ConnectionHandler::InEvent` is renamed to `FromBehaviour`: It describes the message(s) a `ConnectionHandler` can receive from its behaviour via `ConnectionHandler::on_swarm_event`. The `NetworkBehaviour` can send it via the `ToSwarm::NotifyHandler` command.
- `ConnectionHandler::OutEvent` is renamed to `ToBehaviour`: It describes the message(s) a `ConnectionHandler` can send back to the behaviour via the now also renamed `ConnectionHandlerEvent::NotifyBehaviour` (previously `ConnectionHandlerEvent::Custom`)

Resolves: #2854.

Pull-Request: #3848.
This commit is contained in:
Thomas Coratger
2023-05-14 12:58:08 +02:00
committed by GitHub
parent 5b32c8a0d2
commit 6e36e8aa35
65 changed files with 355 additions and 236 deletions

View File

@ -77,14 +77,14 @@ use std::{task::Context, task::Poll};
/// [`NetworkBehaviour::poll`] it will first poll the first `struct` member until it returns
/// [`Poll::Pending`] before moving on to later members.
///
/// Events ([`NetworkBehaviour::OutEvent`]) returned by each `struct` member are wrapped in a new
/// Events ([`NetworkBehaviour::ToSwarm`]) returned by each `struct` member are wrapped in a new
/// `enum` event, with an `enum` variant for each `struct` member. Users can define this event
/// `enum` themselves and provide the name to the derive macro via `#[behaviour(out_event =
/// "MyCustomOutEvent")]`. If the user does not specify an `out_event`, the derive macro generates
/// `enum` themselves and provide the name to the derive macro via `#[behaviour(to_swarm =
/// "MyCustomOutEvent")]`. If the user does not specify an `to_swarm`, the derive macro generates
/// the event definition itself, naming it `<STRUCT_NAME>Event`.
///
/// The aforementioned conversion of each of the event types generated by the struct members to the
/// custom `out_event` is handled by [`From`] implementations which the user needs to define in
/// custom `to_swarm` is handled by [`From`] implementations which the user needs to define in
/// addition to the event `enum` itself.
///
/// ``` rust
@ -92,7 +92,7 @@ use std::{task::Context, task::Poll};
/// # use libp2p_ping as ping;
/// # use libp2p_swarm_derive::NetworkBehaviour;
/// #[derive(NetworkBehaviour)]
/// #[behaviour(out_event = "Event")]
/// #[behaviour(to_swarm = "Event")]
/// # #[behaviour(prelude = "libp2p_swarm::derive_prelude")]
/// struct MyBehaviour {
/// identify: identify::Behaviour,
@ -121,7 +121,7 @@ pub trait NetworkBehaviour: 'static {
type ConnectionHandler: ConnectionHandler;
/// Event generated by the `NetworkBehaviour` and that the swarm will report back.
type OutEvent: Send + 'static;
type ToSwarm: Send + 'static;
/// Callback that is invoked for every new inbound connection.
///
@ -212,7 +212,7 @@ pub trait NetworkBehaviour: 'static {
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>>;
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>>;
}
/// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to.

View File

@ -33,7 +33,7 @@ where
R: NetworkBehaviour,
{
type ConnectionHandler = Either<THandler<L>, THandler<R>>;
type OutEvent = Either<L::OutEvent, R::OutEvent>;
type ToSwarm = Either<L::ToSwarm, R::ToSwarm>;
fn handle_pending_inbound_connection(
&mut self,
@ -156,7 +156,7 @@ where
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
let event = match self {
Either::Left(behaviour) => futures::ready!(behaviour.poll(cx, params))
.map_out(Either::Left)

View File

@ -71,7 +71,7 @@ where
TBehaviour: NetworkBehaviour,
{
type ConnectionHandler = ToggleConnectionHandler<THandler<TBehaviour>>;
type OutEvent = TBehaviour::OutEvent;
type ToSwarm = TBehaviour::ToSwarm;
fn handle_pending_inbound_connection(
&mut self,
@ -182,7 +182,7 @@ where
&mut self,
cx: &mut Context<'_>,
params: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(inner) = self.inner.as_mut() {
inner.poll(cx, params)
} else {
@ -267,8 +267,8 @@ impl<TInner> ConnectionHandler for ToggleConnectionHandler<TInner>
where
TInner: ConnectionHandler,
{
type InEvent = TInner::InEvent;
type OutEvent = TInner::OutEvent;
type FromBehaviour = TInner::FromBehaviour;
type ToBehaviour = TInner::ToBehaviour;
type Error = TInner::Error;
type InboundProtocol = Either<SendWrapper<TInner::InboundProtocol>, SendWrapper<DeniedUpgrade>>;
type OutboundProtocol = TInner::OutboundProtocol;
@ -286,7 +286,7 @@ where
}
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
self.inner
.as_mut()
.expect("Can't receive events if disabled; QED")
@ -307,7 +307,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -209,7 +209,7 @@ where
}
/// Notifies the connection handler of an event.
pub(crate) fn on_behaviour_event(&mut self, event: THandler::InEvent) {
pub(crate) fn on_behaviour_event(&mut self, event: THandler::FromBehaviour) {
self.handler.on_behaviour_event(event);
}
@ -224,7 +224,7 @@ where
pub(crate) fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
) -> Poll<Result<Event<THandler::ToBehaviour>, ConnectionError<THandler::Error>>> {
let Self {
requested_substreams,
muxing,
@ -439,7 +439,7 @@ where
#[cfg(test)]
fn poll_noop_waker(
&mut self,
) -> Poll<Result<Event<THandler::OutEvent>, ConnectionError<THandler::Error>>> {
) -> Poll<Result<Event<THandler::ToBehaviour>, ConnectionError<THandler::Error>>> {
Pin::new(self).poll(&mut Context::from_waker(futures::task::noop_waker_ref()))
}
}
@ -995,8 +995,8 @@ mod tests {
}
impl ConnectionHandler for MockConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
@ -1037,7 +1037,7 @@ mod tests {
}
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
void::unreachable(event)
}
@ -1052,7 +1052,7 @@ mod tests {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {
@ -1069,8 +1069,8 @@ mod tests {
}
impl ConnectionHandler for ConfigurableProtocolConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = ManyProtocolsUpgrade;
type OutboundProtocol = DeniedUpgrade;
@ -1114,7 +1114,7 @@ mod tests {
}
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
void::unreachable(event)
}
@ -1129,7 +1129,7 @@ mod tests {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -90,8 +90,10 @@ where
counters: ConnectionCounters,
/// The managed connections of each peer that are currently considered established.
established:
FnvHashMap<PeerId, FnvHashMap<ConnectionId, EstablishedConnection<THandler::InEvent>>>,
established: FnvHashMap<
PeerId,
FnvHashMap<ConnectionId, EstablishedConnection<THandler::FromBehaviour>>,
>,
/// The pending connections that are currently being negotiated.
pending: HashMap<ConnectionId, PendingConnection>,
@ -285,7 +287,7 @@ pub(crate) enum PoolEvent<THandler: ConnectionHandler> {
id: ConnectionId,
peer_id: PeerId,
/// The produced event.
event: THandler::OutEvent,
event: THandler::ToBehaviour,
},
/// The connection to a node has changed its address.
@ -338,7 +340,7 @@ where
pub(crate) fn get_established(
&mut self,
id: ConnectionId,
) -> Option<&mut EstablishedConnection<THandler::InEvent>> {
) -> Option<&mut EstablishedConnection<THandler::FromBehaviour>> {
self.established
.values_mut()
.find_map(|connections| connections.get_mut(&id))

View File

@ -77,7 +77,7 @@ pub(crate) enum EstablishedConnectionEvent<THandler: ConnectionHandler> {
Notify {
id: ConnectionId,
peer_id: PeerId,
event: THandler::OutEvent,
event: THandler::ToBehaviour,
},
/// A connection closed, possibly due to an error.
///
@ -171,7 +171,7 @@ pub(crate) async fn new_for_established_connection<THandler>(
connection_id: ConnectionId,
peer_id: PeerId,
mut connection: crate::connection::Connection<THandler>,
mut command_receiver: mpsc::Receiver<Command<THandler::InEvent>>,
mut command_receiver: mpsc::Receiver<Command<THandler::FromBehaviour>>,
mut events: mpsc::Sender<EstablishedConnectionEvent<THandler>>,
) where
THandler: ConnectionHandler,

View File

@ -19,7 +19,7 @@ pub struct Behaviour;
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = ConnectionHandler;
type OutEvent = Void;
type ToSwarm = Void;
fn handle_established_inbound_connection(
&mut self,
@ -54,7 +54,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
Poll::Pending
}
@ -81,8 +81,8 @@ impl NetworkBehaviour for Behaviour {
pub struct ConnectionHandler;
impl crate::handler::ConnectionHandler for ConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
@ -93,7 +93,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
SubstreamProtocol::new(DeniedUpgrade, ())
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
void::unreachable(event)
}
@ -108,7 +108,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -99,10 +99,10 @@ use std::{cmp::Ordering, error, fmt, io, task::Context, task::Poll, time::Durati
/// When a connection is closed gracefully, the substreams used by the handler may still
/// continue reading data until the remote closes its side of the connection.
pub trait ConnectionHandler: Send + 'static {
/// Custom event that can be received from the outside.
type InEvent: fmt::Debug + Send + 'static;
/// Custom event that can be produced by the handler and that will be returned to the outside.
type OutEvent: fmt::Debug + Send + 'static;
/// A type representing the message(s) a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`](crate::behaviour::ToSwarm::NotifyHandler)
type FromBehaviour: fmt::Debug + Send + 'static;
/// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) via [`ConnectionHandlerEvent::Custom`].
type ToBehaviour: fmt::Debug + Send + 'static;
/// The type of errors returned by [`ConnectionHandler::poll`].
type Error: error::Error + fmt::Debug + Send + 'static;
/// The inbound upgrade for the protocol(s) used by the handler.
@ -153,7 +153,7 @@ pub trait ConnectionHandler: Send + 'static {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
>;
@ -162,7 +162,7 @@ pub trait ConnectionHandler: Send + 'static {
fn map_in_event<TNewIn, TMap>(self, map: TMap) -> MapInEvent<Self, TNewIn, TMap>
where
Self: Sized,
TMap: Fn(&TNewIn) -> Option<&Self::InEvent>,
TMap: Fn(&TNewIn) -> Option<&Self::FromBehaviour>,
{
MapInEvent::new(self, map)
}
@ -171,7 +171,7 @@ pub trait ConnectionHandler: Send + 'static {
fn map_out_event<TMap, TNewOut>(self, map: TMap) -> MapOutEvent<Self, TMap>
where
Self: Sized,
TMap: FnMut(Self::OutEvent) -> TNewOut,
TMap: FnMut(Self::ToBehaviour) -> TNewOut,
{
MapOutEvent::new(self, map)
}
@ -190,7 +190,7 @@ pub trait ConnectionHandler: Send + 'static {
}
/// Informs the handler about an event from the [`NetworkBehaviour`](super::NetworkBehaviour).
fn on_behaviour_event(&mut self, _event: Self::InEvent);
fn on_behaviour_event(&mut self, _event: Self::FromBehaviour);
fn on_connection_event(
&mut self,

View File

@ -78,8 +78,8 @@ where
L: ConnectionHandler,
R: ConnectionHandler,
{
type InEvent = Either<L::InEvent, R::InEvent>;
type OutEvent = Either<L::OutEvent, R::OutEvent>;
type FromBehaviour = Either<L::FromBehaviour, R::FromBehaviour>;
type ToBehaviour = Either<L::ToBehaviour, R::ToBehaviour>;
type Error = Either<L::Error, R::Error>;
type InboundProtocol = Either<SendWrapper<L::InboundProtocol>, SendWrapper<R::InboundProtocol>>;
type OutboundProtocol =
@ -100,7 +100,7 @@ where
}
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
match (self, event) {
(Either::Left(handler), Either::Left(event)) => handler.on_behaviour_event(event),
(Either::Right(handler), Either::Right(event)) => handler.on_behaviour_event(event),
@ -122,7 +122,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -45,12 +45,12 @@ impl<TConnectionHandler, TMap, TNewIn> ConnectionHandler
for MapInEvent<TConnectionHandler, TNewIn, TMap>
where
TConnectionHandler: ConnectionHandler,
TMap: Fn(TNewIn) -> Option<TConnectionHandler::InEvent>,
TMap: Fn(TNewIn) -> Option<TConnectionHandler::FromBehaviour>,
TNewIn: Debug + Send + 'static,
TMap: Send + 'static,
{
type InEvent = TNewIn;
type OutEvent = TConnectionHandler::OutEvent;
type FromBehaviour = TNewIn;
type ToBehaviour = TConnectionHandler::ToBehaviour;
type Error = TConnectionHandler::Error;
type InboundProtocol = TConnectionHandler::InboundProtocol;
type OutboundProtocol = TConnectionHandler::OutboundProtocol;
@ -78,7 +78,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -40,12 +40,12 @@ impl<TConnectionHandler, TMap> MapOutEvent<TConnectionHandler, TMap> {
impl<TConnectionHandler, TMap, TNewOut> ConnectionHandler for MapOutEvent<TConnectionHandler, TMap>
where
TConnectionHandler: ConnectionHandler,
TMap: FnMut(TConnectionHandler::OutEvent) -> TNewOut,
TMap: FnMut(TConnectionHandler::ToBehaviour) -> TNewOut,
TNewOut: Debug + Send + 'static,
TMap: Send + 'static,
{
type InEvent = TConnectionHandler::InEvent;
type OutEvent = TNewOut;
type FromBehaviour = TConnectionHandler::FromBehaviour;
type ToBehaviour = TNewOut;
type Error = TConnectionHandler::Error;
type InboundProtocol = TConnectionHandler::InboundProtocol;
type OutboundProtocol = TConnectionHandler::OutboundProtocol;
@ -56,7 +56,7 @@ where
self.inner.listen_protocol()
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
self.inner.on_behaviour_event(event)
}
@ -71,7 +71,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -110,8 +110,8 @@ where
H::InboundProtocol: InboundUpgradeSend,
H::OutboundProtocol: OutboundUpgradeSend,
{
type InEvent = (K, <H as ConnectionHandler>::InEvent);
type OutEvent = (K, <H as ConnectionHandler>::OutEvent);
type FromBehaviour = (K, <H as ConnectionHandler>::FromBehaviour);
type ToBehaviour = (K, <H as ConnectionHandler>::ToBehaviour);
type Error = <H as ConnectionHandler>::Error;
type InboundProtocol = Upgrade<K, <H as ConnectionHandler>::InboundProtocol>;
type OutboundProtocol = <H as ConnectionHandler>::OutboundProtocol;
@ -222,7 +222,7 @@ where
}
}
fn on_behaviour_event(&mut self, (key, event): Self::InEvent) {
fn on_behaviour_event(&mut self, (key, event): Self::FromBehaviour) {
if let Some(h) = self.handlers.get_mut(&key) {
h.on_behaviour_event(event)
} else {
@ -245,7 +245,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -121,8 +121,8 @@ where
SubstreamProtocol<TInbound, ()>: Clone,
TEvent: Debug + Send + 'static,
{
type InEvent = TOutbound;
type OutEvent = TEvent;
type FromBehaviour = TOutbound;
type ToBehaviour = TEvent;
type Error = StreamUpgradeError<<Self::OutboundProtocol as OutboundUpgradeSend>::Error>;
type InboundProtocol = TInbound;
type OutboundProtocol = TOutbound;
@ -133,7 +133,7 @@ where
self.listen_protocol.clone()
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
self.send_request(event);
}
@ -148,7 +148,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -40,8 +40,8 @@ impl PendingConnectionHandler {
}
impl ConnectionHandler for PendingConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = PendingUpgrade<String>;
type OutboundProtocol = PendingUpgrade<String>;
@ -52,7 +52,7 @@ impl ConnectionHandler for PendingConnectionHandler {
SubstreamProtocol::new(PendingUpgrade::new(self.protocol_name.clone()), ())
}
fn on_behaviour_event(&mut self, v: Self::InEvent) {
fn on_behaviour_event(&mut self, v: Self::FromBehaviour) {
void::unreachable(v)
}
@ -67,7 +67,7 @@ impl ConnectionHandler for PendingConnectionHandler {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -179,8 +179,8 @@ where
TProto1: ConnectionHandler,
TProto2: ConnectionHandler,
{
type InEvent = Either<TProto1::InEvent, TProto2::InEvent>;
type OutEvent = Either<TProto1::OutEvent, TProto2::OutEvent>;
type FromBehaviour = Either<TProto1::FromBehaviour, TProto2::FromBehaviour>;
type ToBehaviour = Either<TProto1::ToBehaviour, TProto2::ToBehaviour>;
type Error = Either<TProto1::Error, TProto2::Error>;
type InboundProtocol = SelectUpgrade<
SendWrapper<<TProto1 as ConnectionHandler>::InboundProtocol>,
@ -201,7 +201,7 @@ where
SubstreamProtocol::new(choice, (i1, i2)).with_timeout(timeout)
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
match event {
Either::Left(event) => self.proto1.on_behaviour_event(event),
Either::Right(event) => self.proto2.on_behaviour_event(event),
@ -222,7 +222,7 @@ where
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -22,7 +22,7 @@ pub struct Behaviour;
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = ConnectionHandler;
type OutEvent = Void;
type ToSwarm = Void;
fn handle_established_inbound_connection(
&mut self,
@ -57,7 +57,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
Poll::Pending
}
@ -84,8 +84,8 @@ impl NetworkBehaviour for Behaviour {
pub struct ConnectionHandler;
impl crate::handler::ConnectionHandler for ConnectionHandler {
type InEvent = Void;
type OutEvent = Void;
type FromBehaviour = Void;
type ToBehaviour = Void;
type Error = Void;
type InboundProtocol = DeniedUpgrade;
type OutboundProtocol = DeniedUpgrade;
@ -96,7 +96,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
SubstreamProtocol::new(DeniedUpgrade, ())
}
fn on_behaviour_event(&mut self, v: Self::InEvent) {
fn on_behaviour_event(&mut self, v: Self::FromBehaviour) {
void::unreachable(v)
}
@ -111,7 +111,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -165,7 +165,7 @@ use std::{
pub type NegotiatedSubstream = Stream;
/// Event generated by the [`NetworkBehaviour`] that the swarm will report back.
type TBehaviourOutEvent<TBehaviour> = <TBehaviour as NetworkBehaviour>::OutEvent;
type TBehaviourOutEvent<TBehaviour> = <TBehaviour as NetworkBehaviour>::ToSwarm;
/// [`ConnectionHandler`] of the [`NetworkBehaviour`] for all the protocols the [`NetworkBehaviour`]
/// supports.
@ -174,10 +174,10 @@ pub type THandler<TBehaviour> = <TBehaviour as NetworkBehaviour>::ConnectionHand
/// Custom event that can be received by the [`ConnectionHandler`] of the
/// [`NetworkBehaviour`].
pub type THandlerInEvent<TBehaviour> = <THandler<TBehaviour> as ConnectionHandler>::InEvent;
pub type THandlerInEvent<TBehaviour> = <THandler<TBehaviour> as ConnectionHandler>::FromBehaviour;
/// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].
pub type THandlerOutEvent<TBehaviour> = <THandler<TBehaviour> as ConnectionHandler>::OutEvent;
pub type THandlerOutEvent<TBehaviour> = <THandler<TBehaviour> as ConnectionHandler>::ToBehaviour;
/// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`].
pub type THandlerErr<TBehaviour> = <THandler<TBehaviour> as ConnectionHandler>::Error;
@ -743,7 +743,7 @@ where
fn handle_pool_event(
&mut self,
event: PoolEvent<THandler<TBehaviour>>,
) -> Option<SwarmEvent<TBehaviour::OutEvent, THandlerErr<TBehaviour>>> {
) -> Option<SwarmEvent<TBehaviour::ToSwarm, THandlerErr<TBehaviour>>> {
match event {
PoolEvent::ConnectionEstablished {
peer_id,
@ -984,7 +984,7 @@ where
<transport::Boxed<(PeerId, StreamMuxerBox)> as Transport>::ListenerUpgrade,
io::Error,
>,
) -> Option<SwarmEvent<TBehaviour::OutEvent, THandlerErr<TBehaviour>>> {
) -> Option<SwarmEvent<TBehaviour::ToSwarm, THandlerErr<TBehaviour>>> {
match event {
TransportEvent::Incoming {
listener_id: _,
@ -1109,8 +1109,8 @@ where
fn handle_behaviour_event(
&mut self,
event: ToSwarm<TBehaviour::OutEvent, THandlerInEvent<TBehaviour>>,
) -> Option<SwarmEvent<TBehaviour::OutEvent, THandlerErr<TBehaviour>>> {
event: ToSwarm<TBehaviour::ToSwarm, THandlerInEvent<TBehaviour>>,
) -> Option<SwarmEvent<TBehaviour::ToSwarm, THandlerErr<TBehaviour>>> {
match event {
ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)),
ToSwarm::Dial { opts } => {
@ -1194,7 +1194,7 @@ where
fn poll_next_event(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<SwarmEvent<TBehaviour::OutEvent, THandlerErr<TBehaviour>>> {
) -> Poll<SwarmEvent<TBehaviour::ToSwarm, THandlerErr<TBehaviour>>> {
// We use a `this` variable because the compiler can't mutably borrow multiple times
// across a `Deref`.
let this = &mut *self;
@ -1343,8 +1343,8 @@ fn notify_any<THandler, TBehaviour>(
where
TBehaviour: NetworkBehaviour,
THandler: ConnectionHandler<
InEvent = THandlerInEvent<TBehaviour>,
OutEvent = THandlerOutEvent<TBehaviour>,
FromBehaviour = THandlerInEvent<TBehaviour>,
ToBehaviour = THandlerOutEvent<TBehaviour>,
>,
{
let mut pending = SmallVec::new();
@ -1923,7 +1923,7 @@ mod tests {
) -> SwarmBuilder<CallTraceBehaviour<MockBehaviour<T, O>>>
where
T: ConnectionHandler + Clone,
T::OutEvent: Clone,
T::ToBehaviour: Clone,
O: Send + 'static,
{
let id_keys = identity::Keypair::generate_ed25519();

View File

@ -37,7 +37,7 @@ use std::task::{Context, Poll};
pub(crate) struct MockBehaviour<THandler, TOutEvent>
where
THandler: ConnectionHandler + Clone,
THandler::OutEvent: Clone,
THandler::ToBehaviour: Clone,
TOutEvent: Send + 'static,
{
/// The prototype protocols handler that is cloned for every
@ -48,13 +48,13 @@ where
/// The next action to return from `poll`.
///
/// An action is only returned once.
pub(crate) next_action: Option<ToSwarm<TOutEvent, THandler::InEvent>>,
pub(crate) next_action: Option<ToSwarm<TOutEvent, THandler::FromBehaviour>>,
}
impl<THandler, TOutEvent> MockBehaviour<THandler, TOutEvent>
where
THandler: ConnectionHandler + Clone,
THandler::OutEvent: Clone,
THandler::ToBehaviour: Clone,
TOutEvent: Send + 'static,
{
pub(crate) fn new(handler_proto: THandler) -> Self {
@ -69,11 +69,11 @@ where
impl<THandler, TOutEvent> NetworkBehaviour for MockBehaviour<THandler, TOutEvent>
where
THandler: ConnectionHandler + Clone,
THandler::OutEvent: Clone,
THandler::ToBehaviour: Clone,
TOutEvent: Send + 'static,
{
type ConnectionHandler = THandler;
type OutEvent = TOutEvent;
type ToSwarm = TOutEvent;
fn handle_established_inbound_connection(
&mut self,
@ -114,7 +114,7 @@ where
&mut self,
_: &mut Context,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
self.next_action.take().map_or(Poll::Pending, Poll::Ready)
}
@ -381,7 +381,7 @@ where
THandlerOutEvent<TInner>: Clone,
{
type ConnectionHandler = TInner::ConnectionHandler;
type OutEvent = TInner::OutEvent;
type ToSwarm = TInner::ToSwarm;
fn handle_pending_inbound_connection(
&mut self,
@ -558,7 +558,7 @@ where
&mut self,
cx: &mut Context,
args: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
self.poll += 1;
self.inner.poll(cx, args)
}