[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[RFC 03/13] rust/cell: add get_mut() method for BqlCell
From: |
Zhao Liu |
Subject: |
[RFC 03/13] rust/cell: add get_mut() method for BqlCell |
Date: |
Thu, 5 Dec 2024 14:07:04 +0800 |
The get_mut() is useful when doing compound assignment operations, e.g.,
*c.get_mut() += 1.
Implement get_mut() for BqlCell by referring to Cell.
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
rust/qemu-api/src/cell.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/rust/qemu-api/src/cell.rs b/rust/qemu-api/src/cell.rs
index 07b636f26266..95f1cc0b3eb5 100644
--- a/rust/qemu-api/src/cell.rs
+++ b/rust/qemu-api/src/cell.rs
@@ -324,6 +324,31 @@ impl<T> BqlCell<T> {
pub const fn as_ptr(&self) -> *mut T {
self.value.get()
}
+
+ /// Returns a mutable reference to the underlying data.
+ ///
+ /// This call borrows `BqlCell` mutably (at compile-time) which guarantees
+ /// that we possess the only reference.
+ ///
+ /// However be cautious: this method expects `self` to be mutable, which is
+ /// generally not the case when using a `BqlCell`. If you require interior
+ /// mutability by reference, consider using `BqlRefCell` which provides
+ /// run-time checked mutable borrows through its [`borrow_mut`] method.
+ ///
+ /// [`borrow_mut`]: BqlRefCell::borrow_mut()
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use qemu_api::cell::BqlCell;;
+ ///
+ /// let mut c = BqlCell::new(5);
+ /// *c.get_mut() += 1;
+ ///
+ /// assert_eq!(c.get(), 6);
+ pub fn get_mut(&mut self) -> &mut T {
+ self.value.get_mut()
+ }
}
impl<T: Default> BqlCell<T> {
--
2.34.1
[RFC 05/13] rust: add a bit operation binding for deposit64, Zhao Liu, 2024/12/05