Skip to content

Commit dfa8e7c

Browse files
wedsonafDanilo Krummrich
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 a2f1154 commit dfa8e7c

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
@@ -289,14 +289,22 @@ impl<T> Opaque<T> {
289289
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
290290
/// to verify at that point that the inner value is valid.
291291
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
292+
Self::try_ffi_init(move |slot| {
293+
init_func(slot);
294+
Ok(())
295+
})
296+
}
297+
298+
/// Similar to [`Self::ffi_init`], except that the closure can fail.
299+
///
300+
/// To avoid leaks on failure, the closure must drop any fields it has initialised before the
301+
/// failure.
302+
pub fn try_ffi_init<E>(
303+
init_func: impl FnOnce(*mut T) -> Result<(), E>,
304+
) -> impl PinInit<Self, E> {
292305
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
293306
// initialize the `T`.
294-
unsafe {
295-
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
296-
init_func(Self::raw_get(slot));
297-
Ok(())
298-
})
299-
}
307+
unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
300308
}
301309

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

0 commit comments

Comments
 (0)