Skip to content

Commit

Permalink
Disamiguate calls to NetworkBehaviour::inject_event
Browse files Browse the repository at this point in the history
There is a narly 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
  • Loading branch information
thomaseizinger committed Apr 6, 2020
1 parent 8411087 commit d0e5f26
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions misc/core-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
})
});

Expand Down
32 changes: 32 additions & 0 deletions misc/core-derive/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<libp2p::ping::PingEvent> 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::<Bar>();
}
}

0 comments on commit d0e5f26

Please sign in to comment.