Macro kernel::try_pin_init

source ·
macro_rules! try_pin_init {
    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
        $($fields:tt)*
    }) => { ... };
    ($(&$this:ident in)? $t:ident $(::<$($generics:ty),* $(,)?>)? {
        $($fields:tt)*
    }? $err:ty) => { ... };
}
Expand description

Construct an in-place, fallible pinned initializer for structs.

If the initialization can complete without error (or Infallible), then use pin_init!.

You can use the ? operator or use return Err(err) inside the initializer to stop initialization and return the error.

IMPORTANT: if you have unsafe code inside of the initializer you have to ensure that when initialization fails, the memory can be safely deallocated without any further modifications.

This macro defaults the error to Error.

The syntax is identical to pin_init! with the following exception: you can append ? $type after the struct initializer to specify the error type you want to use.

Examples

use kernel::{init::{self, PinInit}, error::Error};
#[pin_data]
struct BigBuf {
    big: Box<[u8; 1024 * 1024 * 1024]>,
    small: [u8; 1024 * 1024],
    ptr: *mut u8,
}

impl BigBuf {
    fn new() -> impl PinInit<Self, Error> {
        try_pin_init!(Self {
            big: Box::init(init::zeroed())?,
            small: [0; 1024 * 1024],
            ptr: core::ptr::null_mut(),
        }? Error)
    }
}
This documentation is an old archive. Please see https://rust.docs.kernel.org instead.