From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1tgLQO-0007oI-1q for mharc-qemu-rust@gnu.org; Fri, 07 Feb 2025 05:17:28 -0500 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 1tgLPu-0007OK-D6 for qemu-rust@nongnu.org; Fri, 07 Feb 2025 05:16:59 -0500 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 1tgLPs-0004HU-SQ for qemu-rust@nongnu.org; Fri, 07 Feb 2025 05:16:58 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1738923416; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=e8ugkz2MCTVi9k0wZ/H/gZ6hjpuLYxOLaMc5B1rAzi0=; b=G4j6LK/sr1p3Kdnb4X+kDRK2pRTKa6ECz92C9PHYyPapxMsLeebB/XIO43ruMcvOKiy02y 8WCPudNbEvwahsdkMlxAYNQF707yuaXpR6MZ3Dr6X5DA2EKk8rxzRYAqPg9mEkClKH8b13 HL2fE4Zr/71unQl+x3W4gra3b2n65Qo= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-456-a7SFKtUMOyePakgVX8UHNA-1; Fri, 07 Feb 2025 05:16:55 -0500 X-MC-Unique: a7SFKtUMOyePakgVX8UHNA-1 X-Mimecast-MFC-AGG-ID: a7SFKtUMOyePakgVX8UHNA Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-ab397fff5a3so213369566b.1 for ; Fri, 07 Feb 2025 02:16:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738923413; x=1739528213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=e8ugkz2MCTVi9k0wZ/H/gZ6hjpuLYxOLaMc5B1rAzi0=; b=fvsIrqoLpiL82siPvMZTjJKw95EB/qna9o8hI0Qf+gCDFMPSB4K/zApH9XeIlJ9He+ UdVy/lfMx2ShvEmG0HaJQFp40auPfAE+iWJvqeq7sHtNxWu0CjQO0yBo5vyteuXBe8ra +eXtz2gQVqAA0+IA06JCULu/lxAIOCgJut6MskINvZ6PyAdaZZmA5Xjg+ue7duwKybSi MTEhx8IoVn/kIef3aXpg9eyBqbfO8kLxKjqdj0zhZ5YOgO1qv3HHDb/fo70I3jeOiugp WlA3NMK8bSqKe7rruRf/OVs7QKmjSf8CQcohmEfti/Xef65ddOAXlnQw0y9Xu9YYSkRy WQ6A== X-Gm-Message-State: AOJu0YylgP2D4/laq6vocICxut05RBB1CUIYYhfgn9QrJ0DKnc2dQWVa o9gbYQ5zqV22iowzuDqCrCwDq5aH9p8qR1Gtl+z8mEpbsGOAZEebhcCBMGXCLaTMVvk3G8xNwNU F3hCC0jO5scMX49gS6tFl87GGBojZl3TqWw429V2bgozUHyypexMi2j+guVG0mQ== X-Gm-Gg: ASbGnctcHDIanTsGjZwAQKuz2LyXjhiMRVeJPI9OyIEhWLRHkybHpVfSrDXUF77un86 T+kCao3l4QwL586cqmMY/WRhqTUeY+mbDBMNbGTP8OfrrFd2+mvmQMX9dYuw2+c1qK4f9iqTbiI uwi6ogGSKYIASYHv2MPFbURsACnChdPz4Lgyy1xLhyzl5ygd37gxv82NJtA81HBH+uPIxuAk2KX ocU+USIiK0vck//RnhhKKnxZBVxw9YZvXSOoVcqvIed3LeIwJlIO7qrJAUalb++Jp8icMOu9+ML /aZpuQ== X-Received: by 2002:a17:907:7e8c:b0:ab6:f782:3ee2 with SMTP id a640c23a62f3a-ab76e896c1cmr679680366b.9.1738923412819; Fri, 07 Feb 2025 02:16:52 -0800 (PST) X-Google-Smtp-Source: AGHT+IFM3lk2ydc7epwpkztg3w/h+v1br1TpLcOGK5HJ3x0fJvwRo4UJf6MhFQZ1H9LxVlL2Y6xFHw== X-Received: by 2002:a17:907:7e8c:b0:ab6:f782:3ee2 with SMTP id a640c23a62f3a-ab76e896c1cmr679677266b.9.1738923412411; Fri, 07 Feb 2025 02:16:52 -0800 (PST) Received: from [192.168.10.3] ([151.62.97.55]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab7732e7232sm241982766b.97.2025.02.07.02.16.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Feb 2025 02:16:46 -0800 (PST) From: Paolo Bonzini To: qemu-devel@nongnu.org Cc: qemu-rust@nongnu.org, zhao1.liu@intel.com Subject: [PATCH 05/12] rust: qom: allow initializing interface vtables Date: Fri, 7 Feb 2025 11:16:16 +0100 Message-ID: <20250207101623.2443552-6-pbonzini@redhat.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250207101623.2443552-1-pbonzini@redhat.com> References: <20250207101623.2443552-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ErpvCtFnUohi1kzUbh-AIy4zjlcbKZ81RMQT7QGf9MI_1738923414 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, 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_H2=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_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-rust@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: QEMU Rust-related patches and discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Feb 2025 10:16:59 -0000 Unlike regular classes, interface vtables can only be obtained via object_class_dynamic_cast. Provide a wrapper that allows accessing the vtable and pass it to a ClassInitImpl implementation, for example ClassInitImpl. Reviewed-by: Zhao Liu Signed-off-by: Paolo Bonzini --- rust/qemu-api/src/prelude.rs | 1 + rust/qemu-api/src/qom.rs | 45 ++++++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/rust/qemu-api/src/prelude.rs b/rust/qemu-api/src/prelude.rs index 87e3ce90f26..254edb476dd 100644 --- a/rust/qemu-api/src/prelude.rs +++ b/rust/qemu-api/src/prelude.rs @@ -9,6 +9,7 @@ pub use crate::qdev::DeviceMethods; +pub use crate::qom::InterfaceType; pub use crate::qom::IsA; pub use crate::qom::Object; pub use crate::qom::ObjectCast; diff --git a/rust/qemu-api/src/qom.rs b/rust/qemu-api/src/qom.rs index fad4759d7a6..fa69ae4c702 100644 --- a/rust/qemu-api/src/qom.rs +++ b/rust/qemu-api/src/qom.rs @@ -66,8 +66,8 @@ use crate::{ bindings::{ - self, object_dynamic_cast, object_get_class, object_get_typename, object_new, object_ref, - object_unref, TypeInfo, + self, object_class_dynamic_cast, object_dynamic_cast, object_get_class, + object_get_typename, object_new, object_ref, object_unref, TypeInfo, }, cell::bql_locked, }; @@ -263,6 +263,47 @@ unsafe fn as_object_mut_ptr(&self) -> *mut Object { } } +/// Trait exposed by all structs corresponding to QOM interfaces. +/// Unlike `ObjectType`, it is implemented on the class type (which provides +/// the vtable for the interfaces). +/// +/// # Safety +/// +/// `TYPE` must match the contents of the `TypeInfo` as found in the C code; +/// right now, interfaces can only be declared in C. +pub unsafe trait InterfaceType: Sized { + /// The name of the type, which can be passed to + /// `object_class_dynamic_cast()` to obtain the pointer to the vtable + /// for this interface. + const TYPE_NAME: &'static CStr; + + /// Initialize the vtable for the interface; the generic argument `T` is the + /// type being initialized, while the generic argument `U` is the type that + /// lists the interface in its `TypeInfo`. + /// + /// # Panics + /// + /// Panic if the incoming argument if `T` does not implement the interface. + fn interface_init< + T: ObjectType + ClassInitImpl + ClassInitImpl, + U: ObjectType, + >( + klass: &mut U::Class, + ) { + unsafe { + // SAFETY: upcasting to ObjectClass is always valid, and the + // return type is either NULL or the argument itself + let result: *mut Self = object_class_dynamic_cast( + (klass as *mut U::Class).cast(), + Self::TYPE_NAME.as_ptr(), + ) + .cast(); + + >::class_init(result.as_mut().unwrap()) + } + } +} + /// This trait provides safe casting operations for QOM objects to raw pointers, /// to be used for example for FFI. The trait can be applied to any kind of /// reference or smart pointers, and enforces correctness through the [`IsA`] -- 2.48.1