Passing documentation for a macro-generated method from macro invocation


#1

I have a macro that generates a public method followed by a bunch of private methods that the public method calls. The public method is the first item generated by the macro and doesn’t have documentation in the macro expansion. Still, (as one should expect from hygiene, I guess,) placing a doc comment before the macro invocation doesn’t cause that doc comment to become documentation for the macro-generated public method.

How can I pass the documentation for the macro-generated method to the macro for the invocation site? (So that I can have different documentation for methods generated by different invocations of the macro.)


#2

Behold!

macro_rules! foo {
    ($e:expr) => {
        #[doc="This is a "] #[doc=$e] #[doc=" function."]
        pub fn foo() {}
    };
}
foo!("abc");

macro_rules! bar {
    ($(#[$meta:meta])*) => {
        $(#[$meta])*
        pub fn bar() {}
    };
}
bar!(
    /// A tailor, a Catholic priest, and a giraffe walk into a
);

The first constructs a composite doc comment, the second just passes a doc comment through directly.


#3

Thank you. This is what I was looking for.