Skip to content
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

Invoking re-exported derive macros #697

Closed
ozaner opened this issue Dec 8, 2023 · 2 comments
Closed

Invoking re-exported derive macros #697

ozaner opened this issue Dec 8, 2023 · 2 comments

Comments

@ozaner
Copy link

ozaner commented Dec 8, 2023

I want to re-export a trait (e.g. AsBytes) and its respective derive macro in a crate and invoke that macro in a downstream crate. It seems, however, that the derive macros assume that zerocopy is one of the invoker's imported crates. Here is a minimal example:


my_reexport crate (deps: zerocopy w/ derive feature)
lib.rs

pub use zerocopy;

my_app crate (deps: my_reexport)
main.rs

use my_reexport::zerocopy::AsBytes;

#[repr(C)]
#[derive(AsBytes)]
struct MyStruct {
    _field_a: u32,
    _field_b: u32,
}

fn main() {}

I get an error highlighted on the AsBytes item in the derive macro:

failed to resolve: could not find zerocopy in the list of imported crates
could not find zerocopy in the list of imported crates

I think other crates solve this problem by providing a crate attribute to their derives, for example
serde has #[serde(crate = "...")].

Apologies if there is already a way to do this, but I haven't found any documentation on the matter.

@joshlf
Copy link
Member

joshlf commented Dec 8, 2023

Hey @ozaner, thanks for reaching out about this! We're aware of the issue and have it tracked in #11. We plan on taking an approach similar to Serde's, although implementing it isn't on our immediate roadmap. I'm gonna close this in favor of that issue, but feel free to follow up with more comments!

@joshlf joshlf closed this as completed Dec 8, 2023
@ozaner
Copy link
Author

ozaner commented Dec 8, 2023

Thanks for pointing me towards that issue. About how to get around this, the issue mentions:

This is not too limiting, I can easily write:

use ::core::marker::Sized;
use ::some_other_crate::zerocopy;

Before the derive macro, but ideally it would just work.

But this workaround doesn't seem to... work? I also checked out the linked fuchsia implementation of the workaround:

Actually, this is not directly needed; the workaround described here works, and is implemented in https://fuchsia-review.googlesource.com/c/fuchsia/+/702383.

But I can't really find where it is implemented. Is there any way to achieve this, that is, reexport zerocopy without having downstream needing to import it? I don't mind if its messy or full of boilerplate.

Thought I'd follow up here rather than pollute the original issue with irrelevant comments on the details of workarounds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants