lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230330141134.6d13090d.gary@garyguo.net>
Date:   Thu, 30 Mar 2023 14:11:34 +0100
From:   Gary Guo <gary@...yguo.net>
To:     Andreas Hindborg <nmi@...aspace.dk>
Cc:     y86-dev@...tonmail.com, Miguel Ojeda <ojeda@...nel.org>,
        Alex Gaynor <alex.gaynor@...il.com>,
        Wedson Almeida Filho <wedsonaf@...il.com>,
        Boqun Feng <boqun.feng@...il.com>,
        Björn Roy Baron <bjorn3_gh@...tonmail.com>,
        Alice Ryhl <alice@...l.io>, rust-for-linux@...r.kernel.org,
        linux-kernel@...r.kernel.org, patches@...ts.linux.dev
Subject: Re: [PATCH v3 01/13] rust: macros: add `quote!` macro

On Thu, 30 Mar 2023 13:42:29 +0200
Andreas Hindborg <nmi@...aspace.dk> wrote:

> y86-dev@...tonmail.com writes:
> 
> > From: Gary Guo <gary@...yguo.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@...yguo.net>
> > Signed-off-by: Benno Lossin <y86-dev@...tonmail.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

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ