Skip to content

Commit 692e893

Browse files
y86-devojeda
authored andcommitted
rust: types: add Opaque::ffi_init
This function allows to easily initialize `Opaque` with the pin-init API. `Opaque::ffi_init` takes a closure and returns a pin-initializer. This pin-initiailizer calls the given closure with a pointer to the inner `T`. Co-developed-by: Gary Guo <[email protected]> Signed-off-by: Gary Guo <[email protected]> Signed-off-by: Benno Lossin <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Fixed typo. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 8586f1a commit 692e893

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

rust/kernel/init.rs

+9
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@
177177
//! }
178178
//! ```
179179
//!
180+
//! For the special case where initializing a field is a single FFI-function call that cannot fail,
181+
//! there exist the helper function [`Opaque::ffi_init`]. This function initialize a single
182+
//! [`Opaque`] field by just delegating to the supplied closure. You can use these in combination
183+
//! with [`pin_init!`].
184+
//!
185+
//! For more information on how to use [`pin_init_from_closure()`], take a look at the uses inside
186+
//! the `kernel` crate. The [`sync`] module is a good starting point.
187+
//!
180188
//! [`sync`]: kernel::sync
181189
//! [pinning]: https://doc.rust-lang.org/std/pin/index.html
182190
//! [structurally pinned fields]:
@@ -187,6 +195,7 @@
187195
//! [`impl PinInit<T, E>`]: PinInit
188196
//! [`impl Init<T, E>`]: Init
189197
//! [`Opaque`]: kernel::types::Opaque
198+
//! [`Opaque::ffi_init`]: kernel::types::Opaque::ffi_init
190199
//! [`pin_data`]: ::macros::pin_data
191200
192201
use crate::{

rust/kernel/types.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Kernel types.
44
5+
use crate::init::{self, PinInit};
56
use alloc::boxed::Box;
67
use core::{
78
cell::UnsafeCell,
@@ -234,6 +235,25 @@ impl<T> Opaque<T> {
234235
Self(MaybeUninit::uninit())
235236
}
236237

238+
/// Creates a pin-initializer from the given initializer closure.
239+
///
240+
/// The returned initializer calls the given closure with the pointer to the inner `T` of this
241+
/// `Opaque`. Since this memory is uninitialized, the closure is not allowed to read from it.
242+
///
243+
/// This function is safe, because the `T` inside of an `Opaque` is allowed to be
244+
/// uninitialized. Additionally, access to the inner `T` requires `unsafe`, so the caller needs
245+
/// to verify at that point that the inner value is valid.
246+
pub fn ffi_init(init_func: impl FnOnce(*mut T)) -> impl PinInit<Self> {
247+
// SAFETY: We contain a `MaybeUninit`, so it is OK for the `init_func` to not fully
248+
// initialize the `T`.
249+
unsafe {
250+
init::pin_init_from_closure::<_, ::core::convert::Infallible>(move |slot| {
251+
init_func(Self::raw_get(slot));
252+
Ok(())
253+
})
254+
}
255+
}
256+
237257
/// Returns a raw pointer to the opaque data.
238258
pub fn get(&self) -> *mut T {
239259
UnsafeCell::raw_get(self.0.as_ptr())

0 commit comments

Comments
 (0)