From 3c90727a2d2b1c9d9b4bbac2c3ef6cc2b3114523 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 11:11:01 +0200 Subject: [PATCH 01/40] NetworkBehaviour OutEvent to ToSwarm --- 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/floodsub/src/layer.rs | 4 ++-- protocols/gossipsub/src/behaviour.rs | 4 ++-- protocols/identify/src/behaviour.rs | 4 ++-- protocols/kad/src/behaviour.rs | 4 ++-- protocols/mdns/src/behaviour.rs | 4 ++-- protocols/perf/src/client/behaviour.rs | 4 ++-- protocols/perf/src/server/behaviour.rs | 4 ++-- protocols/ping/src/lib.rs | 4 ++-- protocols/relay/src/behaviour.rs | 4 ++-- protocols/relay/src/priv_client.rs | 4 ++-- protocols/rendezvous/src/client.rs | 4 ++-- protocols/rendezvous/src/server.rs | 4 ++-- protocols/request-response/src/lib.rs | 4 ++-- swarm-derive/src/lib.rs | 24 ++++++++++++------------ swarm-test/src/lib.rs | 26 +++++++++++++------------- swarm/src/behaviour.rs | 4 ++-- swarm/src/behaviour/either.rs | 4 ++-- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/dummy.rs | 4 ++-- swarm/src/keep_alive.rs | 4 ++-- swarm/src/lib.rs | 12 ++++++------ swarm/src/test.rs | 4 ++-- swarm/tests/swarm_derive.rs | 2 +- 27 files changed, 79 insertions(+), 79 deletions(-) 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 6161fca67bc..60c15313cf4 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 b0d07f7a37b..cb9c7e74bf5 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -208,7 +208,7 @@ pub struct Behaviour { last_probe: Option, - pending_actions: VecDeque::OutEvent, THandlerInEvent>>, + pending_actions: VecDeque::ToSwarm, THandlerInEvent>>, probe_id: ProbeId, @@ -426,7 +426,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 { @@ -593,7 +593,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 c6701ad80ee..09acb065f7b 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -239,7 +239,7 @@ impl NetworkBehaviour for Behaviour { handler::relayed::Handler, Either, >; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -417,7 +417,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/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 13ce3fcee35..9c249b9804d 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/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 8ba50e2db4b..de3e9905616 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -237,7 +237,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -324,7 +324,7 @@ impl NetworkBehaviour for Behaviour { &mut self, _cx: &mut Context<'_>, params: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); } diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index b34c5e428f7..3a17a0bad3d 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1966,7 +1966,7 @@ where TStore: RecordStore + Send + 'static, { type ConnectionHandler = KademliaHandler; - type OutEvent = KademliaEvent; + type ToSwarm = KademliaEvent; fn handle_established_inbound_connection( &mut self, @@ -2307,7 +2307,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/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 92e38c04998..73c85d94626 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 dade91c5a7f..62b85099a00 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/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/ping/src/lib.rs b/protocols/ping/src/lib.rs index 23fe2ba600d..322499cdc03 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -119,7 +119,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, @@ -154,7 +154,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 9f2852dd19e..b1a3f0f3939 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -254,7 +254,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Either; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -685,7 +685,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/priv_client.rs b/protocols/relay/src/priv_client.rs index 2a24607a724..a689db3f122 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -161,7 +161,7 @@ impl Behaviour { impl NetworkBehaviour for Behaviour { type ConnectionHandler = Either; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -302,7 +302,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/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 324d352c0f7..d0419ed8ab2 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/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index d9f2220d839..c347c99b459 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -717,7 +717,7 @@ where TCodec: Codec + Send + Clone + 'static, { type ConnectionHandler = Handler; - type OutEvent = Event; + type ToSwarm = Event; fn handle_established_inbound_connection( &mut self, @@ -937,7 +937,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/src/lib.rs b/swarm-derive/src/lib.rs index 04a32cf91a7..1da05418c4a 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -97,10 +97,10 @@ 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 + // 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 +108,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 +135,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 +146,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 +168,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 +677,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( @@ -737,7 +737,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { #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 +795,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 diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 94bad497e8f..b2b76786942 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -54,7 +54,7 @@ pub trait SwarmExt { async fn connect(&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); } @@ -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/src/behaviour.rs b/swarm/src/behaviour.rs index 9fd014bdc47..7813aef16fa 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -126,7 +126,7 @@ pub trait NetworkBehaviour: 'static { type ConnectionHandler: IntoConnectionHandler; /// Event generated by the `NetworkBehaviour` and that the swarm will report back. - type OutEvent: Send + 'static; + type ToSwarm: Send + 'static; /// Creates a new [`ConnectionHandler`](crate::ConnectionHandler) for a connection with a peer. /// @@ -276,7 +276,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 bd4678a5e58..8dce69b2a3e 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 { diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 4497540a42b..e3e2052b47d 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 } diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index c22a926afe4..e1b2f983830 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 } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 3fba83cd44d..a536214a5a0 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -167,7 +167,7 @@ use std::{ pub type NegotiatedSubstream = Negotiated; /// 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. @@ -806,7 +806,7 @@ where fn handle_pool_event( &mut self, event: PoolEvent>, - ) -> Option>> { + ) -> Option>> { match event { PoolEvent::ConnectionEstablished { peer_id, @@ -1052,7 +1052,7 @@ where as Transport>::ListenerUpgrade, io::Error, >, - ) -> Option>> { + ) -> Option>> { match event { TransportEvent::Incoming { listener_id: _, @@ -1192,8 +1192,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 } => { @@ -1277,7 +1277,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; diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 90738b6a6cf..20b11373b5a 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -114,7 +114,7 @@ where &mut self, _: &mut Context, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { self.next_action.take().map_or(Poll::Pending, Poll::Ready) } @@ -581,7 +581,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..70322f166d4 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -502,7 +502,7 @@ fn custom_out_event_no_type_parameters() { &mut self, _ctx: &mut Context, _: &mut impl PollParameters, - ) -> Poll>> { + ) -> Poll>> { Poll::Pending } From 1e48a4120d9f1599523b96f2dbe9d79ea2c1e2e9 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 11:22:58 +0200 Subject: [PATCH 02/40] ConnectionHandler InEvent to FromSwarm --- protocols/dcutr/src/handler/direct.rs | 4 ++-- protocols/dcutr/src/handler/relayed.rs | 4 ++-- protocols/gossipsub/src/handler.rs | 2 +- protocols/identify/src/handler.rs | 4 ++-- protocols/kad/src/handler_priv.rs | 2 +- protocols/perf/src/client/handler.rs | 4 ++-- protocols/perf/src/server/handler.rs | 4 ++-- protocols/ping/src/handler.rs | 2 +- protocols/relay/src/behaviour/handler.rs | 4 ++-- protocols/relay/src/priv_client/handler.rs | 4 ++-- protocols/rendezvous/src/substream_handler.rs | 4 ++-- protocols/request-response/src/handler.rs | 4 ++-- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/connection.rs | 6 +++--- swarm/src/connection/pool.rs | 4 ++-- swarm/src/connection/pool/task.rs | 2 +- swarm/src/dummy.rs | 4 ++-- swarm/src/handler.rs | 6 +++--- swarm/src/handler/either.rs | 4 ++-- swarm/src/handler/map_in.rs | 4 ++-- swarm/src/handler/map_out.rs | 4 ++-- swarm/src/handler/multi.rs | 4 ++-- swarm/src/handler/one_shot.rs | 4 ++-- swarm/src/handler/pending.rs | 4 ++-- swarm/src/handler/select.rs | 4 ++-- swarm/src/keep_alive.rs | 4 ++-- swarm/src/lib.rs | 4 ++-- 27 files changed, 52 insertions(+), 52 deletions(-) diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index aab212483eb..621f73f3c17 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -40,7 +40,7 @@ pub struct Handler { } impl ConnectionHandler for Handler { - type InEvent = void::Void; + type FromSwarm = void::Void; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = DeniedUpgrade; @@ -52,7 +52,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, _: Self::InEvent) {} + fn on_behaviour_event(&mut self, _: Self::FromSwarm) {} fn connection_keep_alive(&self) -> KeepAlive { KeepAlive::No diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index aefaaeec933..0ecfe1af9bd 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -294,7 +294,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = Command; + type FromSwarm = Command; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -320,7 +320,7 @@ impl ConnectionHandler for Handler { } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { match event { Command::Connect { obs_addrs, attempt } => { self.queued_events diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 269bdcd404f..b028f812a13 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -392,7 +392,7 @@ impl EnabledHandler { } impl ConnectionHandler for Handler { - type InEvent = HandlerIn; + type FromSwarm = HandlerIn; type OutEvent = HandlerEvent; type Error = Void; type InboundOpenInfo = (); diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index c0bd9d928eb..fcdbca8d36e 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -222,7 +222,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = InEvent; + type FromSwarm = InEvent; type OutEvent = Event; type Error = io::Error; type InboundProtocol = SelectUpgrade>; @@ -240,7 +240,7 @@ impl ConnectionHandler for Handler { listen_addrs, supported_protocols, protocol, - }: Self::InEvent, + }: Self::FromSwarm, ) { let info = Info { public_key: self.public_key.clone(), diff --git a/protocols/kad/src/handler_priv.rs b/protocols/kad/src/handler_priv.rs index ed2d05219bd..94e05717eda 100644 --- a/protocols/kad/src/handler_priv.rs +++ b/protocols/kad/src/handler_priv.rs @@ -609,7 +609,7 @@ impl ConnectionHandler for KademliaHandler where TUserData: Clone + fmt::Debug + Send + 'static + Unpin, { - type InEvent = KademliaHandlerIn; + type FromSwarm = KademliaHandlerIn; type OutEvent = KademliaHandlerEvent; type Error = io::Error; // TODO: better error type? type InboundProtocol = Either; diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index f75a43f0a4e..5093b1a9029 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -83,7 +83,7 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type InEvent = Command; + type FromSwarm = Command; type OutEvent = Event; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -95,7 +95,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::FromSwarm) { self.queued_events .push_back(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: SubstreamProtocol::new(ReadyUpgrade::new(crate::PROTOCOL_NAME), command), diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 2946b6d4a4c..251565c1677 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -64,7 +64,7 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = Event; type Error = Void; type InboundProtocol = ReadyUpgrade<&'static [u8]>; @@ -76,7 +76,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::FromSwarm) { void::unreachable(v) } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 2703b274c77..f2de0b5384e 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -254,7 +254,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = crate::Result; type Error = Failure; type InboundProtocol = ReadyUpgrade<&'static [u8]>; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 29b5c4b9dbd..198687b9768 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -598,7 +598,7 @@ enum ReservationRequestFuture { type Futures = FuturesUnordered>; impl ConnectionHandler for Handler { - type InEvent = In; + type FromSwarm = In; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -619,7 +619,7 @@ impl ConnectionHandler for Handler { ) } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { match event { In::AcceptReservationReq { inbound_reservation_req, diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 504fab590f8..e6685907ca2 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -462,7 +462,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type InEvent = In; + type FromSwarm = In; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -476,7 +476,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::FromSwarm) { match event { In::Reserve { to_listener } => { self.queued_events diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index f5f79451b09..11f09568817 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -354,7 +354,7 @@ where TInboundSubstreamHandler: Send + 'static, TOutboundSubstreamHandler: Send + 'static, { - type InEvent = InEvent; + type FromSwarm = InEvent; type OutEvent = OutEvent; type Error = Void; type InboundProtocol = PassthroughProtocol; @@ -400,7 +400,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { match event { InEvent::NewSubstream { open_info } => self.new_substreams.push_back(open_info), InEvent::NotifyInboundSubstream { id, message } => { diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 50cd6adb055..09a9a8a30f1 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -282,7 +282,7 @@ impl ConnectionHandler for Handler where TCodec: Codec + Send + Clone + 'static, { - type InEvent = RequestProtocol; + type FromSwarm = RequestProtocol; type OutEvent = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = ResponseProtocol; @@ -324,7 +324,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::FromSwarm) { self.keep_alive = KeepAlive::Yes; self.outbound.push_back(request); } diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 8dce69b2a3e..f534bf39275 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -273,7 +273,7 @@ impl ConnectionHandler for ToggleConnectionHandler where TInner: ConnectionHandler, { - type InEvent = TInner::InEvent; + type FromSwarm = TInner::FromSwarm; type OutEvent = TInner::OutEvent; type Error = TInner::Error; type InboundProtocol = Either, SendWrapper>; @@ -292,7 +292,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { self.inner .as_mut() .expect("Can't receive events if disabled; QED") diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index e813ad0c66d..763973d7530 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -186,7 +186,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::FromSwarm) { self.handler.on_behaviour_event(event); } @@ -791,7 +791,7 @@ mod tests { } impl ConnectionHandler for MockConnectionHandler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -830,7 +830,7 @@ mod tests { } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { void::unreachable(event) } diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index 68ad16ad36a..149b669fef7 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -95,7 +95,7 @@ where /// The managed connections of each peer that are currently considered established. established: - FnvHashMap>>, + FnvHashMap>>, /// The pending connections that are currently being negotiated. pending: HashMap, @@ -343,7 +343,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..5a0385ba2b6 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -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 e3e2052b47d..74251a8b1c5 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -81,7 +81,7 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = Void; type Error = Void; type InboundProtocol = 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::FromSwarm) { void::unreachable(event) } diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 1917117c44e..a9137ea1656 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -94,7 +94,7 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; /// 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; + type FromSwarm: 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; /// The type of errors returned by [`ConnectionHandler::poll`]. @@ -156,7 +156,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::FromSwarm>, { MapInEvent::new(self, map) } @@ -184,7 +184,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::FromSwarm); fn on_connection_event( &mut self, diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index 92d82371163..ada20f95464 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -192,7 +192,7 @@ where L: ConnectionHandler, R: ConnectionHandler, { - type InEvent = Either; + type FromSwarm = Either; type OutEvent = Either; type Error = Either; type InboundProtocol = Either, SendWrapper>; @@ -214,7 +214,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::InEvent) { + fn on_behaviour_event(&mut self, event: Self::FromSwarm) { 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), diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index 3564de919bb..b7c6ef75bf2 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -45,11 +45,11 @@ 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 FromSwarm = TNewIn; type OutEvent = TConnectionHandler::OutEvent; type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index 773df2b6681..d52d319e44a 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -44,7 +44,7 @@ where TNewOut: Debug + Send + 'static, TMap: Send + 'static, { - type InEvent = TConnectionHandler::InEvent; + type FromSwarm = TConnectionHandler::FromSwarm; type OutEvent = TNewOut; type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; @@ -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::FromSwarm) { self.inner.on_behaviour_event(event) } diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 146a2a96895..87be4e99665 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -223,7 +223,7 @@ where H::InboundProtocol: InboundUpgradeSend, H::OutboundProtocol: OutboundUpgradeSend, { - type InEvent = (K, ::InEvent); + type FromSwarm = (K, ::FromSwarm); type OutEvent = (K, ::OutEvent); type Error = ::Error; type InboundProtocol = Upgrade::InboundProtocol>; @@ -321,7 +321,7 @@ where } } - fn on_behaviour_event(&mut self, (key, event): Self::InEvent) { + fn on_behaviour_event(&mut self, (key, event): Self::FromSwarm) { if let Some(h) = self.handlers.get_mut(&key) { h.on_behaviour_event(event) } else { diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index 29ba45ab678..2a23e3915e6 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -121,7 +121,7 @@ where SubstreamProtocol: Clone, TEvent: Debug + Send + 'static, { - type InEvent = TOutbound; + type FromSwarm = TOutbound; type OutEvent = TEvent; type Error = ConnectionHandlerUpgrErr<::Error>; type InboundProtocol = TInbound; @@ -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::FromSwarm) { self.send_request(event); } diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index a39e498c3f2..e32f79be8aa 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -40,7 +40,7 @@ impl PendingConnectionHandler { } impl ConnectionHandler for PendingConnectionHandler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = Void; type Error = Void; type InboundProtocol = 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::FromSwarm) { void::unreachable(v) } diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index edb9a9154b1..e4879030e13 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -339,7 +339,7 @@ where TProto1: ConnectionHandler, TProto2: ConnectionHandler, { - type InEvent = Either; + type FromSwarm = Either; type OutEvent = Either; type Error = Either; type InboundProtocol = SelectUpgrade< @@ -361,7 +361,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::FromSwarm) { match event { Either::Left(event) => self.proto1.on_behaviour_event(event), Either::Right(event) => self.proto2.on_behaviour_event(event), diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index e1b2f983830..8b37a07f1a2 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -84,7 +84,7 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type InEvent = Void; + type FromSwarm = Void; type OutEvent = Void; type Error = Void; type InboundProtocol = 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::FromSwarm) { void::unreachable(v) } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index a536214a5a0..6b79cf76457 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -177,7 +177,7 @@ pub type THandler = /// Custom event that can be received by the [`ConnectionHandler`] of the /// [`NetworkBehaviour`]. -pub type THandlerInEvent = as ConnectionHandler>::InEvent; +pub type THandlerInEvent = as ConnectionHandler>::FromSwarm; /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. pub type THandlerOutEvent = as ConnectionHandler>::OutEvent; @@ -1426,7 +1426,7 @@ fn notify_any( where TBehaviour: NetworkBehaviour, THandler: ConnectionHandler< - InEvent = THandlerInEvent, + FromSwarm = THandlerInEvent, OutEvent = THandlerOutEvent, >, { From 84120adfb96c05d561cac8bb022facd45090f5b3 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 11:35:26 +0200 Subject: [PATCH 03/40] changelog comment and update version in Cargo.toml --- core/CHANGELOG.md | 2 +- protocols/autonat/src/behaviour.rs | 2 +- protocols/dcutr/src/handler/direct.rs | 4 ++-- protocols/dcutr/src/handler/relayed.rs | 6 ++--- protocols/gossipsub/src/handler.rs | 6 ++--- protocols/identify/src/handler.rs | 2 +- protocols/kad/src/handler_priv.rs | 4 ++-- protocols/perf/src/client/handler.rs | 6 ++--- protocols/perf/src/server/handler.rs | 4 ++-- protocols/ping/src/handler.rs | 2 +- protocols/relay/src/behaviour/handler.rs | 6 ++--- protocols/relay/src/priv_client/handler.rs | 6 ++--- protocols/rendezvous/src/substream_handler.rs | 4 ++-- protocols/request-response/src/handler.rs | 7 +++--- swarm-derive/CHANGELOG.md | 4 ++-- swarm-test/src/lib.rs | 2 +- swarm/CHANGELOG.md | 22 +++++++++++-------- swarm/Cargo.toml | 2 +- swarm/src/behaviour.rs | 2 +- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/connection.rs | 6 ++--- swarm/src/connection/pool.rs | 2 +- swarm/src/connection/pool/task.rs | 2 +- swarm/src/dummy.rs | 4 ++-- swarm/src/handler.rs | 6 ++--- swarm/src/handler/either.rs | 4 ++-- swarm/src/handler/map_in.rs | 4 ++-- swarm/src/handler/map_out.rs | 6 ++--- swarm/src/handler/multi.rs | 4 ++-- swarm/src/handler/one_shot.rs | 4 ++-- swarm/src/handler/pending.rs | 4 ++-- swarm/src/handler/select.rs | 4 ++-- swarm/src/keep_alive.rs | 4 ++-- swarm/src/lib.rs | 6 ++--- swarm/src/test.rs | 10 ++++----- swarm/tests/swarm_derive.rs | 22 +++++++++---------- 36 files changed, 97 insertions(+), 92 deletions(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 9cd4ca30de0..9c532eef102 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -243,7 +243,7 @@ superflucious. Both are removed in favor of a derivation through `THandler` (see [PR 2183]). -- Require `ConnectionHandler::{InEvent,OutEvent,Error}` to implement `Debug` +- Require `ConnectionHandler::{InEvent,ToBehaviour ,Error}` to implement `Debug` (see [PR 2183]). - Remove `DisconnectedPeer::set_connected` and `Pool::add` (see [PR 2195]). diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index cb9c7e74bf5..7a7edac9238 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -159,7 +159,7 @@ pub enum Event { /// The status is retried in a frequency of [`Config::retry_interval`] or [`Config::retry_interval`], depending on whether /// enough confidence in the assumed NAT status was reached or not. /// The confidence increases each time a probe confirms the assumed status, and decreases if a different status is reported. -/// If the confidence is 0, the status is flipped and the Behaviour will report the new status in an `OutEvent`. +/// If the confidence is 0, the status is flipped and the Behaviour will report the new status in an `ToBehaviour `. pub struct Behaviour { // Local peer id local_peer_id: PeerId, diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index 621f73f3c17..8e79db11b53 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -41,7 +41,7 @@ pub struct Handler { impl ConnectionHandler for Handler { type FromSwarm = void::Void; - type OutEvent = Event; + type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -65,7 +65,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 0ecfe1af9bd..1b7a3aef768 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -138,7 +138,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -295,7 +295,7 @@ impl Handler { impl ConnectionHandler for Handler { type FromSwarm = Command; - type OutEvent = Event; + type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, >; @@ -363,7 +363,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index b028f812a13..6e995f581a2 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -224,7 +224,7 @@ impl EnabledHandler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, > { @@ -393,7 +393,7 @@ impl EnabledHandler { impl ConnectionHandler for Handler { type FromSwarm = HandlerIn; - type OutEvent = HandlerEvent; + type ToBehaviour = HandlerEvent; type Error = Void; type InboundOpenInfo = (); type InboundProtocol = either::Either; @@ -456,7 +456,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index fcdbca8d36e..d18e54ad8bf 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -223,7 +223,7 @@ impl Handler { impl ConnectionHandler for Handler { type FromSwarm = InEvent; - type OutEvent = Event; + type ToBehaviour = Event; type Error = io::Error; type InboundProtocol = SelectUpgrade>; type OutboundProtocol = Either>; diff --git a/protocols/kad/src/handler_priv.rs b/protocols/kad/src/handler_priv.rs index 94e05717eda..689bfad69ff 100644 --- a/protocols/kad/src/handler_priv.rs +++ b/protocols/kad/src/handler_priv.rs @@ -610,7 +610,7 @@ where TUserData: Clone + fmt::Debug + Send + 'static + Unpin, { type FromSwarm = KademliaHandlerIn; - type OutEvent = KademliaHandlerEvent; + type ToBehaviour = KademliaHandlerEvent; type Error = io::Error; // TODO: better error type? type InboundProtocol = Either; type OutboundProtocol = KademliaProtocolConfig; @@ -727,7 +727,7 @@ where ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 5093b1a9029..9fd4995ac94 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, >, >, @@ -84,7 +84,7 @@ impl Default for Handler { impl ConnectionHandler for Handler { type FromSwarm = Command; - type OutEvent = Event; + type ToBehaviour = Event; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = ReadyUpgrade<&'static [u8]>; @@ -161,7 +161,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 251565c1677..d60a37577e3 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -65,7 +65,7 @@ impl Default for Handler { impl ConnectionHandler for Handler { type FromSwarm = Void; - type OutEvent = Event; + type ToBehaviour = Event; type Error = Void; type InboundProtocol = ReadyUpgrade<&'static [u8]>; type OutboundProtocol = DeniedUpgrade; @@ -129,7 +129,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 f2de0b5384e..396ba134b4b 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -255,7 +255,7 @@ impl Handler { impl ConnectionHandler for Handler { type FromSwarm = Void; - type OutEvent = crate::Result; + type ToBehaviour = crate::Result; type Error = Failure; type InboundProtocol = ReadyUpgrade<&'static [u8]>; type OutboundProtocol = ReadyUpgrade<&'static [u8]>; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 198687b9768..8e707b640f2 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -348,7 +348,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -599,7 +599,7 @@ type Futures = FuturesUnordered>; impl ConnectionHandler for Handler { type FromSwarm = In; - type OutEvent = Event; + type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, >; @@ -723,7 +723,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index e6685907ca2..a4ba55b3312 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -128,7 +128,7 @@ pub struct Handler { ConnectionHandlerEvent< ::OutboundProtocol, ::OutboundOpenInfo, - ::OutEvent, + ::ToBehaviour, ::Error, >, >, @@ -463,7 +463,7 @@ impl Handler { impl ConnectionHandler for Handler { type FromSwarm = In; - type OutEvent = Event; + type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, >; @@ -513,7 +513,7 @@ impl ConnectionHandler for Handler { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour, Self::Error, >, > { diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index 11f09568817..8bbed1f8590 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -355,7 +355,7 @@ where TOutboundSubstreamHandler: Send + 'static, { type FromSwarm = InEvent; - type OutEvent = OutEvent; + type ToBehaviour = OutEvent; type Error = Void; type InboundProtocol = PassthroughProtocol; type OutboundProtocol = PassthroughProtocol; @@ -454,7 +454,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 09a9a8a30f1..388bfcd47a6 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -283,7 +283,7 @@ where TCodec: Codec + Send + Clone + 'static, { type FromSwarm = RequestProtocol; - type OutEvent = Event; + type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = ResponseProtocol; type OutboundProtocol = RequestProtocol; @@ -336,8 +336,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/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index f13e4d39c95..3ab4d33b748 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.32.0 - Fix `NetworkBehaviour` Derive macro for generic types when `out_event` was not provided. Previously the enum generated - didn't have the `NetworkBehaviour` impl constraints whilst using the generics for `::OutEvent`. + didn't have the `NetworkBehaviour` impl constraints whilst using the generics for `::ToBehaviour `. See [PR 3393]. - Replace `NetworkBehaviour` Derive macro deprecated `inject_*` method implementations @@ -48,7 +48,7 @@ ## 0.29.0 -- Generate `NetworkBehaviour::OutEvent` if not provided through `#[behaviour(out_event = +- Generate `NetworkBehaviour::ToBehaviour ` if not provided through `#[behaviour(out_event = "MyOutEvent")]` and event processing is disabled (default). ## 0.28.0 diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index b2b76786942..a652483fbb9 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -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::ToBehaviour `] 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. /// diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 03d128d76b7..301541117dc 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.43.0 - unreleased + +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromSwarm`, `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToBehaviour`. + ## 0.42.2 - unreleased - Add `ConnectionEvent::{is_outbound,is_inbound}`. See [PR 3625]. @@ -66,15 +70,15 @@ &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - - ) -> Poll> { - + ) -> Poll>> { + - ) -> Poll> { + + ) -> Poll>> { ``` In other words: |Search|Replace| |---|---| - |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| + |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| If you reference `NetworkBehaviourAction` somewhere else as well, you may have to fill in the type of `ConnectionHandler::InEvent` manually as the 2nd parameter. @@ -281,7 +285,7 @@ ## 0.38.0 - Deprecate `NetworkBehaviourEventProcess`. When deriving `NetworkBehaviour` on a custom `struct` users - should either bring their own `OutEvent` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, + should either bring their own `ToBehaviour ` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, when not specified, have the derive macro generate one for the user. See [`NetworkBehaviour` @@ -349,8 +353,8 @@ ``` - When deriving `NetworkBehaviour` on a custom `struct` where the user does not specify their own - `OutEvent` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable - `#[behaviour(event_process = true)]`, then the derive macro generates an `OutEvent` definition for + `ToBehaviour ` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable + `#[behaviour(event_process = true)]`, then the derive macro generates an `ToBehaviour ` definition for the user. See [`NetworkBehaviour` @@ -527,7 +531,7 @@ trait parameters on `Swarm` (previously `ExpandedSwarm`), deriving parameters through associated types on `TBehaviour`. See [PR 2182]. -- Require `ProtocolsHandler::{InEvent,OutEvent,Error}` to implement `Debug` (see +- Require `ProtocolsHandler::{InEvent,ToBehaviour ,Error}` to implement `Debug` (see [PR 2183]). - Implement `ProtocolsHandler` on `either::Either`representing either of two @@ -551,9 +555,9 @@ false. To emulate the previous behaviour, return early within `inject_dial_failure` on `DialError::DialPeerConditionFalse`. See [PR 2191]. -- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::OutEvent` and +- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::ToBehaviour ` and `NetworkBehaviour::ProtocolsHandler`. In most cases, change your generic type - parameters to `NetworkBehaviourAction`. See [PR 2191]. - Return `bool` instead of `Result<(), ()>` for `Swarm::remove_listener`(see diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 820fabc3aac..a8403f139e7 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = "1.62.0" description = "The libp2p swarm" -version = "0.42.2" +version = "0.43.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 7813aef16fa..697c4c0c571 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -81,7 +81,7 @@ use std::{task::Context, task::Poll}; /// it will delegate to each `struct` member and return a concatenated array of all addresses /// returned by the struct members. /// -/// Events ([`NetworkBehaviour::OutEvent`]) returned by each `struct` member are wrapped in a new +/// Events ([`NetworkBehaviour::ToBehaviour `]) 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 diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index f534bf39275..9596f85b6b1 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -274,7 +274,7 @@ where TInner: ConnectionHandler, { type FromSwarm = TInner::FromSwarm; - type OutEvent = TInner::OutEvent; + type ToBehaviour = TInner::ToBehaviour; type Error = TInner::Error; type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = TInner::OutboundProtocol; @@ -313,7 +313,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 763973d7530..b062fb967af 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -201,7 +201,7 @@ where pub(crate) fn poll( self: Pin<&mut Self>, cx: &mut Context<'_>, - ) -> Poll, ConnectionError>> { + ) -> Poll, ConnectionError>> { let Self { requested_substreams, muxing, @@ -792,7 +792,7 @@ mod tests { impl ConnectionHandler for MockConnectionHandler { type FromSwarm = Void; - type OutEvent = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -845,7 +845,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 149b669fef7..a8bca563510 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -289,7 +289,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. diff --git a/swarm/src/connection/pool/task.rs b/swarm/src/connection/pool/task.rs index 5a0385ba2b6..e05a91a3f83 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. /// diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 74251a8b1c5..be7c780349e 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -82,7 +82,7 @@ pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { type FromSwarm = Void; - type OutEvent = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -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 a9137ea1656..ec81f71655b 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -96,7 +96,7 @@ pub trait ConnectionHandler: Send + 'static { /// Custom event that can be received from the outside. type FromSwarm: 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; + 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. @@ -147,7 +147,7 @@ pub trait ConnectionHandler: Send + 'static { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::OutEvent, + Self::ToBehaviour , Self::Error, >, >; @@ -165,7 +165,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) } diff --git a/swarm/src/handler/either.rs b/swarm/src/handler/either.rs index ada20f95464..5c417ebd438 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -193,7 +193,7 @@ where R: ConnectionHandler, { type FromSwarm = Either; - type OutEvent = Either; + type ToBehaviour = Either; type Error = Either; type InboundProtocol = Either, SendWrapper>; type OutboundProtocol = @@ -236,7 +236,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 b7c6ef75bf2..7c6968b8f63 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -50,7 +50,7 @@ where TMap: Send + 'static, { type FromSwarm = TNewIn; - type OutEvent = TConnectionHandler::OutEvent; + 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 d52d319e44a..01ec81c01cf 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 FromSwarm = TConnectionHandler::FromSwarm; - type OutEvent = TNewOut; + type ToBehaviour = TNewOut; type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; type OutboundProtocol = TConnectionHandler::OutboundProtocol; @@ -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 87be4e99665..230bdc30394 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -224,7 +224,7 @@ where H::OutboundProtocol: OutboundUpgradeSend, { type FromSwarm = (K, ::FromSwarm); - type OutEvent = (K, ::OutEvent); + type ToBehaviour = (K, ::ToBehaviour); type Error = ::Error; type InboundProtocol = Upgrade::InboundProtocol>; type OutboundProtocol = ::OutboundProtocol; @@ -344,7 +344,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 2a23e3915e6..e6cd638865c 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -122,7 +122,7 @@ where TEvent: Debug + Send + 'static, { type FromSwarm = TOutbound; - type OutEvent = TEvent; + type ToBehaviour = TEvent; type Error = ConnectionHandlerUpgrErr<::Error>; type InboundProtocol = TInbound; type OutboundProtocol = TOutbound; @@ -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 e32f79be8aa..c4527cbe886 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -41,7 +41,7 @@ impl PendingConnectionHandler { impl ConnectionHandler for PendingConnectionHandler { type FromSwarm = Void; - type OutEvent = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = PendingUpgrade; type OutboundProtocol = PendingUpgrade; @@ -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 e4879030e13..3becd9bd39a 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -340,7 +340,7 @@ where TProto2: ConnectionHandler, { type FromSwarm = Either; - type OutEvent = Either; + type ToBehaviour = Either; type Error = Either; type InboundProtocol = SelectUpgrade< SendWrapper<::InboundProtocol>, @@ -382,7 +382,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 8b37a07f1a2..404ad404787 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -85,7 +85,7 @@ pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { type FromSwarm = Void; - type OutEvent = Void; + type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -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 6b79cf76457..72411af4353 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -180,7 +180,7 @@ pub type THandler = pub type THandlerInEvent = as ConnectionHandler>::FromSwarm; /// 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; @@ -1427,7 +1427,7 @@ where TBehaviour: NetworkBehaviour, THandler: ConnectionHandler< FromSwarm = THandlerInEvent, - OutEvent = THandlerOutEvent, + ToBehaviour = THandlerOutEvent, >, { let mut pending = SmallVec::new(); @@ -2057,7 +2057,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 20b11373b5a..ddf68c45b76 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 @@ -54,7 +54,7 @@ where 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 ToBehaviour = TOutEvent; fn handle_established_inbound_connection( &mut self, @@ -404,7 +404,7 @@ where THandlerOutEvent: Clone, { type ConnectionHandler = TInner::ConnectionHandler; - type OutEvent = TInner::OutEvent; + type ToBehaviour = TInner::ToBehaviour; fn handle_pending_inbound_connection( &mut self, diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 70322f166d4..cba7fa19a3a 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: ::ToBehaviour = 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: ::ToBehaviour = 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: ::ToBehaviour = unimplemented!(); match _out_event { FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { @@ -196,7 +196,7 @@ fn bound() { #[behaviour(prelude = "libp2p_swarm::derive_prelude")] struct Foo where - ::OutEvent: Debug, + ::ToBehaviour: Debug, { ping: ping::Behaviour, bar: T, @@ -211,7 +211,7 @@ fn where_clause() { struct Foo where T: Copy + NetworkBehaviour, - ::OutEvent: Debug, + ::ToBehaviour: 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: ::ToBehaviour = unimplemented!(); match _out_event { BarEvent::Foo(FooEvent::Ping(ping::Event { .. })) => {} } @@ -426,7 +426,7 @@ fn generated_out_event_derive_debug() { fn require_debug() where T: NetworkBehaviour, - ::OutEvent: Debug, + ::ToBehaviour: Debug, { } @@ -467,7 +467,7 @@ fn custom_out_event_no_type_parameters() { impl NetworkBehaviour for TemplatedBehaviour { type ConnectionHandler = dummy::ConnectionHandler; - type OutEvent = void::Void; + type ToBehaviour = void::Void; fn handle_established_inbound_connection( &mut self, @@ -525,17 +525,17 @@ fn custom_out_event_no_type_parameters() { } #[derive(NetworkBehaviour)] - #[behaviour(out_event = "OutEvent", prelude = "libp2p_swarm::derive_prelude")] + #[behaviour(out_event = "ToBehaviour ", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { custom: TemplatedBehaviour, } #[derive(Debug)] - enum OutEvent { + enum ToBehaviour { None, } - impl From for OutEvent { + impl From for ToBehaviour { fn from(_e: void::Void) -> Self { Self::None } From 9a837102cff32b8853d1fe6ddd58889868d9545d Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 11:51:43 +0200 Subject: [PATCH 04/40] clean up --- protocols/autonat/src/behaviour.rs | 2 +- swarm-derive/CHANGELOG.md | 4 ++-- swarm-test/src/lib.rs | 2 +- swarm/CHANGELOG.md | 25 ++++++++++++------------- swarm/Cargo.toml | 2 +- swarm/src/behaviour.rs | 2 +- swarm/tests/swarm_derive.rs | 6 +++--- 7 files changed, 21 insertions(+), 22 deletions(-) diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index 7a7edac9238..cb9c7e74bf5 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -159,7 +159,7 @@ pub enum Event { /// The status is retried in a frequency of [`Config::retry_interval`] or [`Config::retry_interval`], depending on whether /// enough confidence in the assumed NAT status was reached or not. /// The confidence increases each time a probe confirms the assumed status, and decreases if a different status is reported. -/// If the confidence is 0, the status is flipped and the Behaviour will report the new status in an `ToBehaviour `. +/// If the confidence is 0, the status is flipped and the Behaviour will report the new status in an `OutEvent`. pub struct Behaviour { // Local peer id local_peer_id: PeerId, diff --git a/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index 3ab4d33b748..f13e4d39c95 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.32.0 - Fix `NetworkBehaviour` Derive macro for generic types when `out_event` was not provided. Previously the enum generated - didn't have the `NetworkBehaviour` impl constraints whilst using the generics for `::ToBehaviour `. + didn't have the `NetworkBehaviour` impl constraints whilst using the generics for `::OutEvent`. See [PR 3393]. - Replace `NetworkBehaviour` Derive macro deprecated `inject_*` method implementations @@ -48,7 +48,7 @@ ## 0.29.0 -- Generate `NetworkBehaviour::ToBehaviour ` if not provided through `#[behaviour(out_event = +- Generate `NetworkBehaviour::OutEvent` if not provided through `#[behaviour(out_event = "MyOutEvent")]` and event processing is disabled (default). ## 0.28.0 diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index a652483fbb9..0279966a74a 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -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::ToBehaviour `] type, [`SwarmEvent`]s which are not [`SwarmEvent::Behaviour`] will be skipped until the [`Swarm`] returns a behaviour event. +/// If you match against your [`NetworkBehaviour::ToBehaviour`] 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. /// diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 301541117dc..6602b9a1a64 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,12 +1,11 @@ -## 0.43.0 - unreleased - -- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromSwarm`, `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToBehaviour`. - ## 0.42.2 - unreleased - Add `ConnectionEvent::{is_outbound,is_inbound}`. See [PR 3625]. +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromSwarm`, `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToBehaviour`. See [PR 3848]. + [PR 3625]: https://github.com/libp2p/rust-libp2p/pull/3625 +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.42.1 @@ -70,15 +69,15 @@ &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - - ) -> Poll> { - + ) -> Poll>> { + - ) -> Poll> { + + ) -> Poll>> { ``` In other words: |Search|Replace| |---|---| - |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| + |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| If you reference `NetworkBehaviourAction` somewhere else as well, you may have to fill in the type of `ConnectionHandler::InEvent` manually as the 2nd parameter. @@ -285,7 +284,7 @@ ## 0.38.0 - Deprecate `NetworkBehaviourEventProcess`. When deriving `NetworkBehaviour` on a custom `struct` users - should either bring their own `ToBehaviour ` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, + should either bring their own `OutEvent ` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, when not specified, have the derive macro generate one for the user. See [`NetworkBehaviour` @@ -353,8 +352,8 @@ ``` - When deriving `NetworkBehaviour` on a custom `struct` where the user does not specify their own - `ToBehaviour ` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable - `#[behaviour(event_process = true)]`, then the derive macro generates an `ToBehaviour ` definition for + `OutEvent ` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable + `#[behaviour(event_process = true)]`, then the derive macro generates an `OutEvent ` definition for the user. See [`NetworkBehaviour` @@ -531,7 +530,7 @@ trait parameters on `Swarm` (previously `ExpandedSwarm`), deriving parameters through associated types on `TBehaviour`. See [PR 2182]. -- Require `ProtocolsHandler::{InEvent,ToBehaviour ,Error}` to implement `Debug` (see +- Require `ProtocolsHandler::{InEvent,OutEvent ,Error}` to implement `Debug` (see [PR 2183]). - Implement `ProtocolsHandler` on `either::Either`representing either of two @@ -555,9 +554,9 @@ false. To emulate the previous behaviour, return early within `inject_dial_failure` on `DialError::DialPeerConditionFalse`. See [PR 2191]. -- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::ToBehaviour ` and +- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::OutEvent ` and `NetworkBehaviour::ProtocolsHandler`. In most cases, change your generic type - parameters to `NetworkBehaviourAction`. See [PR 2191]. - Return `bool` instead of `Result<(), ()>` for `Swarm::remove_listener`(see diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index a8403f139e7..820fabc3aac 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = "1.62.0" description = "The libp2p swarm" -version = "0.43.0" +version = "0.42.2" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 697c4c0c571..2e6cd4bf42f 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -81,7 +81,7 @@ use std::{task::Context, task::Poll}; /// it will delegate to each `struct` member and return a concatenated array of all addresses /// returned by the struct members. /// -/// Events ([`NetworkBehaviour::ToBehaviour `]) returned by each `struct` member are wrapped in a new +/// Events ([`NetworkBehaviour::ToBehaviour`]) 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 diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index cba7fa19a3a..2c8b4168cdd 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -525,17 +525,17 @@ fn custom_out_event_no_type_parameters() { } #[derive(NetworkBehaviour)] - #[behaviour(out_event = "ToBehaviour ", prelude = "libp2p_swarm::derive_prelude")] + #[behaviour(out_event = "OutEvent", prelude = "libp2p_swarm::derive_prelude")] struct Behaviour { custom: TemplatedBehaviour, } #[derive(Debug)] - enum ToBehaviour { + enum OutEvent { None, } - impl From for ToBehaviour { + impl From for OutEvent { fn from(_e: void::Void) -> Self { Self::None } From 978b422008e3eb0c92101c1d5d68818d9ab958ba Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 11:55:45 +0200 Subject: [PATCH 05/40] typo fix --- swarm-test/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm-test/src/lib.rs b/swarm-test/src/lib.rs index 0279966a74a..65f7adc2fa5 100644 --- a/swarm-test/src/lib.rs +++ b/swarm-test/src/lib.rs @@ -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::ToBehaviour`] 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. /// From 13f1ef419e10cc33530e125869910615c9dfbbbc Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 17:56:23 +0200 Subject: [PATCH 06/40] changelog revert --- core/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 9c532eef102..9cd4ca30de0 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -243,7 +243,7 @@ superflucious. Both are removed in favor of a derivation through `THandler` (see [PR 2183]). -- Require `ConnectionHandler::{InEvent,ToBehaviour ,Error}` to implement `Debug` +- Require `ConnectionHandler::{InEvent,OutEvent,Error}` to implement `Debug` (see [PR 2183]). - Remove `DisconnectedPeer::set_connected` and `Pool::add` (see [PR 2195]). From 11f039990a164615655335de24d835495f1eadd0 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 19:28:54 +0200 Subject: [PATCH 07/40] correcting typo in changelog --- swarm/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 6602b9a1a64..cf97de49174 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -2,7 +2,7 @@ - Add `ConnectionEvent::{is_outbound,is_inbound}`. See [PR 3625]. -- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromSwarm`, `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToBehaviour`. See [PR 3848]. +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. [PR 3625]: https://github.com/libp2p/rust-libp2p/pull/3625 [PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 From cf429c02f5ed51530a8cc697ca6de27d7ad21589 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 19:30:12 +0200 Subject: [PATCH 08/40] remove spurious spaces in changelog --- swarm/CHANGELOG.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index cf97de49174..57d5897ec73 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -69,15 +69,15 @@ &mut self, _: &mut Context<'_>, _: &mut impl PollParameters, - - ) -> Poll> { - + ) -> Poll>> { + - ) -> Poll> { + + ) -> Poll>> { ``` In other words: |Search|Replace| |---|---| - |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| + |`NetworkBehaviourAction`|`NetworkBehaviourAction>`| If you reference `NetworkBehaviourAction` somewhere else as well, you may have to fill in the type of `ConnectionHandler::InEvent` manually as the 2nd parameter. @@ -284,7 +284,7 @@ ## 0.38.0 - Deprecate `NetworkBehaviourEventProcess`. When deriving `NetworkBehaviour` on a custom `struct` users - should either bring their own `OutEvent ` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, + should either bring their own `OutEvent` via `#[behaviour(out_event = "MyBehaviourEvent")]` or, when not specified, have the derive macro generate one for the user. See [`NetworkBehaviour` @@ -352,8 +352,8 @@ ``` - When deriving `NetworkBehaviour` on a custom `struct` where the user does not specify their own - `OutEvent ` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable - `#[behaviour(event_process = true)]`, then the derive macro generates an `OutEvent ` definition for + `OutEvent` via `#[behaviour(out_event = "MyBehaviourEvent")]` and where the user does not enable + `#[behaviour(event_process = true)]`, then the derive macro generates an `OutEvent` definition for the user. See [`NetworkBehaviour` @@ -530,7 +530,7 @@ trait parameters on `Swarm` (previously `ExpandedSwarm`), deriving parameters through associated types on `TBehaviour`. See [PR 2182]. -- Require `ProtocolsHandler::{InEvent,OutEvent ,Error}` to implement `Debug` (see +- Require `ProtocolsHandler::{InEvent,OutEvent,Error}` to implement `Debug` (see [PR 2183]). - Implement `ProtocolsHandler` on `either::Either`representing either of two @@ -554,9 +554,9 @@ false. To emulate the previous behaviour, return early within `inject_dial_failure` on `DialError::DialPeerConditionFalse`. See [PR 2191]. -- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::OutEvent ` and +- Make `NetworkBehaviourAction` generic over `NetworkBehaviour::OutEvent` and `NetworkBehaviour::ProtocolsHandler`. In most cases, change your generic type - parameters to `NetworkBehaviourAction`. See [PR 2191]. - Return `bool` instead of `Result<(), ()>` for `Swarm::remove_listener`(see From 944ad04ea90789df2c2cff5c17ca512024e0b881 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Fri, 28 Apr 2023 19:40:31 +0200 Subject: [PATCH 09/40] change ConnectionHandler FromSwarm to FromBehaviour --- protocols/dcutr/src/handler/direct.rs | 4 ++-- protocols/dcutr/src/handler/relayed.rs | 4 ++-- protocols/gossipsub/src/handler.rs | 2 +- protocols/identify/src/handler.rs | 4 ++-- protocols/kad/src/handler_priv.rs | 2 +- protocols/perf/src/client/handler.rs | 4 ++-- protocols/perf/src/server/handler.rs | 4 ++-- protocols/ping/src/handler.rs | 2 +- protocols/relay/src/behaviour/handler.rs | 4 ++-- protocols/relay/src/priv_client/handler.rs | 4 ++-- protocols/rendezvous/src/substream_handler.rs | 4 ++-- protocols/request-response/src/handler.rs | 4 ++-- swarm/src/behaviour/toggle.rs | 4 ++-- swarm/src/connection.rs | 6 +++--- swarm/src/connection/pool.rs | 8 +++++--- swarm/src/connection/pool/task.rs | 2 +- swarm/src/dummy.rs | 4 ++-- swarm/src/handler.rs | 10 +++++----- swarm/src/handler/either.rs | 4 ++-- swarm/src/handler/map_in.rs | 4 ++-- swarm/src/handler/map_out.rs | 4 ++-- swarm/src/handler/multi.rs | 4 ++-- swarm/src/handler/one_shot.rs | 4 ++-- swarm/src/handler/pending.rs | 4 ++-- swarm/src/handler/select.rs | 4 ++-- swarm/src/keep_alive.rs | 4 ++-- swarm/src/lib.rs | 4 ++-- 27 files changed, 57 insertions(+), 55 deletions(-) diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index 8e79db11b53..58e168c0343 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -40,7 +40,7 @@ pub struct Handler { } impl ConnectionHandler for Handler { - type FromSwarm = void::Void; + type FromBehaviour = void::Void; type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = DeniedUpgrade; @@ -52,7 +52,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, _: Self::FromSwarm) {} + fn on_behaviour_event(&mut self, _: Self::FromBehaviour) {} fn connection_keep_alive(&self) -> KeepAlive { KeepAlive::No diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 1b7a3aef768..bf9bcb9a465 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -294,7 +294,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type FromSwarm = Command; + type FromBehaviour = Command; type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -320,7 +320,7 @@ impl ConnectionHandler for Handler { } } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { match event { Command::Connect { obs_addrs, attempt } => { self.queued_events diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 6e995f581a2..6f78c31ef38 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -392,7 +392,7 @@ impl EnabledHandler { } impl ConnectionHandler for Handler { - type FromSwarm = HandlerIn; + type FromBehaviour = HandlerIn; type ToBehaviour = HandlerEvent; type Error = Void; type InboundOpenInfo = (); diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index d18e54ad8bf..5156e75c5fe 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -222,7 +222,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type FromSwarm = InEvent; + type FromBehaviour = InEvent; type ToBehaviour = Event; type Error = io::Error; type InboundProtocol = SelectUpgrade>; @@ -240,7 +240,7 @@ impl ConnectionHandler for Handler { listen_addrs, supported_protocols, protocol, - }: Self::FromSwarm, + }: Self::FromBehaviour, ) { let info = Info { public_key: self.public_key.clone(), diff --git a/protocols/kad/src/handler_priv.rs b/protocols/kad/src/handler_priv.rs index 689bfad69ff..41b96a3fe0f 100644 --- a/protocols/kad/src/handler_priv.rs +++ b/protocols/kad/src/handler_priv.rs @@ -609,7 +609,7 @@ impl ConnectionHandler for KademliaHandler where TUserData: Clone + fmt::Debug + Send + 'static + Unpin, { - type FromSwarm = KademliaHandlerIn; + type FromBehaviour = KademliaHandlerIn; type ToBehaviour = KademliaHandlerEvent; type Error = io::Error; // TODO: better error type? type InboundProtocol = Either; diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 9fd4995ac94..7d590f3445f 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -83,7 +83,7 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type FromSwarm = Command; + type FromBehaviour = Command; type ToBehaviour = Event; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -95,7 +95,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, command: Self::FromSwarm) { + fn on_behaviour_event(&mut self, command: Self::FromBehaviour) { self.queued_events .push_back(ConnectionHandlerEvent::OutboundSubstreamRequest { protocol: SubstreamProtocol::new(ReadyUpgrade::new(crate::PROTOCOL_NAME), command), diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index d60a37577e3..1800571a6cd 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -64,7 +64,7 @@ impl Default for Handler { } impl ConnectionHandler for Handler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = Event; type Error = Void; type InboundProtocol = ReadyUpgrade<&'static [u8]>; @@ -76,7 +76,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(ReadyUpgrade::new(crate::PROTOCOL_NAME), ()) } - fn on_behaviour_event(&mut self, v: Self::FromSwarm) { + fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { void::unreachable(v) } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 396ba134b4b..d6365df59aa 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -254,7 +254,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = crate::Result; type Error = Failure; type InboundProtocol = ReadyUpgrade<&'static [u8]>; diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index 8e707b640f2..e83bd0b1390 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -598,7 +598,7 @@ enum ReservationRequestFuture { type Futures = FuturesUnordered>; impl ConnectionHandler for Handler { - type FromSwarm = In; + type FromBehaviour = In; type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -619,7 +619,7 @@ impl ConnectionHandler for Handler { ) } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { match event { In::AcceptReservationReq { inbound_reservation_req, diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index a4ba55b3312..a1348789ec6 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -462,7 +462,7 @@ impl Handler { } impl ConnectionHandler for Handler { - type FromSwarm = In; + type FromBehaviour = In; type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr< Either, @@ -476,7 +476,7 @@ impl ConnectionHandler for Handler { SubstreamProtocol::new(inbound_stop::Upgrade {}, ()) } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { match event { In::Reserve { to_listener } => { self.queued_events diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index 8bbed1f8590..f6b4bd2ad2e 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -354,7 +354,7 @@ where TInboundSubstreamHandler: Send + 'static, TOutboundSubstreamHandler: Send + 'static, { - type FromSwarm = InEvent; + type FromBehaviour = InEvent; type ToBehaviour = OutEvent; type Error = Void; type InboundProtocol = PassthroughProtocol; @@ -400,7 +400,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + 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 } => { diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index 388bfcd47a6..f3ef7aeb9f6 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -282,7 +282,7 @@ impl ConnectionHandler for Handler where TCodec: Codec + Send + Clone + 'static, { - type FromSwarm = RequestProtocol; + type FromBehaviour = RequestProtocol; type ToBehaviour = Event; type Error = ConnectionHandlerUpgrErr; type InboundProtocol = ResponseProtocol; @@ -324,7 +324,7 @@ where SubstreamProtocol::new(proto, request_id).with_timeout(self.substream_timeout) } - fn on_behaviour_event(&mut self, request: Self::FromSwarm) { + fn on_behaviour_event(&mut self, request: Self::FromBehaviour) { self.keep_alive = KeepAlive::Yes; self.outbound.push_back(request); } diff --git a/swarm/src/behaviour/toggle.rs b/swarm/src/behaviour/toggle.rs index 9596f85b6b1..c1aee260de2 100644 --- a/swarm/src/behaviour/toggle.rs +++ b/swarm/src/behaviour/toggle.rs @@ -273,7 +273,7 @@ impl ConnectionHandler for ToggleConnectionHandler where TInner: ConnectionHandler, { - type FromSwarm = TInner::FromSwarm; + type FromBehaviour = TInner::FromBehaviour; type ToBehaviour = TInner::ToBehaviour; type Error = TInner::Error; type InboundProtocol = Either, SendWrapper>; @@ -292,7 +292,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { self.inner .as_mut() .expect("Can't receive events if disabled; QED") diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index b062fb967af..b37b4e62203 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -186,7 +186,7 @@ where } /// Notifies the connection handler of an event. - pub(crate) fn on_behaviour_event(&mut self, event: THandler::FromSwarm) { + pub(crate) fn on_behaviour_event(&mut self, event: THandler::FromBehaviour) { self.handler.on_behaviour_event(event); } @@ -791,7 +791,7 @@ mod tests { } impl ConnectionHandler for MockConnectionHandler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -830,7 +830,7 @@ mod tests { } } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { void::unreachable(event) } diff --git a/swarm/src/connection/pool.rs b/swarm/src/connection/pool.rs index a8bca563510..bed844e8f06 100644 --- a/swarm/src/connection/pool.rs +++ b/swarm/src/connection/pool.rs @@ -94,8 +94,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, @@ -343,7 +345,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 e05a91a3f83..175da668bda 100644 --- a/swarm/src/connection/pool/task.rs +++ b/swarm/src/connection/pool/task.rs @@ -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 be7c780349e..ab79357c5ae 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -81,7 +81,7 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -93,7 +93,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { void::unreachable(event) } diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index ec81f71655b..48597690021 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -94,7 +94,7 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; /// 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 FromSwarm: fmt::Debug + Send + 'static; + type FromBehaviour: fmt::Debug + Send + 'static; /// Custom event that can be produced by the handler and that will be returned to the outside. type ToBehaviour: fmt::Debug + Send + 'static; /// The type of errors returned by [`ConnectionHandler::poll`]. @@ -147,7 +147,7 @@ pub trait ConnectionHandler: Send + 'static { ConnectionHandlerEvent< Self::OutboundProtocol, Self::OutboundOpenInfo, - Self::ToBehaviour , + Self::ToBehaviour, Self::Error, >, >; @@ -156,7 +156,7 @@ pub trait ConnectionHandler: Send + 'static { fn map_in_event(self, map: TMap) -> MapInEvent where Self: Sized, - TMap: Fn(&TNewIn) -> Option<&Self::FromSwarm>, + TMap: Fn(&TNewIn) -> Option<&Self::FromBehaviour>, { MapInEvent::new(self, map) } @@ -165,7 +165,7 @@ pub trait ConnectionHandler: Send + 'static { fn map_out_event(self, map: TMap) -> MapOutEvent where Self: Sized, - TMap: FnMut(Self::ToBehaviour ) -> TNewOut, + TMap: FnMut(Self::ToBehaviour) -> TNewOut, { MapOutEvent::new(self, map) } @@ -184,7 +184,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::FromSwarm); + 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 5c417ebd438..5bd860ed266 100644 --- a/swarm/src/handler/either.rs +++ b/swarm/src/handler/either.rs @@ -192,7 +192,7 @@ where L: ConnectionHandler, R: ConnectionHandler, { - type FromSwarm = Either; + type FromBehaviour = Either; type ToBehaviour = Either; type Error = Either; type InboundProtocol = Either, SendWrapper>; @@ -214,7 +214,7 @@ where } } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + 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), diff --git a/swarm/src/handler/map_in.rs b/swarm/src/handler/map_in.rs index 7c6968b8f63..af828999b03 100644 --- a/swarm/src/handler/map_in.rs +++ b/swarm/src/handler/map_in.rs @@ -45,11 +45,11 @@ impl ConnectionHandler for MapInEvent where TConnectionHandler: ConnectionHandler, - TMap: Fn(TNewIn) -> Option, + TMap: Fn(TNewIn) -> Option, TNewIn: Debug + Send + 'static, TMap: Send + 'static, { - type FromSwarm = TNewIn; + type FromBehaviour = TNewIn; type ToBehaviour = TConnectionHandler::ToBehaviour; type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index 01ec81c01cf..918e4b171c1 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -44,7 +44,7 @@ where TNewOut: Debug + Send + 'static, TMap: Send + 'static, { - type FromSwarm = TConnectionHandler::FromSwarm; + type FromBehaviour = TConnectionHandler::FromBehaviour; type ToBehaviour = TNewOut; type Error = TConnectionHandler::Error; type InboundProtocol = TConnectionHandler::InboundProtocol; @@ -56,7 +56,7 @@ where self.inner.listen_protocol() } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { self.inner.on_behaviour_event(event) } diff --git a/swarm/src/handler/multi.rs b/swarm/src/handler/multi.rs index 230bdc30394..e7c32353ae1 100644 --- a/swarm/src/handler/multi.rs +++ b/swarm/src/handler/multi.rs @@ -223,7 +223,7 @@ where H::InboundProtocol: InboundUpgradeSend, H::OutboundProtocol: OutboundUpgradeSend, { - type FromSwarm = (K, ::FromSwarm); + type FromBehaviour = (K, ::FromBehaviour); type ToBehaviour = (K, ::ToBehaviour); type Error = ::Error; type InboundProtocol = Upgrade::InboundProtocol>; @@ -321,7 +321,7 @@ where } } - fn on_behaviour_event(&mut self, (key, event): Self::FromSwarm) { + 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 { diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index e6cd638865c..963ebb5ef2d 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -121,7 +121,7 @@ where SubstreamProtocol: Clone, TEvent: Debug + Send + 'static, { - type FromSwarm = TOutbound; + type FromBehaviour = TOutbound; type ToBehaviour = TEvent; type Error = ConnectionHandlerUpgrErr<::Error>; type InboundProtocol = TInbound; @@ -133,7 +133,7 @@ where self.listen_protocol.clone() } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + fn on_behaviour_event(&mut self, event: Self::FromBehaviour) { self.send_request(event); } diff --git a/swarm/src/handler/pending.rs b/swarm/src/handler/pending.rs index c4527cbe886..3ea731f1adf 100644 --- a/swarm/src/handler/pending.rs +++ b/swarm/src/handler/pending.rs @@ -40,7 +40,7 @@ impl PendingConnectionHandler { } impl ConnectionHandler for PendingConnectionHandler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = Void; type Error = Void; type InboundProtocol = 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::FromSwarm) { + fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { void::unreachable(v) } diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 3becd9bd39a..887588e2064 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -339,7 +339,7 @@ where TProto1: ConnectionHandler, TProto2: ConnectionHandler, { - type FromSwarm = Either; + type FromBehaviour = Either; type ToBehaviour = Either; type Error = Either; type InboundProtocol = SelectUpgrade< @@ -361,7 +361,7 @@ where SubstreamProtocol::new(choice, (i1, i2)).with_timeout(timeout) } - fn on_behaviour_event(&mut self, event: Self::FromSwarm) { + 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), diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index 404ad404787..d90ca2e7f69 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -84,7 +84,7 @@ impl NetworkBehaviour for Behaviour { pub struct ConnectionHandler; impl crate::handler::ConnectionHandler for ConnectionHandler { - type FromSwarm = Void; + type FromBehaviour = Void; type ToBehaviour = Void; type Error = Void; type InboundProtocol = DeniedUpgrade; @@ -96,7 +96,7 @@ impl crate::handler::ConnectionHandler for ConnectionHandler { SubstreamProtocol::new(DeniedUpgrade, ()) } - fn on_behaviour_event(&mut self, v: Self::FromSwarm) { + fn on_behaviour_event(&mut self, v: Self::FromBehaviour) { void::unreachable(v) } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 72411af4353..4de64ad2207 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -177,7 +177,7 @@ pub type THandler = /// Custom event that can be received by the [`ConnectionHandler`] of the /// [`NetworkBehaviour`]. -pub type THandlerInEvent = as ConnectionHandler>::FromSwarm; +pub type THandlerInEvent = as ConnectionHandler>::FromBehaviour; /// Custom event that can be produced by the [`ConnectionHandler`] of the [`NetworkBehaviour`]. pub type THandlerOutEvent = as ConnectionHandler>::ToBehaviour; @@ -1426,7 +1426,7 @@ fn notify_any( where TBehaviour: NetworkBehaviour, THandler: ConnectionHandler< - FromSwarm = THandlerInEvent, + FromBehaviour = THandlerInEvent, ToBehaviour = THandlerOutEvent, >, { From c77045c5abd8897a1e4fbe0b3d4a38fd70c29e83 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 19:54:47 +0200 Subject: [PATCH 10/40] add changelog in libp2p crate --- Cargo.lock | 2 +- libp2p/CHANGELOG.md | 6 ++++++ libp2p/Cargo.toml | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 890eb5583ac..224a4dc5dec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2272,7 +2272,7 @@ checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" [[package]] name = "libp2p" -version = "0.51.3" +version = "0.51.4" dependencies = [ "async-std", "async-trait", diff --git a/libp2p/CHANGELOG.md b/libp2p/CHANGELOG.md index a76b7b45ce7..7b3a1cce746 100644 --- a/libp2p/CHANGELOG.md +++ b/libp2p/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.51.4 - unreleased + +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. + +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 + ## 0.51.3 - Deprecate the `mplex` feature. diff --git a/libp2p/Cargo.toml b/libp2p/Cargo.toml index f979366b71a..7c0ecb08fbf 100644 --- a/libp2p/Cargo.toml +++ b/libp2p/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p" edition = "2021" rust-version = "1.65.0" description = "Peer-to-peer networking library" -version = "0.51.3" +version = "0.51.4" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" From 18c808a533809ec4a3f92a3c4629ba7aa5a864f9 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 20:04:35 +0200 Subject: [PATCH 11/40] update doc of FromBehaviour and ToBehaviour in ConnectionHandler --- swarm/src/handler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 48597690021..eb8b9e02e0d 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -93,9 +93,9 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; /// 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. + /// `FromBehaviour` event that can be received from the outside. type FromBehaviour: fmt::Debug + Send + 'static; - /// Custom event that can be produced by the handler and that will be returned to the outside. + /// `ToBehaviour` event that can be produced by the handler and that will be returned to the outside. type ToBehaviour: fmt::Debug + Send + 'static; /// The type of errors returned by [`ConnectionHandler::poll`]. type Error: error::Error + fmt::Debug + Send + 'static; From 3b53770ab0efee7b1d5c6af540a738a38bde5071 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 20:39:17 +0200 Subject: [PATCH 12/40] Rename out_event to to_swarm and deprecate out_event --- Cargo.lock | 3 ++- examples/dcutr/src/main.rs | 2 +- examples/distributed-key-value-store/src/main.rs | 2 +- examples/file-sharing/src/network.rs | 2 +- examples/ipfs-private/src/main.rs | 2 +- protocols/dcutr/tests/lib.rs | 2 +- swarm-derive/CHANGELOG.md | 7 +++++++ swarm-derive/Cargo.toml | 3 ++- swarm-derive/src/lib.rs | 8 ++++++-- swarm/src/behaviour.rs | 8 ++++---- swarm/tests/swarm_derive.rs | 12 ++++++------ transports/webrtc/examples/listen_ping.rs | 2 +- 12 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 224a4dc5dec..d5468f83467 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2928,9 +2928,10 @@ dependencies = [ [[package]] name = "libp2p-swarm-derive" -version = "0.32.0" +version = "0.32.1" dependencies = [ "heck", + "log", "quote", "syn 2.0.15", ] 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/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 64fad09774e..5eac053ffcf 100644 --- a/examples/file-sharing/src/network.rs +++ b/examples/file-sharing/src/network.rs @@ -404,7 +404,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/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/swarm-derive/CHANGELOG.md b/swarm-derive/CHANGELOG.md index f13e4d39c95..3350a8c3f3a 100644 --- a/swarm-derive/CHANGELOG.md +++ b/swarm-derive/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.32.1 - unreleased + +- Rename `out_event` to `to_swarm` and deprecate `out_event`. See [PR 3848]. + +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 + + ## 0.32.0 - Fix `NetworkBehaviour` Derive macro for generic types when `out_event` was not provided. Previously the enum generated diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 31f70a01b5b..ba7b7725c08 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive" edition = "2021" rust-version = "1.60.0" description = "Procedural macros of libp2p-swarm" -version = "0.32.0" +version = "0.32.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" @@ -17,6 +17,7 @@ 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"] } +log = "0.4" # 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 1da05418c4a..202bf9c55a7 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -96,7 +96,7 @@ 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 + // 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 { @@ -896,7 +896,11 @@ fn parse_attributes(ast: &DeriveInput) -> Result unimplemented!(), Meta::List(_) => unimplemented!(), diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 2e6cd4bf42f..99feacaf7df 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -83,12 +83,12 @@ use std::{task::Context, task::Poll}; /// /// Events ([`NetworkBehaviour::ToBehaviour`]) 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 @@ -96,7 +96,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, diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 2c8b4168cdd..791ecef9fac 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -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, @@ -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 { @@ -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, @@ -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, } 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" )] From 5d1f63a50c560abbafc21ef1ef90e8851c4926ec Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 20:55:18 +0200 Subject: [PATCH 13/40] clean up waste --- Cargo.lock | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 30fe4962426..5d9c6bea94f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2272,11 +2272,7 @@ checksum = "7fc7aa29613bd6a620df431842069224d8bc9011086b1db4c0e0cd47fa03ec9a" [[package]] name = "libp2p" -<<<<<<< HEAD -version = "0.51.4" -======= version = "0.52.0" ->>>>>>> origin/master dependencies = [ "async-std", "async-trait", From 736390a83958b983bb81182fa6ef62017205950e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 21:30:36 +0200 Subject: [PATCH 14/40] fix rustdoc link in swarm behaviour --- swarm/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 99feacaf7df..437bd607a8d 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -81,7 +81,7 @@ use std::{task::Context, task::Poll}; /// it will delegate to each `struct` member and return a concatenated array of all addresses /// returned by the struct members. /// -/// Events ([`NetworkBehaviour::ToBehaviour`]) 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(to_swarm = /// "MyCustomOutEvent")]`. If the user does not specify an `to_swarm`, the derive macro generates From 9cdfc150d63d1ce44525aa108aed4d31a7cc3d73 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 21:39:46 +0200 Subject: [PATCH 15/40] fix some ToBehaviour to ToSwarm in swarm_derive.rs --- swarm/tests/swarm_derive.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 791ecef9fac..b098ff94013 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: ::ToBehaviour = 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: ::ToBehaviour = 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: ::ToBehaviour = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { FooEvent::Ping(ping::Event { .. }) => {} FooEvent::Identify(event) => { @@ -196,7 +196,7 @@ fn bound() { #[behaviour(prelude = "libp2p_swarm::derive_prelude")] struct Foo where - ::ToBehaviour: Debug, + ::ToSwarm: Debug, { ping: ping::Behaviour, bar: T, @@ -211,7 +211,7 @@ fn where_clause() { struct Foo where T: Copy + NetworkBehaviour, - ::ToBehaviour: 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: ::ToBehaviour = unimplemented!(); + let _out_event: ::ToSwarm = unimplemented!(); match _out_event { BarEvent::Foo(FooEvent::Ping(ping::Event { .. })) => {} } @@ -467,7 +467,7 @@ fn custom_out_event_no_type_parameters() { impl NetworkBehaviour for TemplatedBehaviour { type ConnectionHandler = dummy::ConnectionHandler; - type ToBehaviour = void::Void; + type ToSwarm = void::Void; fn handle_established_inbound_connection( &mut self, From dc3e1a296ad384a81dacd9827f16018a88a21f13 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Tue, 2 May 2023 21:55:19 +0200 Subject: [PATCH 16/40] Update swarm/src/handler.rs Co-authored-by: Thomas Eizinger --- swarm/src/handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index eb8b9e02e0d..9c68d34de44 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -93,7 +93,7 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; /// 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 { - /// `FromBehaviour` event that can be received from the outside. + /// A type representing the message(s) a [`NetworkBehaviour`] can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`] type FromBehaviour: fmt::Debug + Send + 'static; /// `ToBehaviour` event that can be produced by the handler and that will be returned to the outside. type ToBehaviour: fmt::Debug + Send + 'static; From add36daca584fcf15f7376d7509a2858c97521fb Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Tue, 2 May 2023 21:55:52 +0200 Subject: [PATCH 17/40] Update swarm/src/handler.rs Co-authored-by: Thomas Eizinger --- swarm/src/handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 9c68d34de44..775e7afef4c 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -95,7 +95,7 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; pub trait ConnectionHandler: Send + 'static { /// A type representing the message(s) a [`NetworkBehaviour`] can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`] type FromBehaviour: fmt::Debug + Send + 'static; - /// `ToBehaviour` event that can be produced by the handler and that will be returned to the outside. + /// A type representing message(s) a [`ConnectionHandler`] can send to a [`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; From 7fe31ef13a2432d8185d9508909e82fb99a9f961 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 22:01:26 +0200 Subject: [PATCH 18/40] rename ConnectionHandlerEvent::Custom to ConnectionHandlerEvent::NotifyBehaviour --- protocols/dcutr/src/handler/direct.rs | 2 +- protocols/dcutr/src/handler/relayed.rs | 89 ++++++------ protocols/gossipsub/src/handler.rs | 14 +- protocols/identify/src/handler.rs | 25 ++-- protocols/kad/src/handler_priv.rs | 48 ++++--- protocols/perf/src/client/handler.rs | 4 +- protocols/perf/src/server/handler.rs | 4 +- protocols/ping/src/handler.rs | 14 +- protocols/relay/src/behaviour/handler.rs | 133 ++++++++++-------- protocols/relay/src/priv_client/handler.rs | 53 +++---- protocols/rendezvous/src/substream_handler.rs | 28 ++-- protocols/request-response/src/handler.rs | 4 +- swarm/src/connection.rs | 2 +- swarm/src/handler.rs | 20 ++- swarm/src/handler/map_out.rs | 4 +- swarm/src/handler/one_shot.rs | 4 +- swarm/src/handler/select.rs | 10 +- 17 files changed, 253 insertions(+), 205 deletions(-) diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index 58e168c0343..3993649d881 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -71,7 +71,7 @@ impl ConnectionHandler for Handler { > { if !self.reported { self.reported = true; - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::DirectConnectionEstablished, )); } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index 02572da738a..15a515e4e26 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -172,12 +172,13 @@ impl Handler { ConnectedPoint::Dialer { address, role_override: _ } => address.clone(), ConnectedPoint::Listener { ..} => unreachable!("`::listen_protocol` denies all incoming substreams as a listener."), }; - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundConnectRequest { - inbound_connect: Box::new(inbound_connect), - remote_addr, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundConnectRequest { + inbound_connect: Box::new(inbound_connect), + remote_addr, + }, + )); } // A connection listener denies all incoming substreams, thus none can ever be fully negotiated. future::Either::Right(output) => void::unreachable(output), @@ -198,11 +199,12 @@ impl Handler { self.endpoint.is_listener(), "A connection dialer never initiates a connection upgrade." ); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundConnectNegotiated { - remote_addrs: obs_addrs, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundConnectNegotiated { + remote_addrs: obs_addrs, + }, + )); } fn on_listen_upgrade_error( @@ -214,31 +216,34 @@ impl Handler { ) { match error { ConnectionHandlerUpgrErr::Timeout => { - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundNegotiationFailed { - error: ConnectionHandlerUpgrErr::Timeout, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundNegotiationFailed { + error: ConnectionHandlerUpgrErr::Timeout, + }, + )); } ConnectionHandlerUpgrErr::Timer => { - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundNegotiationFailed { - error: ConnectionHandlerUpgrErr::Timer, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundNegotiationFailed { + error: ConnectionHandlerUpgrErr::Timer, + }, + )); } ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => { // The remote merely doesn't support the DCUtR protocol. // This is no reason to close the connection, which may // successfully communicate with other protocols already. self.keep_alive = KeepAlive::No; - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundNegotiationFailed { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select( - NegotiationError::Failed, - )), - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundNegotiationFailed { + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select( + NegotiationError::Failed, + )), + }, + )); } _ => { // Anything else is considered a fatal error or misbehaviour of @@ -264,23 +269,25 @@ impl Handler { match error { ConnectionHandlerUpgrErr::Timeout => { - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundNegotiationFailed { - error: ConnectionHandlerUpgrErr::Timeout, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundNegotiationFailed { + error: ConnectionHandlerUpgrErr::Timeout, + }, + )); } ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => { // The remote merely doesn't support the DCUtR protocol. // This is no reason to close the connection, which may // successfully communicate with other protocols already. - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundNegotiationFailed { - error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select( - NegotiationError::Failed, - )), - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundNegotiationFailed { + error: ConnectionHandlerUpgrErr::Upgrade(UpgradeError::Select( + NegotiationError::Failed, + )), + }, + )); } _ => { // Anything else is considered a fatal error or misbehaviour of @@ -380,7 +387,7 @@ impl ConnectionHandler for Handler { self.inbound_connect = None; match result { Ok(addresses) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::InboundConnectNegotiated(addresses), )); } diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index 7c790f62706..c06fc115e64 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -231,9 +231,9 @@ impl EnabledHandler { if !self.peer_kind_sent { if let Some(peer_kind) = self.peer_kind.as_ref() { self.peer_kind_sent = true; - return Poll::Ready(ConnectionHandlerEvent::Custom(HandlerEvent::PeerKind( - peer_kind.clone(), - ))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + HandlerEvent::PeerKind(peer_kind.clone()), + )); } } @@ -260,7 +260,7 @@ impl EnabledHandler { self.last_io_activity = Instant::now(); self.inbound_substream = Some(InboundSubstreamState::WaitingInput(substream)); - return Poll::Ready(ConnectionHandlerEvent::Custom(message)); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(message)); } Poll::Ready(Some(Err(error))) => { log::debug!("Failed to read from inbound stream: {error}"); @@ -465,9 +465,9 @@ impl ConnectionHandler for Handler { Handler::Disabled(DisabledHandler::ProtocolUnsupported { peer_kind_sent }) => { if !*peer_kind_sent { *peer_kind_sent = true; - return Poll::Ready(ConnectionHandlerEvent::Custom(HandlerEvent::PeerKind( - PeerKind::NotSupported, - ))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + HandlerEvent::PeerKind(PeerKind::NotSupported), + )); } Poll::Pending diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index 5156e75c5fe..1c50177202d 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -153,7 +153,7 @@ impl Handler { match output { future::Either::Left(substream) => { self.events - .push(ConnectionHandlerEvent::Custom(Event::Identify)); + .push(ConnectionHandlerEvent::NotifyBehaviour(Event::Identify)); if !self.reply_streams.is_empty() { warn!( "New inbound identify request from {} while a previous one \ @@ -187,14 +187,14 @@ impl Handler { match output { future::Either::Left(remote_info) => { self.events - .push(ConnectionHandlerEvent::Custom(Event::Identified( + .push(ConnectionHandlerEvent::NotifyBehaviour(Event::Identified( remote_info, ))); self.keep_alive = KeepAlive::No; } - future::Either::Right(()) => self - .events - .push(ConnectionHandlerEvent::Custom(Event::IdentificationPushed)), + future::Either::Right(()) => self.events.push(ConnectionHandlerEvent::NotifyBehaviour( + Event::IdentificationPushed, + )), } } @@ -212,10 +212,9 @@ impl Handler { UpgradeError::Apply(Either::Left(ioe)) => UpgradeError::Apply(ioe), UpgradeError::Apply(Either::Right(ioe)) => UpgradeError::Apply(ioe), }); - self.events - .push(ConnectionHandlerEvent::Custom(Event::IdentificationError( - err, - ))); + self.events.push(ConnectionHandlerEvent::NotifyBehaviour( + Event::IdentificationError(err), + )); self.keep_alive = KeepAlive::No; self.trigger_next_identify.reset(self.interval); } @@ -307,16 +306,18 @@ impl ConnectionHandler for Handler { self.inbound_identify_push.take(); if let Ok(info) = res { - return Poll::Ready(ConnectionHandlerEvent::Custom(Event::Identified(info))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event::Identified( + info, + ))); } } // Check for pending replies to send. match self.pending_replies.poll_next_unpin(cx) { - Poll::Ready(Some(Ok(peer_id))) => Poll::Ready(ConnectionHandlerEvent::Custom( + Poll::Ready(Some(Ok(peer_id))) => Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::Identification(peer_id), )), - Poll::Ready(Some(Err(err))) => Poll::Ready(ConnectionHandlerEvent::Custom( + Poll::Ready(Some(Err(err))) => Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::IdentificationError(ConnectionHandlerUpgrErr::Upgrade( libp2p_core::upgrade::UpgradeError::Apply(err), )), diff --git a/protocols/kad/src/handler_priv.rs b/protocols/kad/src/handler_priv.rs index 164071f0c85..60d77b9a262 100644 --- a/protocols/kad/src/handler_priv.rs +++ b/protocols/kad/src/handler_priv.rs @@ -719,7 +719,7 @@ where > { if let ProtocolStatus::Confirmed = self.protocol_status { self.protocol_status = ProtocolStatus::Reported; - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::ProtocolConfirmed { endpoint: self.endpoint.clone(), }, @@ -835,7 +835,7 @@ where Err(error) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::Custom( + ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::QueryError { error: KademliaHandlerQueryErr::Io(error), user_data, @@ -853,10 +853,12 @@ where Poll::Ready(Err(error)) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError { - error: KademliaHandlerQueryErr::Io(error), - user_data, - }) + ConnectionHandlerEvent::NotifyBehaviour( + KademliaHandlerEvent::QueryError { + error: KademliaHandlerQueryErr::Io(error), + user_data, + }, + ) }); return Poll::Ready(event); @@ -879,10 +881,12 @@ where Poll::Ready(Err(error)) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError { - error: KademliaHandlerQueryErr::Io(error), - user_data, - }) + ConnectionHandlerEvent::NotifyBehaviour( + KademliaHandlerEvent::QueryError { + error: KademliaHandlerQueryErr::Io(error), + user_data, + }, + ) }); return Poll::Ready(event); @@ -895,7 +899,9 @@ where *this = OutboundSubstreamState::Closing(substream); let event = process_kad_response(msg, user_data); - return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + event, + ))); } Poll::Pending => { *this = OutboundSubstreamState::WaitingAnswer(substream, user_data); @@ -908,7 +914,9 @@ where user_data, }; - return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + event, + ))); } Poll::Ready(None) => { *this = OutboundSubstreamState::Done; @@ -919,7 +927,9 @@ where user_data, }; - return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + event, + ))); } } } @@ -927,7 +937,7 @@ where *this = OutboundSubstreamState::Done; let event = KademliaHandlerEvent::QueryError { error, user_data }; - return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour(event))); } OutboundSubstreamState::Closing(mut stream) => match stream.poll_close_unpin(cx) { Poll::Ready(Ok(())) | Poll::Ready(Err(_)) => return Poll::Ready(None), @@ -980,7 +990,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::FindNode { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::Custom( + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::FindNodeReq { key, request_id: KademliaRequestId { @@ -992,7 +1002,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::GetProviders { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::Custom( + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::GetProvidersReq { key, request_id: KademliaRequestId { @@ -1007,14 +1017,14 @@ where connection_id, substream, }; - return Poll::Ready(Some(ConnectionHandlerEvent::Custom( + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::AddProvider { key, provider }, ))); } Poll::Ready(Some(Ok(KadRequestMsg::GetValue { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::Custom( + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::GetRecord { key, request_id: KademliaRequestId { @@ -1026,7 +1036,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::PutValue { record }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::Custom( + return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( KademliaHandlerEvent::PutRecord { record, request_id: KademliaRequestId { diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 64208aa3b3d..d7626a0cbb0 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -144,7 +144,7 @@ impl ConnectionHandler for Handler { .pop_front() .expect("requested stream without pending command"); self.queued_events - .push_back(ConnectionHandlerEvent::Custom(Event { + .push_back(ConnectionHandlerEvent::NotifyBehaviour(Event { id, result: Err(error), })); @@ -184,7 +184,7 @@ impl ConnectionHandler for Handler { while let Poll::Ready(Some(result)) = self.outbound.poll_next_unpin(cx) { match result { - Ok(event) => return Poll::Ready(ConnectionHandlerEvent::Custom(event)), + Ok(event) => return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)), Err(e) => { panic!("{e:?}") } diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index 1800571a6cd..e9d526631cd 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -135,7 +135,9 @@ impl ConnectionHandler for Handler { > { while let Poll::Ready(Some(result)) = self.inbound.poll_next_unpin(cx) { match result { - Ok(stats) => return Poll::Ready(ConnectionHandlerEvent::Custom(Event { stats })), + Ok(stats) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event { stats })) + } Err(e) => { error!("{e:?}") } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index d6365df59aa..e316345c809 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -287,7 +287,9 @@ impl ConnectionHandler for Handler { } State::Inactive { reported: false } => { self.state = State::Inactive { reported: true }; - return Poll::Ready(ConnectionHandlerEvent::Custom(Err(Failure::Unsupported))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err( + Failure::Unsupported, + ))); } State::Active => {} } @@ -303,7 +305,7 @@ impl ConnectionHandler for Handler { Poll::Ready(Ok(stream)) => { // A ping from a remote peer has been answered, wait for the next. self.inbound = Some(protocol::recv_ping(stream).boxed()); - return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Pong))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok(Success::Pong))); } } } @@ -328,7 +330,7 @@ impl ConnectionHandler for Handler { return Poll::Ready(ConnectionHandlerEvent::Close(error)); } - return Poll::Ready(ConnectionHandlerEvent::Custom(Err(error))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(error))); } } @@ -347,9 +349,9 @@ impl ConnectionHandler for Handler { self.failures = 0; self.timer.reset(self.config.interval); self.outbound = Some(OutboundState::Idle(stream)); - return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping { - rtt, - }))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok( + Success::Ping { rtt }, + ))); } Poll::Ready(Err(e)) => { self.pending_errors diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index e83bd0b1390..423f52d904e 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -418,21 +418,23 @@ impl Handler { ) { match request { inbound_hop::Req::Reserve(inbound_reservation_req) => { - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::ReservationReqReceived { - inbound_reservation_req, - endpoint: self.endpoint.clone(), - renewed: self.active_reservation.is_some(), - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::ReservationReqReceived { + inbound_reservation_req, + endpoint: self.endpoint.clone(), + renewed: self.active_reservation.is_some(), + }, + )); } inbound_hop::Req::Connect(inbound_circuit_req) => { - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::CircuitReqReceived { - inbound_circuit_req, - endpoint: self.endpoint.clone(), - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::CircuitReqReceived { + inbound_circuit_req, + endpoint: self.endpoint.clone(), + }, + )); } } } @@ -456,17 +458,18 @@ impl Handler { let (tx, rx) = oneshot::channel(); self.alive_lend_out_substreams.push(rx); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundConnectNegotiated { - circuit_id, - src_peer_id, - src_connection_id, - inbound_circuit_req, - dst_handler_notifier: tx, - dst_stream, - dst_pending_data, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundConnectNegotiated { + circuit_id, + src_peer_id, + src_connection_id, + inbound_circuit_req, + dst_handler_notifier: tx, + dst_stream, + dst_pending_data, + }, + )); } fn on_listen_upgrade_error( @@ -502,11 +505,12 @@ impl Handler { } }; - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::CircuitReqReceiveFailed { - error: non_fatal_error, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::CircuitReqReceiveFailed { + error: non_fatal_error, + }, + )); } fn on_dial_upgrade_error( @@ -577,16 +581,17 @@ impl Handler { src_connection_id, } = open_info; - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundConnectNegotiationFailed { - circuit_id, - src_peer_id, - src_connection_id, - inbound_circuit_req, - status, - error: non_fatal_error, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundConnectNegotiationFailed { + circuit_id, + src_peer_id, + src_connection_id, + inbound_circuit_req, + status, + error: non_fatal_error, + }, + )); } } @@ -744,18 +749,22 @@ impl ConnectionHandler for Handler { { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed { - circuit_id, - dst_peer_id, - error: None, - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + Event::CircuitClosed { + circuit_id, + dst_peer_id, + error: None, + }, + )) } Err(e) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed { - circuit_id, - dst_peer_id, - error: Some(e), - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + Event::CircuitClosed { + circuit_id, + dst_peer_id, + error: Some(e), + }, + )) } } } @@ -766,13 +775,15 @@ impl ConnectionHandler for Handler { { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitReqDenied { - circuit_id, - dst_peer_id, - })); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + Event::CircuitReqDenied { + circuit_id, + dst_peer_id, + }, + )); } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::CircuitReqDenyFailed { circuit_id, dst_peer_id, @@ -825,7 +836,7 @@ impl ConnectionHandler for Handler { self.circuits.push(circuit); - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::CircuitReqAccepted { circuit_id, dst_peer_id, @@ -833,7 +844,7 @@ impl ConnectionHandler for Handler { )); } Err((circuit_id, dst_peer_id, error)) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::CircuitReqAcceptFailed { circuit_id, dst_peer_id, @@ -851,7 +862,7 @@ impl ConnectionHandler for Handler { .map(|fut| fut.poll_unpin(cx)) { self.active_reservation = None; - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::ReservationTimedOut {}, )); } @@ -868,12 +879,12 @@ impl ConnectionHandler for Handler { .active_reservation .replace(Delay::new(self.config.reservation_duration)) .is_some(); - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::ReservationReqAccepted { renewed }, )); } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::ReservationReqAcceptFailed { error }, )); } @@ -886,12 +897,12 @@ impl ConnectionHandler for Handler { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::ReservationReqDenied {}, )) } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::Custom( + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( Event::ReservationReqDenyFailed { error }, )); } diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index ac74b6c6021..fdf38f4954a 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -199,9 +199,10 @@ impl Handler { relay_addr: self.remote_addr.clone(), }); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundCircuitEstablished { src_peer_id, limit }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundCircuitEstablished { src_peer_id, limit }, + )); } Reservation::None => { let src_peer_id = inbound_circuit.src_peer_id(); @@ -259,7 +260,7 @@ impl Handler { ); self.queued_events - .push_back(ConnectionHandlerEvent::Custom(event)); + .push_back(ConnectionHandlerEvent::NotifyBehaviour(event)); } // Outbound circuit @@ -277,9 +278,10 @@ impl Handler { })) { Ok(()) => { self.alive_lend_out_substreams.push(rx); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundCircuitEstablished { limit }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundCircuitEstablished { limit }, + )); } Err(_) => debug!( "Oneshot to `client::transport::Dial` future dropped. \ @@ -326,11 +328,12 @@ impl Handler { } }; - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::InboundCircuitReqFailed { - error: non_fatal_error, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::InboundCircuitReqFailed { + error: non_fatal_error, + }, + )); } fn on_dial_upgrade_error( @@ -400,12 +403,13 @@ impl Handler { } let renewal = self.reservation.failed(); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::ReservationReqFailed { - renewal, - error: non_fatal_error, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::ReservationReqFailed { + renewal, + error: non_fatal_error, + }, + )); } OutboundOpenInfo::Connect { send_back } => { let non_fatal_error = match error { @@ -450,11 +454,12 @@ impl Handler { let _ = send_back.send(Err(())); - self.queued_events.push_back(ConnectionHandlerEvent::Custom( - Event::OutboundCircuitReqFailed { - error: non_fatal_error, - }, - )); + self.queued_events + .push_back(ConnectionHandlerEvent::NotifyBehaviour( + Event::OutboundCircuitReqFailed { + error: non_fatal_error, + }, + )); } } } @@ -553,7 +558,7 @@ impl ConnectionHandler for Handler { }); if let Some((src_peer_id, event)) = maybe_event { self.circuit_deny_futs.remove(&src_peer_id); - return Poll::Ready(ConnectionHandlerEvent::Custom(event)); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)); } // Send errors to transport. diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index f6b4bd2ad2e..02fa8ed91a7 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -466,32 +466,28 @@ where match poll_substreams(&mut self.inbound_substreams, cx) { Poll::Ready(Ok((id, message))) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::InboundEvent { - id, - message, - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + OutEvent::InboundEvent { id, message }, + )) } Poll::Ready(Err((id, error))) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::InboundError { - id, - error, - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + OutEvent::InboundError { id, error }, + )) } Poll::Pending => {} } match poll_substreams(&mut self.outbound_substreams, cx) { Poll::Ready(Ok((id, message))) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::OutboundEvent { - id, - message, - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + OutEvent::OutboundEvent { id, message }, + )) } Poll::Ready(Err((id, error))) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::OutboundError { - id, - error, - })) + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + OutEvent::OutboundError { id, error }, + )) } Poll::Pending => {} } diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index e28ca547af8..f15905553d7 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -336,7 +336,7 @@ where // Drain pending events. if let Some(event) = self.pending_events.pop_front() { - return Poll::Ready(ConnectionHandlerEvent::Custom(event)); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)); } else if self.pending_events.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD { self.pending_events.shrink_to_fit(); } @@ -347,7 +347,7 @@ where Ok(((id, rq), rs_sender)) => { // We received an inbound request. self.keep_alive = KeepAlive::Yes; - return Poll::Ready(ConnectionHandlerEvent::Custom(Event::Request { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event::Request { request_id: id, request: rq, sender: rs_sender, diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index b37b4e62203..1c72b8b9fe1 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -238,7 +238,7 @@ where requested_substreams.push(SubstreamRequested::new(user_data, timeout, upgrade)); continue; // Poll handler until exhausted. } - Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { + Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { return Poll::Ready(Ok(Event::Handler(event))); } Poll::Ready(ConnectionHandlerEvent::Close(err)) => { diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 775e7afef4c..8eb7ac16ee4 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -95,7 +95,7 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; pub trait ConnectionHandler: Send + 'static { /// A type representing the message(s) a [`NetworkBehaviour`] can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`] type FromBehaviour: fmt::Debug + Send + 'static; - /// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`] via [`ConnectionHandlerEvent::Custom`]. + /// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`] via [`ConnectionHandlerEvent::NotifyBehaviour`]. type ToBehaviour: fmt::Debug + Send + 'static; /// The type of errors returned by [`ConnectionHandler::poll`]. type Error: error::Error + fmt::Debug + Send + 'static; @@ -381,7 +381,7 @@ pub enum ConnectionHandlerEvent protocol: protocol.map_info(map), } } - ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), + ConnectionHandlerEvent::NotifyBehaviour(val) => { + ConnectionHandlerEvent::NotifyBehaviour(val) + } ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), } } @@ -423,7 +425,9 @@ impl protocol: protocol.map_upgrade(map), } } - ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), + ConnectionHandlerEvent::NotifyBehaviour(val) => { + ConnectionHandlerEvent::NotifyBehaviour(val) + } ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), } } @@ -440,7 +444,9 @@ impl ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } } - ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(map(val)), + ConnectionHandlerEvent::NotifyBehaviour(val) => { + ConnectionHandlerEvent::NotifyBehaviour(map(val)) + } ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), } } @@ -457,7 +463,9 @@ impl ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } } - ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), + ConnectionHandlerEvent::NotifyBehaviour(val) => { + ConnectionHandlerEvent::NotifyBehaviour(val) + } ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(map(val)), } } diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index 918e4b171c1..71a2c6894e6 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -76,7 +76,9 @@ where >, > { self.inner.poll(cx).map(|ev| match ev { - ConnectionHandlerEvent::Custom(ev) => ConnectionHandlerEvent::Custom((self.map)(ev)), + ConnectionHandlerEvent::NotifyBehaviour(ev) => { + ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev)) + } ConnectionHandlerEvent::Close(err) => ConnectionHandlerEvent::Close(err), ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index 963ebb5ef2d..55c7d4c0d6d 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -157,7 +157,9 @@ where } if !self.events_out.is_empty() { - return Poll::Ready(ConnectionHandlerEvent::Custom(self.events_out.remove(0))); + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + self.events_out.remove(0), + )); } else { self.events_out.shrink_to_fit(); } diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 887588e2064..76eaff24d48 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -387,8 +387,8 @@ where >, > { match self.proto1.poll(cx) { - Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(Either::Left(event))); + Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Left(event))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { return Poll::Ready(ConnectionHandlerEvent::Close(Either::Left(event))); @@ -404,8 +404,10 @@ where }; match self.proto2.poll(cx) { - Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { - return Poll::Ready(ConnectionHandlerEvent::Custom(Either::Right(event))); + Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { + return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Right( + event, + ))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { return Poll::Ready(ConnectionHandlerEvent::Close(Either::Right(event))); From 47ff8024604c35634163fe92600c48c9d6882752 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 22:02:49 +0200 Subject: [PATCH 19/40] modify doc ConnectionHandlerEvent::NotifyBehaviour --- swarm/src/handler.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 8eb7ac16ee4..830ed186584 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -380,7 +380,7 @@ pub enum ConnectionHandlerEvent Date: Tue, 2 May 2023 22:10:32 +0200 Subject: [PATCH 20/40] add env_logger implementation for out_event deprecation warning --- Cargo.lock | 1 + swarm-derive/Cargo.toml | 1 + swarm-derive/src/lib.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index c136dccf463..754443d9d9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2930,6 +2930,7 @@ dependencies = [ name = "libp2p-swarm-derive" version = "0.33.0" dependencies = [ + "env_logger 0.10.0", "heck", "log", "quote", diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 9e8f2540171..5f2f53ccb89 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -18,6 +18,7 @@ heck = "0.4" quote = "1.0" syn = { version = "2.0.15", default-features = false, features = ["clone-impls", "derive", "parsing", "printing", "proc-macro"] } log = "0.4" +env_logger = "0.10.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 202bf9c55a7..748409bf0e2 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -855,6 +855,7 @@ struct BehaviourAttributes { /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. fn parse_attributes(ast: &DeriveInput) -> Result { + env_logger::init(); let mut attributes = BehaviourAttributes { prelude_path: syn::parse_quote! { ::libp2p::swarm::derive_prelude }, user_specified_out_event: None, From 9886a30f599fb4efb175437a7e4d8753ac6d1910 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 22:31:09 +0200 Subject: [PATCH 21/40] rustdoc links fix for ConnectionHandler --- swarm/src/handler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 830ed186584..4183636c674 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -93,9 +93,9 @@ pub use select::{ConnectionHandlerSelect, IntoConnectionHandlerSelect}; /// 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 { - /// A type representing the message(s) a [`NetworkBehaviour`] can send to a [`ConnectionHandler`] via [`ToSwarm::NotifyHandler`] + /// 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`] via [`ConnectionHandlerEvent::NotifyBehaviour`]. + /// A type representing message(s) a [`ConnectionHandler`] can send to a [`NetworkBehaviour`](crate::behaviour::NetworkBehaviour) via [`ConnectionHandlerEvent::NotifyBehaviour`]. type ToBehaviour: fmt::Debug + Send + 'static; /// The type of errors returned by [`ConnectionHandler::poll`]. type Error: error::Error + fmt::Debug + Send + 'static; From 5fad024604757bc57e53788af8e6e90b80e8d97e Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 23:06:35 +0200 Subject: [PATCH 22/40] swarm-derive initialize the logger only once --- swarm-derive/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 748409bf0e2..e264a1242c0 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -855,7 +855,9 @@ struct BehaviourAttributes { /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. fn parse_attributes(ast: &DeriveInput) -> Result { - env_logger::init(); + let mut logger_builder = env_logger::Builder::new(); + logger_builder.init(); // Initialize the logger only once. + let mut attributes = BehaviourAttributes { prelude_path: syn::parse_quote! { ::libp2p::swarm::derive_prelude }, user_specified_out_event: None, From ae445488f89a8f09a7182c62457552c4bbc3db0c Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 23:56:17 +0200 Subject: [PATCH 23/40] log::warn to println in swarm-derive --- Cargo.lock | 2 -- swarm-derive/Cargo.toml | 2 -- swarm-derive/src/lib.rs | 8 ++------ swarm/tests/swarm_derive.rs | 2 +- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 608b6bbe452..73bc3957497 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2930,9 +2930,7 @@ dependencies = [ name = "libp2p-swarm-derive" version = "0.33.0" dependencies = [ - "env_logger 0.10.0", "heck", - "log", "quote", "syn 2.0.15", ] diff --git a/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 5f2f53ccb89..8d31a389ae5 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -17,8 +17,6 @@ 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"] } -log = "0.4" -env_logger = "0.10.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 e264a1242c0..68758467190 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -855,9 +855,6 @@ struct BehaviourAttributes { /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. fn parse_attributes(ast: &DeriveInput) -> Result { - let mut logger_builder = env_logger::Builder::new(); - logger_builder.init(); // Initialize the logger only once. - let mut attributes = BehaviourAttributes { prelude_path: syn::parse_quote! { ::libp2p::swarm::derive_prelude }, user_specified_out_event: None, @@ -901,9 +898,8 @@ fn parse_attributes(ast: &DeriveInput) -> Result unimplemented!(), Meta::List(_) => unimplemented!(), @@ -921,7 +917,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()); diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index b098ff94013..588b62383b9 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -426,7 +426,7 @@ fn generated_out_event_derive_debug() { fn require_debug() where T: NetworkBehaviour, - ::ToBehaviour: Debug, + ::ToSwarm: Debug, { } From f48008062087c623aee11fa189e481005b7c82b6 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 2 May 2023 23:58:34 +0200 Subject: [PATCH 24/40] clean up --- swarm-derive/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 68758467190..0b33adb038e 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -898,7 +898,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result unimplemented!(), From eafc2bc20c7d4a229a2691971eb51b2675d87da4 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 3 May 2023 00:11:43 +0200 Subject: [PATCH 25/40] clean up wrong type and change version position changelog --- swarm/CHANGELOG.md | 6 +++--- swarm/src/test.rs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index d04c39fd83b..7aa63428838 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -3,16 +3,16 @@ - Raise MSRV to 1.65. See [PR 3715]. +- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. + [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 +[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.42.2 - Add `ConnectionEvent::{is_outbound,is_inbound}`. See [PR 3625]. -- Rename `NetworkBehaviour::OutEvent` to `NetworkBehaviour::ToSwarm`, `ConnectionHandler::InEvent` to `ConnectionHandler::FromBehaviour`, `ConnectionHandler::OutEvent` to `ConnectionHandler::ToBehaviour`. See [PR 3848]. - [PR 3625]: https://github.com/libp2p/rust-libp2p/pull/3625 -[PR 3848]: https://github.com/libp2p/rust-libp2p/pull/3848 ## 0.42.1 diff --git a/swarm/src/test.rs b/swarm/src/test.rs index ddf68c45b76..cafba8f1de7 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -69,11 +69,11 @@ where impl NetworkBehaviour for MockBehaviour where THandler: ConnectionHandler + Clone, - THandler::ToBehaviour: Clone, + THandler::ToSwarm: Clone, TOutEvent: Send + 'static, { type ConnectionHandler = THandler; - type ToBehaviour = TOutEvent; + type ToSwarm = TOutEvent; fn handle_established_inbound_connection( &mut self, @@ -404,7 +404,7 @@ where THandlerOutEvent: Clone, { type ConnectionHandler = TInner::ConnectionHandler; - type ToBehaviour = TInner::ToBehaviour; + type ToSwarm = TInner::ToSwarm; fn handle_pending_inbound_connection( &mut self, From a19c564498f745e647c88b7274d0241bfe414e4b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 3 May 2023 00:13:40 +0200 Subject: [PATCH 26/40] THandler::InEvent to THandler::FromBehaviour in swarm/src/test.rs --- swarm/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/test.rs b/swarm/src/test.rs index cafba8f1de7..8dc0c782179 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -48,7 +48,7 @@ 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 From 0e28e527b07057b4df5e0431fab7a4fee799ea08 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 3 May 2023 00:20:48 +0200 Subject: [PATCH 27/40] fix clippy --- swarm/src/test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 8dc0c782179..b00cf221fa7 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -69,7 +69,7 @@ where impl NetworkBehaviour for MockBehaviour where THandler: ConnectionHandler + Clone, - THandler::ToSwarm: Clone, + THandler::ToBehaviour: Clone, TOutEvent: Send + 'static, { type ConnectionHandler = THandler; From d88c599469b64b0c048b2d812d7a12e8a38e7ff9 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 3 May 2023 09:32:30 +0200 Subject: [PATCH 28/40] deprecation warning out_event --- swarm-derive/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 0b33adb038e..6cd055f45ee 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -853,6 +853,12 @@ struct BehaviourAttributes { user_specified_out_event: Option, } +#[deprecated( + since = "0.33.0", + note = "The 'out_event' key is deprecated, use 'to_swarm' instead." +)] +trait OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm {} + /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. fn parse_attributes(ast: &DeriveInput) -> Result { let mut attributes = BehaviourAttributes { @@ -898,7 +904,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result unimplemented!(), From 5d3ac92b46862b3d34860faa25e1976463963c2b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 3 May 2023 22:05:08 +0200 Subject: [PATCH 29/40] try out_event deprecation with quote --- .../distributed-key-value-store/src/main.rs | 2 +- swarm-derive/src/lib.rs | 30 ++++++++++++++----- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/examples/distributed-key-value-store/src/main.rs b/examples/distributed-key-value-store/src/main.rs index ce617aca5e4..952e55ba6e7 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(to_swarm = "MyBehaviourEvent")] + #[behaviour(out_event = "MyBehaviourEvent")] struct MyBehaviour { kademlia: Kademlia, mdns: mdns::async_io::Behaviour, diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 6cd055f45ee..a1dedf134a3 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -853,18 +853,13 @@ struct BehaviourAttributes { user_specified_out_event: Option, } -#[deprecated( - since = "0.33.0", - note = "The 'out_event' key is deprecated, use 'to_swarm' instead." -)] -trait OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm {} - /// Parses the `value` of a key=value pair in the `#[behaviour]` attribute into the requested type. fn parse_attributes(ast: &DeriveInput) -> Result { let mut attributes = BehaviourAttributes { prelude_path: syn::parse_quote! { ::libp2p::swarm::derive_prelude }, user_specified_out_event: None, }; + let mut is_out_event = false; for attr in ast .attrs @@ -904,7 +899,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result unimplemented!(), @@ -935,5 +930,26 @@ fn parse_attributes(ast: &DeriveInput) -> Result { + let path_str = quote! { #path }.to_string(); + + syn::parse_str(&path_str).unwrap() + } + None => syn::parse_str("()").unwrap(), + }; + + quote! { + #[deprecated( + since = "0.33.0", + note = "The 'out_event' key is deprecated, use 'to_swarm' instead." + )] + trait OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm{}; + + impl OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm for #out_event_type {} + }; + } + Ok(attributes) } From a3c5e110bf68f562f744597b0be3c348f3515aeb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 4 May 2023 17:44:07 +0200 Subject: [PATCH 30/40] Remove deprecated out-event check --- swarm-derive/src/lib.rs | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index a1dedf134a3..6045e40710f 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -859,7 +859,6 @@ fn parse_attributes(ast: &DeriveInput) -> Result Result unimplemented!(), Meta::List(_) => unimplemented!(), @@ -930,26 +926,5 @@ fn parse_attributes(ast: &DeriveInput) -> Result { - let path_str = quote! { #path }.to_string(); - - syn::parse_str(&path_str).unwrap() - } - None => syn::parse_str("()").unwrap(), - }; - - quote! { - #[deprecated( - since = "0.33.0", - note = "The 'out_event' key is deprecated, use 'to_swarm' instead." - )] - trait OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm{}; - - impl OutEventAttributeIsDeprecatedAndHasBeenRenamedToToSwarm for #out_event_type {} - }; - } - Ok(attributes) } From 8fde04f8976e2cc12aa6c42faabf8c79308b94a9 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Thu, 4 May 2023 17:44:36 +0200 Subject: [PATCH 31/40] Rename final event --- examples/distributed-key-value-store/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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, From a5c2598fd85a8f378052cce7166124dc548400d9 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 11 May 2023 18:43:20 +0200 Subject: [PATCH 32/40] fix break --- protocols/dcutr/src/handler/direct.rs | 3 +- protocols/dcutr/src/handler/relayed.rs | 26 ++-- protocols/gossipsub/src/handler.rs | 14 +- protocols/identify/src/handler.rs | 10 +- protocols/kad/src/handler.rs | 48 +++---- protocols/perf/src/client/handler.rs | 4 +- protocols/perf/src/server/handler.rs | 4 +- protocols/ping/src/handler.rs | 14 +- protocols/relay/src/behaviour/handler.rs | 122 ++++++++---------- protocols/relay/src/priv_client/handler.rs | 42 +++--- protocols/rendezvous/src/substream_handler.rs | 28 ++-- protocols/request-response/src/handler.rs | 4 +- swarm/src/connection.rs | 2 +- swarm/src/handler.rs | 22 +--- swarm/src/handler/map_out.rs | 4 +- swarm/src/handler/one_shot.rs | 4 +- swarm/src/handler/select.rs | 10 +- 17 files changed, 160 insertions(+), 201 deletions(-) diff --git a/protocols/dcutr/src/handler/direct.rs b/protocols/dcutr/src/handler/direct.rs index 3819c23cf7b..a06c9272baa 100644 --- a/protocols/dcutr/src/handler/direct.rs +++ b/protocols/dcutr/src/handler/direct.rs @@ -41,7 +41,6 @@ pub struct Handler { impl ConnectionHandler for Handler { type FromBehaviour = void::Void; type ToBehaviour = Event; - type Error = ConnectionHandlerUpgrErr; type Error = StreamUpgradeError; type InboundProtocol = DeniedUpgrade; type OutboundProtocol = DeniedUpgrade; @@ -71,7 +70,7 @@ impl ConnectionHandler for Handler { > { if !self.reported { self.reported = true; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::DirectConnectionEstablished, )); } diff --git a/protocols/dcutr/src/handler/relayed.rs b/protocols/dcutr/src/handler/relayed.rs index d34f00e3ed2..eb1ad83cecb 100644 --- a/protocols/dcutr/src/handler/relayed.rs +++ b/protocols/dcutr/src/handler/relayed.rs @@ -171,13 +171,12 @@ impl Handler { ConnectedPoint::Dialer { address, role_override: _ } => address.clone(), ConnectedPoint::Listener { ..} => unreachable!("`::listen_protocol` denies all incoming substreams as a listener."), }; - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::InboundConnectRequest { - inbound_connect: Box::new(inbound_connect), - remote_addr, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::InboundConnectRequest { + inbound_connect: Box::new(inbound_connect), + remote_addr, + }, + )); } // A connection listener denies all incoming substreams, thus none can ever be fully negotiated. future::Either::Right(output) => void::unreachable(output), @@ -198,12 +197,11 @@ impl Handler { self.endpoint.is_listener(), "A connection dialer never initiates a connection upgrade." ); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::OutboundConnectNegotiated { - remote_addrs: obs_addrs, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::OutboundConnectNegotiated { + remote_addrs: obs_addrs, + }, + )); } fn on_listen_upgrade_error( @@ -344,7 +342,7 @@ impl ConnectionHandler for Handler { self.inbound_connect = None; match result { Ok(addresses) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::InboundConnectNegotiated(addresses), )); } diff --git a/protocols/gossipsub/src/handler.rs b/protocols/gossipsub/src/handler.rs index b9b54974c0d..ba28ac0af97 100644 --- a/protocols/gossipsub/src/handler.rs +++ b/protocols/gossipsub/src/handler.rs @@ -232,9 +232,9 @@ impl EnabledHandler { if !self.peer_kind_sent { if let Some(peer_kind) = self.peer_kind.as_ref() { self.peer_kind_sent = true; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - HandlerEvent::PeerKind(peer_kind.clone()), - )); + return Poll::Ready(ConnectionHandlerEvent::Custom(HandlerEvent::PeerKind( + peer_kind.clone(), + ))); } } @@ -261,7 +261,7 @@ impl EnabledHandler { self.last_io_activity = Instant::now(); self.inbound_substream = Some(InboundSubstreamState::WaitingInput(substream)); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(message)); + return Poll::Ready(ConnectionHandlerEvent::Custom(message)); } Poll::Ready(Some(Err(error))) => { log::debug!("Failed to read from inbound stream: {error}"); @@ -466,9 +466,9 @@ impl ConnectionHandler for Handler { Handler::Disabled(DisabledHandler::ProtocolUnsupported { peer_kind_sent }) => { if !*peer_kind_sent { *peer_kind_sent = true; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - HandlerEvent::PeerKind(PeerKind::NotSupported), - )); + return Poll::Ready(ConnectionHandlerEvent::Custom(HandlerEvent::PeerKind( + PeerKind::NotSupported, + ))); } Poll::Pending diff --git a/protocols/identify/src/handler.rs b/protocols/identify/src/handler.rs index dfc52f66596..d041fca731a 100644 --- a/protocols/identify/src/handler.rs +++ b/protocols/identify/src/handler.rs @@ -174,13 +174,13 @@ impl Handler { future::Either::Left(remote_info) => { self.update_supported_protocols_for_remote(&remote_info); self.events - .push(ConnectionHandlerEvent::NotifyBehaviour(Event::Identified( + .push(ConnectionHandlerEvent::Custom(Event::Identified( remote_info, ))); } - future::Either::Right(()) => self.events.push(ConnectionHandlerEvent::NotifyBehaviour( - Event::IdentificationPushed, - )), + future::Either::Right(()) => self + .events + .push(ConnectionHandlerEvent::Custom(Event::IdentificationPushed)), } } @@ -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/handler.rs b/protocols/kad/src/handler.rs index 880725e06d5..fe3c30c207e 100644 --- a/protocols/kad/src/handler.rs +++ b/protocols/kad/src/handler.rs @@ -719,7 +719,7 @@ where > { if let ProtocolStatus::Confirmed = self.protocol_status { self.protocol_status = ProtocolStatus::Reported; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::ProtocolConfirmed { endpoint: self.endpoint.clone(), }, @@ -838,7 +838,7 @@ where Err(error) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::NotifyBehaviour( + ConnectionHandlerEvent::Custom( KademliaHandlerEvent::QueryError { error: KademliaHandlerQueryErr::Io(error), user_data, @@ -856,12 +856,10 @@ where Poll::Ready(Err(error)) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::NotifyBehaviour( - KademliaHandlerEvent::QueryError { - error: KademliaHandlerQueryErr::Io(error), - user_data, - }, - ) + ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError { + error: KademliaHandlerQueryErr::Io(error), + user_data, + }) }); return Poll::Ready(event); @@ -884,12 +882,10 @@ where Poll::Ready(Err(error)) => { *this = OutboundSubstreamState::Done; let event = user_data.map(|user_data| { - ConnectionHandlerEvent::NotifyBehaviour( - KademliaHandlerEvent::QueryError { - error: KademliaHandlerQueryErr::Io(error), - user_data, - }, - ) + ConnectionHandlerEvent::Custom(KademliaHandlerEvent::QueryError { + error: KademliaHandlerQueryErr::Io(error), + user_data, + }) }); return Poll::Ready(event); @@ -902,9 +898,7 @@ where *this = OutboundSubstreamState::Closing(substream); let event = process_kad_response(msg, user_data); - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( - event, - ))); + return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); } Poll::Pending => { *this = OutboundSubstreamState::WaitingAnswer(substream, user_data); @@ -917,9 +911,7 @@ where user_data, }; - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( - event, - ))); + return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); } Poll::Ready(None) => { *this = OutboundSubstreamState::Done; @@ -930,9 +922,7 @@ where user_data, }; - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( - event, - ))); + return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); } } } @@ -940,7 +930,7 @@ where *this = OutboundSubstreamState::Done; let event = KademliaHandlerEvent::QueryError { error, user_data }; - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour(event))); + return Poll::Ready(Some(ConnectionHandlerEvent::Custom(event))); } OutboundSubstreamState::Closing(mut stream) => match stream.poll_close_unpin(cx) { Poll::Ready(Ok(())) | Poll::Ready(Err(_)) => return Poll::Ready(None), @@ -993,7 +983,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::FindNode { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(Some(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::FindNodeReq { key, request_id: KademliaRequestId { @@ -1005,7 +995,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::GetProviders { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(Some(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::GetProvidersReq { key, request_id: KademliaRequestId { @@ -1020,14 +1010,14 @@ where connection_id, substream, }; - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(Some(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::AddProvider { key, provider }, ))); } Poll::Ready(Some(Ok(KadRequestMsg::GetValue { key }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(Some(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::GetRecord { key, request_id: KademliaRequestId { @@ -1039,7 +1029,7 @@ where Poll::Ready(Some(Ok(KadRequestMsg::PutValue { record }))) => { *this = InboundSubstreamState::WaitingBehaviour(connection_id, substream, None); - return Poll::Ready(Some(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(Some(ConnectionHandlerEvent::Custom( KademliaHandlerEvent::PutRecord { record, request_id: KademliaRequestId { diff --git a/protocols/perf/src/client/handler.rs b/protocols/perf/src/client/handler.rs index 8a6df43d198..f8a1eff5c10 100644 --- a/protocols/perf/src/client/handler.rs +++ b/protocols/perf/src/client/handler.rs @@ -146,7 +146,7 @@ impl ConnectionHandler for Handler { .pop_front() .expect("requested stream without pending command"); self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour(Event { + .push_back(ConnectionHandlerEvent::Custom(Event { id, result: Err(error), })); @@ -179,7 +179,7 @@ impl ConnectionHandler for Handler { while let Poll::Ready(Some(result)) = self.outbound.poll_next_unpin(cx) { match result { - Ok(event) => return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)), + Ok(event) => return Poll::Ready(ConnectionHandlerEvent::Custom(event)), Err(e) => { panic!("{e:?}") } diff --git a/protocols/perf/src/server/handler.rs b/protocols/perf/src/server/handler.rs index e8f7b72e605..6f5043b66a3 100644 --- a/protocols/perf/src/server/handler.rs +++ b/protocols/perf/src/server/handler.rs @@ -129,9 +129,7 @@ impl ConnectionHandler for Handler { > { while let Poll::Ready(Some(result)) = self.inbound.poll_next_unpin(cx) { match result { - Ok(stats) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event { stats })) - } + Ok(stats) => return Poll::Ready(ConnectionHandlerEvent::Custom(Event { stats })), Err(e) => { error!("{e:?}") } diff --git a/protocols/ping/src/handler.rs b/protocols/ping/src/handler.rs index 47e5fac1956..e0e7c308c7e 100644 --- a/protocols/ping/src/handler.rs +++ b/protocols/ping/src/handler.rs @@ -258,9 +258,7 @@ impl ConnectionHandler for Handler { } State::Inactive { reported: false } => { self.state = State::Inactive { reported: true }; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err( - Failure::Unsupported, - ))); + return Poll::Ready(ConnectionHandlerEvent::Custom(Err(Failure::Unsupported))); } State::Active => {} } @@ -276,7 +274,7 @@ impl ConnectionHandler for Handler { Poll::Ready(Ok(stream)) => { // A ping from a remote peer has been answered, wait for the next. self.inbound = Some(protocol::recv_ping(stream).boxed()); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok(Success::Pong))); + return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Pong))); } } } @@ -301,7 +299,7 @@ impl ConnectionHandler for Handler { return Poll::Ready(ConnectionHandlerEvent::Close(error)); } - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Err(error))); + return Poll::Ready(ConnectionHandlerEvent::Custom(Err(error))); } } @@ -320,9 +318,9 @@ impl ConnectionHandler for Handler { self.failures = 0; self.timer.reset(self.config.interval); self.outbound = Some(OutboundState::Idle(stream)); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Ok( - Success::Ping { rtt }, - ))); + return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping { + rtt, + }))); } Poll::Ready(Err(e)) => { self.pending_errors diff --git a/protocols/relay/src/behaviour/handler.rs b/protocols/relay/src/behaviour/handler.rs index d89eef3ff41..93bd562df40 100644 --- a/protocols/relay/src/behaviour/handler.rs +++ b/protocols/relay/src/behaviour/handler.rs @@ -410,23 +410,21 @@ impl Handler { ) { match request { inbound_hop::Req::Reserve(inbound_reservation_req) => { - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::ReservationReqReceived { - inbound_reservation_req, - endpoint: self.endpoint.clone(), - renewed: self.active_reservation.is_some(), - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::ReservationReqReceived { + inbound_reservation_req, + endpoint: self.endpoint.clone(), + renewed: self.active_reservation.is_some(), + }, + )); } inbound_hop::Req::Connect(inbound_circuit_req) => { - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::CircuitReqReceived { - inbound_circuit_req, - endpoint: self.endpoint.clone(), - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::CircuitReqReceived { + inbound_circuit_req, + endpoint: self.endpoint.clone(), + }, + )); } } } @@ -450,18 +448,17 @@ impl Handler { let (tx, rx) = oneshot::channel(); self.alive_lend_out_substreams.push(rx); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::OutboundConnectNegotiated { - circuit_id, - src_peer_id, - src_connection_id, - inbound_circuit_req, - dst_handler_notifier: tx, - dst_stream, - dst_pending_data, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::OutboundConnectNegotiated { + circuit_id, + src_peer_id, + src_connection_id, + inbound_circuit_req, + dst_handler_notifier: tx, + dst_stream, + dst_pending_data, + }, + )); } fn on_listen_upgrade_error( @@ -528,17 +525,16 @@ impl Handler { src_connection_id, } = open_info; - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::OutboundConnectNegotiationFailed { - circuit_id, - src_peer_id, - src_connection_id, - inbound_circuit_req, - status, - error: non_fatal_error, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::OutboundConnectNegotiationFailed { + circuit_id, + src_peer_id, + src_connection_id, + inbound_circuit_req, + status, + error: non_fatal_error, + }, + )); } } @@ -696,22 +692,18 @@ impl ConnectionHandler for Handler { { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - Event::CircuitClosed { - circuit_id, - dst_peer_id, - error: None, - }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed { + circuit_id, + dst_peer_id, + error: None, + })) } Err(e) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - Event::CircuitClosed { - circuit_id, - dst_peer_id, - error: Some(e), - }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitClosed { + circuit_id, + dst_peer_id, + error: Some(e), + })) } } } @@ -722,15 +714,13 @@ impl ConnectionHandler for Handler { { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - Event::CircuitReqDenied { - circuit_id, - dst_peer_id, - }, - )); + return Poll::Ready(ConnectionHandlerEvent::Custom(Event::CircuitReqDenied { + circuit_id, + dst_peer_id, + })); } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::CircuitReqDenyFailed { circuit_id, dst_peer_id, @@ -783,7 +773,7 @@ impl ConnectionHandler for Handler { self.circuits.push(circuit); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::CircuitReqAccepted { circuit_id, dst_peer_id, @@ -791,7 +781,7 @@ impl ConnectionHandler for Handler { )); } Err((circuit_id, dst_peer_id, error)) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::CircuitReqAcceptFailed { circuit_id, dst_peer_id, @@ -809,7 +799,7 @@ impl ConnectionHandler for Handler { .map(|fut| fut.poll_unpin(cx)) { self.active_reservation = None; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::ReservationTimedOut {}, )); } @@ -826,12 +816,12 @@ impl ConnectionHandler for Handler { .active_reservation .replace(Delay::new(self.config.reservation_duration)) .is_some(); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::ReservationReqAccepted { renewed }, )); } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::ReservationReqAcceptFailed { error }, )); } @@ -844,12 +834,12 @@ impl ConnectionHandler for Handler { match result { Ok(()) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::ReservationReqDenied {}, )) } Err(error) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( + return Poll::Ready(ConnectionHandlerEvent::Custom( Event::ReservationReqDenyFailed { error }, )); } diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 414ec4fc8df..dee51cde664 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -194,10 +194,9 @@ impl Handler { relay_addr: self.remote_addr.clone(), }); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::InboundCircuitEstablished { src_peer_id, limit }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::InboundCircuitEstablished { src_peer_id, limit }, + )); } Reservation::None => { let src_peer_id = inbound_circuit.src_peer_id(); @@ -255,7 +254,7 @@ impl Handler { ); self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour(event)); + .push_back(ConnectionHandlerEvent::Custom(event)); } // Outbound circuit @@ -273,10 +272,9 @@ impl Handler { })) { Ok(()) => { self.alive_lend_out_substreams.push(rx); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::OutboundCircuitEstablished { limit }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::OutboundCircuitEstablished { limit }, + )); } Err(_) => debug!( "Oneshot to `client::transport::Dial` future dropped. \ @@ -352,13 +350,12 @@ impl Handler { } let renewal = self.reservation.failed(); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::ReservationReqFailed { - renewal, - error: non_fatal_error, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::ReservationReqFailed { + renewal, + error: non_fatal_error, + }, + )); } OutboundOpenInfo::Connect { send_back } => { let non_fatal_error = match error { @@ -385,12 +382,11 @@ impl Handler { let _ = send_back.send(Err(())); - self.queued_events - .push_back(ConnectionHandlerEvent::NotifyBehaviour( - Event::OutboundCircuitReqFailed { - error: non_fatal_error, - }, - )); + self.queued_events.push_back(ConnectionHandlerEvent::Custom( + Event::OutboundCircuitReqFailed { + error: non_fatal_error, + }, + )); } } } @@ -489,7 +485,7 @@ impl ConnectionHandler for Handler { }); if let Some((src_peer_id, event)) = maybe_event { self.circuit_deny_futs.remove(&src_peer_id); - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)); + return Poll::Ready(ConnectionHandlerEvent::Custom(event)); } // Send errors to transport. diff --git a/protocols/rendezvous/src/substream_handler.rs b/protocols/rendezvous/src/substream_handler.rs index 62402660271..60bfcdc0402 100644 --- a/protocols/rendezvous/src/substream_handler.rs +++ b/protocols/rendezvous/src/substream_handler.rs @@ -469,28 +469,32 @@ where match poll_substreams(&mut self.inbound_substreams, cx) { Poll::Ready(Ok((id, message))) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - OutEvent::InboundEvent { id, message }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::InboundEvent { + id, + message, + })) } Poll::Ready(Err((id, error))) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - OutEvent::InboundError { id, error }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::InboundError { + id, + error, + })) } Poll::Pending => {} } match poll_substreams(&mut self.outbound_substreams, cx) { Poll::Ready(Ok((id, message))) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - OutEvent::OutboundEvent { id, message }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::OutboundEvent { + id, + message, + })) } Poll::Ready(Err((id, error))) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - OutEvent::OutboundError { id, error }, - )) + return Poll::Ready(ConnectionHandlerEvent::Custom(OutEvent::OutboundError { + id, + error, + })) } Poll::Pending => {} } diff --git a/protocols/request-response/src/handler.rs b/protocols/request-response/src/handler.rs index a8c9055816c..02e9c8710dd 100644 --- a/protocols/request-response/src/handler.rs +++ b/protocols/request-response/src/handler.rs @@ -304,7 +304,7 @@ where // Drain pending events. if let Some(event) = self.pending_events.pop_front() { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)); + return Poll::Ready(ConnectionHandlerEvent::Custom(event)); } else if self.pending_events.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD { self.pending_events.shrink_to_fit(); } @@ -315,7 +315,7 @@ where Ok(((id, rq), rs_sender)) => { // We received an inbound request. self.keep_alive = KeepAlive::Yes; - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Event::Request { + return Poll::Ready(ConnectionHandlerEvent::Custom(Event::Request { request_id: id, request: rq, sender: rs_sender, diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 0a35f074617..363c9e9afad 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -261,7 +261,7 @@ where requested_substreams.push(SubstreamRequested::new(user_data, timeout, upgrade)); continue; // Poll handler until exhausted. } - Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { + Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { return Poll::Ready(Ok(Event::Handler(event))); } Poll::Ready(ConnectionHandlerEvent::Close(err)) => { diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 860d2ac0450..edefb7a1cb9 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -101,7 +101,7 @@ use std::{cmp::Ordering, error, fmt, io, task::Context, task::Poll, time::Durati pub trait ConnectionHandler: 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::NotifyBehaviour`]. + /// 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; @@ -508,8 +508,8 @@ pub enum ConnectionHandlerEvent protocol: protocol.map_info(map), } } - ConnectionHandlerEvent::NotifyBehaviour(val) => { - ConnectionHandlerEvent::NotifyBehaviour(val) - } + ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) @@ -564,9 +562,7 @@ impl protocol: protocol.map_upgrade(map), } } - ConnectionHandlerEvent::NotifyBehaviour(val) => { - ConnectionHandlerEvent::NotifyBehaviour(val) - } + ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) @@ -586,9 +582,7 @@ impl ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } } - ConnectionHandlerEvent::NotifyBehaviour(val) => { - ConnectionHandlerEvent::NotifyBehaviour(map(val)) - } + ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(map(val)), ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(val), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) @@ -608,9 +602,7 @@ impl ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } } - ConnectionHandlerEvent::NotifyBehaviour(val) => { - ConnectionHandlerEvent::NotifyBehaviour(val) - } + ConnectionHandlerEvent::Custom(val) => ConnectionHandlerEvent::Custom(val), ConnectionHandlerEvent::Close(val) => ConnectionHandlerEvent::Close(map(val)), ConnectionHandlerEvent::ReportRemoteProtocols(support) => { ConnectionHandlerEvent::ReportRemoteProtocols(support) diff --git a/swarm/src/handler/map_out.rs b/swarm/src/handler/map_out.rs index e92d1403ce6..f3240a76b2d 100644 --- a/swarm/src/handler/map_out.rs +++ b/swarm/src/handler/map_out.rs @@ -76,9 +76,7 @@ where >, > { self.inner.poll(cx).map(|ev| match ev { - ConnectionHandlerEvent::NotifyBehaviour(ev) => { - ConnectionHandlerEvent::NotifyBehaviour((self.map)(ev)) - } + ConnectionHandlerEvent::Custom(ev) => ConnectionHandlerEvent::Custom((self.map)(ev)), ConnectionHandlerEvent::Close(err) => ConnectionHandlerEvent::Close(err), ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } => { ConnectionHandlerEvent::OutboundSubstreamRequest { protocol } diff --git a/swarm/src/handler/one_shot.rs b/swarm/src/handler/one_shot.rs index 439d3f47ee3..b7c98e189e7 100644 --- a/swarm/src/handler/one_shot.rs +++ b/swarm/src/handler/one_shot.rs @@ -157,9 +157,7 @@ where } if !self.events_out.is_empty() { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour( - self.events_out.remove(0), - )); + return Poll::Ready(ConnectionHandlerEvent::Custom(self.events_out.remove(0))); } else { self.events_out.shrink_to_fit(); } diff --git a/swarm/src/handler/select.rs b/swarm/src/handler/select.rs index 65db4ab525b..48048befc81 100644 --- a/swarm/src/handler/select.rs +++ b/swarm/src/handler/select.rs @@ -227,8 +227,8 @@ where >, > { match self.proto1.poll(cx) { - Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Left(event))); + Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { + return Poll::Ready(ConnectionHandlerEvent::Custom(Either::Left(event))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { return Poll::Ready(ConnectionHandlerEvent::Close(Either::Left(event))); @@ -247,10 +247,8 @@ where }; match self.proto2.poll(cx) { - Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(event)) => { - return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(Either::Right( - event, - ))); + Poll::Ready(ConnectionHandlerEvent::Custom(event)) => { + return Poll::Ready(ConnectionHandlerEvent::Custom(Either::Right(event))); } Poll::Ready(ConnectionHandlerEvent::Close(event)) => { return Poll::Ready(ConnectionHandlerEvent::Close(Either::Right(event))); From c27766a8d2b1eb4e88627be73ddab74f61782925 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 11 May 2023 18:45:30 +0200 Subject: [PATCH 33/40] params to _ --- protocols/identify/src/behaviour.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 203047c4d16..532b86dafa8 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -318,7 +318,7 @@ impl NetworkBehaviour for Behaviour { fn poll( &mut self, _cx: &mut Context<'_>, - params: &mut impl PollParameters, + _: &mut impl PollParameters, ) -> Poll>> { if let Some(event) = self.events.pop_front() { return Poll::Ready(event); From 7e95c4b4d4f3a581d770a46490b2582022c4ea28 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 11 May 2023 20:05:58 +0200 Subject: [PATCH 34/40] try proc_macro_warning --- Cargo.lock | 12 ++++++++++++ examples/dcutr/src/main.rs | 2 +- examples/distributed-key-value-store/Cargo.toml | 2 +- swarm-derive/Cargo.toml | 1 + swarm-derive/src/lib.rs | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2e850e031a0..a5adcee1faa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3003,6 +3003,7 @@ name = "libp2p-swarm-derive" version = "0.33.0" dependencies = [ "heck", + "proc-macro-warning", "quote", "syn 2.0.15", ] @@ -3975,6 +3976,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" diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index e8f5673e7c4..4dbee86fac3 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(to_swarm = "Event", event_process = false)] + #[behaviour(out_event = "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/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 8d31a389ae5..8a085dd01d9 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -17,6 +17,7 @@ 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" # 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 6045e40710f..eafc9f8ec48 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -24,6 +24,7 @@ use heck::ToUpperCamelCase; use proc_macro::TokenStream; use quote::quote; +use quote::ToTokens; use syn::punctuated::Punctuated; use syn::{ parse_macro_input, Data, DataStruct, DeriveInput, Expr, ExprLit, Lit, Meta, MetaNameValue, @@ -859,6 +860,8 @@ fn parse_attributes(ast: &DeriveInput) -> Result Result(meta_token_stream.into()).unwrap(); + let warning = proc_macro_warning::Warning::new_deprecated("test") + .old("foo") + .new("bar") + .span(input.ident.span()) + .build(); + + let token: TokenStream = quote::quote!(#warning).into(); + } match meta { Meta::Path(_) => unimplemented!(), Meta::List(_) => unimplemented!(), From 5eb525843f6b76bb8f0fe2e1ef3a7de526b6a4e8 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Sat, 13 May 2023 22:38:26 +0200 Subject: [PATCH 35/40] add deprecation warning implementation for out_event --- Cargo.lock | 1 + examples/dcutr/src/main.rs | 2 +- swarm-derive/Cargo.toml | 1 + swarm-derive/src/lib.rs | 26 +++++++++++++++----------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b46cdfb83e2..b276f013ecc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3005,6 +3005,7 @@ version = "0.33.0" dependencies = [ "heck", "proc-macro-warning", + "proc-macro2", "quote", "syn 2.0.15", ] 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/swarm-derive/Cargo.toml b/swarm-derive/Cargo.toml index 8a085dd01d9..76b56cdbd45 100644 --- a/swarm-derive/Cargo.toml +++ b/swarm-derive/Cargo.toml @@ -18,6 +18,7 @@ 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 eafc9f8ec48..9c62b6b7c54 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -24,7 +24,6 @@ use heck::ToUpperCamelCase; use proc_macro::TokenStream; use quote::quote; -use quote::ToTokens; use syn::punctuated::Punctuated; use syn::{ parse_macro_input, Data, DataStruct, DeriveInput, Expr, ExprLit, Lit, Meta, MetaNameValue, @@ -55,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, @@ -732,6 +732,8 @@ 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 @@ -852,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. @@ -859,6 +862,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result Result(meta_token_stream.into()).unwrap(); - let warning = proc_macro_warning::Warning::new_deprecated("test") - .old("foo") - .new("bar") - .span(input.ident.span()) - .build(); - - let token: TokenStream = quote::quote!(#warning).into(); } match meta { Meta::Path(_) => unimplemented!(), @@ -942,5 +936,15 @@ fn parse_attributes(ast: &DeriveInput) -> Result Date: Sat, 13 May 2023 22:40:01 +0200 Subject: [PATCH 36/40] clean up --- swarm-derive/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 9c62b6b7c54..61153c1df75 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -865,7 +865,6 @@ fn parse_attributes(ast: &DeriveInput) -> Result Date: Sat, 13 May 2023 22:58:47 +0200 Subject: [PATCH 37/40] fix clippy --- swarm-derive/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 61153c1df75..1d86e01ee45 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -942,7 +942,7 @@ fn parse_attributes(ast: &DeriveInput) -> Result Date: Sat, 13 May 2023 23:22:14 +0200 Subject: [PATCH 38/40] fix clippy --- swarm/src/connection.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index a1c26d0ed24..43be1c98e3a 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -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())) } } @@ -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, >, > { From 722e9ea3282f4da42d56eb41644bb65820f2f3ef Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 14 May 2023 11:20:39 +0100 Subject: [PATCH 39/40] Add test for produced warning --- Cargo.lock | 31 +++++++++++++++++++ swarm/Cargo.toml | 1 + swarm/tests/swarm_derive.rs | 6 ++++ swarm/tests/ui/fail/out_event_deprecation.rs | 21 +++++++++++++ .../ui/fail/out_event_deprecation.stderr | 14 +++++++++ 5 files changed, 73 insertions(+) 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 b276f013ecc..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", ] @@ -5223,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/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/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index 588b62383b9..ab1efaca396 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -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..60c9793229b --- /dev/null +++ b/swarm/tests/ui/fail/out_event_deprecation.stderr @@ -0,0 +1,14 @@ +error: use of deprecated constant `out_event::_w`: + It is deprecated to out_event. + Please instead 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)]` From 25deeb9c09b402cab60a995464594aa9cb111551 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 14 May 2023 11:24:41 +0100 Subject: [PATCH 40/40] Update deprecation message --- swarm-derive/src/lib.rs | 6 +++--- swarm/tests/ui/fail/out_event_deprecation.stderr | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index 1d86e01ee45..d08c42f4f02 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -936,9 +936,9 @@ fn parse_attributes(ast: &DeriveInput) -> Result tests/ui/fail/out_event_deprecation.rs:7:8 | 7 | struct Foo {