no_std_collections 0.1.1

Auxiliary structures and traits for using dynamically resizable arrays (Vectors) in flexible environments, supporting both std and no_std contexts.
Documentation
mod ops;
pub use ops::Ops;

pub unsafe trait SliceOwner: IntoIterator {
    fn len(&self) -> usize;

    fn as_ptr(&self) -> *const Self::Item;

    #[inline(always)]
    fn as_mut_ptr(&mut self) -> *mut Self::Item {
        self.as_ptr() as *mut Self::Item
    }

    #[inline(always)]
    fn as_slice(&self) -> &[Self::Item] {
        unsafe { core::slice::from_raw_parts(self.as_ptr(), self.len()) }
    }

    #[inline(always)]
    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
        unsafe { core::slice::from_raw_parts_mut(self.as_mut_ptr(), self.len()) }
    }
}

unsafe impl<T, const N: usize> SliceOwner for [T; N] {
    fn as_ptr(&self) -> *const Self::Item {
        self as *const T
    }

    fn len(&self) -> usize {
        N
    }

    fn as_mut_ptr(&mut self) -> *mut Self::Item {
        self as *mut T
    }

    fn as_slice(&self) -> &[Self::Item] {
        self.as_slice()
    }

    fn as_mut_slice(&mut self) -> &mut [Self::Item] {
        self.as_mut_slice()
    }
}

#[cfg(feature = "std")]
unsafe impl<T> SliceOwner for Vec<T> {
    fn len(&self) -> usize {
        self.len()
    }

    fn as_ptr(&self) -> *const Self::Item {
        self.as_ptr()
    }

    fn as_mut_ptr(&mut self) -> *mut Self::Item {
        self.as_mut_ptr()
    }
}