fix(swarm-derive): add bounds to OutEvent on generic fields (#3393)

This PR isolates the bugfix for the `NetworkBehaviour` derive implementation for structures with generic fields. When `out_event` was not provided, the generated enum was missing the `NetworkBehaviour` impl constraint for the generic variants whilst using the generics for `<Generic>::OutEvent`.

Meanwhile I also found that the generated `poll` function `loop`s the sub behaviours and either `return`'s when `Poll::Ready` or `break`'s when `Poll::Pending`. This is a relict from when we still had `NetworkBehaviourEventProcess` which had added a branch within this loop that did not `return` but consume the event and `continue`. This trait was removed a while ago meaning this `loop` is no longer needed.
This commit is contained in:
João Oliveira
2023-02-01 21:30:27 +00:00
committed by GitHub
parent 063aab5909
commit b98b03eb7e
3 changed files with 129 additions and 46 deletions

View File

@@ -308,6 +308,44 @@ fn with_either() {
}
}
#[test]
fn with_generics() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Foo<A, B> {
a: A,
b: B,
}
#[allow(dead_code)]
fn foo() {
require_net_behaviour::<
Foo<
libp2p_kad::Kademlia<libp2p_kad::record::store::MemoryStore>,
libp2p_ping::Behaviour,
>,
>();
}
}
#[test]
fn with_generics_mixed() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Foo<A> {
a: A,
ping: libp2p_ping::Behaviour,
}
#[allow(dead_code)]
fn foo() {
require_net_behaviour::<Foo<libp2p_kad::Kademlia<libp2p_kad::record::store::MemoryStore>>>(
);
}
}
#[test]
fn custom_event_with_either() {
use either::Either;