Skip to content

Commit 43c43ad

Browse files
author
Valentin Obst
committed
rust: add try-ffi-init
IMPORTANT: This work is by Wedson A.F.! This patch will not be send to the lists. If patches are send to the list, they will be based on a tree that contains these patches.
1 parent 675309b commit 43c43ad

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

rust/kernel/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ impl<T> Opaque<T> {
250250
}
251251
}
252252

253+
/// Similar to [`Self::ffi_init`], except that the closure can fail.
254+
///
255+
/// To avoid leaks on failure, the closure must drop any fields it has initialised before the
256+
/// failure.
257+
pub fn try_ffi_init<E>(
258+
init_func: impl FnOnce(*mut T) -> Result<(), E>,
259+
) -> impl PinInit<Self, E> {
260+
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
261+
// initialize the `T`.
262+
unsafe { init::pin_init_from_closure(|slot| init_func(Self::raw_get(slot))) }
263+
}
264+
253265
/// Returns a raw pointer to the opaque data.
254266
pub fn get(&self) -> *mut T {
255267
UnsafeCell::get(&self.value).cast::<T>()

0 commit comments

Comments
 (0)