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

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