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§
Required Methods§
Provided Methods§
Sourcefn with<F>(self, post: F) -> With<Self, F>
fn with<F>(self, post: F) -> With<Self, F>
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
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.