use core::mem::MaybeUninit;
use crate::{iter::ArrayWindows, Array, ArrayExt, SizeError};
pub trait Slice {
type Item;
fn copied<A>(&self) -> Result<A, SizeError>
where
A: Array<Item = Self::Item>,
A::Item: Copy;
fn cloned<A>(&self) -> Result<A, SizeError>
where
A: Array<Item = Self::Item>,
A::Item: Clone;
fn array_windows<A>(&self) -> ArrayWindows<A>
where
A: Array<Item = Self::Item>;
}
pub trait MaybeUninitSlice:
Slice<Item = MaybeUninit<<Self as MaybeUninitSlice>::InitItem>>
{
type InitItem;
unsafe fn assume_init(&self) -> &[Self::InitItem];
unsafe fn assume_init_mut(&mut self) -> &mut [Self::InitItem];
}
impl<T> Slice for [T] {
type Item = T;
#[inline]
fn copied<A>(&self) -> Result<A, SizeError>
where
A: Array<Item = Self::Item>,
A::Item: Copy,
{
A::from_slice(self)
}
#[inline]
fn cloned<A>(&self) -> Result<A, SizeError>
where
A: Array<Item = Self::Item>,
A::Item: Clone,
{
A::clone_from_slice(self)
}
#[inline]
fn array_windows<A>(&self) -> ArrayWindows<A>
where
A: Array<Item = Self::Item>,
{
ArrayWindows::new(self)
}
}
impl<T> MaybeUninitSlice for [MaybeUninit<T>] {
type InitItem = T;
#[inline]
unsafe fn assume_init(&self) -> &[Self::InitItem] {
&*(self as *const [MaybeUninit<T>] as *const [T])
}
#[inline]
unsafe fn assume_init_mut(&mut self) -> &mut [Self::InitItem] {
&mut *(self as *mut [MaybeUninit<T>] as *mut [T])
}
}