Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

# 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-rendezvous` `v0.9.0`](protocols/rendezvous/CHANGELOG.md#090).
Expand Down
8 changes: 7 additions & 1 deletion swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# 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

Expand Down
28 changes: 2 additions & 26 deletions swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use heck::ToUpperCamelCase;
use proc_macro::TokenStream;
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
/// the trait documentation for better description.
Expand Down Expand Up @@ -433,29 +433,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
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()`.
//
// We poll each child one by one and wrap around the output.
Expand Down Expand Up @@ -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>> {
use libp2p::futures::prelude::*;
#(#poll_stmts)*
let f: std::task::Poll<#network_behaviour_action<Self::OutEvent, Self::ConnectionHandler>> = #poll_method;
f
std::task::Poll::Pending
}
}
};
Expand Down
81 changes: 1 addition & 80 deletions swarm-derive/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,38 +128,7 @@ fn three_fields_non_last_ignored() {
}

#[test]
fn custom_polling() {
#[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() {
fn custom_event() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyEvent")]
Expand Down Expand Up @@ -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]
fn custom_event_mismatching_field_names() {
#[allow(dead_code)]
Expand Down
4 changes: 0 additions & 4 deletions swarm/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// `#[behaviour(ignore)]`.
///
Expand Down