use core::{ array, borrow::{Borrow, BorrowMut}, fmt::Debug, ops::{ Bound, Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, }, ptr, }; use alloc::{ boxed::Box, collections::{BTreeSet, BinaryHeap, LinkedList, VecDeque}, rc::Rc, vec::Vec, }; use bevy_platform_support::sync::Arc; use super::{unique_slice, TrustedEntityBorrow, UniqueEntityIter, UniqueEntitySlice}; /// An array that contains only unique entities. /// /// It can be obtained through certain methods on [`UniqueEntitySlice`], /// and some [`TryFrom`] implementations. #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct UniqueEntityArray([T; N]); impl UniqueEntityArray { /// Constructs a `UniqueEntityArray` from a [`[T; N]`] unsafely. /// /// # Safety /// /// `array` must contain only unique elements. pub const unsafe fn from_array_unchecked(array: [T; N]) -> Self { Self(array) } /// Constructs a `&UniqueEntityArray` from a [`&[T; N]`] unsafely. /// /// # Safety /// /// `array` must contain only unique elements. pub const unsafe fn from_array_ref_unchecked(array: &[T; N]) -> &Self { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { &*(ptr::from_ref(array).cast()) } } /// Constructs a `Box` from a [`Box<[T; N]>`] unsafely. /// /// # Safety /// /// `array` must contain only unique elements. pub unsafe fn from_boxed_array_unchecked(array: Box<[T; N]>) -> Box { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(array).cast()) } } /// Casts `self` into the inner array. pub fn into_boxed_inner(self: Box) -> Box<[T; N]> { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(self).cast()) } } /// Constructs a `Arc` from a [`Arc<[T; N]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_arc_array_unchecked(slice: Arc<[T; N]>) -> Arc { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. pub fn into_arc_inner(this: Arc) -> Arc<[T; N]> { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(this).cast()) } } // Constructs a `Rc` from a [`Rc<[T; N]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_rc_array_unchecked(slice: Rc<[T; N]>) -> Rc { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. pub fn into_rc_inner(self: Rc) -> Rc<[T; N]> { // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(self).cast()) } } /// Return the inner array. pub fn into_inner(self) -> [T; N] { self.0 } /// Returns a reference to the inner array. pub fn as_inner(&self) -> &[T; N] { &self.0 } /// Returns a slice containing the entire array. Equivalent to `&s[..]`. pub const fn as_slice(&self) -> &UniqueEntitySlice { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.as_slice()) } } /// Returns a mutable slice containing the entire array. Equivalent to /// `&mut s[..]`. pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } } /// Borrows each element and returns an array of references with the same /// size as `self`. /// /// Equivalent to [`[T; N]::as_ref`](array::each_ref). pub fn each_ref(&self) -> UniqueEntityArray<&T, N> { UniqueEntityArray(self.0.each_ref()) } } impl Deref for UniqueEntityArray { type Target = UniqueEntitySlice; fn deref(&self) -> &Self::Target { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(&self.0) } } } impl DerefMut for UniqueEntityArray { fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(&mut self.0) } } } impl Default for UniqueEntityArray { fn default() -> Self { Self(Default::default()) } } impl<'a, T: TrustedEntityBorrow, const N: usize> IntoIterator for &'a UniqueEntityArray { type Item = &'a T; type IntoIter = unique_slice::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.iter()) } } } impl IntoIterator for UniqueEntityArray { type Item = T; type IntoIter = IntoIter; fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.into_iter()) } } } impl AsRef> for UniqueEntityArray { fn as_ref(&self) -> &UniqueEntitySlice { self } } impl AsMut> for UniqueEntityArray { fn as_mut(&mut self) -> &mut UniqueEntitySlice { self } } impl Borrow> for UniqueEntityArray { fn borrow(&self) -> &UniqueEntitySlice { self } } impl BorrowMut> for UniqueEntityArray { fn borrow_mut(&mut self) -> &mut UniqueEntitySlice { self } } impl Index<(Bound, Bound)> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: Range) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> for UniqueEntityArray { type Output = UniqueEntitySlice; fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } } } impl Index for UniqueEntityArray { type Output = T; fn index(&self, key: usize) -> &T { self.0.index(key) } } impl IndexMut<(Bound, Bound)> for UniqueEntityArray { fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> for UniqueEntityArray { fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> for UniqueEntityArray { fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut for UniqueEntityArray { fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> for UniqueEntityArray { fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> for UniqueEntityArray { fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> for UniqueEntityArray { fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl From<&[T; 1]> for UniqueEntityArray { fn from(value: &[T; 1]) -> Self { Self(value.clone()) } } impl From<&[T; 0]> for UniqueEntityArray { fn from(value: &[T; 0]) -> Self { Self(value.clone()) } } impl From<&mut [T; 1]> for UniqueEntityArray { fn from(value: &mut [T; 1]) -> Self { Self(value.clone()) } } impl From<&mut [T; 0]> for UniqueEntityArray { fn from(value: &mut [T; 0]) -> Self { Self(value.clone()) } } impl From<[T; 1]> for UniqueEntityArray { fn from(value: [T; 1]) -> Self { Self(value) } } impl From<[T; 0]> for UniqueEntityArray { fn from(value: [T; 0]) -> Self { Self(value) } } impl From> for (T,) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T, T, T, T, T) { fn from(array: UniqueEntityArray) -> Self { Self::from(array.into_inner()) } } impl From> for BTreeSet { fn from(value: UniqueEntityArray) -> Self { BTreeSet::from(value.0) } } impl From> for BinaryHeap { fn from(value: UniqueEntityArray) -> Self { BinaryHeap::from(value.0) } } impl From> for LinkedList { fn from(value: UniqueEntityArray) -> Self { LinkedList::from(value.0) } } impl From> for Vec { fn from(value: UniqueEntityArray) -> Self { Vec::from(value.0) } } impl From> for VecDeque { fn from(value: UniqueEntityArray) -> Self { VecDeque::from(value.0) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq<&UniqueEntitySlice> for UniqueEntityArray { fn eq(&self, other: &&UniqueEntitySlice) -> bool { self.0.eq(&other.as_inner()) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq> for UniqueEntityArray { fn eq(&self, other: &UniqueEntitySlice) -> bool { self.0.eq(other.as_inner()) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq<&UniqueEntityArray> for Vec { fn eq(&self, other: &&UniqueEntityArray) -> bool { self.eq(&other.0) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq<&UniqueEntityArray> for VecDeque { fn eq(&self, other: &&UniqueEntityArray) -> bool { self.eq(&other.0) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq<&mut UniqueEntityArray> for VecDeque { fn eq(&self, other: &&mut UniqueEntityArray) -> bool { self.eq(&other.0) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq> for Vec { fn eq(&self, other: &UniqueEntityArray) -> bool { self.eq(&other.0) } } impl, U: TrustedEntityBorrow, const N: usize> PartialEq> for VecDeque { fn eq(&self, other: &UniqueEntityArray) -> bool { self.eq(&other.0) } } pub type IntoIter = UniqueEntityIter>; impl UniqueEntityIter> { /// Returns an immutable slice of all elements that have not been yielded /// yet. /// /// Equivalent to [`array::IntoIter::as_slice`]. pub fn as_slice(&self) -> &UniqueEntitySlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } } /// Returns a mutable slice of all elements that have not been yielded yet. /// /// Equivalent to [`array::IntoIter::as_mut_slice`]. pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.as_mut_inner().as_mut_slice()) } } }