8000 rust: xarray: add mutable access through `Guard` · Rust-for-Linux/linux@5f1db1a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f1db1a

Browse files
committed
rust: xarray: add mutable access through Guard
This patch adds mutable access through the XArray Guard. Signed-off-by: Andreas Hindborg <a.hindborg@samsung.com>
1 parent 78e5379 commit 5f1db1a

File tree

1 file changed

+36
-1
lines changed
Expand file tree

1 file changed

+36
-1
lines changed

rust/kernel/xarray.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ use crate::{
1010
prelude::*,
1111
types::{ForeignOwnable, Opaque, ScopeGuard},
1212
};
13-
use core::{ffi::c_ulong, marker::PhantomData, mem, ops::Deref, ptr::NonNull};
13+
use core::{
14+
ffi::c_ulong,
15+
marker::PhantomData,
16+
mem,
17+
ops::{Deref, DerefMut},
18+
pin::Pin,
19+
ptr::NonNull,
20+
};
1421

1522
/// Flags passed to `XArray::new` to configure the `XArray`.
1623
type Flags = bindings::gfp_t;
@@ -75,6 +82,20 @@ impl<'a, T: ForeignOwnable> Guard<'a, T> {
7582
// (`self.0`).
7683
unsafe { T::borrow(self.0.as_ptr().cast()) }
7784
}
85+
86+
/// Mutably borrow the underlying value wrapped by the `Guard`.
87+
///
88+
/// Returns a `T::BorrowedMut` type for the owned `ForeignOwnable` type.
89+
pub fn borrow_mut(&mut self) -> T::BorrowedMut<'_> {
90+
// SAFETY: `self.0` was produced by a call to `into_foreign` when it was
91+
// inserted into the `XArray`. Because obtaining a `Guard` for the
92+
// underlying value requires taking a lock, this is the only guard in
93+
// existence for the value pointed to by `self.0`. Becuase of the
94+
// existence of `&'a mut self` and the transfer of the lifetime `'a` to
95+
// the return type of this function, the value returned by `borrow_mut`
96+
// cannot overlap with other calls to `borrow` or `borrow_mut`.
97+
unsafe { T::borrow_mut(self.0.as_ptr() as _) }
98+
}
7899
}
79100

80101
// Convenience impl for `ForeignOwnable` types whose `Borrowe 8000 d`
@@ -91,6 +112,20 @@ where
91112
}
92113
}
93114

115+
// Convenience impl for `ForeignOwnable` types whose `Borrowed` form implements
116+
// `DerefMut`
117+
impl<'a, T: ForeignOwnable> DerefMut for Guard<'a, T>
118+
where
119+
T::Borrowed<'a>: Deref,
120+
T::BorrowedMut<'a>: DerefMut,
121+
for<'b> T::Borrowed<'b>: Into<&'b <T::Borrowed<'a> as Deref>::Target>,
122+
for<'b> T::BorrowedMut<'b>: Into<&'b mut <T::Borrowed<'a> as Deref>::Target>,
123+
{
124+
fn deref_mut(&mut self) -> &mut Self::Target {
125+
self.borrow_mut().into()
126+
}
127+
}
128+
94129
impl<'a, T: ForeignOwnable> Drop for Guard<'a, T> {
95130
fn drop(&mut self) {
96131
// SAFETY: By the type invariant, we own the XArray lock, so we must

0 commit comments

Comments
 (0)
0