[PATCH] rust: sync: create the `get_mut()` function
From: Guilherme Giacomo Simoes
Date: Thu Jan 30 2025 - 13:52:18 EST
Create a `get_mut()` function that receive a mutable instance of Lock,
and return a mutable reference to data because if the instance is
mutable, the rust compiler guarantee the access control.
Suggested-by: Björn Roy Baron <bjorn3_gh@xxxxxxxxxxxxxx>
Signed-off-by: Guilherme Giacomo Simoes <trintaeoitogc@xxxxxxxxx>
---
rust/kernel/sync/lock.rs | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs
index eb80048e0110..3f9d78bcb37c 100644
--- a/rust/kernel/sync/lock.rs
+++ b/rust/kernel/sync/lock.rs
@@ -140,6 +140,12 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni
}),
})
}
+
+ /// Get a mutable reference to data
+ pub fn get_mut(&mut self) -> &mut T {
+ // SAFETY: the caller must guarantee that the instance is only used in one place
+ unsafe { &mut *self.data.get() }
+ }
}
impl<B: Backend> Lock<(), B> {
--
2.34.1