[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <ZTf1_iB9AgHlPnls@boqun-archlinux>
Date: Tue, 24 Oct 2023 09:51:10 -0700
From: Boqun Feng <boqun.feng@...il.com>
To: Trevor Gross <tmgross@...ch.edu>
Cc: Miguel Ojeda <ojeda@...nel.org>,
Alex Gaynor <alex.gaynor@...il.com>,
Wedson Almeida Filho <wedsonaf@...il.com>,
Gary Guo <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>,
Benno Lossin <benno.lossin@...ton.me>,
Andreas Hindborg <a.hindborg@...sung.com>,
Alice Ryhl <aliceryhl@...gle.com>,
Martin Rodriguez Reboredo <yakoyoku@...il.com>,
rust-for-linux@...r.kernel.org, linux-kernel@...r.kernel.org,
FUJITA Tomonori <fujita.tomonori@...il.com>
Subject: Re: [PATCH] rust: macros: update 'paste!' macro to accept string
literals
On Sun, Oct 08, 2023 at 05:48:18AM -0400, Trevor Gross wrote:
> Enable combining identifiers with string literals in the 'paste!' macro.
> This allows combining user-specified strings with affixes to create
> namespaced identifiers.
>
> This sample code:
>
> macro_rules! m {
> ($name:lit) => {
> paste!(struct [<_some_ $name _struct_>];)
> }
> }
>
> m!("foo_bar");
>
> Would previously cause a compilation error. It will now generate:
>
> struct _some_foo_bar_struct_;
>
> Reported-by: FUJITA Tomonori <fujita.tomonori@...il.com>
> Signed-off-by: Trevor Gross <tmgross@...ch.edu>
This looks good to me, but could you (in a follow-up patch mabye) add an
example demonstrating the usage, which could also serve as a test if we
can run doctest for macro doc. Thanks!
Regards,
Boqun
> ---
>
> Original mention of this problem in [1]
>
> [1]: https://lore.kernel.org/rust-for-linux/20231008.164906.1151622782836568538.fujita.tomonori@gmail.com/
>
> rust/macros/paste.rs | 10 +++++++++-
> 1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs
> index 385a78434224..f40d42b35b58 100644
> --- a/rust/macros/paste.rs
> +++ b/rust/macros/paste.rs
> @@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree {
> loop {
> match tokens.next() {
> None => break,
> - Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())),
> + Some(TokenTree::Literal(lit)) => {
> + // Allow us to concat string literals by stripping quotes
> + let mut value = lit.to_string();
> + if value.starts_with('"') && value.ends_with('"') {
> + value.remove(0);
> + value.pop();
> + }
> + segments.push((value, lit.span()));
> + }
> Some(TokenTree::Ident(ident)) => {
> let mut value = ident.to_string();
> if value.starts_with("r#") {
> --
> 2.34.1
>
>
Powered by blists - more mailing lists