dambi 0.1.1

Single-threaded (!Send + !Sync) primitives
Documentation
use std::future::Future;
use std::marker::PhantomData;
use std::mem::{self, MaybeUninit};
use std::pin::Pin;
use std::task::{Context, Poll};

const ALIGN: usize = 16;

#[repr(C, align(16))]
struct Storage<const SIZE: usize> {
    bytes: [u8; SIZE],
}

pub struct InlineFuture<'a, Out: 'a, const SIZE: usize = 2048> {
    storage: MaybeUninit<Storage<SIZE>>,
    poll_fn: unsafe fn(*mut u8, &mut Context<'_>) -> Poll<Out>,
    drop_fn: unsafe fn(*mut u8),
    try_fn: Option<unsafe fn(*mut u8) -> Out>,
    _marker: PhantomData<&'a ()>,
}

impl<'a, Out: 'a, const SIZE: usize> InlineFuture<'a, Out, SIZE> {
    pub fn new<F>(fut: F) -> Self
    where
        F: Future<Output = Out> + 'a,
    {
        assert_inline_fits::<F, SIZE>();
        Self {
            storage: init_storage(fut),
            poll_fn: poll_impl::<F, Out>,
            drop_fn: drop_impl::<F>,
            try_fn: None,
            _marker: PhantomData,
        }
    }

    /// # Safety
    ///
    /// `poll_fn` and `drop_fn` must consistently target the same
    /// concrete `F` written into `storage`.
    pub unsafe fn from_raw<F: 'a>(
        fut: F,
        poll_fn: unsafe fn(*mut u8, &mut Context<'_>) -> Poll<Out>,
        drop_fn: unsafe fn(*mut u8),
    ) -> Self {
        assert_inline_fits::<F, SIZE>();
        Self {
            storage: init_storage(fut),
            poll_fn,
            drop_fn,
            try_fn: None,
            _marker: PhantomData,
        }
    }

    /// SAFETY: `try_fn` must consume the inline `F` exactly once and leave
    /// the storage logically uninitialized for the subsequent `Drop`.
    pub fn with_try_fn(mut self, try_fn: unsafe fn(*mut u8) -> Out) -> Self {
        self.try_fn = Some(try_fn);
        self
    }

    pub fn try_now(self) -> std::result::Result<Out, Self> {
        let Some(try_fn) = self.try_fn else {
            return Err(self);
        };
        let mut this = mem::ManuallyDrop::new(self);
        // SAFETY: `try_fn` was installed alongside the matching `F` and
        // consumes it; `ManuallyDrop` suppresses the regular `drop_fn`.
        Ok(unsafe { try_fn(this.storage.as_mut_ptr().cast::<u8>()) })
    }

    /// # Safety
    ///
    /// Caller must guarantee `self` lives at a stable address
    /// until the future completes (no move after the first poll).
    /// Typical use: parked inside an mmap-backed slab whose slots are
    /// pinned by construction.
    #[inline(always)]
    pub unsafe fn poll_pinned(&mut self, cx: &mut Context<'_>) -> Poll<Out> {
        unsafe { Pin::new_unchecked(self).poll(cx) }
    }
}

impl<'a, Out: 'a, const SIZE: usize> Future for InlineFuture<'a, Out, SIZE> {
    type Output = Out;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        // SAFETY: `storage` holds the concrete `F` installed in `new`/
        // `from_raw`, and `poll_fn` is its matching vtable entry.
        unsafe {
            let this = self.get_unchecked_mut();
            (this.poll_fn)(this.storage.as_mut_ptr().cast::<u8>(), cx)
        }
    }
}

impl<'a, Out: 'a, const SIZE: usize> Drop for InlineFuture<'a, Out, SIZE> {
    fn drop(&mut self) {
        // SAFETY: `drop_fn` matches the concrete `F` written into `storage`.
        unsafe { (self.drop_fn)(self.storage.as_mut_ptr().cast::<u8>()) };
    }
}

fn assert_inline_fits<F, const SIZE: usize>() {
    assert!(
        mem::size_of::<F>() <= SIZE,
        "InlineFuture: future size {} exceeds inline storage {} for {}",
        mem::size_of::<F>(),
        SIZE,
        std::any::type_name::<F>()
    );
    assert!(
        mem::align_of::<F>() <= ALIGN,
        "InlineFuture: future align {} exceeds inline align {}",
        mem::align_of::<F>(),
        ALIGN
    );
}

fn init_storage<F, const SIZE: usize>(fut: F) -> MaybeUninit<Storage<SIZE>> {
    let mut storage = MaybeUninit::<Storage<SIZE>>::uninit();
    let raw = storage.as_mut_ptr().cast::<u8>().cast::<F>();
    // SAFETY: `assert_inline_fits` guarantees `F` fits and is aligned.
    unsafe { raw.write(fut) };
    storage
}

unsafe fn poll_impl<F, Out>(raw: *mut u8, cx: &mut Context<'_>) -> Poll<Out>
where
    F: Future<Output = Out>,
{
    // SAFETY: `raw` points to the moved `F` written by `init_storage`;
    // `poll_pinned`'s caller pinned the enclosing storage.
    let pin = unsafe { Pin::new_unchecked(&mut *(raw as *mut F)) };
    pin.poll(cx)
}

unsafe fn drop_impl<F>(raw: *mut u8) {
    // SAFETY: `raw` points to the moved `F`; this is its sole drop site.
    unsafe { std::ptr::drop_in_place(raw as *mut F) };
}