try_pin_init in kernel - Rust
Skip to main content try_pin_init kernel Macro try_ pin_ init Source macro_rules! try_pin_init { ($($args:tt)*) => { ... }; } Expand description Construct an in-place, fallible pinned initializer for struct s. 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::error::Error; use pin_init::init_zeroed; #[pin_data] struct BigBuf { big: KBox<[u8; 1024 * 1024 * 1024 ]>, small: [u8; 1024 * 1024 ], ptr: *mut u8, } impl BigBuf { fn new() -> impl PinInit< Self , Error> { try_pin_init! ( Self { big: KBox::init(init_zeroed(), GFP_KERNEL) ? , small: [ 0 ; 1024 * 1024 ], ptr: core::ptr::null_mut(), } ?
Error) } }