use super::*;
pub unsafe trait HasPinData {
type PinData: PinData;
unsafe fn __pin_data() -> Self::PinData;
}
pub unsafe trait PinData: Copy {
type Datee: ?Sized + HasPinData;
fn make_closure<F, O, E>(self, f: F) -> F
where
F: FnOnce(*mut Self::Datee) -> Result<O, E>,
{
f
}
}
pub unsafe trait HasInitData {
type InitData: InitData;
unsafe fn __init_data() -> Self::InitData;
}
pub unsafe trait InitData: Copy {
type Datee: ?Sized + HasInitData;
fn make_closure<F, O, E>(self, f: F) -> F
where
F: FnOnce(*mut Self::Datee) -> Result<O, E>,
{
f
}
}
pub struct AllData<T: ?Sized>(PhantomData<fn(Box<T>) -> Box<T>>);
impl<T: ?Sized> Clone for AllData<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for AllData<T> {}
unsafe impl<T: ?Sized> InitData for AllData<T> {
type Datee = T;
}
unsafe impl<T: ?Sized> HasInitData for T {
type InitData = AllData<T>;
unsafe fn __init_data() -> Self::InitData {
AllData(PhantomData)
}
}
pub struct StackInit<T>(MaybeUninit<T>, bool);
impl<T> Drop for StackInit<T> {
#[inline]
fn drop(&mut self) {
if self.1 {
unsafe { self.0.assume_init_drop() };
}
}
}
impl<T> StackInit<T> {
#[inline]
pub fn uninit() -> Self {
Self(MaybeUninit::uninit(), false)
}
#[inline]
pub unsafe fn init<E>(&mut self, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {
unsafe { init.__pinned_init(self.0.as_mut_ptr())? };
self.1 = true;
Ok(unsafe { Pin::new_unchecked(self.0.assume_init_mut()) })
}
}
pub struct DropGuard<T: ?Sized>(*mut T, Cell<bool>);
impl<T: ?Sized> DropGuard<T> {
#[inline]
pub unsafe fn new(ptr: *mut T) -> Self {
Self(ptr, Cell::new(true))
}
#[inline]
pub unsafe fn forget(&self) {
self.1.set(false);
}
}
impl<T: ?Sized> Drop for DropGuard<T> {
#[inline]
fn drop(&mut self) {
if self.1.get() {
unsafe { ptr::drop_in_place(self.0) }
}
}
}
pub struct OnlyCallFromDrop(());
impl OnlyCallFromDrop {
pub unsafe fn create() -> Self {
Self(())
}
}