qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Junjie Mao <junjie.mao@hotmail.com>,
	Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
Cc: qemu-devel@nongnu.org
Subject: Re: [PATCH] rust: introduce alternative implementation of offset_of!
Date: Mon, 21 Oct 2024 08:31:59 +0200	[thread overview]
Message-ID: <5db62c58-b6e3-4f83-9954-ff2bcb1108bc@redhat.com> (raw)
In-Reply-To: <SY0P300MB10265E25557DB71EE426525795432@SY0P300MB1026.AUSP300.PROD.OUTLOOK.COM>

On 10/21/24 07:40, Junjie Mao wrote:
> offset_of! was stabilized in Rust 1.77.0. Use an alternative implemenation
> that was found on the Rust forums, and whose author agreed to license as
> MIT for use in QEMU.
> 
> The alternative allows only one level of field access, but apart from this
> can be used just by replacing core::mem::offset_of! with
> qemu_api::offset_of!.
> 
> Using offset_of! prior Rust 1.77.0 requires the structure to have the
> derive(qemu_api_macros::Offsets) attribute.
> 
> Apply as a replacement of 10/16 of Paolo's RFC series [1].
> 
> Also remove subprojects/syn-2.0.66 if there is an existing build. An additional
> feature cfg is added to packagefiles/syn-2-rs/meson.build, which requires meson
> to re-checkout the subproject.
> 
> [1] https://lore.kernel.org/qemu-devel/20241015131735.518771-1-pbonzini@redhat.com
> 
> Co-authored-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Junjie Mao <junjie.mao@hotmail.com>

Thanks.  I still prefer to keep the procedural macro code minimal, and 
have the code generation in a separate macro, but this is a nice start!

> +fn is_c_repr(input: &DeriveInput) -> Result<(), proc_macro2::TokenStream> {
> +    let expected = parse_quote! { #[repr(C)] };
> +
> +    for attr in &input.attrs {
> +        if attr == &expected {
> +            return Ok(());
> +        }
> +    }
> +
> +    Err(quote! { "Can only generate offsets for structs with a C representation." })
> +}

Probably can use any() here.

> +/// A derive macro that generate field offsets for using `offset_of!` in
> +/// versions of Rust prior to 1.77
> +#[proc_macro_derive(Offsets)]
> +pub fn derive_offsets(input: TokenStream) -> TokenStream {
> +    let input = parse_macro_input!(input as DeriveInput);
> +
> +    let expanded = match derive_offsets_or_error(input) {
> +        Ok(ts) => ts,
> +        Err(msg) => quote! { compile_error!(#msg); },

It should use quote_spanned! here.

Paolo



  reply	other threads:[~2024-10-21  6:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-21  5:40 [PATCH] rust: introduce alternative implementation of offset_of! Junjie Mao
2024-10-21  6:31 ` Paolo Bonzini [this message]
2024-10-21  6:46   ` Junjie Mao
2024-10-21  8:04     ` Paolo Bonzini
2024-10-22  2:39       ` Junjie Mao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5db62c58-b6e3-4f83-9954-ff2bcb1108bc@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=junjie.mao@hotmail.com \
    --cc=manos.pitsidianakis@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).