Hiding private implementation details for a proc macro

Hi all,
I'm working on bindings to a C plugin API that requires exposing some unsafe code in a user's code for bootstrapping and declaring plugins. My plan is to do all this through a proc macro that automatically generates the correct implementation of this code, but doing this means exposing internal plumbing that I would really rather not expose outside of the macro invocation.

I know there is an option to use a lot of #[doc(hidden)] and to prefix things with multiple underscores, but is there a way to only export crate items/modules for use in macros in a way that the compiler would otherwise object to?

No. Unfortunately, code generated by proc macros has no privileges that can affect item visibility.

I would recommend that you design your code to minimize the number of special macro-only items, regardless. As a general principle, users should be able to access the functionality of your library without being obligated to use your macros — and even if this is not practical to achieve completely, it’s good to review the items’ API design in the same way you would if they were public and documented.

You should be especially careful with macros that generate unsafe code, because it’s often possible for the caller to provide an odd environment, or use additional macros, in ways that result in your generated unsafe code being unsound.