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

@ -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,
>,
> {