From: Danilo Krummrich Date: Thu, 19 Dec 2024 17:04:07 +0000 (+0100) Subject: rust: types: add `Opaque::pin_init` X-Git-Tag: v6.14-rc1~55^2~82 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2d3bf6ffe26439444b55dd5af7b06d1aca3a042d;p=thirdparty%2Fkernel%2Flinux.git rust: types: add `Opaque::pin_init` Analogous to `Opaque::new` add `Opaque::pin_init`, which instead of a value `T` takes a `PinInit` and returns a `PinInit>`. Reviewed-by: Alice Ryhl Suggested-by: Alice Ryhl Signed-off-by: Danilo Krummrich Tested-by: Dirk Behme Tested-by: Fabien Parent Link: https://lore.kernel.org/r/20241219170425.12036-6-dakr@kernel.org Signed-off-by: Greg Kroah-Hartman --- diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs index ec6457bb3084a..3aea6af9a0bca 100644 --- a/rust/kernel/types.rs +++ b/rust/kernel/types.rs @@ -281,6 +281,17 @@ impl Opaque { } } + /// Create an opaque pin-initializer from the given pin-initializer. + pub fn pin_init(slot: impl PinInit) -> impl PinInit { + Self::ffi_init(|ptr: *mut T| { + // SAFETY: + // - `ptr` is a valid pointer to uninitialized memory, + // - `slot` is not accessed on error; the call is infallible, + // - `slot` is pinned in memory. + let _ = unsafe { init::PinInit::::__pinned_init(slot, ptr) }; + }) + } + /// Creates a pin-initializer from the given initializer closure. /// /// The returned initializer calls the given closure with the pointer to the inner `T` of this