-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[derive(NetworkBehaviour)]
fails on structs with generics
#2879
Comments
I may be wrong but It may be related to #2842 and I dont believe substrate upgraded to 0.48 where the changes would be needed (assuming they are ignoring those fields) though i havent looked at their source so i wouldnt know what they are doing. It also sounds like youre looking for |
Yes, using types that don't implement We have a test where the annotated rust-libp2p/swarm-derive/tests/test.rs Lines 183 to 195 in c650dc1
|
@dmitry-markin would the following fix your example? #[derive(NetworkBehaviour)]
#[behaviour(out_event = "BadOutEvent<T>")]
- struct BadBehaviour<T> {
+ struct BadBehaviour<T: NetworkBehaviour> {
mdns: Mdns,
- template: Option<T>,
+ template: Toggle<T>,
} |
In the example above I wasn't able to implement But when I make The error is the same:
Most likely I just don't understand something quite trivial and make a silly mistake. But can't spot it, so will appreciate the help a lot. |
@dmitry-markin Going from the top of my head from looking over your code, I would assume this would probably fix the error youre getting #[derive(NetworkBehaviour)]
#[behaviour(out_event = "BadOutEvent")]
struct BadBehaviour<T: 'static + Send> {
mdns: Mdns,
custom: CustomBehaviour<T>,
} I dont believe it likes impl<T: 'static + Send> From<<CustomBehaviour<T> as NetworkBehaviour>::OutEvent>
for BadOutEvent<T>
{
fn from(e: T) -> Self {
Self::Template(e)
}
} Since you wouldnt be using Can test this sometime tomorrow though but im pretty sure by skimming over your code in that repo that there might need more changes since |
@dariusc93 thanks, that fixes at least one problem: removing template parameter from Unfortunately, in Substrate we have different number of template parameters in a behaviour struct and its out event. Here is the toy example for this: In this case the compilation fails with the following error:
|
I think that is something we should support. Does it work if you let the macro generate the outevent for you? |
It looks like the generated code implies that the template parameters of the
Yes, it works, I just needed to import |
I've pushed a fix to this in #2907. Please review that the added test corresponds to your usecase. Also, could you try and patch that branch into your application and see if it makes the error go away? |
Yes, the test covers the use case with the only difference that our behavior has two template parameters, and out event one, and in the test they are one and zero correspondingly. Patching the branch into Substrate also made the error go away. @thomaseizinger thanks a lot for the bugfix! |
Good to hear! I'll wait for a review of #2907 from @mxinden and then we can merge that. |
Summary
It looks like there is a regression in libp2p-0.48.0:
#[derive(NetworkBehaviour)]
fails to generate code if the original struct has generic parameters.Expected behaviour
#[derive(NetworkBehaviour)]
generates the implementation if the struct has generic parameters.Actual behaviour
Compilation fails with the error:
Please see the minimal example:
main.rs
(In this case the compilation would fail anyway, because
Option<T>
does not implementNetworkBehaviour
, but the compilation fails earlier with the same error as in the real-life example in Substrate, where the templatized type actually implementsNetworkBehaviour
.)Version
Additional information
It also looks like there is another, may be connected, bug:
#[derive(NetworkBehaviour)]
fails to generate<STRUCT_NAME>Event
if no#[behaviour(out_event = "...")]
provided (even without generics):Would you like to work on fixing this bug?
No, unfortunately I don't understand what's happening.
CC @mxinden
The text was updated successfully, but these errors were encountered: