From 6e36e8aa35689cc0098ae55eee1423fb12ca07de Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Sun, 14 May 2023 12:58:08 +0200 Subject: [PATCH] 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. --- Cargo.lock | 44 +++++++++++++++++ examples/dcutr/src/main.rs | 2 +- .../distributed-key-value-store/Cargo.toml | 2 +- .../distributed-key-value-store/src/main.rs | 2 +- examples/file-sharing/src/network.rs | 2 +- examples/ipfs-private/src/main.rs | 2 +- libp2p/CHANGELOG.md | 3 ++ misc/allow-block-list/src/lib.rs | 4 +- misc/connection-limits/src/lib.rs | 4 +- protocols/autonat/src/behaviour.rs | 6 +-- protocols/dcutr/src/behaviour_impl.rs | 4 +- protocols/dcutr/src/handler/direct.rs | 8 +-- protocols/dcutr/src/handler/relayed.rs | 10 ++-- protocols/dcutr/tests/lib.rs | 2 +- protocols/floodsub/src/layer.rs | 4 +- protocols/gossipsub/src/behaviour.rs | 4 +- protocols/gossipsub/src/handler.rs | 8 +-- protocols/identify/src/behaviour.rs | 4 +- protocols/identify/src/handler.rs | 6 +-- protocols/kad/src/behaviour.rs | 4 +- protocols/kad/src/handler.rs | 6 +-- protocols/mdns/src/behaviour.rs | 4 +- protocols/perf/src/client/behaviour.rs | 4 +- protocols/perf/src/client/handler.rs | 10 ++-- protocols/perf/src/server/behaviour.rs | 4 +- protocols/perf/src/server/handler.rs | 8 +-- protocols/ping/src/handler.rs | 4 +- protocols/ping/src/lib.rs | 4 +- protocols/relay/src/behaviour.rs | 4 +- protocols/relay/src/behaviour/handler.rs | 10 ++-- protocols/relay/src/priv_client.rs | 4 +- protocols/relay/src/priv_client/handler.rs | 10 ++-- protocols/rendezvous/src/client.rs | 4 +- protocols/rendezvous/src/server.rs | 4 +- protocols/rendezvous/src/substream_handler.rs | 8 +-- protocols/request-response/src/handler.rs | 11 +++-- protocols/request-response/src/lib.rs | 4 +- swarm-derive/CHANGELOG.md | 3 ++ swarm-derive/Cargo.toml | 2 + swarm-derive/src/lib.rs | 49 +++++++++++++------ swarm-test/src/lib.rs | 28 +++++------ swarm/CHANGELOG.md | 5 +- swarm/Cargo.toml | 1 + swarm/src/behaviour.rs | 14 +++--- swarm/src/behaviour/either.rs | 4 +- swarm/src/behaviour/toggle.rs | 12 ++--- swarm/src/connection.rs | 22 ++++----- swarm/src/connection/pool.rs | 10 ++-- swarm/src/connection/pool/task.rs | 4 +- swarm/src/dummy.rs | 12 ++--- swarm/src/handler.rs | 16 +++--- swarm/src/handler/either.rs | 8 +-- swarm/src/handler/map_in.rs | 8 +-- swarm/src/handler/map_out.rs | 10 ++-- swarm/src/handler/multi.rs | 8 +-- swarm/src/handler/one_shot.rs | 8 +-- swarm/src/handler/pending.rs | 8 +-- swarm/src/handler/select.rs | 8 +-- swarm/src/keep_alive.rs | 12 ++--- swarm/src/lib.rs | 22 ++++----- swarm/src/test.rs | 16 +++--- swarm/tests/swarm_derive.rs | 36 ++++++++------ swarm/tests/ui/fail/out_event_deprecation.rs | 21 ++++++++ .../ui/fail/out_event_deprecation.stderr | 14 ++++++ transports/webrtc/examples/listen_ping.rs | 2 +- 65 files changed, 355 insertions(+), 236 deletions(-) create mode 100644 swarm/tests/ui/fail/out_event_deprecation.rs create mode 100644 swarm/tests/ui/fail/out_event_deprecation.stderr diff --git a/Cargo.lock b/Cargo.lock index 3bb82a6a6aa..9422ccd9af4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -541,6 +541,15 @@ version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b645a089122eccb6111b4f81cbc1a49f5900ac4666bb93ac027feaecf15607bf" +[[package]] +name = "basic-toml" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0de75129aa8d0cceaf750b89013f0e08804d6ec61416da787b35ad0d7cddf1" +dependencies = [ + "serde", +] + [[package]] name = "bimap" version = "0.6.3" @@ -1816,6 +1825,12 @@ dependencies = [ "polyval 0.5.3", ] +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "gloo-timers" version = "0.2.6" @@ -2995,6 +3010,7 @@ dependencies = [ "rand 0.8.5", "smallvec", "tokio", + "trybuild", "void", "wasm-bindgen-futures", ] @@ -3004,6 +3020,8 @@ name = "libp2p-swarm-derive" version = "0.33.0" dependencies = [ "heck", + "proc-macro-warning", + "proc-macro2", "quote", "syn 2.0.15", ] @@ -3976,6 +3994,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "proc-macro-warning" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e99670bafb56b9a106419397343bdbc8b8742c3cc449fec6345f86173f47cd4" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.15", +] + [[package]] name = "proc-macro2" version = "1.0.56" @@ -5210,6 +5239,21 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" +[[package]] +name = "trybuild" +version = "1.0.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501dbdbb99861e4ab6b60eb6a7493956a9defb644fd034bc4a5ef27c693c8a3a" +dependencies = [ + "basic-toml", + "glob", + "once_cell", + "serde", + "serde_derive", + "serde_json", + "termcolor", +] + [[package]] name = "turn" version = "0.6.1" diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 4dbee86fac3..e8f5673e7c4 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -104,7 +104,7 @@ fn main() -> Result<(), Box> { .boxed(); #[derive(NetworkBehaviour)] - #[behaviour(out_event = "Event", event_process = false)] + #[behaviour(to_swarm = "Event", event_process = false)] struct Behaviour { relay_client: relay::client::Behaviour, ping: ping::Behaviour, diff --git a/examples/distributed-key-value-store/Cargo.toml b/examples/distributed-key-value-store/Cargo.toml index b690baac2e8..8d9681e52d2 100644 --- a/examples/distributed-key-value-store/Cargo.toml +++ b/examples/distributed-key-value-store/Cargo.toml @@ -11,4 +11,4 @@ async-trait = "0.1" env_logger = "0.10" futures = "0.3.28" libp2p = { path = "../../libp2p", features = ["async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "websocket", "yamux"] } -multiaddr = { version = "0.17.1" } \ No newline at end of file +multiaddr = { version = "0.17.1" } diff --git a/examples/distributed-key-value-store/src/main.rs b/examples/distributed-key-value-store/src/main.rs index 952e55ba6e7..ce617aca5e4 100644 --- a/examples/distributed-key-value-store/src/main.rs +++ b/examples/distributed-key-value-store/src/main.rs @@ -71,7 +71,7 @@ async fn main() -> Result<(), Box> { // We create a custom network behaviour that combines Kademlia and mDNS. #[derive(NetworkBehaviour)] - #[behaviour(out_event = "MyBehaviourEvent")] + #[behaviour(to_swarm = "MyBehaviourEvent")] struct MyBehaviour { kademlia: Kademlia, mdns: mdns::async_io::Behaviour, diff --git a/examples/file-sharing/src/network.rs b/examples/file-sharing/src/network.rs index 404aef01c4d..c88db696dff 100644 --- a/examples/file-sharing/src/network.rs +++ b/examples/file-sharing/src/network.rs @@ -408,7 +408,7 @@ impl EventLoop { } #[derive(NetworkBehaviour)] -#[behaviour(out_event = "ComposedEvent")] +#[behaviour(to_swarm = "ComposedEvent")] struct ComposedBehaviour { request_response: request_response::Behaviour, kademlia: Kademlia, diff --git a/examples/ipfs-private/src/main.rs b/examples/ipfs-private/src/main.rs index fd18581407b..364cccd02a1 100644 --- a/examples/ipfs-private/src/main.rs +++ b/examples/ipfs-private/src/main.rs @@ -145,7 +145,7 @@ async fn main() -> Result<(), Box> { // We create a custom network behaviour that combines gossipsub, ping and identify. #[derive(NetworkBehaviour)] - #[behaviour(out_event = "MyBehaviourEvent")] + #[behaviour(to_swarm = "MyBehaviourEvent")] struct MyBehaviour { gossipsub: gossipsub::Behaviour, identify: identify::Behaviour, diff --git a/libp2p/CHANGELOG.md b/libp2p/CHANGELOG.md index bc47bcb0827..18206718799 100644 --- a/libp2p/CHANGELOG.md +++ b/libp2p/CHANGELOG.md @@ -9,9 +9,12 @@ This newtype enforces additional variants like a leading forward-slash. We encourage users to use `StreamProtocol` when implementing `UpgradeInfo`. See [PR 3746]. + +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. [PR 3746]: https://github.com/libp2p/rust-libp2p/pull/3746 [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.51.3 diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index d501ab73324..521aa0026cc 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -191,7 +191,7 @@ where S: Enforce, { type ConnectionHandler = dummy::ConnectionHandler; - type OutEvent = Void; + type ToSwarm = Void; fn handle_established_inbound_connection( &mut self, @@ -261,7 +261,7 @@ where &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(peer) = self.close_connections.pop_front() { return Poll::Ready(ToSwarm::CloseConnection { peer_id: peer, diff --git a/misc/connection-limits/src/lib.rs b/misc/connection-limits/src/lib.rs index b781e83d92d..1a568cb7ab9 100644 --- a/misc/connection-limits/src/lib.rs +++ b/misc/connection-limits/src/lib.rs @@ -201,7 +201,7 @@ impl ConnectionLimits { impl NetworkBehaviour for Behaviour { type ConnectionHandler = dummy::ConnectionHandler; - type OutEvent = Void; + type ToSwarm = Void; fn handle_pending_inbound_connection( &mut self, @@ -355,7 +355,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { Poll::Pending } } diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index c46d8044989..17681341489 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -209,7 +209,7 @@ pub struct Behaviour { last_probe: Option, - pending_actions: VecDeque::OutEvent, THandlerInEvent>>, + pending_actions: VecDeque::ToSwarm, THandlerInEvent>>, probe_id: ProbeId, @@ -427,7 +427,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = as NetworkBehaviour>::ConnectionHandler; - type OutEvent = Event; + type ToSwarm = Event; fn poll(&mut self, cx: &mut Context<'_>, params: &mut impl PollParameters) -> Poll { loop { @@ -594,7 +594,7 @@ impl NetworkBehaviour for Behaviour { } } -type Action = ToSwarm<::OutEvent, THandlerInEvent>; +type Action = ToSwarm<::ToSwarm, THandlerInEvent>; // Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol. trait HandleInnerEvent { diff --git a/protocols/dcutr/src/behaviour_impl.rs b/protocols/dcutr/src/behaviour_impl.rs index 2c1698f2258..5b89f2ef2c2 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -237,7 +237,7 @@ impl NetworkBehaviour for Behaviour { handler::relayed::Handler, Either, >; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -415,7 +415,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index e336f750915..a06c9272baa 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -39,8 +39,8 @@ pub struct Handler { } impl ConnectionHandler for Handler { - type InEvent = void::Void; - type OutEvent = Event; + type FromBehaviour = void::Void; + type ToBehaviour = Event; type Error = StreamUpgradeError; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -51,7 +51,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, _: Self::InEvent) {} + fn on_behaviour_event(&mut self, _: Self::FromBehaviour) {} fn connection_keep_alive(&self) -> KeepAlive { KeepAlive::No @@ -64,7 +64,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 8b59b80457b..eb1ad83cecb 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -135,7 +135,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -254,8 +254,8 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = Command; - type OutEvent = Event; + type FromBehaviour = Command; + type ToBehaviour = Event; type Error = StreamUpgradeError< Either, >; @@ -280,7 +280,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 { Command::Connect { obs_addrs } => { self.queued_events @@ -323,7 +323,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/dcutr/tests/lib.rs b/protocols/dcutr/tests/lib.rs index ba190782b12..5c5fdbb8384 100644 --- a/protocols/dcutr/tests/lib.rs +++ b/protocols/dcutr/tests/lib.rs @@ -137,7 +137,7 @@ fn build_client() -> Swarm { #[derive(NetworkBehaviour)] #[behaviour( - out_event = "ClientEvent", + to_swarm = "ClientEvent", event_process = false, prelude = "libp2p_swarm::derive_prelude" )] diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index a3673a13ed1..f8a498912d6 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -329,7 +329,7 @@ impl Floodsub { impl NetworkBehaviour for Floodsub { type ConnectionHandler = OneShotHandler; - type OutEvent = FloodsubEvent; + type ToSwarm = FloodsubEvent; fn handle_established_inbound_connection( &mut self, @@ -470,7 +470,7 @@ impl NetworkBehaviour for Floodsub { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index c2d6b8da3fe..ba0594a9801 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3307,7 +3307,7 @@ where F: Send + 'static + TopicSubscriptionFilter, { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -3465,7 +3465,7 @@ where &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 5a5b91d00d1..615d4ae132b 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -225,7 +225,7 @@ impl EnabledHandler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, > { @@ -393,8 +393,8 @@ impl EnabledHandler { } impl ConnectionHandler for Handler { - type InEvent = HandlerIn; - type OutEvent = HandlerEvent; + type FromBehaviour = HandlerIn; + type ToBehaviour = HandlerEvent; type Error = Void; type InboundOpenInfo = (); type InboundProtocol = either::Either; @@ -457,7 +457,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 6ed242dd8d1..532b86dafa8 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -234,7 +234,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; #[allow(deprecated)] fn handle_established_inbound_connection( @@ -319,7 +319,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index dfcf2279497..d041fca731a 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -242,8 +242,8 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = InEvent; - type OutEvent = Event; + type FromBehaviour = InEvent; + type ToBehaviour = Event; type Error = io::Error; type InboundProtocol = SelectUpgrade>; type OutboundProtocol = Either>; @@ -254,7 +254,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(SelectUpgrade::new(Identify, Push::inbound()), ()) } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { match event { InEvent::AddressesChanged(addresses) => { self.external_addresses = addresses; diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 9193770e09a..856a994056a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1960,7 +1960,7 @@ where TStore: RecordStore + Send + 'static, { type ConnectionHandler = KademliaHandler; - type OutEvent = KademliaEvent; + type ToSwarm = KademliaEvent; fn handle_established_inbound_connection( &mut self, @@ -2301,7 +2301,7 @@ where &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { let now = Instant::now(); // Calculate the available capacity for queries triggered by background jobs. diff --git a/protocols/kad/src/handler.rs b/protocols/kad/src/handler.rs index 25daa8dcd15..75721c0dc31 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -599,8 +599,8 @@ impl ConnectionHandler for KademliaHandler where TUserData: Clone + fmt::Debug + Send + 'static + Unpin, { - type InEvent = KademliaHandlerIn; - type OutEvent = KademliaHandlerEvent; + type FromBehaviour = KademliaHandlerIn; + type ToBehaviour = KademliaHandlerEvent; type Error = io::Error; // TODO: better error type? type InboundProtocol = Either; type OutboundProtocol = KademliaProtocolConfig; @@ -701,7 +701,7 @@ where ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 5186ce91cb7..8f01439403f 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -173,7 +173,7 @@ where P: Provider, { type ConnectionHandler = dummy::ConnectionHandler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -252,7 +252,7 @@ where &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { // Poll ifwatch. while let Poll::Ready(Some(event)) = Pin::new(&mut self.if_watch).poll_next(cx) { match event { diff --git a/protocols/perf/src/client/behaviour.rs b/protocols/perf/src/client/behaviour.rs index 920baed1131..3dd932b0c77 100644 --- a/protocols/perf/src/client/behaviour.rs +++ b/protocols/perf/src/client/behaviour.rs @@ -82,7 +82,7 @@ pub enum PerfError { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_outbound_connection( &mut self, @@ -147,7 +147,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index a87e82cc384..f8a1eff5c10 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -56,7 +56,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -86,8 +86,8 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type InEvent = Command; - type OutEvent = Event; + type FromBehaviour = Command; + type ToBehaviour = Event; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = ReadyUpgrade; @@ -98,7 +98,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, command: Self::InEvent) { + fn on_behaviour_event(&mut self, command: Self::FromBehaviour) { self.requested_streams.push_back(command); self.queued_events .push_back(ConnectionHandlerEvent::OutboundSubstreamRequest { @@ -168,7 +168,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/perf/src/server/behaviour.rs b/protocols/perf/src/server/behaviour.rs index 5d63475c999..6f0047913b6 100644 --- a/protocols/perf/src/server/behaviour.rs +++ b/protocols/perf/src/server/behaviour.rs @@ -55,7 +55,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -110,7 +110,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.queued_events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index ab70ba9aa6e..6f5043b66a3 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -63,8 +63,8 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type InEvent = Void; - type OutEvent = Event; + type FromBehaviour = Void; + type ToBehaviour = Event; type Error = Void; type InboundProtocol = ReadyUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -75,7 +75,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(ReadyUpgrade::new(crate::PROTOCOL_NAME), ()) } - fn on_behaviour_event(&mut self, v: Self::InEvent) { + fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { void::unreachable(v) } @@ -123,7 +123,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 94b484927b8..c21b234b3e3 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -229,8 +229,8 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = Void; - type OutEvent = crate::Result; + type FromBehaviour = Void; + type ToBehaviour = crate::Result; type Error = Failure; type InboundProtocol = ReadyUpgrade; type OutboundProtocol = ReadyUpgrade; diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index aa33c76984f..85be866dbf5 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -98,7 +98,7 @@ impl Default for Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -133,7 +133,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(e) = self.events.pop_back() { let Event { result, peer } = &e; diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index b2d6538fc04..133a5fa7d50 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -249,7 +249,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Either; - 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>> { + ) -> Poll>> { if let Some(action) = self.queued_actions.pop_front() { return Poll::Ready(action.build(self.local_peer_id, &self.external_addresses)); } diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 7494d055f36..752e5aeac89 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -340,7 +340,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -546,8 +546,8 @@ enum ReservationRequestFuture { type Futures = FuturesUnordered>; impl ConnectionHandler for Handler { - type InEvent = In; - type OutEvent = Event; + type FromBehaviour = In; + type ToBehaviour = Event; type Error = StreamUpgradeError< Either, >; @@ -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, >, > { diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index 8592c57d3b6..1d4237e83d8 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -156,7 +156,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Either; - 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>> { + ) -> Poll>> { if let Some(action) = self.queued_actions.pop_front() { return Poll::Ready(action); } diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index c134031ad7c..dee51cde664 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -123,7 +123,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::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, >; @@ -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, >, > { diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index e6f842ae942..a8527ba5dfe 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -163,7 +163,7 @@ pub enum Event { impl NetworkBehaviour for Behaviour { type ConnectionHandler = SubstreamConnectionHandler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -244,7 +244,7 @@ impl NetworkBehaviour for Behaviour { &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 1311d4f903f..8568076202a 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -110,7 +110,7 @@ pub enum Event { impl NetworkBehaviour for Behaviour { type ConnectionHandler = SubstreamConnectionHandler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -165,7 +165,7 @@ impl NetworkBehaviour for Behaviour { &mut self, cx: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Poll::Ready(ExpiredRegistration(registration)) = self.registrations.poll(cx) { return Poll::Ready(ToSwarm::GenerateEvent(Event::RegistrationExpired( registration, diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index 289c6a36d7e..6fc5cb561b2 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -354,8 +354,8 @@ where TInboundSubstreamHandler: Send + 'static, TOutboundSubstreamHandler: Send + 'static, { - type InEvent = InEvent; - type OutEvent = OutEvent; + type FromBehaviour = InEvent; + type ToBehaviour = OutEvent; type Error = Void; type InboundProtocol = PassthroughProtocol; type OutboundProtocol = PassthroughProtocol; @@ -402,7 +402,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { match event { InEvent::NewSubstream { open_info } => self.new_substreams.push_back(open_info), InEvent::NotifyInboundSubstream { id, message } => { @@ -456,7 +456,7 @@ where ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 3a323d75edc..02e9c8710dd 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -239,8 +239,8 @@ impl ConnectionHandler for Handler where TCodec: Codec + Send + Clone + 'static, { - type InEvent = RequestProtocol; - type OutEvent = Event; + type FromBehaviour = RequestProtocol; + type ToBehaviour = Event; type Error = StreamUpgradeError; type InboundProtocol = ResponseProtocol; type OutboundProtocol = RequestProtocol; @@ -281,7 +281,7 @@ where SubstreamProtocol::new(proto, request_id).with_timeout(self.substream_timeout) } - fn on_behaviour_event(&mut self, request: Self::InEvent) { + fn on_behaviour_event(&mut self, request: Self::FromBehaviour) { self.keep_alive = KeepAlive::Yes; self.outbound.push_back(request); } @@ -293,8 +293,9 @@ where fn poll( &mut self, cx: &mut Context<'_>, - ) -> Poll, RequestId, Self::OutEvent, Self::Error>> - { + ) -> Poll< + ConnectionHandlerEvent, RequestId, Self::ToBehaviour, Self::Error>, + > { // Check for a pending (fatal) error. if let Some(err) = self.pending_error.take() { // The handler will not be polled again by the `Swarm`. diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 4267f83da8c..10c78703e45 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -683,7 +683,7 @@ where TCodec: Codec + Send + Clone + 'static, { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -878,7 +878,7 @@ where &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(ev) = self.pending_events.pop_front() { return Poll::Ready(ev); } else if self.pending_events.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD { diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 9628e7ea777..900e0d56d3d 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -3,7 +3,10 @@ - Raise MSRV to 1.65. See [PR 3715]. +- Rename `out_event` to `to_swarm` and deprecate `out_event`. See [PR 3848]. + [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.32.0 diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 8d31a389ae5..76b56cdbd45 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -17,6 +17,8 @@ proc-macro = true heck = "0.4" quote = "1.0" syn = { version = "2.0.15", default-features = false, features = ["clone-impls", "derive", "parsing", "printing", "proc-macro"] } +proc-macro-warning = "0.3.1" +proc-macro2 = "1.0" # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 04a32cf91a7..d08c42f4f02 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -54,6 +54,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { let BehaviourAttributes { prelude_path, user_specified_out_event, + deprecation_tokenstream, } = match parse_attributes(ast) { Ok(attrs) => attrs, Err(e) => return e, @@ -96,11 +97,11 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { }; let (out_event_name, out_event_definition, out_event_from_clauses) = { - // If we find a `#[behaviour(out_event = "Foo")]` attribute on the - // struct, we set `Foo` as the out event. If not, the `OutEvent` is + // If we find a `#[behaviour(to_swarm = "Foo")]` attribute on the + // struct, we set `Foo` as the out event. If not, the `ToSwarm` is // generated. match user_specified_out_event { - // User provided `OutEvent`. + // User provided `ToSwarm`. Some(name) => { let definition = None; let from_clauses = data_struct @@ -108,12 +109,12 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { .iter() .map(|field| { let ty = &field.ty; - quote! {#name: From< <#ty as #trait_to_impl>::OutEvent >} + quote! {#name: From< <#ty as #trait_to_impl>::ToSwarm >} }) .collect::>(); (name, definition, from_clauses) } - // User did not provide `OutEvent`. Generate it. + // User did not provide `ToSwarm`. Generate it. None => { let enum_name_str = ast.ident.to_string() + "Event"; let enum_name: syn::Type = @@ -135,7 +136,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { let enum_variants = fields .clone() - .map(|(variant, ty)| quote! {#variant(<#ty as #trait_to_impl>::OutEvent)}); + .map(|(variant, ty)| quote! {#variant(<#ty as #trait_to_impl>::ToSwarm)}); let visibility = &ast.vis; @@ -146,7 +147,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { let additional_debug = fields .clone() - .map(|(_variant, ty)| quote! { <#ty as #trait_to_impl>::OutEvent : ::core::fmt::Debug }) + .map(|(_variant, ty)| quote! { <#ty as #trait_to_impl>::ToSwarm : ::core::fmt::Debug }) .collect::>(); let where_clause = { @@ -168,7 +169,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { .map(|where_clause| quote! {#where_clause, #(#additional_debug),*}); let match_variants = fields.map(|(variant, _ty)| variant); - let msg = format!("`NetworkBehaviour::OutEvent` produced by {name}."); + let msg = format!("`NetworkBehaviour::ToSwarm` produced by {name}."); Some(quote! { #[doc = #msg] @@ -677,9 +678,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { } let generate_event_match_arm = { - // If the `NetworkBehaviour`'s `OutEvent` is generated by the derive macro, wrap the sub - // `NetworkBehaviour` `OutEvent` in the variant of the generated `OutEvent`. If the - // `NetworkBehaviour`'s `OutEvent` is provided by the user, use the corresponding `From` + // If the `NetworkBehaviour`'s `ToSwarm` is generated by the derive macro, wrap the sub + // `NetworkBehaviour` `ToSwarm` in the variant of the generated `ToSwarm`. If the + // `NetworkBehaviour`'s `ToSwarm` is provided by the user, use the corresponding `From` // implementation. let into_out_event = if out_event_definition.is_some() { let event_variant: syn::Variant = syn::parse_str( @@ -731,13 +732,15 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { // Now the magic happens. let final_quote = quote! { + #deprecation_tokenstream + #out_event_definition impl #impl_generics #trait_to_impl for #name #ty_generics #where_clause { type ConnectionHandler = #connection_handler_ty; - type OutEvent = #out_event_reference; + type ToSwarm = #out_event_reference; #[allow(clippy::needless_question_mark)] fn handle_pending_inbound_connection( @@ -795,7 +798,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { } } - fn poll(&mut self, cx: &mut std::task::Context, poll_params: &mut impl #poll_parameters) -> std::task::Poll<#network_behaviour_action>> { + fn poll(&mut self, cx: &mut std::task::Context, poll_params: &mut impl #poll_parameters) -> std::task::Poll<#network_behaviour_action>> { use #prelude_path::futures::*; #(#poll_stmts)* std::task::Poll::Pending @@ -851,6 +854,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { struct BehaviourAttributes { prelude_path: syn::Path, user_specified_out_event: Option, + deprecation_tokenstream: proc_macro2::TokenStream, } /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. @@ -858,7 +862,9 @@ fn parse_attributes(ast: &DeriveInput) -> Result Result unimplemented!(), Meta::List(_) => unimplemented!(), @@ -914,7 +923,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result { return Err(syn::Error::new_spanned( name_value.value, - "`out_event` value must be a quoted type", + "`to_swarm` value must be a quoted type", ) .to_compile_error() .into()); @@ -926,5 +935,15 @@ fn parse_attributes(ast: &DeriveInput) -> Result(&mut self, other: &mut Swarm) where T: NetworkBehaviour + Send, - ::OutEvent: Debug; + ::ToSwarm: Debug; /// Dial the provided address and wait until a connection has been established. /// @@ -68,7 +68,7 @@ pub trait SwarmExt { async fn wait(&mut self, predicate: P) -> E where P: Fn( - SwarmEvent<::OutEvent, THandlerErr>, + SwarmEvent<::ToSwarm, THandlerErr>, ) -> Option, P: Send; @@ -82,12 +82,12 @@ pub trait SwarmExt { /// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`. async fn next_swarm_event( &mut self, - ) -> SwarmEvent<::OutEvent, THandlerErr>; + ) -> SwarmEvent<::ToSwarm, THandlerErr>; /// Returns the next behaviour event or times out after 10 seconds. /// /// If the 10s timeout does not fit your usecase, please fall back to `StreamExt::next`. - async fn next_behaviour_event(&mut self) -> ::OutEvent; + async fn next_behaviour_event(&mut self) -> ::ToSwarm; async fn loop_on_next(self); } @@ -108,7 +108,7 @@ pub trait SwarmExt { /// This function utilizes the [`TryIntoOutput`] trait. /// Similar as to the number of expected events, the type of event is inferred based on your usage. /// If you match against a [`SwarmEvent`], the first [`SwarmEvent`] will be returned. -/// If you match against your [`NetworkBehaviour::OutEvent`] type, [`SwarmEvent`]s which are not [`SwarmEvent::Behaviour`] will be skipped until the [`Swarm`] returns a behaviour event. +/// If you match against your [`NetworkBehaviour::ToSwarm`] type, [`SwarmEvent`]s which are not [`SwarmEvent::Behaviour`] will be skipped until the [`Swarm`] returns a behaviour event. /// /// You can implement the [`TryIntoOutput`] for any other type to further customize this behaviour. /// @@ -136,11 +136,11 @@ pub async fn drive< ) -> ([Out1; NUM_EVENTS_SWARM_1], [Out2; NUM_EVENTS_SWARM_2]) where TBehaviour2: NetworkBehaviour + Send, - TBehaviour2::OutEvent: Debug, + TBehaviour2::ToSwarm: Debug, TBehaviour1: NetworkBehaviour + Send, - TBehaviour1::OutEvent: Debug, - SwarmEvent>: TryIntoOutput, - SwarmEvent>: TryIntoOutput, + TBehaviour1::ToSwarm: Debug, + SwarmEvent>: TryIntoOutput, + SwarmEvent>: TryIntoOutput, Out1: Debug, Out2: Debug, { @@ -199,7 +199,7 @@ impl TryIntoOutput SwarmExt for Swarm where B: NetworkBehaviour + Send, - ::OutEvent: Debug, + ::ToSwarm: Debug, { type NB = B; @@ -226,7 +226,7 @@ where async fn connect(&mut self, other: &mut Swarm) where T: NetworkBehaviour + Send, - ::OutEvent: Debug, + ::ToSwarm: Debug, { let external_addresses = other .external_addresses() @@ -283,7 +283,7 @@ where async fn wait(&mut self, predicate: P) -> E where - P: Fn(SwarmEvent<::OutEvent, THandlerErr>) -> Option, + P: Fn(SwarmEvent<::ToSwarm, THandlerErr>) -> Option, P: Send, { loop { @@ -345,7 +345,7 @@ where async fn next_swarm_event( &mut self, - ) -> SwarmEvent<::OutEvent, THandlerErr> { + ) -> SwarmEvent<::ToSwarm, THandlerErr> { match futures::future::select( futures_timer::Delay::new(Duration::from_secs(10)), self.select_next_some(), @@ -361,7 +361,7 @@ where } } - async fn next_behaviour_event(&mut self) -> ::OutEvent { + async fn next_behaviour_event(&mut self) -> ::ToSwarm { loop { if let Ok(event) = self.next_swarm_event().await.try_into_behaviour_event() { return event; diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 86023f5541f..6c4bb389482 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -39,12 +39,14 @@ - `ConnectionHandlerEvent::ReportRemoteProtocols` - `ConnectionEvent::LocalProtocolsChange` - `ConnectionEvent::RemoteProtocolsChange` - + See [PR 3651]. - Deprecate the `NegotiatedSubstream` type and replace it with `Stream`. See [PR 3912]. +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. + [PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605 [PR 3651]: https://github.com/libp2p/rust-libp2p/pull/3651 [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 @@ -55,6 +57,7 @@ [PR 3885]: https://github.com/libp2p/rust-libp2p/pull/3885 [PR 3886]: https://github.com/libp2p/rust-libp2p/pull/3886 [PR 3912]: https://github.com/libp2p/rust-libp2p/pull/3912 +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.42.2 diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 483c65f8dfd..51214454973 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -54,6 +54,7 @@ libp2p-yamux = { workspace = true } quickcheck = { workspace = true } void = "1" once_cell = "1.17.1" +trybuild = "1.0.80" [[test]] name = "swarm_derive" diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 9953b08793b..e35f412f0ce 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -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 `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>>; + ) -> Poll>>; } /// Parameters passed to `poll()`, that the `NetworkBehaviour` has access to. diff --git a/swarm/src/behaviour/either.rs b/swarm/src/behaviour/either.rs index bf59949ccfe..c6e0870d11c 100644 --- a/swarm/src/behaviour/either.rs +++ b/swarm/src/behaviour/either.rs @@ -33,7 +33,7 @@ where R: NetworkBehaviour, { type ConnectionHandler = Either, THandler>; - type OutEvent = Either; + type ToSwarm = Either; fn handle_pending_inbound_connection( &mut self, @@ -156,7 +156,7 @@ where &mut self, cx: &mut Context<'_>, params: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { let event = match self { Either::Left(behaviour) => futures::ready!(behaviour.poll(cx, params)) .map_out(Either::Left) diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 0e687fe9355..92bd8963502 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -71,7 +71,7 @@ where TBehaviour: NetworkBehaviour, { type ConnectionHandler = ToggleConnectionHandler>; - type OutEvent = TBehaviour::OutEvent; + type ToSwarm = TBehaviour::ToSwarm; fn handle_pending_inbound_connection( &mut self, @@ -182,7 +182,7 @@ where &mut self, cx: &mut Context<'_>, params: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(inner) = self.inner.as_mut() { inner.poll(cx, params) } else { @@ -267,8 +267,8 @@ impl ConnectionHandler for ToggleConnectionHandler where TInner: ConnectionHandler, { - type InEvent = TInner::InEvent; - type OutEvent = TInner::OutEvent; + type FromBehaviour = TInner::FromBehaviour; + type ToBehaviour = TInner::ToBehaviour; type Error = TInner::Error; type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = TInner::OutboundProtocol; @@ -286,7 +286,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { self.inner .as_mut() .expect("Can't receive events if disabled; QED") @@ -307,7 +307,7 @@ where ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 30f95a29317..43be1c98e3a 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -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, ConnectionError>> { + ) -> Poll, ConnectionError>> { let Self { requested_substreams, muxing, @@ -439,7 +439,7 @@ where #[cfg(test)] fn poll_noop_waker( &mut self, - ) -> Poll, ConnectionError>> { + ) -> Poll, ConnectionError>> { 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, >, > { diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 32a3924232c..36fa0adf3e9 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -90,8 +90,10 @@ where counters: ConnectionCounters, /// The managed connections of each peer that are currently considered established. - established: - FnvHashMap>>, + established: FnvHashMap< + PeerId, + FnvHashMap>, + >, /// The pending connections that are currently being negotiated. pending: HashMap, @@ -285,7 +287,7 @@ pub(crate) enum PoolEvent { id: ConnectionId, peer_id: PeerId, /// The produced event. - event: THandler::OutEvent, + event: THandler::ToBehaviour, }, /// The connection to a node has changed its address. @@ -338,7 +340,7 @@ where pub(crate) fn get_established( &mut self, id: ConnectionId, - ) -> Option<&mut EstablishedConnection> { + ) -> Option<&mut EstablishedConnection> { self.established .values_mut() .find_map(|connections| connections.get_mut(&id)) diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index dd318f77d30..175da668bda 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -77,7 +77,7 @@ pub(crate) enum EstablishedConnectionEvent { Notify { id: ConnectionId, peer_id: PeerId, - event: THandler::OutEvent, + event: THandler::ToBehaviour, }, /// A connection closed, possibly due to an error. /// @@ -171,7 +171,7 @@ pub(crate) async fn new_for_established_connection( connection_id: ConnectionId, peer_id: PeerId, mut connection: crate::connection::Connection, - mut command_receiver: mpsc::Receiver>, + mut command_receiver: mpsc::Receiver>, mut events: mpsc::Sender>, ) where THandler: ConnectionHandler, diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 83d03a7aadb..563d86a667e 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -19,7 +19,7 @@ pub struct Behaviour; impl NetworkBehaviour for Behaviour { type ConnectionHandler = ConnectionHandler; - type OutEvent = Void; + type ToSwarm = Void; fn handle_established_inbound_connection( &mut self, @@ -54,7 +54,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { Poll::Pending } @@ -81,8 +81,8 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type InEvent = Void; - type OutEvent = Void; + type FromBehaviour = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -93,7 +93,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { void::unreachable(event) } @@ -108,7 +108,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 2ce58638bd1..edefb7a1cb9 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -99,10 +99,10 @@ use std::{cmp::Ordering, error, fmt, io, task::Context, task::Poll, time::Durati /// When a connection is closed gracefully, the substreams used by the handler may still /// continue reading data until the remote closes its side of the connection. pub trait ConnectionHandler: Send + 'static { - /// Custom event that can be received from the outside. - type InEvent: fmt::Debug + Send + 'static; - /// Custom event that can be produced by the handler and that will be returned to the outside. - type OutEvent: fmt::Debug + Send + 'static; + /// A type representing the message(s) a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`](crate::behaviour::ToSwarm::NotifyHandler) + type FromBehaviour: fmt::Debug + Send + 'static; + /// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) via [`ConnectionHandlerEvent::Custom`]. + type ToBehaviour: fmt::Debug + Send + 'static; /// The type of errors returned by [`ConnectionHandler::poll`]. type Error: error::Error + fmt::Debug + Send + 'static; /// The inbound upgrade for the protocol(s) used by the handler. @@ -153,7 +153,7 @@ pub trait ConnectionHandler: Send + 'static { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, >; @@ -162,7 +162,7 @@ pub trait ConnectionHandler: Send + 'static { fn map_in_event(self, map: TMap) -> MapInEvent where Self: Sized, - TMap: Fn(&TNewIn) -> Option<&Self::InEvent>, + TMap: Fn(&TNewIn) -> Option<&Self::FromBehaviour>, { MapInEvent::new(self, map) } @@ -171,7 +171,7 @@ pub trait ConnectionHandler: Send + 'static { fn map_out_event(self, map: TMap) -> MapOutEvent where Self: Sized, - TMap: FnMut(Self::OutEvent) -> TNewOut, + TMap: FnMut(Self::ToBehaviour) -> TNewOut, { MapOutEvent::new(self, map) } @@ -190,7 +190,7 @@ pub trait ConnectionHandler: Send + 'static { } /// Informs the handler about an event from the [`NetworkBehaviour`](super::NetworkBehaviour). - fn on_behaviour_event(&mut self, _event: Self::InEvent); + fn on_behaviour_event(&mut self, _event: Self::FromBehaviour); fn on_connection_event( &mut self, diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index 08c914abd47..6a60427228d 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -78,8 +78,8 @@ where L: ConnectionHandler, R: ConnectionHandler, { - type InEvent = Either; - type OutEvent = Either; + type FromBehaviour = Either; + type ToBehaviour = Either; type Error = Either; type InboundProtocol = Either, SendWrapper>; 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, >, > { diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index 3564de919bb..af828999b03 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -45,12 +45,12 @@ impl ConnectionHandler for MapInEvent where TConnectionHandler: ConnectionHandler, - TMap: Fn(TNewIn) -> Option, + TMap: Fn(TNewIn) -> Option, 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, >, > { diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index 349aa553764..f3240a76b2d 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -40,12 +40,12 @@ impl MapOutEvent { impl ConnectionHandler for MapOutEvent 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, >, > { diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 5caf611278f..ced94f1213c 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -110,8 +110,8 @@ where H::InboundProtocol: InboundUpgradeSend, H::OutboundProtocol: OutboundUpgradeSend, { - type InEvent = (K, ::InEvent); - type OutEvent = (K, ::OutEvent); + type FromBehaviour = (K, ::FromBehaviour); + type ToBehaviour = (K, ::ToBehaviour); type Error = ::Error; type InboundProtocol = Upgrade::InboundProtocol>; type OutboundProtocol = ::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, >, > { diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index c4da5877c96..b7c98e189e7 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -121,8 +121,8 @@ where SubstreamProtocol: Clone, TEvent: Debug + Send + 'static, { - type InEvent = TOutbound; - type OutEvent = TEvent; + type FromBehaviour = TOutbound; + type ToBehaviour = TEvent; type Error = StreamUpgradeError<::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, >, > { diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index 7cf8b9209fa..ee6829356bd 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -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; type OutboundProtocol = PendingUpgrade; @@ -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, >, > { diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 204d11ce502..48048befc81 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -179,8 +179,8 @@ where TProto1: ConnectionHandler, TProto2: ConnectionHandler, { - type InEvent = Either; - type OutEvent = Either; + type FromBehaviour = Either; + type ToBehaviour = Either; type Error = Either; type InboundProtocol = SelectUpgrade< SendWrapper<::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, >, > { diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index aa4da2db826..f91f80990a6 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -22,7 +22,7 @@ pub struct Behaviour; impl NetworkBehaviour for Behaviour { type ConnectionHandler = ConnectionHandler; - type OutEvent = Void; + type ToSwarm = Void; fn handle_established_inbound_connection( &mut self, @@ -57,7 +57,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { Poll::Pending } @@ -84,8 +84,8 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type InEvent = Void; - type OutEvent = Void; + type FromBehaviour = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -96,7 +96,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, v: Self::InEvent) { + fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { void::unreachable(v) } @@ -111,7 +111,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 3360a0bfa99..16dd9511d35 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -165,7 +165,7 @@ use std::{ pub type NegotiatedSubstream = Stream; /// Event generated by the [`NetworkBehaviour`] that the swarm will report back. -type TBehaviourOutEvent = ::OutEvent; +type TBehaviourOutEvent = ::ToSwarm; /// [`ConnectionHandler`] of the [`NetworkBehaviour`] for all the protocols the [`NetworkBehaviour`] /// supports. @@ -174,10 +174,10 @@ pub type THandler = ::ConnectionHand /// Custom event that can be received by the [`ConnectionHandler`] of the /// [`NetworkBehaviour`]. -pub type THandlerInEvent = as ConnectionHandler>::InEvent; +pub type THandlerInEvent = as ConnectionHandler>::FromBehaviour; /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. -pub type THandlerOutEvent = as ConnectionHandler>::OutEvent; +pub type THandlerOutEvent = as ConnectionHandler>::ToBehaviour; /// Custom error that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. pub type THandlerErr = as ConnectionHandler>::Error; @@ -743,7 +743,7 @@ where fn handle_pool_event( &mut self, event: PoolEvent>, - ) -> Option>> { + ) -> Option>> { match event { PoolEvent::ConnectionEstablished { peer_id, @@ -984,7 +984,7 @@ where as Transport>::ListenerUpgrade, io::Error, >, - ) -> Option>> { + ) -> Option>> { match event { TransportEvent::Incoming { listener_id: _, @@ -1109,8 +1109,8 @@ where fn handle_behaviour_event( &mut self, - event: ToSwarm>, - ) -> Option>> { + event: ToSwarm>, + ) -> Option>> { match event { ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)), ToSwarm::Dial { opts } => { @@ -1194,7 +1194,7 @@ where fn poll_next_event( mut self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll>> { + ) -> Poll>> { // We use a `this` variable because the compiler can't mutably borrow multiple times // across a `Deref`. let this = &mut *self; @@ -1343,8 +1343,8 @@ fn notify_any( where TBehaviour: NetworkBehaviour, THandler: ConnectionHandler< - InEvent = THandlerInEvent, - OutEvent = THandlerOutEvent, + FromBehaviour = THandlerInEvent, + ToBehaviour = THandlerOutEvent, >, { let mut pending = SmallVec::new(); @@ -1923,7 +1923,7 @@ mod tests { ) -> SwarmBuilder>> where T: ConnectionHandler + Clone, - T::OutEvent: Clone, + T::ToBehaviour: Clone, O: Send + 'static, { let id_keys = identity::Keypair::generate_ed25519(); diff --git a/swarm/src/test.rs b/swarm/src/test.rs index ccb282bd3f1..70d4e790956 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -37,7 +37,7 @@ use std::task::{Context, Poll}; pub(crate) struct MockBehaviour where THandler: ConnectionHandler + Clone, - THandler::OutEvent: Clone, + THandler::ToBehaviour: Clone, TOutEvent: Send + 'static, { /// The prototype protocols handler that is cloned for every @@ -48,13 +48,13 @@ where /// The next action to return from `poll`. /// /// An action is only returned once. - pub(crate) next_action: Option>, + pub(crate) next_action: Option>, } impl MockBehaviour where THandler: ConnectionHandler + Clone, - THandler::OutEvent: Clone, + THandler::ToBehaviour: Clone, TOutEvent: Send + 'static, { pub(crate) fn new(handler_proto: THandler) -> Self { @@ -69,11 +69,11 @@ where impl NetworkBehaviour for MockBehaviour where THandler: ConnectionHandler + Clone, - THandler::OutEvent: Clone, + THandler::ToBehaviour: Clone, TOutEvent: Send + 'static, { type ConnectionHandler = THandler; - type OutEvent = TOutEvent; + type ToSwarm = TOutEvent; fn handle_established_inbound_connection( &mut self, @@ -114,7 +114,7 @@ where &mut self, _: &mut Context, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { self.next_action.take().map_or(Poll::Pending, Poll::Ready) } @@ -381,7 +381,7 @@ where THandlerOutEvent: Clone, { type ConnectionHandler = TInner::ConnectionHandler; - type OutEvent = TInner::OutEvent; + type ToSwarm = TInner::ToSwarm; fn handle_pending_inbound_connection( &mut self, @@ -558,7 +558,7 @@ where &mut self, cx: &mut Context, args: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { self.poll += 1; self.inner.poll(cx, args) } diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 87f99e35736..ab1efaca396 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -56,7 +56,7 @@ fn one_field() { clippy::used_underscore_binding )] fn foo() { - let _out_event: ::OutEvent = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { FooEvent::Ping(ping::Event { .. }) => {} } @@ -80,7 +80,7 @@ fn two_fields() { clippy::used_underscore_binding )] fn foo() { - let _out_event: ::OutEvent = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { @@ -108,7 +108,7 @@ fn three_fields() { clippy::used_underscore_binding )] fn foo() { - let _out_event: ::OutEvent = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { @@ -125,7 +125,7 @@ fn three_fields() { fn custom_event() { #[allow(dead_code)] #[derive(NetworkBehaviour)] - #[behaviour(out_event = "MyEvent", prelude = "libp2p_swarm::derive_prelude")] + #[behaviour(to_swarm = "MyEvent", prelude = "libp2p_swarm::derive_prelude")] struct Foo { ping: ping::Behaviour, identify: identify::Behaviour, @@ -159,7 +159,7 @@ fn custom_event() { fn custom_event_mismatching_field_names() { #[allow(dead_code)] #[derive(NetworkBehaviour)] - #[behaviour(out_event = "MyEvent", prelude = "libp2p_swarm::derive_prelude")] + #[behaviour(to_swarm = "MyEvent", prelude = "libp2p_swarm::derive_prelude")] struct Foo { a: ping::Behaviour, b: identify::Behaviour, @@ -196,7 +196,7 @@ fn bound() { #[behaviour(prelude = "libp2p_swarm::derive_prelude")] struct Foo where - ::OutEvent: Debug, + ::ToSwarm: Debug, { ping: ping::Behaviour, bar: T, @@ -211,7 +211,7 @@ fn where_clause() { struct Foo where T: Copy + NetworkBehaviour, - ::OutEvent: Debug, + ::ToSwarm: Debug, { ping: ping::Behaviour, bar: T, @@ -241,7 +241,7 @@ fn nested_derives_with_import() { clippy::used_underscore_binding )] fn foo() { - let _out_event: ::OutEvent = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { BarEvent::Foo(FooEvent::Ping(ping::Event { .. })) => {} } @@ -271,7 +271,7 @@ fn custom_event_emit_event_through_poll() { #[allow(dead_code, clippy::large_enum_variant)] #[derive(NetworkBehaviour)] #[behaviour( - out_event = "BehaviourOutEvent", + to_swarm = "BehaviourOutEvent", prelude = "libp2p_swarm::derive_prelude" )] struct Foo { @@ -400,7 +400,7 @@ fn custom_event_with_either() { #[allow(dead_code)] #[derive(NetworkBehaviour)] #[behaviour( - out_event = "BehaviourOutEvent", + to_swarm = "BehaviourOutEvent", prelude = "libp2p_swarm::derive_prelude" )] struct Foo { @@ -426,7 +426,7 @@ fn generated_out_event_derive_debug() { fn require_debug() where T: NetworkBehaviour, - ::OutEvent: Debug, + ::ToSwarm: Debug, { } @@ -437,7 +437,7 @@ fn generated_out_event_derive_debug() { fn multiple_behaviour_attributes() { #[allow(dead_code)] #[derive(NetworkBehaviour)] - #[behaviour(out_event = "FooEvent")] + #[behaviour(to_swarm = "FooEvent")] #[behaviour(prelude = "libp2p_swarm::derive_prelude")] struct Foo { ping: ping::Behaviour, @@ -467,7 +467,7 @@ fn custom_out_event_no_type_parameters() { impl NetworkBehaviour for TemplatedBehaviour { type ConnectionHandler = dummy::ConnectionHandler; - type OutEvent = void::Void; + type ToSwarm = void::Void; fn handle_established_inbound_connection( &mut self, @@ -502,7 +502,7 @@ fn custom_out_event_no_type_parameters() { &mut self, _ctx: &mut Context, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { Poll::Pending } @@ -525,7 +525,7 @@ fn custom_out_event_no_type_parameters() { } #[derive(NetworkBehaviour)] - #[behaviour(out_event = "OutEvent", prelude = "libp2p_swarm::derive_prelude")] + #[behaviour(to_swarm = "OutEvent", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { custom: TemplatedBehaviour, } @@ -544,3 +544,9 @@ fn custom_out_event_no_type_parameters() { require_net_behaviour::>(); require_net_behaviour::>(); } + +#[test] +fn ui() { + let t = trybuild::TestCases::new(); + t.compile_fail("tests/ui/fail/*.rs"); +} diff --git a/swarm/tests/ui/fail/out_event_deprecation.rs b/swarm/tests/ui/fail/out_event_deprecation.rs new file mode 100644 index 00000000000..f552ca32d10 --- /dev/null +++ b/swarm/tests/ui/fail/out_event_deprecation.rs @@ -0,0 +1,21 @@ +#![deny(warnings)] // Ensure the warnings we produce surface as errors. + +use libp2p_ping as ping; + +#[derive(libp2p_swarm::NetworkBehaviour)] +#[behaviour(out_event = "FooEvent", prelude = "libp2p_swarm::derive_prelude")] +struct Foo { + ping: ping::Behaviour, +} + +struct FooEvent; + +impl From for FooEvent { + fn from(_: ping::Event) -> Self { + unimplemented!() + } +} + +fn main() { + +} diff --git a/swarm/tests/ui/fail/out_event_deprecation.stderr b/swarm/tests/ui/fail/out_event_deprecation.stderr new file mode 100644 index 00000000000..75a65f43f6d --- /dev/null +++ b/swarm/tests/ui/fail/out_event_deprecation.stderr @@ -0,0 +1,14 @@ +error: use of deprecated constant `out_event_renamed_to_to_swarm::_w`: + It is deprecated to use the out_event attribute `#[behaviour(out_event = ...)]`. + Please instead use the to_swarm attribute `#[behaviour(to_swarm = ...)]`. + --> tests/ui/fail/out_event_deprecation.rs:7:8 + | +7 | struct Foo { + | ^^^ + | +note: the lint level is defined here + --> tests/ui/fail/out_event_deprecation.rs:1:9 + | +1 | #![deny(warnings)] // Ensure the warnings we produce surface as errors. + | ^^^^^^^^ + = note: `#[deny(deprecated)]` implied by `#[deny(warnings)]` diff --git a/transports/webrtc/examples/listen_ping.rs b/transports/webrtc/examples/listen_ping.rs index 219acf0d08b..47155bb88df 100644 --- a/transports/webrtc/examples/listen_ping.rs +++ b/transports/webrtc/examples/listen_ping.rs @@ -38,7 +38,7 @@ fn create_swarm() -> Result> { #[derive(NetworkBehaviour, Default)] #[behaviour( - out_event = "Event", + to_swarm = "Event", event_process = false, prelude = "libp2p_swarm::derive_prelude" )]