#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::{boxed::Box, sync::Arc};
#[cfg(feature = "alloc")]
use core::alloc::AllocError;
use core::{mem::MaybeUninit, pin::Pin};
#[cfg(feature = "std")]
use std::sync::Arc;
#[cfg(not(feature = "alloc"))]
type AllocError = core::convert::Infallible;
use crate::{
init_from_closure, pin_init_from_closure, InPlaceWrite, Init, PinInit, ZeroableOption,
};
pub extern crate alloc;
unsafe impl<T> ZeroableOption for Box<T> {}
pub trait InPlaceInit<T>: Sized {
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
where
E: From<AllocError>;
fn pin_init(init: impl PinInit<T>) -> Result<Pin<Self>, AllocError> {
let init = unsafe {
pin_init_from_closure(|slot| match init.__pinned_init(slot) {
Ok(()) => Ok(()),
Err(i) => match i {},
})
};
Self::try_pin_init(init)
}
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
where
E: From<AllocError>;
fn init(init: impl Init<T>) -> Result<Self, AllocError> {
let init = unsafe {
init_from_closure(|slot| match init.__init(slot) {
Ok(()) => Ok(()),
Err(i) => match i {},
})
};
Self::try_init(init)
}
}
#[cfg(feature = "alloc")]
macro_rules! try_new_uninit {
($type:ident) => {
$type::try_new_uninit()?
};
}
#[cfg(all(feature = "std", not(feature = "alloc")))]
macro_rules! try_new_uninit {
($type:ident) => {
$type::new_uninit()
};
}
impl<T> InPlaceInit<T> for Box<T> {
#[inline]
fn try_pin_init<E>(init: impl PinInit<T, E>) -> Result<Pin<Self>, E>
where
E: From<AllocError>,
{
try_new_uninit!(Box).write_pin_init(init)
}
#[inline]
fn try_init<E>(init: impl Init<T, E>) -> Result<Self, E>
where
E: From<AllocError>,
{
try_new_uninit!(Box).write_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>,
{
let mut this = try_new_uninit!(Arc);
let Some(slot) = Arc::get_mut(&mut this) else {
unsafe { core::hint::unreachable_unchecked() }
};
let slot = slot.as_mut_ptr();
unsafe { init.__pinned_init(slot)? };
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>,
{
let mut this = try_new_uninit!(Arc);
let Some(slot) = Arc::get_mut(&mut this) else {
unsafe { core::hint::unreachable_unchecked() }
};
let slot = slot.as_mut_ptr();
unsafe { init.__init(slot)? };
Ok(unsafe { this.assume_init() })
}
}
impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
type Initialized = Box<T>;
fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
let slot = self.as_mut_ptr();
unsafe { init.__init(slot)? };
Ok(unsafe { self.assume_init() })
}
fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
let slot = self.as_mut_ptr();
unsafe { init.__pinned_init(slot)? };
Ok(unsafe { self.assume_init() }.into())
}
}