Macro parameter expansion without spaces, is it possible?

I have an expansion like this in a macro:

stringify!( $ns::$services )

It will generate something like: remote :: Add, but I would like it to be remote::Add, and I need it to be a &'static str so I'm not to fond on post processing it...

I have tried with paste::expr!, but I need it inside another paste::expr! block and it doesn't seem to support being nested.

I also checked std::any::type_name, but as expected that gives me a full path, which is not what I wanted either.

Ah, I found it:

concat!( stringify!($ns) , "::", stringify!($services) )

How about

concat!(stringify!($ns), "::", stringify!($services)) 

Thanks, just found it at exactly the same time. :smiley:

macro_rules! stringify_path {(
    $path:path
) => (
    stringify!($path)
)}

stringify_path!($ns::$services)

Nice to see there was more than one way to skin a cat, but alice's solution works without introducing a new macro, so I'll stick to that.

1 Like

Indeed, their solution is simpler.

Mine has more of an academical interest: it shows that the whitespaces stringify! inserts are actually token separators, and when you feed $ns :: $services directly to stringify!, it is seeing three distinct tokens; whereas when using an intermediate macro, you can group-tokenize these three tokens into a single :path (or :expr, etc.) token, hence the lack of whitespace in between.

1 Like

When looking at error messages with RUSTFLAGS "-Z external-macro-backtrace" I also see spaces between every token, so I suspected that rustc expands macros like this for safety, but now I'm not sure, if you say it comes from the stringify macro.

I think that external-macro-backtrace and trace_macros! may share stringify!'s space-separation logic.