From nobody Sat Nov 15 17:48:31 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1748987323; cv=none; d=zohomail.com; s=zohoarc; b=R2pz1lhegRtE/RGZWSqhrbadEvmqb9pF9yYIgIMxiR/sK2bntAYbjJTLDUEFQu8NXKp/ainuCCLbhBYUQuziNGKbVLJPYoznziD4vRC6HZ2I/G69kno3kAO3xGhQq2h1mhbjUBMUYMpFCkd/mmYtS4ulkGXs2MDU6movlcYMpxw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1748987323; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=sm4FDV9XcvaUoJSoDVM8BTJfAISob2kgbvzU5BLD5+0=; b=NepsYeAdBhqlBRI09lOSl3wa7u8Y0AuXp7BiNew3Qm5aFeoDSboKksaHHyil2vtux2GsiA6IILktL/5ZwwSjONz+A5UDWIUHXBPWrlCcGYjyd61sUgtaW2ZiRbt4ZJ3Ubi4OrJneYjwk5l8TMgrbx1/HCPp4gw507UZ4Id9+Mik= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1748987323161995.0263775876625; Tue, 3 Jun 2025 14:48:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1uMZSz-0005ak-KY; Tue, 03 Jun 2025 17:46:42 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uMZSS-0004yV-RB for qemu-devel@nongnu.org; Tue, 03 Jun 2025 17:46:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uMZSP-00068Y-LY for qemu-devel@nongnu.org; Tue, 03 Jun 2025 17:46:08 -0400 Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-468-xByV4RX5OGOR2edI7JHqKw-1; Tue, 03 Jun 2025 17:46:03 -0400 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-3a4f6cc5332so3209102f8f.2 for ; Tue, 03 Jun 2025 14:46:03 -0700 (PDT) Received: from [192.168.10.48] ([151.49.64.79]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a4efe5b871sm19777113f8f.13.2025.06.03.14.45.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jun 2025 14:45:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1748987164; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sm4FDV9XcvaUoJSoDVM8BTJfAISob2kgbvzU5BLD5+0=; b=EuKHFjlFCoh17FGsSAlJYe1sLwVsmJ2jDA1SocAF3/zUR+qQu7uAc0czz2qDcwTNasP+cA +kDyginFr4WWjavguRYeF+1KpYgBcGiLz2ei9ioTN1Tx82d7iQcgn2DZVyHeCg8UFv5u/l j5YdteSPS1YUW68VKBnEjI/HsYRjoq4= X-MC-Unique: xByV4RX5OGOR2edI7JHqKw-1 X-Mimecast-MFC-AGG-ID: xByV4RX5OGOR2edI7JHqKw_1748987162 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1748987161; x=1749591961; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=sm4FDV9XcvaUoJSoDVM8BTJfAISob2kgbvzU5BLD5+0=; b=qxtfC9D5pQNlEbQLmpsas3N6o+g9CGNkDjQf8784HTl3ADGSfS7InUkgkEO6qC77GX wx6MvFaM9/h/NVB/i7XboY9g7mQS2Uvs1wQRmaZtSacfOWDof57K4HhTDd9sdTSIrTtU S6ctx6PALFvOwTesIKsQjur0PH8XIAxs8TUeqz4bQ7BQ0wQ4HM0EbW01JUW1u10MAuav VBegsTozZz8ZgqSi+DZun3zx6Nv9F+eWLgpQZBAvFSfGiXYZ+N1Z1apR5GMXZgvH/AnM rSWkoRwaMegDhjnD3Qydt/237BNVBPLYPRfRWTtLB1GP0y8abOc774bJc0v+8+A1cIcs cgjQ== X-Gm-Message-State: AOJu0YyTkbDJid7uk3qH8G2hw1YANL7iouPjh4t/C9vXpp3WKPPp+wsy 95WF2VJ63L9p8oDj2Vbz4qBUtpr+Z0qA+5kGiyoUQTq9NHndas+6qHPadNqGfgi/MR3BDwpv99S XhjDH+751vDeCeyh4YgGuj+b2WJGFr8IpoklTM46W8KDTdlEtLGjk0THT8WAwTucB7YPcUGEi65 wXB8l+hS+7rY/qlRqfLoawVyFONZF504AdXeLFvvp/ X-Gm-Gg: ASbGncttboOiGR8PWjIcN5csHzfHyIc8hqmRnQBJep+B37WMdX0UW16l3ekJj11YDsh ZFhInBJqSQYK7HT1ZTwRdQtz9ZY49/XW0el5tPuG+xcJixjSi264Q/LS/Wm+4b0qlQUEKOMW+aq Mtfp5TEdqggPUCnMJnpD7DB17Lla/1Aa+7x5L3HoB51zVUFjOgG9jRZNc4TGpy35w6hS5mFb+/H 4xaSeQr4n6hShBZ5jdPiombq3T04TZrxlMIn6cRzphR3nBdYLvq6BdO/asHvoohwLaPCcnSiWop rF9kgcHaiO+gjg== X-Received: by 2002:a05:6000:381:b0:3a0:b940:d479 with SMTP id ffacd0b85a97d-3a51d97982amr258396f8f.53.1748987161461; Tue, 03 Jun 2025 14:46:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtoZMiK+sn5VloQukrhegxv82SPV8te6CzEGyaHsfhD7kGxZtOG0CaGD0nl5tNpCbIXFfNIQ== X-Received: by 2002:a05:6000:381:b0:3a0:b940:d479 with SMTP id ffacd0b85a97d-3a51d97982amr258376f8f.53.1748987160979; Tue, 03 Jun 2025 14:46:00 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Subject: [PULL 16/16] rust: qemu-api-macros: add from_bits and into_bits to #[derive(TryInto)] Date: Tue, 3 Jun 2025 23:45:23 +0200 Message-ID: <20250603214523.131185-17-pbonzini@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250603214523.131185-1-pbonzini@redhat.com> References: <20250603214523.131185-1-pbonzini@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=pbonzini@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -21 X-Spam_score: -2.2 X-Spam_bar: -- X-Spam_report: (-2.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.128, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1748987324172116600 Content-Type: text/plain; charset="utf-8" These const functions make it possible to use enums easily together with the bitfield-struct crate. Signed-off-by: Paolo Bonzini --- rust/qemu-api-macros/src/lib.rs | 48 ++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib= .rs index ceb79f09f97..103470785e3 100644 --- a/rust/qemu-api-macros/src/lib.rs +++ b/rust/qemu-api-macros/src/lib.rs @@ -193,23 +193,51 @@ fn get_variants(input: &DeriveInput) -> Result<&Punct= uated, Macr } =20 #[rustfmt::skip::macros(quote)] -fn derive_tryinto_or_error(input: DeriveInput) -> Result { - let repr =3D get_repr_uN(&input, "#[derive(TryInto)]")?; - - let name =3D &input.ident; - let variants =3D get_variants(&input)?; +fn derive_tryinto_body( + name: &Ident, + variants: &Punctuated, + repr: &Path, +) -> Result { let discriminants: Vec<&Ident> =3D variants.iter().map(|f| &f.ident).c= ollect(); =20 Ok(quote! { + #(const #discriminants: #repr =3D #name::#discriminants as #repr;)= *; + match value { + #(#discriminants =3D> Ok(#name::#discriminants),)* + _ =3D> Err(value), + } + }) +} + +#[rustfmt::skip::macros(quote)] +fn derive_tryinto_or_error(input: DeriveInput) -> Result { + let repr =3D get_repr_uN(&input, "#[derive(TryInto)]")?; + let name =3D &input.ident; + let body =3D derive_tryinto_body(name, get_variants(&input)?, &repr)?; + let errmsg =3D format!("invalid value for {name}"); + + Ok(quote! { + impl #name { + #[allow(dead_code)] + pub const fn into_bits(self) -> #repr { + self as #repr + } + + #[allow(dead_code)] + pub const fn from_bits(value: #repr) -> Self { + match ({ + #body + }) { + Ok(x) =3D> x, + Err(_) =3D> panic!(#errmsg) + } + } + } impl core::convert::TryFrom<#repr> for #name { type Error =3D #repr; =20 fn try_from(value: #repr) -> Result { - #(const #discriminants: #repr =3D #name::#discriminants as= #repr;)*; - match value { - #(#discriminants =3D> Ok(Self::#discriminants),)* - _ =3D> Err(value), - } + #body } } }) --=20 2.49.0