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
use super::SliceOwner;

#[inline]
#[track_caller]
fn slice_end_index_len_fail(index: usize, len: usize) -> ! {
    panic!("range end index {index} out of range for slice of length {len}");
}

pub unsafe trait Ops: SliceOwner {
    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[inline]
    unsafe fn get_unchecked(&self, index: usize) -> &Self::Item {
        &*self.as_ptr().add(index)
    }

    #[inline]
    fn get(&self, index: usize) -> &Self::Item {
        if index > self.len() {
            slice_end_index_len_fail(index, self.len());
        }

        unsafe { self.get_unchecked(index) }
    }

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

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

    #[inline]
    fn first(&self) -> Option<&Self::Item> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.first_unchecked() })
        }
    }

    #[inline]
    fn first_mut(&mut self) -> Option<&mut Self::Item> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.first_mut_unchecked() })
        }
    }

    #[inline(always)]
    unsafe fn last_unchecked(&self) -> &Self::Item {
        &*self.as_ptr().add(self.len().unchecked_sub(1))
    }

    #[inline(always)]
    unsafe fn last_mut_unchecked(&mut self) -> &mut Self::Item {
        &mut *self.as_mut_ptr().add(self.len().unchecked_sub(1))
    }

    #[inline]
    fn last(&self) -> Option<&Self::Item> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.last_unchecked() })
        }
    }

    #[inline]
    fn last_mut(&mut self) -> Option<&mut Self::Item> {
        if self.is_empty() {
            None
        } else {
            Some(unsafe { self.last_mut_unchecked() })
        }
    }

    unsafe fn split_at_unchecked(&self, mid: usize) -> (&[Self::Item], &[Self::Item]) {
        let len = self.len();
        let ptr = self.as_ptr();
        unsafe {
            (
                core::slice::from_raw_parts(ptr, mid),
                core::slice::from_raw_parts(ptr.add(mid), len.unchecked_sub(mid)),
            )
        }
    }

    unsafe fn split_at_mut_unchecked(
        &mut self,
        mid: usize,
    ) -> (&mut [Self::Item], &mut [Self::Item]) {
        let len = self.len();
        let ptr = self.as_mut_ptr();
        unsafe {
            (
                core::slice::from_raw_parts_mut(ptr, mid),
                core::slice::from_raw_parts_mut(ptr.add(mid), len.unchecked_sub(mid)),
            )
        }
    }

    #[inline]
    fn split_at_checked(&self, mid: usize) -> Option<(&[Self::Item], &[Self::Item])> {
        if mid <= self.len() {
            Some(unsafe { self.split_at_unchecked(mid) })
        } else {
            None
        }
    }

    #[inline]
    fn split_at_mut_checked(
        &mut self,
        mid: usize,
    ) -> Option<(&mut [Self::Item], &mut [Self::Item])> {
        if mid <= self.len() {
            Some(unsafe { self.split_at_mut_unchecked(mid) })
        } else {
            None
        }
    }

    #[inline]
    fn split_at(&self, mid: usize) -> (&[Self::Item], &[Self::Item]) {
        match self.split_at_checked(mid) {
            Some(pair) => pair,
            None => panic!("mid > len"),
        }
    }

    #[inline]
    fn split_at_mut(&mut self, mid: usize) -> (&mut [Self::Item], &mut [Self::Item]) {
        match self.split_at_mut_checked(mid) {
            Some(pair) => pair,
            None => panic!("mid > len"),
        }
    }
}