Skip to main content

Init

Trait Init 

Source
pub unsafe trait Init<T: ?Sized, E = Infallible>: PinInit<T, E> {
    // Provided method
    fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
       where F: FnOnce(&mut T) -> Result<(), E> { ... }
}
Expand description

An initializer for T.

To use this initializer, you will need a suitable memory location that can hold a T. This can be Box<T>, Arc<T> or even the stack (see stack_pin_init!). Because PinInit<T, E> is a super trait, you can use every function that takes it as well.

Also see the module description.

§Safety

When implementing this trait you will need to take great care. Also there are probably very few cases where a manual implementation is necessary. Use init_from_closure where possible.

The PinInit::__init function must work without the pinning requirement; the caller is allowed to move the pointee after initialization.

Provided Methods§

Source

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value.

If f returns an error the value is dropped and the initializer will forward the error.

§Examples
use pin_init::{init, init_zeroed, Init};

struct Foo {
    buf: [u8; 1_000_000],
}

impl Foo {
    fn setup(&mut self) {
        println!("Setting up foo");
    }
}

let foo = init!(Foo {
    buf <- init_zeroed()
}).chain(|foo| {
    foo.setup();
    Ok(())
});

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T, E> Init<T, E> for Result<T, E>

Implementors§

Source§

impl<T: ?Sized, E, I, F> Init<T, E> for ChainInit<I, F, T, E>
where I: Init<T, E>, F: FnOnce(&mut T) -> Result<(), E>,

Source§

impl<T> Init<T> for T