use crate::Array;
use core::{
borrow::{Borrow, BorrowMut},
fmt::Debug,
ops::{Index, IndexMut, Range},
};
use typenum::Unsigned;
pub unsafe trait ArraySize: Unsigned + Debug {
type ArrayType<T>: AssocArraySize<Size = Self>
+ AsRef<[T]>
+ AsMut<[T]>
+ Borrow<[T]>
+ BorrowMut<[T]>
+ From<Array<T, Self>>
+ Index<usize>
+ Index<Range<usize>>
+ IndexMut<usize>
+ IndexMut<Range<usize>>
+ Into<Array<T, Self>>
+ IntoIterator<Item = T>;
}
pub trait AssocArraySize: Sized {
type Size: ArraySize;
}
impl<T, U> AssocArraySize for Array<T, U>
where
U: ArraySize,
{
type Size = U;
}
pub trait AsArrayRef<T>: AssocArraySize {
fn as_array_ref(&self) -> &Array<T, Self::Size>;
}
pub trait AsArrayMut<T>: AsArrayRef<T> {
fn as_array_mut(&mut self) -> &mut Array<T, Self::Size>;
}
impl<T, U> AsArrayRef<T> for Array<T, U>
where
U: ArraySize,
{
fn as_array_ref(&self) -> &Self {
self
}
}
impl<T, U> AsArrayMut<T> for Array<T, U>
where
U: ArraySize,
{
fn as_array_mut(&mut self) -> &mut Self {
self
}
}
impl<T, U, const N: usize> AsArrayRef<T> for [T; N]
where
Self: AssocArraySize<Size = U>,
U: ArraySize<ArrayType<T> = Self>,
{
fn as_array_ref(&self) -> &Array<T, U> {
self.into()
}
}
impl<T, U, const N: usize> AsArrayMut<T> for [T; N]
where
Self: AssocArraySize<Size = U>,
U: ArraySize<ArrayType<T> = Self>,
{
fn as_array_mut(&mut self) -> &mut Array<T, U> {
self.into()
}
}
pub trait SliceExt<T>: sealed::Sealed {
fn as_hybrid_array<U: ArraySize>(&self) -> Option<&Array<T, U>>;
fn as_mut_hybrid_array<U: ArraySize>(&mut self) -> Option<&mut Array<T, U>>;
fn as_hybrid_chunks<U: ArraySize>(&self) -> (&[Array<T, U>], &[T]);
fn as_hybrid_chunks_mut<U: ArraySize>(&mut self) -> (&mut [Array<T, U>], &mut [T]);
}
impl<T> SliceExt<T> for [T] {
fn as_hybrid_array<U: ArraySize>(&self) -> Option<&Array<T, U>> {
Array::slice_as_array(self)
}
fn as_mut_hybrid_array<U: ArraySize>(&mut self) -> Option<&mut Array<T, U>> {
Array::slice_as_mut_array(self)
}
fn as_hybrid_chunks<U: ArraySize>(&self) -> (&[Array<T, U>], &[T]) {
Array::slice_as_chunks(self)
}
fn as_hybrid_chunks_mut<U: ArraySize>(&mut self) -> (&mut [Array<T, U>], &mut [T]) {
Array::slice_as_chunks_mut(self)
}
}
impl<T> sealed::Sealed for [T] {}
mod sealed {
pub trait Sealed {}
}