Skip to content

Commit 9c49161

Browse files
wedsonafdakr
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 4104105 commit 9c49161

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
@@ -259,14 +259,22 @@ impl<T> Opaque<T> {
259259
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
260260
/// to verify at that point that the inner value is valid.
261261
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
262+
Self::try_ffi_init(move |slot| {
263+
init_func(slot);
264+
Ok(())
265+
})
266+
}
267+
268+
/// Similar to [`Self::ffi_init`], except that the closure can fail.
269+
///
270+
/// To avoid leaks on failure, the closure must drop any fields it has initialised before the
271+
/// failure.
272+
pub fn try_ffi_init<E>(
273+
init_func: impl FnOnce(*mut T) -> Result<(), E>,
274+
) -> impl PinInit<Self, E> {
262275
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
263276
// initialize the `T`.
264-
unsafe {
265-
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
266-
init_func(Self::raw_get(slot));
267-
Ok(())
268-
})
269-
}
277+
unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
270278
}
271279

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

0 commit comments

Comments
 (0)