use crate::{create_array, Array};
use core::{
borrow::{Borrow, BorrowMut},
cmp::Ordering,
fmt,
iter::IntoIterator,
ops::{Deref, DerefMut, Index, IndexMut},
slice::{Iter, IterMut, SliceIndex},
};
#[cfg(feature = "with_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "with_arrayvec")]
pub type ArrayVecArrayWrapper<A> = arrayvec::ArrayVec<ArrayWrapper<A>>;
#[cfg(feature = "with_smallvec")]
pub type SmallVecArrayWrapper<A> = smallvec::SmallVec<ArrayWrapper<A>>;
pub struct ArrayWrapper<A> {
pub(crate) array: A,
}
impl<A> ArrayWrapper<A>
where
A: Array,
{
pub fn new(array: A) -> Self {
Self { array }
}
}
#[cfg(feature = "with_arrayvec")]
unsafe impl<A> arrayvec::Array for ArrayWrapper<A>
where
A: Array,
{
type Index = usize;
type Item = A::Item;
const CAPACITY: usize = A::CAPACITY;
fn as_slice(&self) -> &[Self::Item] {
self.array.slice()
}
fn as_mut_slice(&mut self) -> &mut [Self::Item] {
self.array.slice_mut()
}
}
#[cfg(feature = "with_smallvec")]
unsafe impl<A> smallvec::Array for ArrayWrapper<A>
where
A: Array,
{
type Item = A::Item;
fn size() -> usize {
A::CAPACITY
}
}
#[cfg(feature = "with_serde")]
impl<'de, A, T> Deserialize<'de> for ArrayWrapper<A>
where
A: Array<Item = T>,
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
use core::marker::PhantomData;
use serde::de::{Error, SeqAccess, Visitor};
struct ArrayWrapperVisitor<'de, A, T>(PhantomData<(&'de (), A, T)>);
impl<'de, A, T> Visitor<'de> for ArrayWrapperVisitor<'de, A, T>
where
A: Array<Item = T>,
T: Deserialize<'de>,
{
type Value = ArrayWrapper<A>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "an array with no more than {} items", A::CAPACITY)
}
fn visit_seq<SA>(self, mut seq: SA) -> Result<Self::Value, SA::Error>
where
SA: SeqAccess<'de>,
{
let array = unsafe {
let mut array: core::mem::MaybeUninit<A> = core::mem::MaybeUninit::uninit();
for (idx, value_ptr) in (&mut *array.as_mut_ptr()).slice_mut().iter_mut().enumerate() {
if let Some(value) = seq.next_element()? {
core::ptr::write(value_ptr, value);
} else {
return Err(SA::Error::invalid_length(idx, &self));
}
}
array.assume_init()
};
Ok(ArrayWrapper { array })
}
}
deserializer.deserialize_seq(ArrayWrapperVisitor::<A, T>(PhantomData))
}
}
#[cfg(feature = "with_serde")]
impl<A> Serialize for ArrayWrapper<A>
where
A: Array,
A::Item: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_seq(self)
}
}
impl<A> AsRef<A> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn as_ref(&self) -> &A {
&self.array
}
}
impl<A> AsRef<[A::Item]> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn as_ref(&self) -> &[A::Item] {
&self.array.slice()
}
}
impl<A> AsMut<A> for ArrayWrapper<A>
where
A: Array,
{
fn as_mut(&mut self) -> &mut A {
&mut self.array
}
}
impl<A> AsMut<[A::Item]> for ArrayWrapper<A>
where
A: Array,
{
fn as_mut(&mut self) -> &mut [A::Item] {
self.array.slice_mut()
}
}
impl<A> Borrow<A> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn borrow(&self) -> &A {
&self.array
}
}
impl<A> Borrow<[A::Item]> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn borrow(&self) -> &[A::Item] {
self.array.slice()
}
}
impl<A> BorrowMut<A> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn borrow_mut(&mut self) -> &mut A {
&mut self.array
}
}
impl<A> BorrowMut<[A::Item]> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn borrow_mut(&mut self) -> &mut [A::Item] {
self.array.slice_mut()
}
}
impl<A> Clone for ArrayWrapper<A>
where
A: Array + Clone,
A::Item: Clone,
{
#[inline]
fn clone(&self) -> Self {
Self { array: self.array.clone() }
}
}
impl<A> Copy for ArrayWrapper<A>
where
A: Array + Copy,
A::Item: Copy,
{
}
impl<A> Default for ArrayWrapper<A>
where
A: Array,
A::Item: Default,
{
#[inline]
fn default() -> Self {
ArrayWrapper { array: create_array(|_| A::Item::default()) }
}
}
impl<A> Deref for ArrayWrapper<A>
where
A: Array,
{
type Target = A;
#[inline]
fn deref(&self) -> &Self::Target {
&self.array
}
}
impl<A> DerefMut for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.array
}
}
impl<A> Eq for ArrayWrapper<A>
where
A: Array,
A::Item: Eq,
{
}
impl<A> From<A> for ArrayWrapper<A>
where
A: Array,
{
#[inline]
fn from(from: A) -> Self {
Self { array: from }
}
}
impl<I, A> Index<I> for ArrayWrapper<A>
where
A: Array,
I: SliceIndex<[A::Item]>,
{
type Output = <I as SliceIndex<[A::Item]>>::Output;
#[inline]
fn index(&self, idx: I) -> &Self::Output {
&self.array.slice()[idx]
}
}
impl<I, A> IndexMut<I> for ArrayWrapper<A>
where
A: Array,
I: SliceIndex<[A::Item]>,
{
#[inline]
fn index_mut(&mut self, idx: I) -> &mut Self::Output {
&mut self.array.slice_mut()[idx]
}
}
impl<'a, A> IntoIterator for &'a ArrayWrapper<A>
where
A: Array,
{
type Item = &'a A::Item;
type IntoIter = Iter<'a, A::Item>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.array.slice().iter()
}
}
impl<'a, A> IntoIterator for &'a mut ArrayWrapper<A>
where
A: Array,
{
type Item = &'a mut A::Item;
type IntoIter = IterMut<'a, A::Item>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.array.slice_mut().iter_mut()
}
}
impl<A> Ord for ArrayWrapper<A>
where
A: Array,
A::Item: Ord,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.array.slice().cmp(&other.array.slice())
}
}
impl<A> PartialEq for ArrayWrapper<A>
where
A: Array,
A::Item: PartialEq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.array.slice() == other.array.slice()
}
}
impl<A> PartialOrd for ArrayWrapper<A>
where
A: Array,
A::Item: PartialOrd,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.array.slice().partial_cmp(&other.array.slice())
}
}
impl<A> fmt::Debug for ArrayWrapper<A>
where
A: Array,
A::Item: fmt::Debug,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.array.slice()).finish()
}
}