Disambiguate calls to NetworkBehaviour::inject_event (#1543)

* 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

* Update misc/core-derive/src/lib.rs

Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>

* Fix build and add CHANGELOG

Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
This commit is contained in:
Thomas Eizinger 2020-04-10 00:01:26 +10:00 committed by GitHub
parent 8adf474b38
commit 803eb10dcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 2 deletions

View File

@ -1,5 +1,8 @@
# Version ???
- `libp2p-core-derive`: Disambiguate calls to `NetworkBehaviour::inject_event`.
[PR 1543](https://github.com/libp2p/rust-libp2p/pull/1543)
- `libp2p-floodsub`: Allow sent messages seen as subscribed.
[PR 1520](https://github.com/libp2p/rust-libp2p/pull/1520)

View File

@ -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 => #trait_to_impl::inject_event(&mut self.#i, peer_id, connection_id, ev) },
None => quote!{ #elem => #trait_to_impl::inject_event(&mut self.#field_n, peer_id, connection_id, ev) },
})
});

View File

@ -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>();
}
}