qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rust: declare self as qemu_api for proc-macros
@ 2025-07-28 11:48 Manos Pitsidianakis
  2025-07-29 14:19 ` Paolo Bonzini
  2025-07-30  8:29 ` Zhao Liu
  0 siblings, 2 replies; 3+ messages in thread
From: Manos Pitsidianakis @ 2025-07-28 11:48 UTC (permalink / raw)
  To: qemu-devel; +Cc: qemu-rust, Manos Pitsidianakis

Fix an outstanding TODO.

Declaring `extern crate self as qemu_api` allows use of `qemu_api`
within the qemu_api crate; this allows the Wrapper derive macro and
future proc macros to be used interchangeably in the qemu_api crate and
other crates. This is not required currently and is only for
future-proofing.

Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
---
 rust/qemu-api-macros/src/lib.rs | 14 ++++++--------
 rust/qemu-api/src/lib.rs        |  3 +++
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
index b525d89c09e496a1f7f5582dc6d994e318f62bca..e01ac26cf3dd5a1b06ce095ef32b27a67e0def19 100644
--- a/rust/qemu-api-macros/src/lib.rs
+++ b/rust/qemu-api-macros/src/lib.rs
@@ -115,23 +115,21 @@ fn derive_opaque_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream
     let field = &get_unnamed_field(&input, "#[derive(Wrapper)]")?;
     let typ = &field.ty;
 
-    // TODO: how to add "::qemu_api"?  For now, this is only used in the
-    // qemu_api crate so it's not a problem.
     Ok(quote! {
-        unsafe impl crate::cell::Wrapper for #name {
-            type Wrapped = <#typ as crate::cell::Wrapper>::Wrapped;
+        unsafe impl ::qemu_api::cell::Wrapper for #name {
+            type Wrapped = <#typ as ::qemu_api::cell::Wrapper>::Wrapped;
         }
         impl #name {
-            pub unsafe fn from_raw<'a>(ptr: *mut <Self as crate::cell::Wrapper>::Wrapped) -> &'a Self {
+            pub unsafe fn from_raw<'a>(ptr: *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped) -> &'a Self {
                 let ptr = ::std::ptr::NonNull::new(ptr).unwrap().cast::<Self>();
                 unsafe { ptr.as_ref() }
             }
 
-            pub const fn as_mut_ptr(&self) -> *mut <Self as crate::cell::Wrapper>::Wrapped {
+            pub const fn as_mut_ptr(&self) -> *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped {
                 self.0.as_mut_ptr()
             }
 
-            pub const fn as_ptr(&self) -> *const <Self as crate::cell::Wrapper>::Wrapped {
+            pub const fn as_ptr(&self) -> *const <Self as ::qemu_api::cell::Wrapper>::Wrapped {
                 self.0.as_ptr()
             }
 
@@ -139,7 +137,7 @@ pub const fn as_void_ptr(&self) -> *mut ::core::ffi::c_void {
                 self.0.as_void_ptr()
             }
 
-            pub const fn raw_get(slot: *mut Self) -> *mut <Self as crate::cell::Wrapper>::Wrapped {
+            pub const fn raw_get(slot: *mut Self) -> *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped {
                 slot.cast()
             }
         }
diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
index 86dcd8ef17a9ecd040742a99e4b0421e19ec88f6..44d995cfdb990e0d17e7216cf4d1f04a5a980a1f 100644
--- a/rust/qemu-api/src/lib.rs
+++ b/rust/qemu-api/src/lib.rs
@@ -32,6 +32,9 @@
 pub mod vmstate;
 pub mod zeroable;
 
+// Allow proc-macros to refer to `::qemu_api` inside the `qemu_api` crate (this crate).
+extern crate self as qemu_api;
+
 use std::{
     alloc::{GlobalAlloc, Layout},
     ffi::c_void,

---
base-commit: c017386f28c03a03b8f14444f8671d3d8f7180fe
change-id: 20250728-self-as-qemu_api-de5ad829a034

--
γαῖα πυρί μιχθήτω



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: declare self as qemu_api for proc-macros
  2025-07-28 11:48 [PATCH] rust: declare self as qemu_api for proc-macros Manos Pitsidianakis
@ 2025-07-29 14:19 ` Paolo Bonzini
  2025-07-30  8:29 ` Zhao Liu
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2025-07-29 14:19 UTC (permalink / raw)
  To: Manos Pitsidianakis, qemu-devel; +Cc: qemu-rust

On 7/28/25 13:48, Manos Pitsidianakis wrote:
> Fix an outstanding TODO.
> 
> Declaring `extern crate self as qemu_api` allows use of `qemu_api`
> within the qemu_api crate; this allows the Wrapper derive macro and
> future proc macros to be used interchangeably in the qemu_api crate and
> other crates. This is not required currently and is only for
> future-proofing.
> 
> Signed-off-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>

I had no idea 'extern crate self' existed!  Nice, I queued it because 
it's useful for the purpose of splitting the qemu_api crate.

Paolo

> ---
>   rust/qemu-api-macros/src/lib.rs | 14 ++++++--------
>   rust/qemu-api/src/lib.rs        |  3 +++
>   2 files changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/rust/qemu-api-macros/src/lib.rs b/rust/qemu-api-macros/src/lib.rs
> index b525d89c09e496a1f7f5582dc6d994e318f62bca..e01ac26cf3dd5a1b06ce095ef32b27a67e0def19 100644
> --- a/rust/qemu-api-macros/src/lib.rs
> +++ b/rust/qemu-api-macros/src/lib.rs
> @@ -115,23 +115,21 @@ fn derive_opaque_or_error(input: DeriveInput) -> Result<proc_macro2::TokenStream
>       let field = &get_unnamed_field(&input, "#[derive(Wrapper)]")?;
>       let typ = &field.ty;
>   
> -    // TODO: how to add "::qemu_api"?  For now, this is only used in the
> -    // qemu_api crate so it's not a problem.
>       Ok(quote! {
> -        unsafe impl crate::cell::Wrapper for #name {
> -            type Wrapped = <#typ as crate::cell::Wrapper>::Wrapped;
> +        unsafe impl ::qemu_api::cell::Wrapper for #name {
> +            type Wrapped = <#typ as ::qemu_api::cell::Wrapper>::Wrapped;
>           }
>           impl #name {
> -            pub unsafe fn from_raw<'a>(ptr: *mut <Self as crate::cell::Wrapper>::Wrapped) -> &'a Self {
> +            pub unsafe fn from_raw<'a>(ptr: *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped) -> &'a Self {
>                   let ptr = ::std::ptr::NonNull::new(ptr).unwrap().cast::<Self>();
>                   unsafe { ptr.as_ref() }
>               }
>   
> -            pub const fn as_mut_ptr(&self) -> *mut <Self as crate::cell::Wrapper>::Wrapped {
> +            pub const fn as_mut_ptr(&self) -> *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped {
>                   self.0.as_mut_ptr()
>               }
>   
> -            pub const fn as_ptr(&self) -> *const <Self as crate::cell::Wrapper>::Wrapped {
> +            pub const fn as_ptr(&self) -> *const <Self as ::qemu_api::cell::Wrapper>::Wrapped {
>                   self.0.as_ptr()
>               }
>   
> @@ -139,7 +137,7 @@ pub const fn as_void_ptr(&self) -> *mut ::core::ffi::c_void {
>                   self.0.as_void_ptr()
>               }
>   
> -            pub const fn raw_get(slot: *mut Self) -> *mut <Self as crate::cell::Wrapper>::Wrapped {
> +            pub const fn raw_get(slot: *mut Self) -> *mut <Self as ::qemu_api::cell::Wrapper>::Wrapped {
>                   slot.cast()
>               }
>           }
> diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
> index 86dcd8ef17a9ecd040742a99e4b0421e19ec88f6..44d995cfdb990e0d17e7216cf4d1f04a5a980a1f 100644
> --- a/rust/qemu-api/src/lib.rs
> +++ b/rust/qemu-api/src/lib.rs
> @@ -32,6 +32,9 @@
>   pub mod vmstate;
>   pub mod zeroable;
>   
> +// Allow proc-macros to refer to `::qemu_api` inside the `qemu_api` crate (this crate).
> +extern crate self as qemu_api;
> +
>   use std::{
>       alloc::{GlobalAlloc, Layout},
>       ffi::c_void,
> 
> ---
> base-commit: c017386f28c03a03b8f14444f8671d3d8f7180fe
> change-id: 20250728-self-as-qemu_api-de5ad829a034
> 
> --
> γαῖα πυρί μιχθήτω
> 
> 
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rust: declare self as qemu_api for proc-macros
  2025-07-28 11:48 [PATCH] rust: declare self as qemu_api for proc-macros Manos Pitsidianakis
  2025-07-29 14:19 ` Paolo Bonzini
@ 2025-07-30  8:29 ` Zhao Liu
  1 sibling, 0 replies; 3+ messages in thread
From: Zhao Liu @ 2025-07-30  8:29 UTC (permalink / raw)
  To: Manos Pitsidianakis; +Cc: qemu-devel, qemu-rust

> diff --git a/rust/qemu-api/src/lib.rs b/rust/qemu-api/src/lib.rs
> index 86dcd8ef17a9ecd040742a99e4b0421e19ec88f6..44d995cfdb990e0d17e7216cf4d1f04a5a980a1f 100644
> --- a/rust/qemu-api/src/lib.rs
> +++ b/rust/qemu-api/src/lib.rs
> @@ -32,6 +32,9 @@
>  pub mod vmstate;
>  pub mod zeroable;
>  
> +// Allow proc-macros to refer to `::qemu_api` inside the `qemu_api` crate (this crate).
> +extern crate self as qemu_api;
> +
>  use std::{
>      alloc::{GlobalAlloc, Layout},
>      ffi::c_void,

I've learned something new as well. Though late,

Reviewed-by: Zhao Liu <zhao1.liu@intel.com>



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-30  8:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-28 11:48 [PATCH] rust: declare self as qemu_api for proc-macros Manos Pitsidianakis
2025-07-29 14:19 ` Paolo Bonzini
2025-07-30  8:29 ` Zhao Liu

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).