[PATCH 1/7] rust: bql: add BqlRefCell::get_mut()

Paolo Bonzini posted 7 patches 1 week, 1 day ago
Maintainers: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>
[PATCH 1/7] rust: bql: add BqlRefCell::get_mut()
Posted by Paolo Bonzini 1 week, 1 day ago
This method is rarely useful in QEMU due to the pervasiveness of
shared references, but add it for when a &mut BqlRefCell<> is used.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 rust/bql/src/cell.rs | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/rust/bql/src/cell.rs b/rust/bql/src/cell.rs
index 24ab294b60d..8a0c8c14ad4 100644
--- a/rust/bql/src/cell.rs
+++ b/rust/bql/src/cell.rs
@@ -580,6 +580,25 @@ pub fn borrow_mut(&self) -> BqlRefMut<'_, T> {
         }
     }
 
+    /// Returns a mutable reference to the underlying data in this cell,
+    /// while the owner already has a mutable reference to the cell.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use bql::BqlRefCell;
+    ///
+    /// let mut c = BqlRefCell::new(5);
+    ///
+    /// *c.get_mut() = 10;
+    /// ```
+    #[inline]
+    pub const fn get_mut(&mut self) -> &mut T {
+        // SAFETY: there cannot be any outstanding borrow,
+        // since `self` is mutably owned.
+        unsafe { &mut *self.as_ptr() }
+    }
+
     /// Returns a raw pointer to the underlying data in this cell.
     ///
     /// # Examples
-- 
2.51.0
Re: [PATCH 1/7] rust: bql: add BqlRefCell::get_mut()
Posted by Zhao Liu 5 days ago
On Sat, Sep 20, 2025 at 04:29:52PM +0200, Paolo Bonzini wrote:
> Date: Sat, 20 Sep 2025 16:29:52 +0200
> From: Paolo Bonzini <pbonzini@redhat.com>
> Subject: [PATCH 1/7] rust: bql: add BqlRefCell::get_mut()
> X-Mailer: git-send-email 2.51.0
> 
> This method is rarely useful in QEMU due to the pervasiveness of
> shared references, but add it for when a &mut BqlRefCell<> is used.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  rust/bql/src/cell.rs | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
> 
> diff --git a/rust/bql/src/cell.rs b/rust/bql/src/cell.rs
> index 24ab294b60d..8a0c8c14ad4 100644
> --- a/rust/bql/src/cell.rs
> +++ b/rust/bql/src/cell.rs
> @@ -580,6 +580,25 @@ pub fn borrow_mut(&self) -> BqlRefMut<'_, T> {
>          }
>      }
>  
> +    /// Returns a mutable reference to the underlying data in this cell,
> +    /// while the owner already has a mutable reference to the cell.
> +    ///
> +    /// # Examples
> +    ///
> +    /// ```
> +    /// use bql::BqlRefCell;
> +    ///
> +    /// let mut c = BqlRefCell::new(5);
> +    ///
> +    /// *c.get_mut() = 10;
> +    /// ```
> +    #[inline]
> +    pub const fn get_mut(&mut self) -> &mut T {
> +        // SAFETY: there cannot be any outstanding borrow,
> +        // since `self` is mutably owned.
> +        unsafe { &mut *self.as_ptr() }

Why not use UnsafeCell::get_mut?

self.value.get_mut()

Regards,
Zhao
Re: [PATCH 1/7] rust: bql: add BqlRefCell::get_mut()
Posted by Paolo Bonzini 4 days, 3 hours ago
On Tue, Sep 23, 2025, 16:50 Zhao Liu <zhao1.liu@intel.com> wrote:

> > +    pub const fn get_mut(&mut self) -> &mut T {
> > +        // SAFETY: there cannot be any outstanding borrow,
> > +        // since `self` is mutably owned.
> > +        unsafe { &mut *self.as_ptr() }
>
> Why not use UnsafeCell::get_mut?
>
> self.value.get_mut()
>

Uh, of course. I thought it was too new, but it's even available as const
in 1.83.

Paolo

Regards,
> Zhao
>
>