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

@ -249,7 +249,7 @@ impl Behaviour {
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Either<Handler, dummy::ConnectionHandler>;
type OutEvent = Event;
type ToSwarm = Event;
fn handle_established_inbound_connection(
&mut self,
@ -671,7 +671,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
_cx: &mut Context<'_>,
_: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses));
}

View File

@ -340,7 +340,7 @@ pub struct Handler {
ConnectionHandlerEvent<
<Self as ConnectionHandler>::OutboundProtocol,
<Self as ConnectionHandler>::OutboundOpenInfo,
<Self as ConnectionHandler>::OutEvent,
<Self as ConnectionHandler>::ToBehaviour,
<Self as ConnectionHandler>::Error,
>,
>,
@ -546,8 +546,8 @@ enum ReservationRequestFuture {
type Futures<T> = FuturesUnordered<BoxFuture<'static, T>>;
impl ConnectionHandler for Handler {
type InEvent = In;
type OutEvent = Event;
type FromBehaviour = In;
type ToBehaviour = Event;
type Error = StreamUpgradeError<
Either<inbound_hop::FatalUpgradeError, outbound_stop::FatalUpgradeError>,
>;
@ -567,7 +567,7 @@ impl ConnectionHandler for Handler {
)
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
match event {
In::AcceptReservationReq {
inbound_reservation_req,
@ -671,7 +671,7 @@ impl ConnectionHandler for Handler {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {

View File

@ -156,7 +156,7 @@ impl Behaviour {
impl NetworkBehaviour for Behaviour {
type ConnectionHandler = Either<Handler, dummy::ConnectionHandler>;
type OutEvent = Event;
type ToSwarm = Event;
fn handle_established_inbound_connection(
&mut self,
@ -293,7 +293,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
cx: &mut Context<'_>,
_poll_parameters: &mut impl PollParameters,
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(action) = self.queued_actions.pop_front() {
return Poll::Ready(action);
}

View File

@ -123,7 +123,7 @@ pub struct Handler {
ConnectionHandlerEvent<
<Self as ConnectionHandler>::OutboundProtocol,
<Self as ConnectionHandler>::OutboundOpenInfo,
<Self as ConnectionHandler>::OutEvent,
<Self as ConnectionHandler>::ToBehaviour,
<Self as ConnectionHandler>::Error,
>,
>,
@ -393,8 +393,8 @@ impl Handler {
}
impl ConnectionHandler for Handler {
type InEvent = In;
type OutEvent = Event;
type FromBehaviour = In;
type ToBehaviour = Event;
type Error = StreamUpgradeError<
Either<inbound_stop::FatalUpgradeError, outbound_hop::FatalUpgradeError>,
>;
@ -407,7 +407,7 @@ impl ConnectionHandler for Handler {
SubstreamProtocol::new(inbound_stop::Upgrade {}, ())
}
fn on_behaviour_event(&mut self, event: Self::InEvent) {
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
match event {
In::Reserve { to_listener } => {
self.queued_events
@ -444,7 +444,7 @@ impl ConnectionHandler for Handler {
ConnectionHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
Self::OutEvent,
Self::ToBehaviour,
Self::Error,
>,
> {