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}
157
158impl<T: EntityEquivalent> Default for UniqueEntityEquivalentArray<T, 0> {
159    fn default() -> Self {
160        Self(Default::default())
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_iterator_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_iterator_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    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {
225        // SAFETY: All elements in the original slice are unique.
226        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
227    }
228}
229
230impl<T: EntityEquivalent, const N: usize> Index<Range<usize>>
231    for UniqueEntityEquivalentArray<T, N>
232{
233    type Output = UniqueEntityEquivalentSlice<T>;
234    fn index(&self, key: Range<usize>) -> &Self::Output {
235        // SAFETY: All elements in the original slice are unique.
236        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
237    }
238}
239
240impl<T: EntityEquivalent, const N: usize> Index<RangeFrom<usize>>
241    for UniqueEntityEquivalentArray<T, N>
242{
243    type Output = UniqueEntityEquivalentSlice<T>;
244    fn index(&self, key: RangeFrom<usize>) -> &Self::Output {
245        // SAFETY: All elements in the original slice are unique.
246        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
247    }
248}
249
250impl<T: EntityEquivalent, const N: usize> Index<RangeFull> for UniqueEntityEquivalentArray<T, N> {
251    type Output = UniqueEntityEquivalentSlice<T>;
252    fn index(&self, key: RangeFull) -> &Self::Output {
253        // SAFETY: All elements in the original slice are unique.
254        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
255    }
256}
257
258impl<T: EntityEquivalent, const N: usize> Index<RangeInclusive<usize>>
259    for UniqueEntityEquivalentArray<T, N>
260{
261    type Output = UniqueEntityEquivalentSlice<T>;
262    fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {
263        // SAFETY: All elements in the original slice are unique.
264        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
265    }
266}
267
268impl<T: EntityEquivalent, const N: usize> Index<RangeTo<usize>>
269    for UniqueEntityEquivalentArray<T, N>
270{
271    type Output = UniqueEntityEquivalentSlice<T>;
272    fn index(&self, key: RangeTo<usize>) -> &Self::Output {
273        // SAFETY: All elements in the original slice are unique.
274        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
275    }
276}
277
278impl<T: EntityEquivalent, const N: usize> Index<RangeToInclusive<usize>>
279    for UniqueEntityEquivalentArray<T, N>
280{
281    type Output = UniqueEntityEquivalentSlice<T>;
282    fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {
283        // SAFETY: All elements in the original slice are unique.
284        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
285    }
286}
287
288impl<T: EntityEquivalent, const N: usize> Index<usize> for UniqueEntityEquivalentArray<T, N> {
289    type Output = T;
290    fn index(&self, key: usize) -> &T {
291        self.0.index(key)
292    }
293}
294
295impl<T: EntityEquivalent, const N: usize> IndexMut<(Bound<usize>, Bound<usize>)>
296    for UniqueEntityEquivalentArray<T, N>
297{
298    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {
299        // SAFETY: All elements in the original slice are unique.
300        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
301    }
302}
303
304impl<T: EntityEquivalent, const N: usize> IndexMut<Range<usize>>
305    for UniqueEntityEquivalentArray<T, N>
306{
307    fn index_mut(&mut self, key: Range<usize>) -> &mut Self::Output {
308        // SAFETY: All elements in the original slice are unique.
309        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
310    }
311}
312
313impl<T: EntityEquivalent, const N: usize> IndexMut<RangeFrom<usize>>
314    for UniqueEntityEquivalentArray<T, N>
315{
316    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self::Output {
317        // SAFETY: All elements in the original slice are unique.
318        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
319    }
320}
321
322impl<T: EntityEquivalent, const N: usize> IndexMut<RangeFull>
323    for UniqueEntityEquivalentArray<T, N>
324{
325    fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output {
326        // SAFETY: All elements in the original slice are unique.
327        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
328    }
329}
330
331impl<T: EntityEquivalent, const N: usize> IndexMut<RangeInclusive<usize>>
332    for UniqueEntityEquivalentArray<T, N>
333{
334    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self::Output {
335        // SAFETY: All elements in the original slice are unique.
336        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
337    }
338}
339
340impl<T: EntityEquivalent, const N: usize> IndexMut<RangeTo<usize>>
341    for UniqueEntityEquivalentArray<T, N>
342{
343    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self::Output {
344        // SAFETY: All elements in the original slice are unique.
345        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
346    }
347}
348
349impl<T: EntityEquivalent, const N: usize> IndexMut<RangeToInclusive<usize>>
350    for UniqueEntityEquivalentArray<T, N>
351{
352    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self::Output {
353        // SAFETY: All elements in the original slice are unique.
354        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
355    }
356}
357
358impl<T: EntityEquivalent + Clone> From<&[T; 1]> for UniqueEntityEquivalentArray<T, 1> {
359    fn from(value: &[T; 1]) -> Self {
360        Self(value.clone())
361    }
362}
363
364impl<T: EntityEquivalent + Clone> From<&[T; 0]> for UniqueEntityEquivalentArray<T, 0> {
365    fn from(value: &[T; 0]) -> Self {
366        Self(value.clone())
367    }
368}
369
370impl<T: EntityEquivalent + Clone> From<&mut [T; 1]> for UniqueEntityEquivalentArray<T, 1> {
371    fn from(value: &mut [T; 1]) -> Self {
372        Self(value.clone())
373    }
374}
375
376impl<T: EntityEquivalent + Clone> From<&mut [T; 0]> for UniqueEntityEquivalentArray<T, 0> {
377    fn from(value: &mut [T; 0]) -> Self {
378        Self(value.clone())
379    }
380}
381
382impl<T: EntityEquivalent> From<[T; 1]> for UniqueEntityEquivalentArray<T, 1> {
383    fn from(value: [T; 1]) -> Self {
384        Self(value)
385    }
386}
387
388impl<T: EntityEquivalent> From<[T; 0]> for UniqueEntityEquivalentArray<T, 0> {
389    fn from(value: [T; 0]) -> Self {
390        Self(value)
391    }
392}
393
394impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 1>> for (T,) {
395    fn from(array: UniqueEntityEquivalentArray<T, 1>) -> Self {
396        Self::from(array.into_inner())
397    }
398}
399
400impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 2>> for (T, T) {
401    fn from(array: UniqueEntityEquivalentArray<T, 2>) -> Self {
402        Self::from(array.into_inner())
403    }
404}
405
406impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 3>> for (T, T, T) {
407    fn from(array: UniqueEntityEquivalentArray<T, 3>) -> Self {
408        Self::from(array.into_inner())
409    }
410}
411
412impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 4>> for (T, T, T, T) {
413    fn from(array: UniqueEntityEquivalentArray<T, 4>) -> Self {
414        Self::from(array.into_inner())
415    }
416}
417
418impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 5>> for (T, T, T, T, T) {
419    fn from(array: UniqueEntityEquivalentArray<T, 5>) -> Self {
420        Self::from(array.into_inner())
421    }
422}
423
424impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 6>> for (T, T, T, T, T, T) {
425    fn from(array: UniqueEntityEquivalentArray<T, 6>) -> Self {
426        Self::from(array.into_inner())
427    }
428}
429
430impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 7>> for (T, T, T, T, T, T, T) {
431    fn from(array: UniqueEntityEquivalentArray<T, 7>) -> Self {
432        Self::from(array.into_inner())
433    }
434}
435
436impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 8>> for (T, T, T, T, T, T, T, T) {
437    fn from(array: UniqueEntityEquivalentArray<T, 8>) -> Self {
438        Self::from(array.into_inner())
439    }
440}
441
442impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 9>> for (T, T, T, T, T, T, T, T, T) {
443    fn from(array: UniqueEntityEquivalentArray<T, 9>) -> Self {
444        Self::from(array.into_inner())
445    }
446}
447
448impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 10>>
449    for (T, T, T, T, T, T, T, T, T, T)
450{
451    fn from(array: UniqueEntityEquivalentArray<T, 10>) -> Self {
452        Self::from(array.into_inner())
453    }
454}
455
456impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 11>>
457    for (T, T, T, T, T, T, T, T, T, T, T)
458{
459    fn from(array: UniqueEntityEquivalentArray<T, 11>) -> Self {
460        Self::from(array.into_inner())
461    }
462}
463
464impl<T: EntityEquivalent> From<UniqueEntityEquivalentArray<T, 12>>
465    for (T, T, T, T, T, T, T, T, T, T, T, T)
466{
467    fn from(array: UniqueEntityEquivalentArray<T, 12>) -> Self {
468        Self::from(array.into_inner())
469    }
470}
471
472impl<T: EntityEquivalent + Ord, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
473    for BTreeSet<T>
474{
475    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
476        BTreeSet::from(value.0)
477    }
478}
479
480impl<T: EntityEquivalent + Ord, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
481    for BinaryHeap<T>
482{
483    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
484        BinaryHeap::from(value.0)
485    }
486}
487
488impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
489    for LinkedList<T>
490{
491    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
492        LinkedList::from(value.0)
493    }
494}
495
496impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>> for Vec<T> {
497    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
498        Vec::from(value.0)
499    }
500}
501
502impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>> for VecDeque<T> {
503    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
504        VecDeque::from(value.0)
505    }
506}
507
508impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
509    PartialEq<&UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentArray<T, N>
510{
511    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
512        self.0.eq(&other.as_inner())
513    }
514}
515
516impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
517    PartialEq<UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentArray<T, N>
518{
519    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
520        self.0.eq(other.as_inner())
521    }
522}
523
524impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
525    PartialEq<&UniqueEntityEquivalentArray<U, N>> for Vec<T>
526{
527    fn eq(&self, other: &&UniqueEntityEquivalentArray<U, N>) -> bool {
528        self.eq(&other.0)
529    }
530}
531
532impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
533    PartialEq<&UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
534{
535    fn eq(&self, other: &&UniqueEntityEquivalentArray<U, N>) -> bool {
536        self.eq(&other.0)
537    }
538}
539
540impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
541    PartialEq<&mut UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
542{
543    fn eq(&self, other: &&mut UniqueEntityEquivalentArray<U, N>) -> bool {
544        self.eq(&other.0)
545    }
546}
547
548impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
549    PartialEq<UniqueEntityEquivalentArray<U, N>> for Vec<T>
550{
551    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
552        self.eq(&other.0)
553    }
554}
555
556impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
557    PartialEq<UniqueEntityEquivalentArray<U, N>> for VecDeque<T>
558{
559    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
560        self.eq(&other.0)
561    }
562}
563
564/// A by-value array iterator.
565///
566/// Equivalent to [`array::IntoIter`].
567pub type IntoIter<const N: usize, T = Entity> = UniqueEntityIter<array::IntoIter<T, N>>;
568
569impl<T: EntityEquivalent, const N: usize> UniqueEntityIter<array::IntoIter<T, N>> {
570    /// Returns an immutable slice of all elements that have not been yielded
571    /// yet.
572    ///
573    /// Equivalent to [`array::IntoIter::as_slice`].
574    pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
575        // SAFETY: All elements in the original slice are unique.
576        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
577    }
578
579    /// Returns a mutable slice of all elements that have not been yielded yet.
580    ///
581    /// Equivalent to [`array::IntoIter::as_mut_slice`].
582    pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
583        // SAFETY: All elements in the original slice are unique.
584        unsafe {
585            UniqueEntityEquivalentSlice::from_slice_unchecked_mut(
586                self.as_mut_inner().as_mut_slice(),
587            )
588        }
589    }
590}