Skip to content

Commit 6e36e8a

Browse files
authored
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.
1 parent 5b32c8a commit 6e36e8a

File tree

65 files changed

+355
-236
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+355
-236
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/dcutr/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ fn main() -> Result<(), Box<dyn Error>> {
104104
.boxed();
105105

106106
#[derive(NetworkBehaviour)]
107-
#[behaviour(out_event = "Event", event_process = false)]
107+
#[behaviour(to_swarm = "Event", event_process = false)]
108108
struct Behaviour {
109109
relay_client: relay::client::Behaviour,
110110
ping: ping::Behaviour,

examples/distributed-key-value-store/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ async-trait = "0.1"
1111
env_logger = "0.10"
1212
futures = "0.3.28"
1313
libp2p = { path = "../../libp2p", features = ["async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "websocket", "yamux"] }
14-
multiaddr = { version = "0.17.1" }
14+
multiaddr = { version = "0.17.1" }

examples/distributed-key-value-store/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
7171

7272
// We create a custom network behaviour that combines Kademlia and mDNS.
7373
#[derive(NetworkBehaviour)]
74-
#[behaviour(out_event = "MyBehaviourEvent")]
74+
#[behaviour(to_swarm = "MyBehaviourEvent")]
7575
struct MyBehaviour {
7676
kademlia: Kademlia<MemoryStore>,
7777
mdns: mdns::async_io::Behaviour,

examples/file-sharing/src/network.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ impl EventLoop {
408408
}
409409

410410
#[derive(NetworkBehaviour)]
411-
#[behaviour(out_event = "ComposedEvent")]
411+
#[behaviour(to_swarm = "ComposedEvent")]
412412
struct ComposedBehaviour {
413413
request_response: request_response::Behaviour<FileExchangeCodec>,
414414
kademlia: Kademlia<MemoryStore>,

examples/ipfs-private/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
145145

146146
// We create a custom network behaviour that combines gossipsub, ping and identify.
147147
#[derive(NetworkBehaviour)]
148-
#[behaviour(out_event = "MyBehaviourEvent")]
148+
#[behaviour(to_swarm = "MyBehaviourEvent")]
149149
struct MyBehaviour {
150150
gossipsub: gossipsub::Behaviour,
151151
identify: identify::Behaviour,

libp2p/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
This newtype enforces additional variants like a leading forward-slash.
1010
We encourage users to use `StreamProtocol` when implementing `UpgradeInfo`.
1111
See [PR 3746].
12+
13+
- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848].
1214

1315
[PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746
1416
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
17+
[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848
1518

1619
## 0.51.3
1720

misc/allow-block-list/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ where
191191
S: Enforce,
192192
{
193193
type ConnectionHandler = dummy::ConnectionHandler;
194-
type OutEvent = Void;
194+
type ToSwarm = Void;
195195

196196
fn handle_established_inbound_connection(
197197
&mut self,
@@ -261,7 +261,7 @@ where
261261
&mut self,
262262
cx: &mut Context<'_>,
263263
_: &mut impl PollParameters,
264-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
264+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
265265
if let Some(peer) = self.close_connections.pop_front() {
266266
return Poll::Ready(ToSwarm::CloseConnection {
267267
peer_id: peer,

misc/connection-limits/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl ConnectionLimits {
201201

202202
impl NetworkBehaviour for Behaviour {
203203
type ConnectionHandler = dummy::ConnectionHandler;
204-
type OutEvent = Void;
204+
type ToSwarm = Void;
205205

206206
fn handle_pending_inbound_connection(
207207
&mut self,
@@ -355,7 +355,7 @@ impl NetworkBehaviour for Behaviour {
355355
&mut self,
356356
_: &mut Context<'_>,
357357
_: &mut impl PollParameters,
358-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
358+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
359359
Poll::Pending
360360
}
361361
}

protocols/autonat/src/behaviour.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub struct Behaviour {
209209

210210
last_probe: Option<Instant>,
211211

212-
pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>>,
212+
pending_actions: VecDeque<ToSwarm<<Self as NetworkBehaviour>::ToSwarm, THandlerInEvent<Self>>>,
213213

214214
probe_id: ProbeId,
215215

@@ -427,7 +427,7 @@ impl Behaviour {
427427
impl NetworkBehaviour for Behaviour {
428428
type ConnectionHandler =
429429
<request_response::Behaviour<AutoNatCodec> as NetworkBehaviour>::ConnectionHandler;
430-
type OutEvent = Event;
430+
type ToSwarm = Event;
431431

432432
fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters) -> Poll<Action> {
433433
loop {
@@ -594,7 +594,7 @@ impl NetworkBehaviour for Behaviour {
594594
}
595595
}
596596

597-
type Action = ToSwarm<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
597+
type Action = ToSwarm<<Behaviour as NetworkBehaviour>::ToSwarm, THandlerInEvent<Behaviour>>;
598598

599599
// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
600600
trait HandleInnerEvent {

protocols/dcutr/src/behaviour_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl NetworkBehaviour for Behaviour {
237237
handler::relayed::Handler,
238238
Either<handler::direct::Handler, dummy::ConnectionHandler>,
239239
>;
240-
type OutEvent = Event;
240+
type ToSwarm = Event;
241241

242242
fn handle_established_inbound_connection(
243243
&mut self,
@@ -415,7 +415,7 @@ impl NetworkBehaviour for Behaviour {
415415
&mut self,
416416
_cx: &mut Context<'_>,
417417
_: &mut impl PollParameters,
418-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
418+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
419419
if let Some(event) = self.queued_events.pop_front() {
420420
return Poll::Ready(event);
421421
}

protocols/dcutr/src/handler/direct.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ pub struct Handler {
3939
}
4040

4141
impl ConnectionHandler for Handler {
42-
type InEvent = void::Void;
43-
type OutEvent = Event;
42+
type FromBehaviour = void::Void;
43+
type ToBehaviour = Event;
4444
type Error = StreamUpgradeError<std::io::Error>;
4545
type InboundProtocol = DeniedUpgrade;
4646
type OutboundProtocol = DeniedUpgrade;
@@ -51,7 +51,7 @@ impl ConnectionHandler for Handler {
5151
SubstreamProtocol::new(DeniedUpgrade, ())
5252
}
5353

54-
fn on_behaviour_event(&mut self, _: Self::InEvent) {}
54+
fn on_behaviour_event(&mut self, _: Self::FromBehaviour) {}
5555

5656
fn connection_keep_alive(&self) -> KeepAlive {
5757
KeepAlive::No
@@ -64,7 +64,7 @@ impl ConnectionHandler for Handler {
6464
ConnectionHandlerEvent<
6565
Self::OutboundProtocol,
6666
Self::OutboundOpenInfo,
67-
Self::OutEvent,
67+
Self::ToBehaviour,
6868
Self::Error,
6969
>,
7070
> {

protocols/dcutr/src/handler/relayed.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub struct Handler {
135135
ConnectionHandlerEvent<
136136
<Self as ConnectionHandler>::OutboundProtocol,
137137
<Self as ConnectionHandler>::OutboundOpenInfo,
138-
<Self as ConnectionHandler>::OutEvent,
138+
<Self as ConnectionHandler>::ToBehaviour,
139139
<Self as ConnectionHandler>::Error,
140140
>,
141141
>,
@@ -254,8 +254,8 @@ impl Handler {
254254
}
255255

256256
impl ConnectionHandler for Handler {
257-
type InEvent = Command;
258-
type OutEvent = Event;
257+
type FromBehaviour = Command;
258+
type ToBehaviour = Event;
259259
type Error = StreamUpgradeError<
260260
Either<protocol::inbound::UpgradeError, protocol::outbound::UpgradeError>,
261261
>;
@@ -280,7 +280,7 @@ impl ConnectionHandler for Handler {
280280
}
281281
}
282282

283-
fn on_behaviour_event(&mut self, event: Self::InEvent) {
283+
fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
284284
match event {
285285
Command::Connect { obs_addrs } => {
286286
self.queued_events
@@ -323,7 +323,7 @@ impl ConnectionHandler for Handler {
323323
ConnectionHandlerEvent<
324324
Self::OutboundProtocol,
325325
Self::OutboundOpenInfo,
326-
Self::OutEvent,
326+
Self::ToBehaviour,
327327
Self::Error,
328328
>,
329329
> {

protocols/dcutr/tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn build_client() -> Swarm<Client> {
137137

138138
#[derive(NetworkBehaviour)]
139139
#[behaviour(
140-
out_event = "ClientEvent",
140+
to_swarm = "ClientEvent",
141141
event_process = false,
142142
prelude = "libp2p_swarm::derive_prelude"
143143
)]

protocols/floodsub/src/layer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl Floodsub {
329329

330330
impl NetworkBehaviour for Floodsub {
331331
type ConnectionHandler = OneShotHandler<FloodsubProtocol, FloodsubRpc, InnerMessage>;
332-
type OutEvent = FloodsubEvent;
332+
type ToSwarm = FloodsubEvent;
333333

334334
fn handle_established_inbound_connection(
335335
&mut self,
@@ -470,7 +470,7 @@ impl NetworkBehaviour for Floodsub {
470470
&mut self,
471471
_: &mut Context<'_>,
472472
_: &mut impl PollParameters,
473-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
473+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
474474
if let Some(event) = self.events.pop_front() {
475475
return Poll::Ready(event);
476476
}

protocols/gossipsub/src/behaviour.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,7 @@ where
33073307
F: Send + 'static + TopicSubscriptionFilter,
33083308
{
33093309
type ConnectionHandler = Handler;
3310-
type OutEvent = Event;
3310+
type ToSwarm = Event;
33113311

33123312
fn handle_established_inbound_connection(
33133313
&mut self,
@@ -3465,7 +3465,7 @@ where
34653465
&mut self,
34663466
cx: &mut Context<'_>,
34673467
_: &mut impl PollParameters,
3468-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
3468+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
34693469
if let Some(event) = self.events.pop_front() {
34703470
return Poll::Ready(event);
34713471
}

protocols/gossipsub/src/handler.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl EnabledHandler {
225225
ConnectionHandlerEvent<
226226
<Handler as ConnectionHandler>::OutboundProtocol,
227227
<Handler as ConnectionHandler>::OutboundOpenInfo,
228-
<Handler as ConnectionHandler>::OutEvent,
228+
<Handler as ConnectionHandler>::ToBehaviour,
229229
<Handler as ConnectionHandler>::Error,
230230
>,
231231
> {
@@ -393,8 +393,8 @@ impl EnabledHandler {
393393
}
394394

395395
impl ConnectionHandler for Handler {
396-
type InEvent = HandlerIn;
397-
type OutEvent = HandlerEvent;
396+
type FromBehaviour = HandlerIn;
397+
type ToBehaviour = HandlerEvent;
398398
type Error = Void;
399399
type InboundOpenInfo = ();
400400
type InboundProtocol = either::Either<ProtocolConfig, DeniedUpgrade>;
@@ -457,7 +457,7 @@ impl ConnectionHandler for Handler {
457457
ConnectionHandlerEvent<
458458
Self::OutboundProtocol,
459459
Self::OutboundOpenInfo,
460-
Self::OutEvent,
460+
Self::ToBehaviour,
461461
Self::Error,
462462
>,
463463
> {

protocols/identify/src/behaviour.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl Behaviour {
234234

235235
impl NetworkBehaviour for Behaviour {
236236
type ConnectionHandler = Handler;
237-
type OutEvent = Event;
237+
type ToSwarm = Event;
238238

239239
#[allow(deprecated)]
240240
fn handle_established_inbound_connection(
@@ -319,7 +319,7 @@ impl NetworkBehaviour for Behaviour {
319319
&mut self,
320320
_cx: &mut Context<'_>,
321321
_: &mut impl PollParameters,
322-
) -> Poll<ToSwarm<Self::OutEvent, THandlerInEvent<Self>>> {
322+
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
323323
if let Some(event) = self.events.pop_front() {
324324
return Poll::Ready(event);
325325
}

0 commit comments

Comments
 (0)