Passing Token Stream into proc macro from another crate within proc macro

I have a crate where that I have to work with a graphics crate called vulkano and I have a proc macro I've written that replaces some idents so that I don't have to import vulkano along with my crate into projects:

#[proc_macro]
pub fn shader(input: proc_macro::TokenStream) -> proc_macro::TokenStream{
    proc_macro::TokenStream::from(
    replace_crate_name(
        vulkano_shaders::shader!(input)
    ))
}


fn replace_crate_name(input: TokenStream) -> TokenStream{}

I can't seem to find a way to pass the token stream that I get in my shader macro straight into the vulkano_shaders::shader! macro (line 5), is there a really obvious way to do this that I am missing or is this much more complicated than I thought? Any help would be very appreciated

You have to create a new TokenStream that adds the macro call around the original input in order to call the other macro. The quote crate is the easiest way to do that, though it isn't required.

It looks to me like you're actually trying to parse the output of the vulkano macro in your macro, which isn't possible the way you have this set up. The vulkano macro, can only be expanded after this proc macro sends the tokens back to the compiler.

So would there be a way to send the tokens to the compiler and then pass them into the replace function to expand it, I would have thought it would work since it outputs a token stream which surely contains the tokens that would later be expanded but I guess that isn't how it works

It does eventually, but your proc macro is isolated from the compiler. You have to return the token stream to the compiler for it to process the macro in the output.

I don't think it's currently possible to force expansion of macros present in the input to another macro. The compiler assumes it's just part of the input to your macro and sends the raw tokens instead of interpreting them as a macro and expanding them first.

There may be some tricks you can use to work around that, but if there are I'm not familiar with them.

Ah okay thank you for explaining that, I'll see if I can find a work around but if not I can always copy the other macro and have it run within mine, thank you for your help

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.