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

reexport path while using derive #2829

Closed
kuqmua opened this issue Sep 26, 2024 · 1 comment
Closed

reexport path while using derive #2829

kuqmua opened this issue Sep 26, 2024 · 1 comment

Comments

@kuqmua
Copy link

kuqmua commented Sep 26, 2024

imagine you creating new crate what depends on serde and reexport it

pub use serde::*;

In your crate you creating a new proc_macro. In proc macro you creating something like

let pub_struct_something = quote::quote!{
    #[derive(your_new_crate::Serialize)]
    pub struct Something {
        my_field: bool
    }
};

quote::quote!{
    #pub_struct_something
    #some_other_logic
}.into()

The problem is - generated code is still trying to import modules from serde, not from your_new_crate.
I suggest wrap "expand_derive_serialize"(or something) in

pub fn derive_serialize(input: TokenStream) -> TokenStream {
    let mut input = parse_macro_input!(input as DeriveInput);
    ser::expand_derive_serialize(&mut input)
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

into new public function, something like

"expand_derive_serialize_WITH_PATH_TO_CRATE"

with additional(maybe few) parameters what change all serde occcurences to parameter value.
Of course defalut would be just serde.

For example new implementation would be

pub fn derive_serialize(input: TokenStream) -> TokenStream {
    let mut input = parse_macro_input!(input as DeriveInput);
    ser::expand_derive_serialize_WITH_PATH_TO_CRATE(&mut input, "serde")
        .unwrap_or_else(syn::Error::into_compile_error)
        .into()
}

and you can write something like

let generated_serialize_token_stream_for_something_struct = serde::expand_derive_serialize_WITH_PATH_TO_CRATE(&mut possible_something_struct_token_stream, "your_crate");

Well i can write implemenation mannualy in my macro but in time i think it will be outdated i just wont know about some vulnerability or new optimization.

@dtolnay
Copy link
Member

dtolnay commented Sep 26, 2024

I would prefer not to work around this in the way you are asking.

rust-lang/rust#54363 (comment) has a writeup of my preferred approach.

@dtolnay dtolnay closed this as completed Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants