Macro pinned_init::init

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

Construct an in-place initializer for structs.

This macro defaults the error to Infallible. If you need a different error, then use try_init!.

The syntax is identical to pin_init! and its safety caveats also apply:

  • unsafe code must guarantee either full initialization or return an error and allow deallocation of the memory.
  • the fields are initialized in the order given in the initializer.
  • no references to fields are allowed to be created inside of the initializer.

This initializer is for initializing data in-place that might later be moved. If you want to pin-initialize, use pin_init!.

§Examples

use pinned_init::*;
struct BigBuf {
    big: Box<[u8; 1024 * 1024 * 1024]>,
    small: [u8; 1024 * 1024],
}

impl BigBuf {
    fn new() -> impl Init<Self, AllocError> {
        try_init!(Self {
            small <- zeroed(),
            big: Box::init(zeroed())?,
        }? AllocError)
    }
}