Skip to content

Commit 1c1fce6

Browse files
author
Danilo Krummrich
committed
rust: types: add Opaque::pin_init
Analogous to `Opaque::new` add `Opaque::pin_init`, which instead of a value `T` takes a `PinInit<T>` and returns a `PinInit<Opaque<T>>`. Reviewed-by: Alice Ryhl <[email protected]> Suggested-by: Alice Ryhl <[email protected]> Signed-off-by: Danilo Krummrich <[email protected]>
1 parent d1000e8 commit 1c1fce6

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

rust/kernel/types.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ impl<T> Opaque<T> {
281281
}
282282
}
283283

284+
/// Create an opaque pin-initializer from the given pin-initializer.
285+
pub fn pin_init(slot: impl PinInit<T>) -> impl PinInit<Self> {
286+
Self::ffi_init(|ptr: *mut T| {
287+
// SAFETY:
288+
// - `ptr` is a valid pointer to uninitialized memory,
289+
// - `slot` is not accessed on error; the call is infallible,
290+
// - `slot` is pinned in memory.
291+
let _ = unsafe { init::PinInit::<T>::__pinned_init(slot, ptr) };
292+
})
293+
}
294+
284295
/// Creates a pin-initializer from the given initializer closure.
285296
///
286297
/// The returned initializer calls the given closure with the pointer to the inner `T` of this

0 commit comments

Comments
 (0)