dtolnay / paste

Macros for all your token pasting needs
Apache License 2.0
1.02k stars 56 forks source link

Generate doc-comments based on provided tokens #40

Closed Michael-F-Bryan closed 4 years ago

Michael-F-Bryan commented 4 years ago

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.

dtolnay commented 4 years ago

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!() }
Michael-F-Bryan commented 4 years ago

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 4 years ago

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!

dhedey commented 5 months ago

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