use crate::{iter::IteratorExt, Array};
pub trait ArrayAsRef<'a>: Array
where
<Self as Array>::Item: 'a,
{
type AsRef: Array<Item = &'a <Self as Array>::Item>;
type AsMut: Array<Item = &'a mut <Self as Array>::Item>;
fn as_ref_array(&'a self) -> Self::AsRef;
fn as_mut_array(&'a mut self) -> Self::AsMut;
}
impl<'a, T: 'a> ArrayAsRef<'a> for [T; 0] {
type AsMut = [&'a mut T; 0];
type AsRef = [&'a T; 0];
#[inline]
fn as_ref_array(&'a self) -> Self::AsRef {
[]
}
#[inline]
fn as_mut_array(&'a mut self) -> Self::AsMut {
[]
}
}
macro_rules! as_ref_impl {
($e:tt) => {
impl<'a, T: 'a> ArrayAsRef<'a> for [T; $e] {
type AsMut = [&'a mut T; $e];
type AsRef = [&'a T; $e];
#[inline]
fn as_ref_array(&'a self) -> Self::AsRef {
self.as_slice().iter().collect_array::<Self::AsRef>()
}
#[inline]
fn as_mut_array(&'a mut self) -> Self::AsMut {
self.as_mut_slice()
.iter_mut()
.collect_array::<Self::AsMut>()
}
}
};
}
array_impls!(as_ref_impl);