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

fix(derive): restore support for inline generic type constraints #5003

Merged
merged 7 commits into from
Dec 21, 2023
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: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.1", path = "protocols/request-response" }
libp2p-server = { version = "0.12.5", path = "misc/server" }
libp2p-swarm = { version = "0.44.1", path = "swarm" }
libp2p-swarm-derive = { version = "=0.34.1", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-derive = { version = "=0.34.2", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" }
libp2p-tcp = { version = "0.41.0", path = "transports/tcp" }
libp2p-tls = { version = "0.3.0", path = "transports/tls" }
Expand Down
5 changes: 5 additions & 0 deletions swarm-derive/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.34.2

- Restore support for generic constraints on behaviours combined with `out_event` generated by `NetworkBehaviour` where no where clause is used.
See [PR 5003](https://github.com/libp2p/rust-libp2p/pull/5003).

## 0.34.1

- Always forward all variants of `FromSwarm`.
Expand Down
2 changes: 1 addition & 1 deletion swarm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-swarm-derive"
edition = "2021"
rust-version = { workspace = true }
description = "Procedural macros of libp2p-swarm"
version = "0.34.1"
version = "0.34.2"
aakoshh marked this conversation as resolved.
Show resolved Hide resolved
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
2 changes: 1 addition & 1 deletion swarm-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result<Toke

Some(quote! {
#[doc = #msg]
#visibility enum #enum_name #ty_generics
#visibility enum #enum_name #impl_generics
#where_clause
{
#(#enum_variants),*
Expand Down
85 changes: 85 additions & 0 deletions swarm/tests/swarm_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,91 @@ fn with_generics_mixed() {
}
}

#[test]
fn with_generics_constrained() {
use std::task::{Context, Poll};
trait Mark {}
struct Marked;
impl Mark for Marked {}

/// A struct with a generic constraint, for which we manually implement `NetworkBehaviour`.
#[allow(dead_code)]
struct Bar<A: Mark> {
a: A,
}

impl<A: Mark + 'static> NetworkBehaviour for Bar<A> {
type ConnectionHandler = dummy::ConnectionHandler;
type ToSwarm = void::Void;

fn handle_established_inbound_connection(
&mut self,
_: libp2p_swarm::ConnectionId,
_: libp2p_identity::PeerId,
_: &Multiaddr,
_: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}

fn handle_established_outbound_connection(
&mut self,
_: libp2p_swarm::ConnectionId,
_: libp2p_identity::PeerId,
_: &Multiaddr,
_: Endpoint,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}

fn on_swarm_event(&mut self, _event: FromSwarm) {}

fn on_connection_handler_event(
&mut self,
_: libp2p_identity::PeerId,
_: libp2p_swarm::ConnectionId,
_: THandlerOutEvent<Self>,
) {
}

fn poll(
&mut self,
_: &mut Context<'_>,
) -> Poll<libp2p_swarm::ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
Poll::Pending
}
}

/// A struct which uses the above, inheriting the generic constraint,
/// for which we want to derive the `NetworkBehaviour`.
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Foo1<A: Mark> {
bar: Bar<A>,
}

/// A struct which uses the above, inheriting the generic constraint,
/// for which we want to derive the `NetworkBehaviour`.
///
/// Using a where clause instead of inline constraint.
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Foo2<A>
where
A: Mark,
{
bar: Bar<A>,
}

#[allow(dead_code)]
fn foo() {
require_net_behaviour::<Foo1<Marked>>();
require_net_behaviour::<Foo2<Marked>>();
}
}

#[test]
fn custom_event_with_either() {
use either::Either;
Expand Down