-
Notifications
You must be signed in to change notification settings - Fork 999
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
libp2p_swarm_derive: Add crate
parameter to configure libp2p
crate name
#3006
libp2p_swarm_derive: Add crate
parameter to configure libp2p
crate name
#3006
Conversation
70604ae
to
ed03603
Compare
ed03603
to
3e96486
Compare
3e96486
to
11d8ee8
Compare
crate
parameter to configure libp2p
crate name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One suggestion. Otherwise looks good to me.
let user_provided_libp2p_prefix = ast | ||
.attrs | ||
.iter() | ||
.filter_map(get_meta_items) | ||
.flatten() | ||
.filter_map(|meta_item| { | ||
if let syn::NestedMeta::Meta(syn::Meta::NameValue(ref m)) = meta_item { | ||
if m.path.is_ident("crate") { | ||
if let syn::Lit::Str(ref s) = m.lit { | ||
return Some(syn::parse_str::<syn::TypePath>(&s.value()).unwrap()); | ||
} | ||
} | ||
} | ||
None | ||
}) | ||
.next(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mostly the same as the logic for out_event
, right? What do you think of moving it into a function called in both places?
#[test] | ||
fn custom_crate() { | ||
use libp2p as mylibp2p; | ||
#[allow(dead_code)] | ||
#[derive(NetworkBehaviour)] | ||
#[behaviour(crate = "mylibp2p")] | ||
struct Foo { | ||
ping: ping::Behaviour, | ||
identify: identify::Behaviour, | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn foo() { | ||
require_net_behaviour::<Foo>(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test also already passes with current master (if you remove line 141).
Correct me if I am wrong, but I believe the current ::libp2p
prefix imports libp2p from the crate level, which means that aliases in a module should not be an issue? @mxinden?
I think it is only a problem if the crate is imported in the Cargo.toml
with a different name.
serde
on the other hand states for their crate
attribute that:
This is normally only applicable when invoking re-exported Serde derives from a public macro in a different crate.
I am not against adding this parameter, but could we add a test that fails on current master and would be fixed with this? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I understand, the issue is not clashing symbols but renaming the crate in your manifest.
To test that, we'd have to make a separate test crate with a renamed import.
This make me wonder, should we perhaps just require certain symbols to be present for the macro to work?
That would make #3023 easier to implement.
Or perhaps we could make a seperate "derive" prelude? Like libp2p::swarm_derive_prelude
that includes all the required symbols. Then we can add a configuration knob here that allows you to change where that prelude is coming from.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This make me wonder, should we perhaps just require certain symbols to be present for the macro to work?
That would make #3023 easier to implement.
Or perhaps we could make a seperate "derive" prelude? Like
libp2p::swarm_derive_prelude
that includes all the required symbols. Then we can add a configuration knob here that allows you to change where that prelude is coming from.
Actually, don't worry about it, I already have a solution to that.
If we end up shipping #3055, then this PR is obsolete as the #[behaviour(prelude = "mylibp2p::swarm::derive_prelude") |
Closing in favor of #3055 which is about to be merged. |
Description
When importing libp2p with a use statement and a custom alias name, the code generated by the NetworkBehaviour derive macro does not use this alias and does not compile. This PR enhances the derive macro with a new parameter "crate" that can be used to specify the custom alias name.
Open Questions
None
Change checklist