Skip to main content

bevy_ecs/entity/
unique_array.rs

1//! A wrapper around entity arrays with a uniqueness invariant.
2
3use core::{
4    array,
5    borrow::{Borrow, BorrowMut},
6    fmt::Debug,
7    ops::{
8        Bound, Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive,
9        RangeTo, RangeToInclusive,
10    },
11    ptr,
12};
13
14use alloc::{
15    boxed::Box,
16    collections::{BTreeSet, BinaryHeap, LinkedList, VecDeque},
17    rc::Rc,
18    vec::Vec,
19};
20
21use bevy_platform::sync::Arc;
22
23use super::{
24    unique_slice::{self, UniqueEntityEquivalentSlice},
25    Entity, EntityEquivalent, UniqueEntityIter,
26};
27
28/// An array that contains only unique entities.
29///
30/// It can be obtained through certain methods on [`UniqueEntityEquivalentSlice`],
31/// and some [`TryFrom`] implementations.
32///
33/// When `T` is [`Entity`], use [`UniqueEntityArray`].
34#[repr(transparent)]
35#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
36pub struct UniqueEntityEquivalentArray<T: EntityEquivalent, const N: usize>([T; N]);
37
38/// An array that contains only unique [`Entity`].
39///
40/// This is the default case of a [`UniqueEntityEquivalentArray`].
41pub type UniqueEntityArray<const N: usize> = UniqueEntityEquivalentArray<Entity, N>;
42
43impl<T: EntityEquivalent, const N: usize> UniqueEntityEquivalentArray<T, N> {
44    /// Constructs a `UniqueEntityEquivalentArray` from a [`[T; N]`] unsafely.
45    ///
46    /// # Safety
47    ///
48    /// `array` must contain only unique elements.
49    pub const unsafe fn from_array_unchecked(array: [T; N]) -> Self {
50        Self(array)
51    }
52
53    /// Constructs a `&UniqueEntityEquivalentArray` from a [`&[T; N]`] unsafely.
54    ///
55    /// # Safety
56    ///
57    /// `array` must contain only unique elements.
58    pub const unsafe fn from_array_ref_unchecked(array: &[T; N]) -> &Self {
59        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
60        unsafe { &*(ptr::from_ref(array).cast()) }
61    }
62
63    /// Constructs a `Box<UniqueEntityEquivalentArray>` from a [`Box<[T; N]>`] unsafely.
64    ///
65    /// # Safety
66    ///
67    /// `array` must contain only unique elements.
68    pub unsafe fn from_boxed_array_unchecked(array: Box<[T; N]>) -> Box<Self> {
69        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
70        unsafe { Box::from_raw(Box::into_raw(array).cast()) }
71    }
72
73    /// Casts `self` into the inner array.
74    pub fn into_boxed_inner(self: Box<Self>) -> Box<[T; N]> {
75        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
76        unsafe { Box::from_raw(Box::into_raw(self).cast()) }
77    }
78
79    /// Constructs a `Arc<UniqueEntityEquivalentArray>` from a [`Arc<[T; N]>`] unsafely.
80    ///
81    /// # Safety
82    ///
83    /// `slice` must contain only unique elements.
84    pub unsafe fn from_arc_array_unchecked(slice: Arc<[T; N]>) -> Arc<Self> {
85        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
86        unsafe { Arc::from_raw(Arc::into_raw(slice).cast()) }
87    }
88
89    /// Casts `self` to the inner array.
90    pub fn into_arc_inner(this: Arc<Self>) -> Arc<[T; N]> {
91        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
92        unsafe { Arc::from_raw(Arc::into_raw(this).cast()) }
93    }
94
95    // Constructs a `Rc<UniqueEntityEquivalentArray>` from a [`Rc<[T; N]>`] unsafely.
96    ///
97    /// # Safety
98    ///
99    /// `slice` must contain only unique elements.
100    pub unsafe fn from_rc_array_unchecked(slice: Rc<[T; N]>) -> Rc<Self> {
101        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
102        unsafe { Rc::from_raw(Rc::into_raw(slice).cast()) }
103    }
104
105    /// Casts `self` to the inner array.
106    pub fn into_rc_inner(self: Rc<Self>) -> Rc<[T; N]> {
107        // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N].
108        unsafe { Rc::from_raw(Rc::into_raw(self).cast()) }
109    }
110
111    /// Return the inner array.
112    pub fn into_inner(self) -> [T; N] {
113        self.0
114    }
115
116    /// Returns a reference to the inner array.
117    pub const fn as_inner(&self) -> &[T; N] {
118        &self.0
119    }
120
121    /// Returns a slice containing the entire array. Equivalent to `&s[..]`.
122    pub const fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
123        // SAFETY: All elements in the original array are unique.
124        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.as_slice()) }
125    }
126
127    /// Returns a mutable slice containing the entire array. Equivalent to
128    /// `&mut s[..]`.
129    pub const fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
130        // SAFETY: All elements in the original array are unique.
131        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
132    }
133
134    /// Borrows each element and returns an array of references with the same
135    /// size as `self`.
136    ///
137    /// Equivalent to [`[T; N]::as_ref`](array::each_ref).
138    pub fn each_ref(&self) -> UniqueEntityEquivalentArray<&T, N> {
139        UniqueEntityEquivalentArray(self.0.each_ref())
140    }
141}
142
143impl<T: EntityEquivalent, const N: usize> Deref for UniqueEntityEquivalentArray<T, N> {
144    type Target = UniqueEntityEquivalentSlice<T>;
145
146    fn deref(&self) -> &Self::Target {
147        self.as_slice()
148    }
149}
150
151impl<T: EntityEquivalent, const N: usize> DerefMut for UniqueEntityEquivalentArray<T, N> {
152    fn deref_mut(&mut self) -> &mut Self::Target {
153        self.as_mut_slice()
154    }
155}
156
157impl<T: EntityEquivalent> Default for UniqueEntityEquivalentArray<T, 0> {
158    fn default() -> Self {
159        // SAFETY: An empty array cannot contain duplicates.
160        unsafe { Self::from_array_unchecked([]) }
161    }
162}
163
164impl<'a, T: EntityEquivalent, const N: usize> IntoIterator
165    for &'a UniqueEntityEquivalentArray<T, N>
166{
167    type Item = &'a T;
168
169    type IntoIter = unique_slice::Iter<'a, T>;
170
171    fn into_iter(self) -> Self::IntoIter {
172        // SAFETY: All elements in the original array are unique.
173        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) }
174    }
175}
176
177impl<T: EntityEquivalent, const N: usize> IntoIterator for UniqueEntityEquivalentArray<T, N> {
178    type Item = T;
179
180    type IntoIter = IntoIter<N, T>;
181
182    fn into_iter(self) -> Self::IntoIter {
183        // SAFETY: All elements in the original array are unique.
184        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) }
185    }
186}
187
188impl<T: EntityEquivalent, const N: usize> AsRef<UniqueEntityEquivalentSlice<T>>
189    for UniqueEntityEquivalentArray<T, N>
190{
191    fn as_ref(&self) -> &UniqueEntityEquivalentSlice<T> {
192        self
193    }
194}
195
196impl<T: EntityEquivalent, const N: usize> AsMut<UniqueEntityEquivalentSlice<T>>
197    for UniqueEntityEquivalentArray<T, N>
198{
199    fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
200        self
201    }
202}
203
204impl<T: EntityEquivalent, const N: usize> Borrow<UniqueEntityEquivalentSlice<T>>
205    for UniqueEntityEquivalentArray<T, N>
206{
207    fn borrow(&self) -> &UniqueEntityEquivalentSlice<T> {
208        self
209    }
210}
211
212impl<T: EntityEquivalent, const N: usize> BorrowMut<UniqueEntityEquivalentSlice<T>>
213    for UniqueEntityEquivalentArray<T, N>
214{
215    fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
216        self
217    }
218}
219
220impl<T: EntityEquivalent, const N: usize> Index<(Bound<usize>, Bound<usize>)>
221    for UniqueEntityEquivalentArray<T, N>
222{
223    type Output = UniqueEntityEquivalentSlice<T>;
224
225    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {
226        // SAFETY: All elements in the original slice are unique.
227        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
228    }
229}
230
231impl<T: EntityEquivalent, const N: usize> Index<Range<usize>>
232    for UniqueEntityEquivalentArray<T, N>
233{
234    type Output = UniqueEntityEquivalentSlice<T>;
235
236    fn index(&self, key: Range<usize>) -> &Self::Output {
237        // SAFETY: All elements in the original slice are unique.
238        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
239    }
240}
241
242impl<T: EntityEquivalent, const N: usize> Index<RangeFrom<usize>>
243    for UniqueEntityEquivalentArray<T, N>
244{
245    type Output = UniqueEntityEquivalentSlice<T>;
246
247    fn index(&self, key: RangeFrom<usize>) -> &Self::Output {
248        // SAFETY: All elements in the original slice are unique.
249        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
250    }
251}
252
253impl<T: EntityEquivalent, const N: usize> Index<RangeFull> for UniqueEntityEquivalentArray<T, N> {
254    type Output = UniqueEntityEquivalentSlice<T>;
255
256    fn index(&self, key: RangeFull) -> &Self::Output {
257        // SAFETY: All elements in the original slice are unique.
258        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
259    }
260}
261
262impl<T: EntityEquivalent, const N: usize> Index<RangeInclusive<usize>>
263    for UniqueEntityEquivalentArray<T, N>
264{
265    type Output = UniqueEntityEquivalentSlice<T>;
266
267    fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {
268        // SAFETY: All elements in the original slice are unique.
269        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
270    }
271}
272
273impl<T: EntityEquivalent, const N: usize> Index<RangeTo<usize>>
274    for UniqueEntityEquivalentArray<T, N>
275{
276    type Output = UniqueEntityEquivalentSlice<T>;
277
278    fn index(&self, key: RangeTo<usize>) -> &Self::Output {
279        // SAFETY: All elements in the original slice are unique.
280        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
281    }
282}
283
284impl<T: EntityEquivalent, const N: usize> Index<RangeToInclusive<usize>>
285    for UniqueEntityEquivalentArray<T, N>
286{
287    type Output = UniqueEntityEquivalentSlice<T>;
288
289    fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {
290        // SAFETY: All elements in the original slice are unique.
291        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
292    }
293}
294
295impl<T: EntityEquivalent, const N: usize> Index<usize> for UniqueEntityEquivalentArray<T, N> {
296    type Output = T;
297
298    fn index(&self, key: usize) -> &T {
299        self.0.index(key)
300    }
301}
302
303impl<T: EntityEquivalent, const N: usize> IndexMut<(Bound<usize>, Bound<usize>)>
304    for UniqueEntityEquivalentArray<T, N>
305{
306    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {
307        // SAFETY: All elements in the original slice are unique.
308        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
309    }
310}
311
312impl<T: EntityEquivalent, const N: usize> IndexMut<Range<usize>>
313    for UniqueEntityEquivalentArray<T, N>
314{
315    fn index_mut(&mut self, key: Range<usize>) -> &mut Self::Output {
316        // SAFETY: All elements in the original slice are unique.
317        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
318    }
319}
320
321impl<T: EntityEquivalent, const N: usize> IndexMut<RangeFrom<usize>>
322    for UniqueEntityEquivalentArray<T, N>
323{
324    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self::Output {
325        // SAFETY: All elements in the original slice are unique.
326        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
327    }
328}
329
330impl<T: EntityEquivalent, const N: usize> IndexMut<RangeFull>
331    for UniqueEntityEquivalentArray<T, N>
332{
333    fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output {
334        // SAFETY: All elements in the original slice are unique.
335        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
336    }
337}
338
339impl<T: EntityEquivalent, const N: usize> IndexMut<RangeInclusive<usize>>
340    for UniqueEntityEquivalentArray<T, N>
341{
342    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self::Output {
343        // SAFETY: All elements in the original slice are unique.
344        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
345    }
346}
347
348impl<T: EntityEquivalent, const N: usize> IndexMut<RangeTo<usize>>
349    for UniqueEntityEquivalentArray<T, N>
350{
351    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self::Output {
352        // SAFETY: All elements in the original slice are unique.
353        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
354    }
355}
356
357impl<T: EntityEquivalent, const N: usize> IndexMut<RangeToInclusive<usize>>
358    for UniqueEntityEquivalentArray<T, N>
359{
360    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self::Output {
361        // SAFETY: All elements in the original slice are unique.
362        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
363    }
364}
365
366impl<T: EntityEquivalent + Clone> From<&[T; 1]> for UniqueEntityEquivalentArray<T, 1> {
367    fn from(value: &[T; 1]) -> Self {
368        // SAFETY: An array with 1 element cannot contain duplicates.
369        unsafe { Self::from_array_unchecked(value.clone()) }
370    }
371}
372
373impl<T: EntityEquivalent + Clone> From<&[T; 0]> for UniqueEntityEquivalentArray<T, 0> {
374    fn from(value: &[T; 0]) -> Self {
375        // SAFETY: An empty array cannot contain duplicates.
376        unsafe { Self::from_array_unchecked(value.clone()) }
377    }
378}
379
380impl<T: EntityEquivalent + Clone> From<&mut [T; 1]> for UniqueEntityEquivalentArray<T, 1> {
381    fn from(value: &mut [T; 1]) -> Self {
382        // SAFETY: An array with 1 element cannot contain duplicates.
383        unsafe { Self::from_array_unchecked(value.clone()) }
384    }
385}
386
387impl<T: EntityEquivalent + Clone> From<&mut [T; 0]> for UniqueEntityEquivalentArray<T, 0> {
388    fn from(value: &mut [T; 0]) -> Self {
389        // SAFETY: An empty array cannot contain duplicates.
390        unsafe { Self::from_array_unchecked(value.clone()) }
391    }
392}
393
394impl<T: EntityEquivalent> From<[T; 1]> for UniqueEntityEquivalentArray<T, 1> {
395    fn from(value: [T; 1]) -> Self {
396        // SAFETY: An array with 1 element cannot contain duplicates.
397        unsafe { Self::from_array_unchecked(value) }
398    }
399}
400
401impl<T: EntityEquivalent> From<[T; 0]> for UniqueEntityEquivalentArray<T, 0> {
402    fn from(value: [T; 0]) -> Self {
403        // SAFETY: An empty array cannot contain duplicates.
404        unsafe { Self::from_array_unchecked(value) }
405    }
406}
407
408impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 1>> for (T,) {
409    fn from(array: UniqueEntityEquivalentArray<T, 1>) -> Self {
410        Self::from(array.into_inner())
411    }
412}
413
414impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 2>> for (T, T) {
415    fn from(array: UniqueEntityEquivalentArray<T, 2>) -> Self {
416        Self::from(array.into_inner())
417    }
418}
419
420impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 3>> for (T, T, T) {
421    fn from(array: UniqueEntityEquivalentArray<T, 3>) -> Self {
422        Self::from(array.into_inner())
423    }
424}
425
426impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 4>> for (T, T, T, T) {
427    fn from(array: UniqueEntityEquivalentArray<T, 4>) -> Self {
428        Self::from(array.into_inner())
429    }
430}
431
432impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 5>> for (T, T, T, T, T) {
433    fn from(array: UniqueEntityEquivalentArray<T, 5>) -> Self {
434        Self::from(array.into_inner())
435    }
436}
437
438impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 6>> for (T, T, T, T, T, T) {
439    fn from(array: UniqueEntityEquivalentArray<T, 6>) -> Self {
440        Self::from(array.into_inner())
441    }
442}
443
444impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 7>> for (T, T, T, T, T, T, T) {
445    fn from(array: UniqueEntityEquivalentArray<T, 7>) -> Self {
446        Self::from(array.into_inner())
447    }
448}
449
450impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 8>> for (T, T, T, T, T, T, T, T) {
451    fn from(array: UniqueEntityEquivalentArray<T, 8>) -> Self {
452        Self::from(array.into_inner())
453    }
454}
455
456impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 9>> for (T, T, T, T, T, T, T, T, T) {
457    fn from(array: UniqueEntityEquivalentArray<T, 9>) -> Self {
458        Self::from(array.into_inner())
459    }
460}
461
462impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 10>>
463    for (T, T, T, T, T, T, T, T, T, T)
464{
465    fn from(array: UniqueEntityEquivalentArray<T, 10>) -> Self {
466        Self::from(array.into_inner())
467    }
468}
469
470impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 11>>
471    for (T, T, T, T, T, T, T, T, T, T, T)
472{
473    fn from(array: UniqueEntityEquivalentArray<T, 11>) -> Self {
474        Self::from(array.into_inner())
475    }
476}
477
478impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 12>>
479    for (T, T, T, T, T, T, T, T, T, T, T, T)
480{
481    fn from(array: UniqueEntityEquivalentArray<T, 12>) -> Self {
482        Self::from(array.into_inner())
483    }
484}
485
486impl<T: EntityEquivalent + Ord, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
487    for BTreeSet<T>
488{
489    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
490        BTreeSet::from(value.0)
491    }
492}
493
494impl<T: EntityEquivalent + Ord, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
495    for BinaryHeap<T>
496{
497    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
498        BinaryHeap::from(value.0)
499    }
500}
501
502impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
503    for LinkedList<T>
504{
505    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
506        LinkedList::from(value.0)
507    }
508}
509
510impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>> for Vec<T> {
511    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
512        Vec::from(value.0)
513    }
514}
515
516impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>> for VecDeque<T> {
517    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
518        VecDeque::from(value.0)
519    }
520}
521
522impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
523    PartialEq<&UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentArray<T, N>
524{
525    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
526        self.0.eq(&other.as_inner())
527    }
528}
529
530impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
531    PartialEq<UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentArray<T, N>
532{
533    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
534        self.0.eq(other.as_inner())
535    }
536}
537
538impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
539    PartialEq<&UniqueEntityEquivalentArray<U, N>> for Vec<T>
540{
541    fn eq(&self, other: &&UniqueEntityEquivalentArray<U, N>) -> bool {
542        self.eq(&other.0)
543    }
544}
545
546impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
547    PartialEq<&UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
548{
549    fn eq(&self, other: &&UniqueEntityEquivalentArray<U, N>) -> bool {
550        self.eq(&other.0)
551    }
552}
553
554impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
555    PartialEq<&mut UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
556{
557    fn eq(&self, other: &&mut UniqueEntityEquivalentArray<U, N>) -> bool {
558        self.eq(&other.0)
559    }
560}
561
562impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
563    PartialEq<UniqueEntityEquivalentArray<U, N>> for Vec<T>
564{
565    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
566        self.eq(&other.0)
567    }
568}
569
570impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
571    PartialEq<UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
572{
573    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
574        self.eq(&other.0)
575    }
576}
577
578/// A by-value array iterator.
579///
580/// Equivalent to [`array::IntoIter`].
581pub type IntoIter<const N: usize, T = Entity> = UniqueEntityIter<array::IntoIter<T, N>>;
582
583impl<T: EntityEquivalent, const N: usize> UniqueEntityIter<array::IntoIter<T, N>> {
584    /// Returns an immutable slice of all elements that have not been yielded
585    /// yet.
586    ///
587    /// Equivalent to [`array::IntoIter::as_slice`].
588    pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
589        // SAFETY: All elements in the original slice are unique.
590        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
591    }
592
593    /// Returns a mutable slice of all elements that have not been yielded yet.
594    ///
595    /// Equivalent to [`array::IntoIter::as_mut_slice`].
596    pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
597        // SAFETY: All elements in the original slice are unique.
598        unsafe {
599            UniqueEntityEquivalentSlice::from_slice_unchecked_mut(
600                self.as_mut_inner().as_mut_slice(),
601            )
602        }
603    }
604}