[v3,01/13] rust: macros: add `quote!` macro

Message ID 20230329223239.138757-2-y86-dev@protonmail.com
State New
Headers
Series Rust pin-init API for pinned initialization of structs |

Commit Message

y86-dev March 29, 2023, 10:32 p.m. UTC
  From: Gary Guo <gary@garyguo.net>

Add the `quote!` macro for creating `TokenStream`s directly via the
given Rust tokens. It also supports repetitions using iterators.

It will be used by the pin-init API proc-macros to generate code.

Signed-off-by: Gary Guo <gary@garyguo.net>
Signed-off-by: Benno Lossin <y86-dev@protonmail.com>
---
 rust/macros/lib.rs   |   2 +
 rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+)
 create mode 100644 rust/macros/quote.rs

--
2.39.2
  

Comments

Andreas Hindborg March 30, 2023, 11:42 a.m. UTC | #1
y86-dev@protonmail.com writes:

> From: Gary Guo <gary@garyguo.net>
>
> Add the `quote!` macro for creating `TokenStream`s directly via the
> given Rust tokens. It also supports repetitions using iterators.
>
> It will be used by the pin-init API proc-macros to generate code.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Benno Lossin <y86-dev@protonmail.com>
> ---
>  rust/macros/lib.rs   |   2 +
>  rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 147 insertions(+)
>  create mode 100644 rust/macros/quote.rs
>
> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> index c1d385e345b9..82b520f024dd 100644
> --- a/rust/macros/lib.rs
> +++ b/rust/macros/lib.rs
> @@ -2,6 +2,8 @@
>
>  //! Crate for all kernel procedural macros.
>
> +#[macro_use]
> +mod quote;
>  mod concat_idents;
>  mod helpers;
>  mod module;
> diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
> new file mode 100644
> index 000000000000..7efb009c25a9
> --- /dev/null
> +++ b/rust/macros/quote.rs
> @@ -0,0 +1,145 @@
> +// SPDX-License-Identifier: Apache-2.0 OR MIT
> +
> +use proc_macro::{TokenStream, TokenTree};
> +
> +pub(crate) trait ToTokens {
> +    fn to_tokens(&self, tokens: &mut TokenStream);
> +}
> +
> +impl<T: ToTokens> ToTokens for Option<T> {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        if let Some(v) = self {
> +            v.to_tokens(tokens);
> +        }
> +    }
> +}
> +
> +impl ToTokens for proc_macro::Group {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend([TokenTree::from(self.clone())]);
> +    }
> +}
> +
> +impl ToTokens for TokenTree {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend([self.clone()]);
> +    }
> +}
> +
> +impl ToTokens for TokenStream {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend(self.clone());
> +    }
> +}
> +
> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
> +/// the given span.
> +///
> +/// This is a similar to the
> +/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
> +/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
> +#[allow(unused_macros)]
> +macro_rules! quote_spanned {
> +    ($span:expr => $($tt:tt)*) => {
> +    #[allow(clippy::vec_init_then_push)]
> +    {
> +        let mut tokens = Vec::new();
> +        let span = $span;
> +        quote_spanned!(@proc tokens span $($tt)*);
> +        proc_macro::TokenStream::from_iter(tokens)

Should this be `::alloc::vec::Vec` and
`::proc_macro::TokenStream::from_iter` ?

Best regards,
Andreas

> +    }};
> +    (@proc $v:ident $span:ident) => {};
> +    (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
> +        let mut ts = proc_macro::TokenStream::new();
> +        crate::quote::ToTokens::to_tokens(&$id, &mut ts);
> +        $v.extend(ts.into_iter());
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
> +        for token in $id {
> +            let mut ts = proc_macro::TokenStream::new();
> +            crate::quote::ToTokens::to_tokens(&token, &mut ts);
> +            $v.extend(ts.into_iter());
> +        }
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Parenthesis,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Bracket,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Brace,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident :: $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint))
> +        );
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident : $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident , $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident @ $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('@', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident ! $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('!', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
> +        $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(stringify!($id), $span)));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +}
> +
> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
> +/// mixed site span ([`Span::mixed_site()`]).
> +///
> +/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
> +/// from the `quote` crate but provides only just enough functionality needed by the current
> +/// `macros` crate.
> +///
> +/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
> +#[allow(unused_macros)]
> +macro_rules! quote {
> +    ($($tt:tt)*) => {
> +        quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*)
> +    }
> +}
  
Andreas Hindborg March 30, 2023, 12:01 p.m. UTC | #2
Andreas Hindborg <nmi@metaspace.dk> writes:

> y86-dev@protonmail.com writes:
>
>> From: Gary Guo <gary@garyguo.net>
>>
>> Add the `quote!` macro for creating `TokenStream`s directly via the
>> given Rust tokens. It also supports repetitions using iterators.
>>
>> It will be used by the pin-init API proc-macros to generate code.
>>
>> Signed-off-by: Gary Guo <gary@garyguo.net>
>> Signed-off-by: Benno Lossin <y86-dev@protonmail.com>
>> ---
>>  rust/macros/lib.rs   |   2 +
>>  rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 147 insertions(+)
>>  create mode 100644 rust/macros/quote.rs
>>
>> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
>> index c1d385e345b9..82b520f024dd 100644
>> --- a/rust/macros/lib.rs
>> +++ b/rust/macros/lib.rs
>> @@ -2,6 +2,8 @@
>>
>>  //! Crate for all kernel procedural macros.
>>
>> +#[macro_use]
>> +mod quote;
>>  mod concat_idents;
>>  mod helpers;
>>  mod module;
>> diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
>> new file mode 100644
>> index 000000000000..7efb009c25a9
>> --- /dev/null
>> +++ b/rust/macros/quote.rs
>> @@ -0,0 +1,145 @@
>> +// SPDX-License-Identifier: Apache-2.0 OR MIT
>> +
>> +use proc_macro::{TokenStream, TokenTree};
>> +
>> +pub(crate) trait ToTokens {
>> +    fn to_tokens(&self, tokens: &mut TokenStream);
>> +}
>> +
>> +impl<T: ToTokens> ToTokens for Option<T> {
>> +    fn to_tokens(&self, tokens: &mut TokenStream) {
>> +        if let Some(v) = self {
>> +            v.to_tokens(tokens);
>> +        }
>> +    }
>> +}
>> +
>> +impl ToTokens for proc_macro::Group {
>> +    fn to_tokens(&self, tokens: &mut TokenStream) {
>> +        tokens.extend([TokenTree::from(self.clone())]);
>> +    }
>> +}
>> +
>> +impl ToTokens for TokenTree {
>> +    fn to_tokens(&self, tokens: &mut TokenStream) {
>> +        tokens.extend([self.clone()]);
>> +    }
>> +}
>> +
>> +impl ToTokens for TokenStream {
>> +    fn to_tokens(&self, tokens: &mut TokenStream) {
>> +        tokens.extend(self.clone());
>> +    }
>> +}
>> +
>> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
>> +/// the given span.
>> +///
>> +/// This is a similar to the
>> +/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
>> +/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
>> +#[allow(unused_macros)]
>> +macro_rules! quote_spanned {
>> +    ($span:expr => $($tt:tt)*) => {
>> +    #[allow(clippy::vec_init_then_push)]
>> +    {
>> +        let mut tokens = Vec::new();
>> +        let span = $span;
>> +        quote_spanned!(@proc tokens span $($tt)*);
>> +        proc_macro::TokenStream::from_iter(tokens)
>
> Should this be `::alloc::vec::Vec` and
> `::proc_macro::TokenStream::from_iter` ?
>
> Best regards,
> Andreas
>
>> +    }};
>> +    (@proc $v:ident $span:ident) => {};
>> +    (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
>> +        let mut ts = proc_macro::TokenStream::new();
>> +        crate::quote::ToTokens::to_tokens(&$id, &mut ts);
>> +        $v.extend(ts.into_iter());
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
>> +        for token in $id {
>> +            let mut ts = proc_macro::TokenStream::new();
>> +            crate::quote::ToTokens::to_tokens(&token, &mut ts);
>> +            $v.extend(ts.into_iter());

Also, could this be without `into_iter()`: `$v.extend(ts)`?

BR Andreas

>> +        }
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
>> +        let mut tokens = Vec::new();
>> +        quote_spanned!(@proc tokens $span $($inner)*);
>> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
>> +            proc_macro::Delimiter::Parenthesis,
>> +            proc_macro::TokenStream::from_iter(tokens)
>> +        )));
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
>> +        let mut tokens = Vec::new();
>> +        quote_spanned!(@proc tokens $span $($inner)*);
>> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
>> +            proc_macro::Delimiter::Bracket,
>> +            proc_macro::TokenStream::from_iter(tokens)
>> +        )));
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
>> +        let mut tokens = Vec::new();
>> +        quote_spanned!(@proc tokens $span $($inner)*);
>> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
>> +            proc_macro::Delimiter::Brace,
>> +            proc_macro::TokenStream::from_iter(tokens)
>> +        )));
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident :: $($tt:tt)*) => {
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint))
>> +        );
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
>> +        );
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident : $($tt:tt)*) => {
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
>> +        );
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident , $($tt:tt)*) => {
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone))
>> +        );
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident @ $($tt:tt)*) => {
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('@', proc_macro::Spacing::Alone))
>> +        );
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident ! $($tt:tt)*) => {
>> +        $v.push(
>> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('!', proc_macro::Spacing::Alone))
>> +        );
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +    (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
>> +        $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(stringify!($id), $span)));
>> +        quote_spanned!(@proc $v $span $($tt)*);
>> +    };
>> +}
>> +
>> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
>> +/// mixed site span ([`Span::mixed_site()`]).
>> +///
>> +/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
>> +/// from the `quote` crate but provides only just enough functionality needed by the current
>> +/// `macros` crate.
>> +///
>> +/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
>> +#[allow(unused_macros)]
>> +macro_rules! quote {
>> +    ($($tt:tt)*) => {
>> +        quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*)
>> +    }
>> +}
  
Gary Guo March 30, 2023, 1:11 p.m. UTC | #3
On Thu, 30 Mar 2023 13:42:29 +0200
Andreas Hindborg <nmi@metaspace.dk> wrote:

> y86-dev@protonmail.com writes:
> 
> > From: Gary Guo <gary@garyguo.net>
> >
> > Add the `quote!` macro for creating `TokenStream`s directly via the
> > given Rust tokens. It also supports repetitions using iterators.
> >
> > It will be used by the pin-init API proc-macros to generate code.
> >
> > Signed-off-by: Gary Guo <gary@garyguo.net>
> > Signed-off-by: Benno Lossin <y86-dev@protonmail.com>
> > ---
> >  rust/macros/lib.rs   |   2 +
> >  rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++
> >  2 files changed, 147 insertions(+)
> >  create mode 100644 rust/macros/quote.rs
> >
> > diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> > index c1d385e345b9..82b520f024dd 100644
> > --- a/rust/macros/lib.rs
> > +++ b/rust/macros/lib.rs
> > @@ -2,6 +2,8 @@
> >
> >  //! Crate for all kernel procedural macros.
> >
> > +#[macro_use]
> > +mod quote;
> >  mod concat_idents;
> >  mod helpers;
> >  mod module;
> > diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
> > new file mode 100644
> > index 000000000000..7efb009c25a9
> > --- /dev/null
> > +++ b/rust/macros/quote.rs
> > @@ -0,0 +1,145 @@
> > +// SPDX-License-Identifier: Apache-2.0 OR MIT
> > +
> > +use proc_macro::{TokenStream, TokenTree};
> > +
> > +pub(crate) trait ToTokens {
> > +    fn to_tokens(&self, tokens: &mut TokenStream);
> > +}
> > +
> > +impl<T: ToTokens> ToTokens for Option<T> {
> > +    fn to_tokens(&self, tokens: &mut TokenStream) {
> > +        if let Some(v) = self {
> > +            v.to_tokens(tokens);
> > +        }
> > +    }
> > +}
> > +
> > +impl ToTokens for proc_macro::Group {
> > +    fn to_tokens(&self, tokens: &mut TokenStream) {
> > +        tokens.extend([TokenTree::from(self.clone())]);
> > +    }
> > +}
> > +
> > +impl ToTokens for TokenTree {
> > +    fn to_tokens(&self, tokens: &mut TokenStream) {
> > +        tokens.extend([self.clone()]);
> > +    }
> > +}
> > +
> > +impl ToTokens for TokenStream {
> > +    fn to_tokens(&self, tokens: &mut TokenStream) {
> > +        tokens.extend(self.clone());
> > +    }
> > +}
> > +
> > +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
> > +/// the given span.
> > +///
> > +/// This is a similar to the
> > +/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
> > +/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
> > +#[allow(unused_macros)]
> > +macro_rules! quote_spanned {
> > +    ($span:expr => $($tt:tt)*) => {
> > +    #[allow(clippy::vec_init_then_push)]
> > +    {
> > +        let mut tokens = Vec::new();
> > +        let span = $span;
> > +        quote_spanned!(@proc tokens span $($tt)*);
> > +        proc_macro::TokenStream::from_iter(tokens)  
> 
> Should this be `::alloc::vec::Vec` and
> `::proc_macro::TokenStream::from_iter` ?
> 
> Best regards,
> Andreas

Hi Andres,

This macro is used only internally, so I didn't really bother to fully
qualify all the names.

Best,
Gary
  
Alice Ryhl March 30, 2023, 1:33 p.m. UTC | #4
On 3/30/23 00:32, y86-dev@protonmail.com wrote:
> From: Gary Guo <gary@garyguo.net>
> 
> Add the `quote!` macro for creating `TokenStream`s directly via the
> given Rust tokens. It also supports repetitions using iterators.
> 
> It will be used by the pin-init API proc-macros to generate code.
> 
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Benno Lossin <y86-dev@protonmail.com>

Reviewed-by: Alice Ryhl <aliceryhl@google.com>
  
Andreas Hindborg March 31, 2023, 10:43 a.m. UTC | #5
y86-dev@protonmail.com writes:

> From: Gary Guo <gary@garyguo.net>
>
> Add the `quote!` macro for creating `TokenStream`s directly via the
> given Rust tokens. It also supports repetitions using iterators.
>
> It will be used by the pin-init API proc-macros to generate code.
>
> Signed-off-by: Gary Guo <gary@garyguo.net>
> Signed-off-by: Benno Lossin <y86-dev@protonmail.com>
> ---

Reviewed-by: Andreas Hindborg <a.hindborg@samsung.com>

>  rust/macros/lib.rs   |   2 +
>  rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 147 insertions(+)
>  create mode 100644 rust/macros/quote.rs
>
> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
> index c1d385e345b9..82b520f024dd 100644
> --- a/rust/macros/lib.rs
> +++ b/rust/macros/lib.rs
> @@ -2,6 +2,8 @@
>
>  //! Crate for all kernel procedural macros.
>
> +#[macro_use]
> +mod quote;
>  mod concat_idents;
>  mod helpers;
>  mod module;
> diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
> new file mode 100644
> index 000000000000..7efb009c25a9
> --- /dev/null
> +++ b/rust/macros/quote.rs
> @@ -0,0 +1,145 @@
> +// SPDX-License-Identifier: Apache-2.0 OR MIT
> +
> +use proc_macro::{TokenStream, TokenTree};
> +
> +pub(crate) trait ToTokens {
> +    fn to_tokens(&self, tokens: &mut TokenStream);
> +}
> +
> +impl<T: ToTokens> ToTokens for Option<T> {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        if let Some(v) = self {
> +            v.to_tokens(tokens);
> +        }
> +    }
> +}
> +
> +impl ToTokens for proc_macro::Group {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend([TokenTree::from(self.clone())]);
> +    }
> +}
> +
> +impl ToTokens for TokenTree {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend([self.clone()]);
> +    }
> +}
> +
> +impl ToTokens for TokenStream {
> +    fn to_tokens(&self, tokens: &mut TokenStream) {
> +        tokens.extend(self.clone());
> +    }
> +}
> +
> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
> +/// the given span.
> +///
> +/// This is a similar to the
> +/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
> +/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
> +#[allow(unused_macros)]
> +macro_rules! quote_spanned {
> +    ($span:expr => $($tt:tt)*) => {
> +    #[allow(clippy::vec_init_then_push)]
> +    {
> +        let mut tokens = Vec::new();
> +        let span = $span;
> +        quote_spanned!(@proc tokens span $($tt)*);
> +        proc_macro::TokenStream::from_iter(tokens)
> +    }};
> +    (@proc $v:ident $span:ident) => {};
> +    (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
> +        let mut ts = proc_macro::TokenStream::new();
> +        crate::quote::ToTokens::to_tokens(&$id, &mut ts);
> +        $v.extend(ts.into_iter());
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
> +        for token in $id {
> +            let mut ts = proc_macro::TokenStream::new();
> +            crate::quote::ToTokens::to_tokens(&token, &mut ts);
> +            $v.extend(ts.into_iter());
> +        }
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Parenthesis,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Bracket,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
> +        let mut tokens = Vec::new();
> +        quote_spanned!(@proc tokens $span $($inner)*);
> +        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
> +            proc_macro::Delimiter::Brace,
> +            proc_macro::TokenStream::from_iter(tokens)
> +        )));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident :: $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint))
> +        );
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident : $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident , $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident @ $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('@', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident ! $($tt:tt)*) => {
> +        $v.push(
> +            proc_macro::TokenTree::Punct(proc_macro::Punct::new('!', proc_macro::Spacing::Alone))
> +        );
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +    (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
> +        $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(stringify!($id), $span)));
> +        quote_spanned!(@proc $v $span $($tt)*);
> +    };
> +}
> +
> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
> +/// mixed site span ([`Span::mixed_site()`]).
> +///
> +/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
> +/// from the `quote` crate but provides only just enough functionality needed by the current
> +/// `macros` crate.
> +///
> +/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
> +#[allow(unused_macros)]
> +macro_rules! quote {
> +    ($($tt:tt)*) => {
> +        quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*)
> +    }
> +}
  

Patch

diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs
index c1d385e345b9..82b520f024dd 100644
--- a/rust/macros/lib.rs
+++ b/rust/macros/lib.rs
@@ -2,6 +2,8 @@ 

 //! Crate for all kernel procedural macros.

+#[macro_use]
+mod quote;
 mod concat_idents;
 mod helpers;
 mod module;
diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs
new file mode 100644
index 000000000000..7efb009c25a9
--- /dev/null
+++ b/rust/macros/quote.rs
@@ -0,0 +1,145 @@ 
+// SPDX-License-Identifier: Apache-2.0 OR MIT
+
+use proc_macro::{TokenStream, TokenTree};
+
+pub(crate) trait ToTokens {
+    fn to_tokens(&self, tokens: &mut TokenStream);
+}
+
+impl<T: ToTokens> ToTokens for Option<T> {
+    fn to_tokens(&self, tokens: &mut TokenStream) {
+        if let Some(v) = self {
+            v.to_tokens(tokens);
+        }
+    }
+}
+
+impl ToTokens for proc_macro::Group {
+    fn to_tokens(&self, tokens: &mut TokenStream) {
+        tokens.extend([TokenTree::from(self.clone())]);
+    }
+}
+
+impl ToTokens for TokenTree {
+    fn to_tokens(&self, tokens: &mut TokenStream) {
+        tokens.extend([self.clone()]);
+    }
+}
+
+impl ToTokens for TokenStream {
+    fn to_tokens(&self, tokens: &mut TokenStream) {
+        tokens.extend(self.clone());
+    }
+}
+
+/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
+/// the given span.
+///
+/// This is a similar to the
+/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the
+/// `quote` crate but provides only just enough functionality needed by the current `macros` crate.
+#[allow(unused_macros)]
+macro_rules! quote_spanned {
+    ($span:expr => $($tt:tt)*) => {
+    #[allow(clippy::vec_init_then_push)]
+    {
+        let mut tokens = Vec::new();
+        let span = $span;
+        quote_spanned!(@proc tokens span $($tt)*);
+        proc_macro::TokenStream::from_iter(tokens)
+    }};
+    (@proc $v:ident $span:ident) => {};
+    (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => {
+        let mut ts = proc_macro::TokenStream::new();
+        crate::quote::ToTokens::to_tokens(&$id, &mut ts);
+        $v.extend(ts.into_iter());
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => {
+        for token in $id {
+            let mut ts = proc_macro::TokenStream::new();
+            crate::quote::ToTokens::to_tokens(&token, &mut ts);
+            $v.extend(ts.into_iter());
+        }
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => {
+        let mut tokens = Vec::new();
+        quote_spanned!(@proc tokens $span $($inner)*);
+        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
+            proc_macro::Delimiter::Parenthesis,
+            proc_macro::TokenStream::from_iter(tokens)
+        )));
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => {
+        let mut tokens = Vec::new();
+        quote_spanned!(@proc tokens $span $($inner)*);
+        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
+            proc_macro::Delimiter::Bracket,
+            proc_macro::TokenStream::from_iter(tokens)
+        )));
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => {
+        let mut tokens = Vec::new();
+        quote_spanned!(@proc tokens $span $($inner)*);
+        $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new(
+            proc_macro::Delimiter::Brace,
+            proc_macro::TokenStream::from_iter(tokens)
+        )));
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident :: $($tt:tt)*) => {
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint))
+        );
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
+        );
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident : $($tt:tt)*) => {
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone))
+        );
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident , $($tt:tt)*) => {
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone))
+        );
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident @ $($tt:tt)*) => {
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new('@', proc_macro::Spacing::Alone))
+        );
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident ! $($tt:tt)*) => {
+        $v.push(
+            proc_macro::TokenTree::Punct(proc_macro::Punct::new('!', proc_macro::Spacing::Alone))
+        );
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+    (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => {
+        $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(stringify!($id), $span)));
+        quote_spanned!(@proc $v $span $($tt)*);
+    };
+}
+
+/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with
+/// mixed site span ([`Span::mixed_site()`]).
+///
+/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro
+/// from the `quote` crate but provides only just enough functionality needed by the current
+/// `macros` crate.
+///
+/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site
+#[allow(unused_macros)]
+macro_rules! quote {
+    ($($tt:tt)*) => {
+        quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*)
+    }
+}