mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-23 14:51:34 +00:00
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:
@ -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.
|
||||
|
Reference in New Issue
Block a user