Skip to content

Commit da4a526

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]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent 8d8f785 commit da4a526

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

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

0 commit comments

Comments
 (0)