/// Smart pointer that can initialize memory in-place. pubtrait InPlaceInit<T>: Sized { /// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this /// type. /// /// If `T: !Unpin` it will not be able to move afterwards. fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> where
E: From<AllocError>;
/// Use the given pin-initializer to pin-initialize a `T` inside of a new smart pointer of this /// type. /// /// If `T: !Unpin` it will not be able to move afterwards. fn pin_init(init: impl PinInit<T>) -> Result<Pin<Self>, AllocError> { // SAFETY: We delegate to `init` and only change the error type. let init = unsafe {
pin_init_from_closure(|slot| match init.__pinned_init(slot) {
Ok(()) => Ok(()),
Err(i) => match i {},
})
}; Self::try_pin_init(init)
}
/// Use the given initializer to in-place initialize a `T`. fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E> where
E: From<AllocError>;
/// Use the given initializer to in-place initialize a `T`. fn init(init: impl Init<T>) -> Result<Self, AllocError> { // SAFETY: We delegate to `init` and only change the error type. let init = unsafe {
init_from_closure(|slot| match init.__init(slot) {
Ok(()) => Ok(()),
Err(i) => match i {},
})
}; Self::try_init(init)
}
}
impl<T> InPlaceInit<T> for Arc<T> { #[inline] fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E> where
E: From<AllocError>,
{ letmut this = try_new_uninit!(Arc); let Some(slot) = Arc::get_mut(&mut this) else { // SAFETY: the Arc has just been created and has no external references unsafe { core::hint::unreachable_unchecked() }
}; let slot = slot.as_mut_ptr(); // SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later. unsafe { init.__pinned_init(slot)? }; // SAFETY: All fields have been initialized and this is the only `Arc` to that data.
Ok(unsafe { Pin::new_unchecked(this.assume_init()) })
}
#[inline] fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E> where
E: From<AllocError>,
{ letmut this = try_new_uninit!(Arc); let Some(slot) = Arc::get_mut(&mut this) else { // SAFETY: the Arc has just been created and has no external references unsafe { core::hint::unreachable_unchecked() }
}; let slot = slot.as_mut_ptr(); // SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid. unsafe { init.__init(slot)? }; // SAFETY: All fields have been initialized.
Ok(unsafe { this.assume_init() })
}
}
impl<T> InPlaceWrite<T> forBox<MaybeUninit<T>> { type Initialized = Box<T>;
fn write_init<E>(mutself, init: impl Init<T, E>) -> Result<Self::Initialized, E> { let slot = self.as_mut_ptr(); // SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid. unsafe { init.__init(slot)? }; // SAFETY: All fields have been initialized.
Ok(unsafe { self.assume_init() })
}
fn write_pin_init<E>(mutself, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> { let slot = self.as_mut_ptr(); // SAFETY: When init errors/panics, slot will get deallocated but not dropped, // slot is valid and will not be moved, because we pin it later. unsafe { init.__pinned_init(slot)? }; // SAFETY: All fields have been initialized.
Ok(unsafe { self.assume_init() }.into())
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-26)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.