rust-libp2p icon indicating copy to clipboard operation
rust-libp2p copied to clipboard

trait bound for deriving `NetworkBehaviour`

Open divagant-martian opened this issue 3 years ago • 4 comments

When deriving NetworkBehaviour on a struct with generics, not being able to include bounds in the resulting trait implementation forces the bounds to be added to the struct and not the impl only. This potentially generates a great deal of trait propagation (in behaviour composition, for example)

Maybe there is a way to do this but I don't seem to find it in the docs

divagant-martian avatar Feb 17 '22 13:02 divagant-martian

@divagant-martian would you mind providing an example?

not being able to include bounds in the resulting trait implementation

I am not sure I follow. Why can you not add a bound to the trait implementation?

For example, the below compiles:

trait ToBeImplemented {}

trait SomeBound {}

struct Foo<T> {
    something: T,
}

impl<T> ToBeImplemented for Foo<T> where T: SomeBound {}

mxinden avatar Feb 18 '22 17:02 mxinden

@mxinden as I said, the problem is in the derivation of the NetworkBehaviour implementation. I don't understand what is your example trying to prove. That's exactly what I would like.. for the derived NetworkBehaviour impl

divagant-martian avatar Feb 18 '22 17:02 divagant-martian

Ah, sorry I didn't read properly.

Maybe there is a way to do this but I don't seem to find it in the docs

I am not aware of such mechanism.

mxinden avatar Feb 18 '22 17:02 mxinden

Got it. I'll check around how it's usually done in derive macros, and if possible implement it

divagant-martian avatar Feb 18 '22 17:02 divagant-martian

Let me know if still an issue.

thomaseizinger avatar Mar 29 '23 11:03 thomaseizinger

No longer an issue!

divagant-martian avatar Mar 31 '23 16:03 divagant-martian