dtolnay/paste

Generate doc-comments based on provided tokens

Michael-F-Bryan opened this issue · 4 comments

Would it be possible to use paste as a workaround for rust-lang/rust#52607? I was thinking you could add a :stringify modifier which will wrap the concatenated text in a string literal.

Imagine being able to write something like this:

macro_rules! func {
  ($name:ident, $ret:ident) => {
    /// Create a new [`
    #[doc = [< $ret:stringify >] ]
    /// `] object.
    pub fn $name() -> $ret { todo!() }
  }
}

func!(foo, Bar);

// expands to

/// Create a new [`Bar`] object.
pub fn foo() -> Bar { todo!() }

This is kinda related to #29 in that we're playing with #[doc] attributes, with the difference being where the string comes from. In that issue it comes from stringify!($name) in a previous macro invocation, but here we'd be using paste to generate the doc string directly.

I am tentatively interested in supporting this.

As an alternative to :stringify, it's possible we could just concatenate anything inside a doc attribute.

#[doc = "Create a new [`" $ret "`] object."]
pub fn $name() -> $ret { todo!() }

Your proposed solution feels a lot more powerful and ergonomic. It'd be a pain to make a new #[doc = [< $ret:stringify >] ] attribute every time you want to add something to a doc-comment.

newAM commented

I have not found a way to do this (with this or any other crate). Having the ability to do this would make my day!

I don't suppose we ever implemented something like Michael suggested?

My use case is to inject a stringified ident into another macro, e.g. #[sbor(categorize_as = [< $generic_ident:stringify >])] - I've tried the short-hand #[sbor(categorize_as = "" $generic_ident "")] just in case the doc string parsing was more generic, but it didn't work.

EDIT: I've published my own pre-processor which is inspired by paste, but more powerful - this is available now in this crate, but might move out soon: https://docs.rs/sbor-derive/1.2.0/sbor_derive/macro.eager_replace.html