From 2499dbd13ead1dad5a769a37031e46b138d09a10 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 6 Apr 2020 16:30:45 +1000 Subject: [PATCH] Disambiguate calls to NetworkBehaviour::inject_event There is a gnarly edge-case with the custom-derive where rustc cannot disambiguate the call if: - The NetworkBehaviourEventProcess trait is imported - We nest NetworkBehaviours that use the custom-derive --- misc/core-derive/src/lib.rs | 4 ++-- misc/core-derive/tests/test.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/misc/core-derive/src/lib.rs b/misc/core-derive/src/lib.rs index 1153276f57a..f66953bb0e6 100644 --- a/misc/core-derive/src/lib.rs +++ b/misc/core-derive/src/lib.rs @@ -293,8 +293,8 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream { } Some(match field.ident { - Some(ref i) => quote!{ #elem => self.#i.inject_event(peer_id, connection_id, ev) }, - None => quote!{ #elem => self.#field_n.inject_event(peer_id, connection_id, ev) }, + Some(ref i) => quote!{ #elem => ::libp2p::swarm::NetworkBehaviour::inject_event(&mut self.#i, peer_id, connection_id, ev) }, + None => quote!{ #elem => ::libp2p::swarm::NetworkBehaviour::inject_event(&mut self.#field_n, peer_id, connection_id, ev) }, }) }); diff --git a/misc/core-derive/tests/test.rs b/misc/core-derive/tests/test.rs index e0c5cfdf564..b65fb16eda7 100644 --- a/misc/core-derive/tests/test.rs +++ b/misc/core-derive/tests/test.rs @@ -204,3 +204,35 @@ fn where_clause() { bar: T, } } + +#[test] +fn nested_derives_with_import() { + use libp2p::swarm::NetworkBehaviourEventProcess; + + #[allow(dead_code)] + #[derive(NetworkBehaviour)] + struct Foo { + ping: libp2p::ping::Ping, + } + + #[allow(dead_code)] + #[derive(NetworkBehaviour)] + struct Bar { + foo: Foo, + } + + impl NetworkBehaviourEventProcess for Foo { + fn inject_event(&mut self, _: libp2p::ping::PingEvent) { + } + } + + impl NetworkBehaviourEventProcess<()> for Bar { + fn inject_event(&mut self, _: ()) { + } + } + + #[allow(dead_code)] + fn bar() { + require_net_behaviour::(); + } +}