Skip to content

Commit 14513c0

Browse files
committed
rust: init: introduce Opaque::try_ffi_init
We'll need it, for example, when calling `register_filesystem` to initialise a file system registration. Signed-off-by: Wedson Almeida Filho <[email protected]>
1 parent 883e433 commit 14513c0

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

rust/kernel/types.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,22 @@ impl<T> Opaque<T> {
237237
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
238238
/// to verify at that point that the inner value is valid.
239239
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
240+
Self::try_ffi_init(move |slot| {
241+
init_func(slot);
242+
Ok(())
243+
})
244+
}
245+
246+
/// Similar to [`Self::ffi_init`], except that the closure can fail.
247+
///
248+
/// To avoid leaks on failure, the closure must drop any fields it has initialised before the
249+
/// failure.
250+
pub fn try_ffi_init<E>(
251+
init_func: impl FnOnce(*mut T) -> Result<(), E>,
252+
) -> impl PinInit<Self, E> {
240253
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
241254
// initialize the `T`.
242-
unsafe {
243-
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
244-
init_func(Self::raw_get(slot));
245-
Ok(())
246-
})
247-
}
255+
unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
248256
}
249257

250258
/// Returns a raw pointer to the opaque data.

0 commit comments

Comments
 (0)