swarm-derive/: Remove support for custom poll method (#2841)

With the removal of `NetworkBehaviourEventProcess` there is no more need for a
custom poll method.
This commit is contained in:
Max Inden
2022-08-28 10:51:49 +02:00
committed by GitHub
parent a3dec471c0
commit 247b5536d4
5 changed files with 12 additions and 111 deletions

View File

@ -45,6 +45,8 @@
# 0.48.0 [unreleased] # 0.48.0 [unreleased]
- Update to [`libp2p-swarm-derive` `v0.30.0`](swarm-derive/CHANGELOG.md#0300).
- Update to [`libp2p-dcutr` `v0.6.0`](protocols/dcutr/CHANGELOG.md#060). - Update to [`libp2p-dcutr` `v0.6.0`](protocols/dcutr/CHANGELOG.md#060).
- Update to [`libp2p-rendezvous` `v0.9.0`](protocols/rendezvous/CHANGELOG.md#090). - Update to [`libp2p-rendezvous` `v0.9.0`](protocols/rendezvous/CHANGELOG.md#090).

View File

@ -1,6 +1,12 @@
# 0.30.0 - [unreleased] # 0.30.0 - [unreleased]
- Remove support for removed `NetworkBehaviourEventProcess`. - Remove support for removed `NetworkBehaviourEventProcess`. See [PR 2840].
- Remove support for custom `poll` method on `NetworkBehaviour` via `#[behaviour(poll_method =
"poll")]`. See [PR 2841].
[PR 2840]: https://github.com/libp2p/rust-libp2p/pull/2840
[PR 2841]: https://github.com/libp2p/rust-libp2p/pull/2841
# 0.29.0 # 0.29.0

View File

@ -23,7 +23,7 @@
use heck::ToUpperCamelCase; use heck::ToUpperCamelCase;
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::quote; use quote::quote;
use syn::{parse_macro_input, Data, DataStruct, DeriveInput, Ident}; use syn::{parse_macro_input, Data, DataStruct, DeriveInput};
/// Generates a delegating `NetworkBehaviour` implementation for the struct this is used for. See /// Generates a delegating `NetworkBehaviour` implementation for the struct this is used for. See
/// the trait documentation for better description. /// the trait documentation for better description.
@ -433,29 +433,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
out_handler.unwrap_or(quote! {()}) // TODO: See test `empty`. out_handler.unwrap_or(quote! {()}) // TODO: See test `empty`.
}; };
// The method to use to poll.
// If we find a `#[behaviour(poll_method = "poll")]` attribute on the struct, we call
// `self.poll()` at the end of the polling.
let poll_method = {
let mut poll_method = quote! {std::task::Poll::Pending};
for meta_items in ast.attrs.iter().filter_map(get_meta_items) {
for meta_item in meta_items {
match meta_item {
syn::NestedMeta::Meta(syn::Meta::NameValue(ref m))
if m.path.is_ident("poll_method") =>
{
if let syn::Lit::Str(ref s) = m.lit {
let ident: Ident = syn::parse_str(&s.value()).unwrap();
poll_method = quote! {#name::#ident(self, cx, poll_params)};
}
}
_ => (),
}
}
}
poll_method
};
// List of statements to put in `poll()`. // List of statements to put in `poll()`.
// //
// We poll each child one by one and wrap around the output. // We poll each child one by one and wrap around the output.
@ -638,8 +615,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<Self::OutEvent, Self::ConnectionHandler>> { fn poll(&mut self, cx: &mut std::task::Context, poll_params: &mut impl #poll_parameters) -> std::task::Poll<#network_behaviour_action<Self::OutEvent, Self::ConnectionHandler>> {
use libp2p::futures::prelude::*; use libp2p::futures::prelude::*;
#(#poll_stmts)* #(#poll_stmts)*
let f: std::task::Poll<#network_behaviour_action<Self::OutEvent, Self::ConnectionHandler>> = #poll_method; std::task::Poll::Pending
f
} }
} }
}; };

View File

@ -128,38 +128,7 @@ fn three_fields_non_last_ignored() {
} }
#[test] #[test]
fn custom_polling() { fn custom_event() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo")]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
}
impl Foo {
fn foo(
&mut self,
_: &mut std::task::Context,
_: &mut impl libp2p::swarm::PollParameters,
) -> std::task::Poll<
libp2p::swarm::NetworkBehaviourAction<
<Self as NetworkBehaviour>::OutEvent,
<Self as NetworkBehaviour>::ConnectionHandler,
>,
> {
std::task::Poll::Pending
}
}
#[allow(dead_code)]
fn foo() {
require_net_behaviour::<Foo>();
}
}
#[test]
fn custom_event_no_polling() {
#[allow(dead_code)] #[allow(dead_code)]
#[derive(NetworkBehaviour)] #[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyEvent")] #[behaviour(out_event = "MyEvent")]
@ -191,54 +160,6 @@ fn custom_event_no_polling() {
} }
} }
#[test]
fn custom_event_and_polling() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(poll_method = "foo", out_event = "MyEvent")]
struct Foo {
ping: libp2p::ping::Ping,
identify: libp2p::identify::Identify,
}
enum MyEvent {
Ping(libp2p::ping::PingEvent),
Identify(libp2p::identify::IdentifyEvent),
}
impl From<libp2p::ping::PingEvent> for MyEvent {
fn from(event: libp2p::ping::PingEvent) -> Self {
MyEvent::Ping(event)
}
}
impl From<libp2p::identify::IdentifyEvent> for MyEvent {
fn from(event: libp2p::identify::IdentifyEvent) -> Self {
MyEvent::Identify(event)
}
}
impl Foo {
fn foo(
&mut self,
_: &mut std::task::Context,
_: &mut impl libp2p::swarm::PollParameters,
) -> std::task::Poll<
libp2p::swarm::NetworkBehaviourAction<
<Self as NetworkBehaviour>::OutEvent,
<Self as NetworkBehaviour>::ConnectionHandler,
>,
> {
std::task::Poll::Pending
}
}
#[allow(dead_code)]
fn foo() {
require_net_behaviour::<Foo>();
}
}
#[test] #[test]
fn custom_event_mismatching_field_names() { fn custom_event_mismatching_field_names() {
#[allow(dead_code)] #[allow(dead_code)]

View File

@ -118,10 +118,6 @@ pub(crate) type THandlerOutEvent<THandler> =
/// } /// }
/// ``` /// ```
/// ///
/// Optionally one can provide a custom `poll` function through the `#[behaviour(poll_method =
/// "poll")]` attribute. This function must have the same signature as the [`NetworkBehaviour#poll`]
/// function and will be called last within the generated [`NetworkBehaviour`] implementation.
///
/// Struct members that don't implement [`NetworkBehaviour`] must be annotated with /// Struct members that don't implement [`NetworkBehaviour`] must be annotated with
/// `#[behaviour(ignore)]`. /// `#[behaviour(ignore)]`.
/// ///