#![allow(clippy::len_without_is_empty)]
use core::ptr;
pub trait SliceIndex: Slice
where
Self: Sized,
{
#[doc(hidden)]
fn index_from(self, index: usize) -> Self;
#[doc(hidden)]
fn index_to(self, index: usize) -> Self;
#[doc(hidden)]
fn index_full(self, from: usize, to: usize) -> Self;
}
pub trait Slice
where
Self: Sized,
{
type Item: Copy;
#[doc(hidden)]
fn len(&self) -> usize;
#[doc(hidden)]
fn as_ref(&self) -> &[Self::Item];
#[doc(hidden)]
fn as_ptr(&self) -> ptr::NonNull<Self::Item>;
}
pub trait SliceMut: Slice
where
Self: Sized,
{
fn as_mut(&mut self) -> &mut [Self::Item];
fn as_mut_ptr(&mut self) -> ptr::NonNull<Self::Item>;
}
impl<T> SliceIndex for &[T]
where
T: Copy,
{
#[inline]
fn index_from(self, index: usize) -> Self {
self.get(index..).unwrap_or_default()
}
#[inline]
fn index_to(self, index: usize) -> Self {
self.get(..index).unwrap_or_default()
}
#[inline]
fn index_full(self, from: usize, to: usize) -> Self {
self.get(from..to).unwrap_or_default()
}
}
impl<T> Slice for &[T]
where
T: Copy,
{
type Item = T;
#[inline]
fn len(&self) -> usize {
<[T]>::len(self)
}
#[inline]
fn as_ref(&self) -> &[Self::Item] {
self
}
#[inline]
fn as_ptr(&self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_ptr(&self[..]) as *mut _) }
}
}
impl<T, const N: usize> Slice for [T; N]
where
T: Copy,
{
type Item = T;
#[inline]
fn len(&self) -> usize {
N
}
#[inline]
fn as_ref(&self) -> &[Self::Item] {
&self[..]
}
#[inline]
fn as_ptr(&self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_ptr(self) as *mut _) }
}
}
impl<T, const N: usize> Slice for &[T; N]
where
T: Copy,
{
type Item = T;
#[inline]
fn len(&self) -> usize {
N
}
#[inline]
fn as_ref(&self) -> &[Self::Item] {
&self[..]
}
#[inline]
fn as_ptr(&self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_ptr(&self[..]) as *mut _) }
}
}
impl<T> SliceIndex for &mut [T]
where
T: Copy,
{
#[inline]
fn index_from(self, index: usize) -> Self {
self.get_mut(index..).unwrap_or_default()
}
#[inline]
fn index_to(self, index: usize) -> Self {
self.get_mut(..index).unwrap_or_default()
}
#[inline]
fn index_full(self, from: usize, to: usize) -> Self {
self.get_mut(from..to).unwrap_or_default()
}
}
impl<T> Slice for &mut [T]
where
T: Copy,
{
type Item = T;
#[inline]
fn len(&self) -> usize {
<[T]>::len(self)
}
#[inline]
fn as_ref(&self) -> &[Self::Item] {
&self[..]
}
#[inline]
fn as_ptr(&self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_ptr(&self[..]) as *mut _) }
}
}
impl<T, const N: usize> Slice for &mut [T; N]
where
T: Copy,
{
type Item = T;
#[inline]
fn len(&self) -> usize {
N
}
#[inline]
fn as_ref(&self) -> &[Self::Item] {
&self[..]
}
#[inline]
fn as_ptr(&self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_ptr(&self[..]) as *mut _) }
}
}
impl<T> SliceMut for &mut [T]
where
T: Copy,
{
#[inline]
fn as_mut(&mut self) -> &mut [Self::Item] {
self
}
#[inline]
fn as_mut_ptr(&mut self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_mut_ptr(self)) }
}
}
impl<T, const N: usize> SliceMut for [T; N]
where
T: Copy,
{
#[inline]
fn as_mut(&mut self) -> &mut [Self::Item] {
self
}
#[inline]
fn as_mut_ptr(&mut self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_mut_ptr(&mut self[..])) }
}
}
impl<T, const N: usize> SliceMut for &mut [T; N]
where
T: Copy,
{
#[inline]
fn as_mut(&mut self) -> &mut [Self::Item] {
&mut self[..]
}
#[inline]
fn as_mut_ptr(&mut self) -> ptr::NonNull<Self::Item> {
unsafe { ptr::NonNull::new_unchecked(<[T]>::as_mut_ptr(&mut self[..])) }
}
}