[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20241022213221.2383-2-dakr@kernel.org>
Date: Tue, 22 Oct 2024 23:31:38 +0200
From: Danilo Krummrich <dakr@...nel.org>
To: gregkh@...uxfoundation.org,
rafael@...nel.org,
bhelgaas@...gle.com,
ojeda@...nel.org,
alex.gaynor@...il.com,
boqun.feng@...il.com,
gary@...yguo.net,
bjorn3_gh@...tonmail.com,
benno.lossin@...ton.me,
tmgross@...ch.edu,
a.hindborg@...sung.com,
aliceryhl@...gle.com,
airlied@...il.com,
fujita.tomonori@...il.com,
lina@...hilina.net,
pstanner@...hat.com,
ajanulgu@...hat.com,
lyude@...hat.com,
robh@...nel.org,
daniel.almeida@...labora.com,
saravanak@...gle.com
Cc: rust-for-linux@...r.kernel.org,
linux-kernel@...r.kernel.org,
linux-pci@...r.kernel.org,
devicetree@...r.kernel.org,
Wedson Almeida Filho <walmeida@...rosoft.com>,
Danilo Krummrich <dakr@...nel.org>
Subject: [PATCH v3 01/16] rust: init: introduce `Opaque::try_ffi_init`
From: Wedson Almeida Filho <walmeida@...rosoft.com>
This is the same as `Opaque::ffi_init`, but returns a `Result`.
This is used by subsequent patches to implement the FFI init of static
driver structures on registration.
Signed-off-by: Wedson Almeida Filho <walmeida@...rosoft.com>
Signed-off-by: Danilo Krummrich <dakr@...nel.org>
---
rust/kernel/types.rs | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)
diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs
index ced143600eb1..236e8de5844b 100644
--- a/rust/kernel/types.rs
+++ b/rust/kernel/types.rs
@@ -239,14 +239,22 @@ pub const fn uninit() -> Self {
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
/// to verify at that point that the inner value is valid.
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
+ Self::try_ffi_init(move |slot| {
+ init_func(slot);
+ Ok(())
+ })
+ }
+
+ /// Similar to [`Self::ffi_init`], except that the closure can fail.
+ ///
+ /// To avoid leaks on failure, the closure must drop any fields it has initialised before the
+ /// failure.
+ pub fn try_ffi_init<E>(
+ init_func: impl FnOnce(*mut T) -> Result<(), E>,
+ ) -> impl PinInit<Self, E> {
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
// initialize the `T`.
- unsafe {
- init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
- init_func(Self::raw_get(slot));
- Ok(())
- })
- }
+ unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
}
/// Returns a raw pointer to the opaque data.
--
2.46.2
Powered by blists - more mailing lists