I have a procedural macro which generates a utility function which is then used by the generated code. The function does not depend on the input of the marco, and it would be better to further reduce code duplication by reusing the same function between different macro calls. But I would not like to put the function in a crate and make it public, because it has no meaning for users and is used exclusively inside a macro. Is there a neat way to avoid code duplication, i.e. generate some code block/function just once at global scope/scope visible only to the macro code?
What you do is add
#[doc(hidden)] to it so
rustdoc won't show it, and give it a name that indicates users shouldn't be calling it (or put it in a
macro_internal module). Finally, at the top of the doc comments, add a note that the function (or the whole module) is exempt from stability guarantees and it can change at any time.
Basically, you hide it behind the cushions and glare at anyone that touches it.
There is no way to stop a user from directly accessing implementation details of your macro. Remember: a macro can't do anything that you couldn't write by hand yourself. If your users can't access a symbol, neither can your macro.