Skip to content

Commit a13e700

Browse files
wedsonafValentin Obst
authored andcommitted
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 bd2d900 commit a13e700

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
@@ -240,14 +240,22 @@ impl<T> Opaque<T> {
240240
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
241241
/// to verify at that point that the inner value is valid.
242242
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
243+
Self::try_ffi_init(move |slot| {
244+
init_func(slot);
245+
Ok(())
246+
})
247+
}
248+
249+
/// Similar to [`Self::ffi_init`], except that the closure can fail.
250+
///
251+
/// To avoid leaks on failure, the closure must drop any fields it has initialised before the
252+
/// failure.
253+
pub fn try_ffi_init<E>(
254+
init_func: impl FnOnce(*mut T) -> Result<(), E>,
255+
) -> impl PinInit<Self, E> {
243256
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
244257
// initialize the `T`.
245-
unsafe {
246-
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
247-
init_func(Self::raw_get(slot));
248-
Ok(())
249-
})
250-
}
258+
unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
251259
}
252260

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

0 commit comments

Comments
 (0)