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