arraybox 0.1.4

A box with fixed capacity, backed by a byte array (it can be stored on the stack too). Implements fixed capacity `ArrayBox`.
Documentation
#![feature(ptr_metadata)]
#![feature(unsize)]

#![no_std]

use core::borrow::{Borrow, BorrowMut};
use core::fmt::{self, Debug, Display, Formatter};
use core::marker::{PhantomData, Unsize};
use core::mem::{ManuallyDrop, MaybeUninit, align_of, size_of};
use core::ops::{Deref, DerefMut};
use core::ptr::{self, Pointee};

/// A helper type for creating [`ArrayBox`] with buffer appropriated for any of two types.
///
/// The type satisfies the following properties:
///
/// 1. `size_of::<AnyOf2<T1, T2>>() >= size_of::<T1>()`
/// 2. `size_of::<AnyOf2<T1, T2>>() >= size_of::<T2>()`
/// 3. `align_of::<AnyOf2<T1, T2>>() >= align_of::<T1>()`
/// 4. `align_of::<AnyOf2<T1, T2>>() >= align_of::<T2>()`
#[repr(C)]
pub union AnyOf2<T1, T2> {
    _a: ManuallyDrop<T1>,
    _b: ManuallyDrop<T2>,
}

/// A stack-allocated container that can store dynamically sized types.
///
/// `B` type parameter specifies buffer size (`== size_of::<B>()`), and alignment (`== align_of::<B>()`).
pub struct ArrayBox<'a, T: ?Sized + 'a, B> {
    buf: MaybeUninit<B>,
    metadata: <T as Pointee>::Metadata,
    phantom: PhantomData<&'a T>,
}

impl<'a, T: ?Sized + 'a, B> Drop for ArrayBox<'a, T, B> {
    fn drop(&mut self) {
        unsafe { ptr::drop_in_place(self.as_mut_ptr()) };
    }
}

impl<'a, T: ?Sized + 'a, B> ArrayBox<'a, T, B> {
    /// Allocates memory on stack and then places `source` into it as DST `T`.
    pub const fn new<S: Unsize<T>>(mut source: S) -> Self {
        assert!(align_of::<B>() >= align_of::<S>());
        assert!(size_of::<B>() >= size_of::<S>());
        let metadata = (&mut source as *mut T).to_raw_parts().1;
        let mut res = ArrayBox { buf: MaybeUninit::uninit(), metadata, phantom: PhantomData };
        unsafe { ptr::write::<S>(res.buf.as_mut_ptr() as *mut u8 as *mut S, source) };
        res
    }

    /// Return raw immutable pointer to the stored object.
    pub fn as_ptr(&self) -> *const T {
        let metadata = self.metadata;
        ptr::from_raw_parts(self.buf.as_ptr() as *const u8 as *const (), metadata)
    }

    /// Return raw mutable pointer to the stored object.
    pub fn as_mut_ptr(&mut self) -> *mut T {
        let metadata = self.metadata;
        ptr::from_raw_parts_mut(self.buf.as_mut_ptr() as *mut u8 as *mut (), metadata)
    }
}

impl<'a, T: ?Sized + 'a, B> AsRef<T> for ArrayBox<'a, T, B> {
    fn as_ref(&self) -> &T {
        unsafe { &*self.as_ptr() }
    }
}

impl<'a, T: ?Sized + 'a, B> AsMut<T> for ArrayBox<'a, T, B> {
    fn as_mut(&mut self) -> &mut T {
        unsafe { &mut *self.as_mut_ptr() }
    }
}

impl<'a, T: ?Sized + 'a, B> Borrow<T> for ArrayBox<'a, T, B> {
    fn borrow(&self) -> &T { self.as_ref() }
}

impl<'a, T: ?Sized + 'a, B> BorrowMut<T> for ArrayBox<'a, T, B> {
    fn borrow_mut(&mut self) -> &mut T { self.as_mut() }
}

impl<'a, T: ?Sized + 'a, B> Deref for ArrayBox<'a, T, B> {
    type Target = T;

    fn deref(&self) -> &T { self.as_ref() }
}

impl<'a, T: ?Sized + 'a, B> DerefMut for ArrayBox<'a, T, B> {
    fn deref_mut(&mut self) -> &mut T { self.as_mut() }
}

impl<'a, T: Debug + ?Sized + 'a, B> Debug for ArrayBox<'a, T, B> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl<'a, T: Display + ?Sized + 'a, B> Display for ArrayBox<'a, T, B> {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.as_ref().fmt(f)
    }
}