Trait moveit::new::New

source ·
pub unsafe trait New: Sized {
    type Output;

    // Required method
    unsafe fn new(self, this: Pin<&mut MaybeUninit<Self::Output>>);

    // Provided method
    fn with<F>(self, post: F) -> With<Self, F>
       where F: FnOnce(Pin<&mut Self::Output>) { ... }
}
Expand description

An in-place constructor for a particular type.

Safety

New::new() must leave its destination argument in a valid, initialized state.

Required Associated Types§

source

type Output

The type to construct.

Required Methods§

source

unsafe fn new(self, this: Pin<&mut MaybeUninit<Self::Output>>)

Construct a new value using the arguments stored in self.

Safety

this must be freshly-created memory; this function must not be used to mutate a previously-pinned pointer that has had self: Pin functions called on it.

Provided Methods§

source

fn with<F>(self, post: F) -> With<Self, F>where F: FnOnce(Pin<&mut Self::Output>),

Adds a post-construction operation.

This function wraps self in an another New type which will call post once the main emplacement operation is complete. This is most useful for the case where creation of the value itself does not depend on the final address, but where some address-sensitive setup may want to occur; this can help minimize the scope (or even need for) unsafe.

This function is best combined with other helpers:

pub struct MyType { /* ... */ }

impl MyType {
  pub fn new() -> impl New<Output = Self> {
    new::of(MyType { /* ... */ }).with(|this| {
      // Address-sensitive setup can occur here.
    })
  }
}

Note: The return value of this function should not be relied upon; a future version will replace it with impl New.

Implementors§