Skip to content

Commit 4e6b48c

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 bee8e1d commit 4e6b48c

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
@@ -247,6 +247,18 @@ impl<T> Opaque<T> {
247247
}
248248
}
249249

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

0 commit comments

Comments
 (0)