bevy_ecs/entity/
unique_slice.rs

1//! A wrapper around entity slices with a uniqueness invariant.
2
3use core::{
4    array::TryFromSliceError,
5    borrow::Borrow,
6    cmp::Ordering,
7    fmt::Debug,
8    iter::FusedIterator,
9    ops::{
10        Bound, Deref, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
11        RangeToInclusive,
12    },
13    ptr,
14    slice::{self, SliceIndex},
15};
16
17use alloc::{
18    borrow::{Cow, ToOwned},
19    boxed::Box,
20    collections::VecDeque,
21    rc::Rc,
22    vec::Vec,
23};
24
25use bevy_platform::sync::Arc;
26
27use super::{
28    unique_vec::{self, UniqueEntityEquivalentVec},
29    Entity, EntityEquivalent, EntitySet, EntitySetIterator, FromEntitySetIterator,
30    UniqueEntityEquivalentArray, UniqueEntityIter,
31};
32
33/// A slice that contains only unique entities.
34///
35/// This can be obtained by slicing [`UniqueEntityEquivalentVec`].
36///
37/// When `T` is [`Entity`], use [`UniqueEntitySlice`].
38#[repr(transparent)]
39#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
40pub struct UniqueEntityEquivalentSlice<T: EntityEquivalent>([T]);
41
42/// A slice that contains only unique [`Entity`].
43///
44/// This is the default case of a [`UniqueEntityEquivalentSlice`].
45pub type UniqueEntitySlice = UniqueEntityEquivalentSlice<Entity>;
46
47impl<T: EntityEquivalent> UniqueEntityEquivalentSlice<T> {
48    /// Constructs a `UniqueEntityEquivalentSlice` from a [`&[T]`] unsafely.
49    ///
50    /// # Safety
51    ///
52    /// `slice` must contain only unique elements.
53    pub const unsafe fn from_slice_unchecked(slice: &[T]) -> &Self {
54        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
55        unsafe { &*(ptr::from_ref(slice) as *const Self) }
56    }
57
58    /// Constructs a `UniqueEntityEquivalentSlice` from a [`&mut [T]`] unsafely.
59    ///
60    /// # Safety
61    ///
62    /// `slice` must contain only unique elements.
63    pub const unsafe fn from_slice_unchecked_mut(slice: &mut [T]) -> &mut Self {
64        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
65        unsafe { &mut *(ptr::from_mut(slice) as *mut Self) }
66    }
67
68    /// Casts to `self` to a standard slice.
69    pub const fn as_inner(&self) -> &[T] {
70        &self.0
71    }
72
73    /// Constructs a `UniqueEntityEquivalentSlice` from a [`Box<[T]>`] unsafely.
74    ///
75    /// # Safety
76    ///
77    /// `slice` must contain only unique elements.
78    pub unsafe fn from_boxed_slice_unchecked(slice: Box<[T]>) -> Box<Self> {
79        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
80        unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) }
81    }
82
83    /// Casts `self` to the inner slice.
84    pub fn into_boxed_inner(self: Box<Self>) -> Box<[T]> {
85        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
86        unsafe { Box::from_raw(Box::into_raw(self) as *mut [T]) }
87    }
88
89    /// Constructs a `UniqueEntityEquivalentSlice` from a [`Arc<[T]>`] unsafely.
90    ///
91    /// # Safety
92    ///
93    /// `slice` must contain only unique elements.
94    pub unsafe fn from_arc_slice_unchecked(slice: Arc<[T]>) -> Arc<Self> {
95        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
96        unsafe { Arc::from_raw(Arc::into_raw(slice) as *mut Self) }
97    }
98
99    /// Casts `self` to the inner slice.
100    pub fn into_arc_inner(this: Arc<Self>) -> Arc<[T]> {
101        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
102        unsafe { Arc::from_raw(Arc::into_raw(this) as *mut [T]) }
103    }
104
105    // Constructs a `UniqueEntityEquivalentSlice` from a [`Rc<[T]>`] unsafely.
106    ///
107    /// # Safety
108    ///
109    /// `slice` must contain only unique elements.
110    pub unsafe fn from_rc_slice_unchecked(slice: Rc<[T]>) -> Rc<Self> {
111        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
112        unsafe { Rc::from_raw(Rc::into_raw(slice) as *mut Self) }
113    }
114
115    /// Casts `self` to the inner slice.
116    pub fn into_rc_inner(self: Rc<Self>) -> Rc<[T]> {
117        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
118        unsafe { Rc::from_raw(Rc::into_raw(self) as *mut [T]) }
119    }
120
121    /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty.
122    ///
123    /// Equivalent to [`[T]::split_first`](slice::split_first).
124    pub const fn split_first(&self) -> Option<(&T, &Self)> {
125        let Some((first, rest)) = self.0.split_first() else {
126            return None;
127        };
128        // SAFETY: All elements in the original slice are unique.
129        Some((first, unsafe { Self::from_slice_unchecked(rest) }))
130    }
131
132    /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty.
133    ///
134    /// Equivalent to [`[T]::split_last`](slice::split_last).
135    pub const fn split_last(&self) -> Option<(&T, &Self)> {
136        let Some((last, rest)) = self.0.split_last() else {
137            return None;
138        };
139        // SAFETY: All elements in the original slice are unique.
140        Some((last, unsafe { Self::from_slice_unchecked(rest) }))
141    }
142
143    /// Returns an array reference to the first `N` items in the slice.
144    ///
145    /// Equivalent to [`[T]::first_chunk`](slice::first_chunk).
146    pub const fn first_chunk<const N: usize>(&self) -> Option<&UniqueEntityEquivalentArray<T, N>> {
147        let Some(chunk) = self.0.first_chunk() else {
148            return None;
149        };
150        // SAFETY: All elements in the original slice are unique.
151        Some(unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk) })
152    }
153
154    /// Returns an array reference to the first `N` items in the slice and the remaining slice.
155    ///
156    /// Equivalent to [`[T]::split_first_chunk`](slice::split_first_chunk).
157    pub const fn split_first_chunk<const N: usize>(
158        &self,
159    ) -> Option<(
160        &UniqueEntityEquivalentArray<T, N>,
161        &UniqueEntityEquivalentSlice<T>,
162    )> {
163        let Some((chunk, rest)) = self.0.split_first_chunk() else {
164            return None;
165        };
166        // SAFETY: All elements in the original slice are unique.
167        unsafe {
168            Some((
169                UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk),
170                Self::from_slice_unchecked(rest),
171            ))
172        }
173    }
174
175    /// Returns an array reference to the last `N` items in the slice and the remaining slice.
176    ///
177    /// Equivalent to [`[T]::split_last_chunk`](slice::split_last_chunk).
178    pub const fn split_last_chunk<const N: usize>(
179        &self,
180    ) -> Option<(
181        &UniqueEntityEquivalentSlice<T>,
182        &UniqueEntityEquivalentArray<T, N>,
183    )> {
184        let Some((rest, chunk)) = self.0.split_last_chunk() else {
185            return None;
186        };
187        // SAFETY: All elements in the original slice are unique.
188        unsafe {
189            Some((
190                Self::from_slice_unchecked(rest),
191                UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk),
192            ))
193        }
194    }
195
196    /// Returns an array reference to the last `N` items in the slice.
197    ///
198    /// Equivalent to [`[T]::last_chunk`](slice::last_chunk).
199    pub const fn last_chunk<const N: usize>(&self) -> Option<&UniqueEntityEquivalentArray<T, N>> {
200        let Some(chunk) = self.0.last_chunk() else {
201            return None;
202        };
203        // SAFETY: All elements in the original slice are unique.
204        Some(unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk) })
205    }
206
207    /// Returns a reference to a subslice.
208    ///
209    /// Equivalent to the range functionality of [`[T]::get`].
210    ///
211    /// Note that only the inner [`[T]::get`] supports indexing with a [`usize`].
212    ///
213    /// [`[T]::get`]: `slice::get`
214    pub fn get<I>(&self, index: I) -> Option<&Self>
215    where
216        Self: Index<I>,
217        I: SliceIndex<[T], Output = [T]>,
218    {
219        self.0.get(index).map(|slice|
220            // SAFETY: All elements in the original slice are unique.
221            unsafe { Self::from_slice_unchecked(slice) })
222    }
223
224    /// Returns a mutable reference to a subslice.
225    ///
226    /// Equivalent to the range functionality of [`[T]::get_mut`].
227    ///
228    /// Note that `UniqueEntityEquivalentSlice::get_mut` cannot be called with a [`usize`].
229    ///
230    /// [`[T]::get_mut`]: `slice::get_mut`s
231    pub fn get_mut<I>(&mut self, index: I) -> Option<&mut Self>
232    where
233        Self: Index<I>,
234        I: SliceIndex<[T], Output = [T]>,
235    {
236        self.0.get_mut(index).map(|slice|
237            // SAFETY: All elements in the original slice are unique.
238            unsafe { Self::from_slice_unchecked_mut(slice) })
239    }
240
241    /// Returns a reference to a subslice, without doing bounds checking.
242    ///
243    /// Equivalent to the range functionality of [`[T]::get_unchecked`].
244    ///
245    /// Note that only the inner [`[T]::get_unchecked`] supports indexing with a [`usize`].
246    ///
247    /// # Safety
248    ///
249    /// `index` must be safe to use with [`[T]::get_unchecked`]
250    ///
251    /// [`[T]::get_unchecked`]: `slice::get_unchecked`
252    pub unsafe fn get_unchecked<I>(&self, index: I) -> &Self
253    where
254        Self: Index<I>,
255        I: SliceIndex<[T], Output = [T]>,
256    {
257        // SAFETY: All elements in the original slice are unique.
258        unsafe { Self::from_slice_unchecked(self.0.get_unchecked(index)) }
259    }
260    /// Returns a mutable reference to a subslice, without doing bounds checking.
261    ///
262    /// Equivalent to the range functionality of [`[T]::get_unchecked_mut`].
263    ///
264    /// Note that `UniqueEntityEquivalentSlice::get_unchecked_mut` cannot be called with an index.
265    ///
266    /// # Safety
267    ///
268    /// `index` must be safe to use with [`[T]::get_unchecked_mut`]
269    ///
270    /// [`[T]::get_unchecked_mut`]: `slice::get_unchecked_mut`
271    pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut Self
272    where
273        Self: Index<I>,
274        I: SliceIndex<[T], Output = [T]>,
275    {
276        // SAFETY: All elements in the original slice are unique.
277        unsafe { Self::from_slice_unchecked_mut(self.0.get_unchecked_mut(index)) }
278    }
279
280    /// Returns an unsafe mutable pointer to the slice's buffer.
281    pub const fn as_mut_ptr(&mut self) -> *mut T {
282        self.0.as_mut_ptr()
283    }
284
285    /// Returns the two unsafe mutable pointers spanning the slice.
286    pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> {
287        self.0.as_mut_ptr_range()
288    }
289
290    /// Swaps two elements in the slice.
291    pub fn swap(&mut self, a: usize, b: usize) {
292        self.0.swap(a, b);
293    }
294
295    /// Reverses the order of elements in the slice, in place.
296    pub fn reverse(&mut self) {
297        self.0.reverse();
298    }
299
300    /// Returns an iterator over the slice.
301    pub fn iter(&self) -> Iter<'_, T> {
302        // SAFETY: All elements in the original slice are unique.
303        unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.iter()) }
304    }
305
306    /// Returns an iterator over all contiguous windows of length
307    /// `size`.
308    ///
309    /// Equivalent to [`[T]::windows`].
310    ///
311    /// [`[T]::windows`]: `slice::windows`
312    pub fn windows(&self, size: usize) -> Windows<'_, T> {
313        // SAFETY: Any subslice of a unique slice is also unique.
314        unsafe {
315            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.windows(size))
316        }
317    }
318
319    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
320    /// beginning of the slice.
321    ///
322    /// Equivalent to [`[T]::chunks`].
323    ///
324    /// [`[T]::chunks`]: `slice::chunks`
325    pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> {
326        // SAFETY: Any subslice of a unique slice is also unique.
327        unsafe {
328            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(
329                self.0.chunks(chunk_size),
330            )
331        }
332    }
333
334    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
335    /// beginning of the slice.
336    ///
337    /// Equivalent to [`[T]::chunks_mut`].
338    ///
339    /// [`[T]::chunks_mut`]: `slice::chunks_mut`
340    pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> {
341        // SAFETY: Any subslice of a unique slice is also unique.
342        unsafe {
343            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
344                self.0.chunks_mut(chunk_size),
345            )
346        }
347    }
348
349    ///
350    ///
351    /// Equivalent to [`[T]::chunks_exact`].
352    ///
353    /// [`[T]::chunks_exact`]: `slice::chunks_exact`
354    pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> {
355        // SAFETY: Any subslice of a unique slice is also unique.
356        unsafe {
357            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(
358                self.0.chunks_exact(chunk_size),
359            )
360        }
361    }
362
363    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
364    /// beginning of the slice.
365    ///
366    /// Equivalent to [`[T]::chunks_exact_mut`].
367    ///
368    /// [`[T]::chunks_exact_mut`]: `slice::chunks_exact_mut`
369    pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> {
370        // SAFETY: Any subslice of a unique slice is also unique.
371        unsafe {
372            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
373                self.0.chunks_exact_mut(chunk_size),
374            )
375        }
376    }
377
378    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
379    /// of the slice.
380    ///
381    /// Equivalent to [`[T]::rchunks`].
382    ///
383    /// [`[T]::rchunks`]: `slice::rchunks`
384    pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> {
385        // SAFETY: Any subslice of a unique slice is also unique.
386        unsafe {
387            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(
388                self.0.rchunks(chunk_size),
389            )
390        }
391    }
392
393    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
394    /// of the slice.
395    ///
396    /// Equivalent to [`[T]::rchunks_mut`].
397    ///
398    /// [`[T]::rchunks_mut`]: `slice::rchunks_mut`
399    pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> {
400        // SAFETY: Any subslice of a unique slice is also unique.
401        unsafe {
402            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
403                self.0.rchunks_mut(chunk_size),
404            )
405        }
406    }
407
408    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the
409    /// end of the slice.
410    ///
411    /// Equivalent to [`[T]::rchunks_exact`].
412    ///
413    /// [`[T]::rchunks_exact`]: `slice::rchunks_exact`
414    pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> {
415        // SAFETY: Any subslice of a unique slice is also unique.
416        unsafe {
417            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(
418                self.0.rchunks_exact(chunk_size),
419            )
420        }
421    }
422
423    /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end
424    /// of the slice.
425    ///
426    /// Equivalent to [`[T]::rchunks_exact_mut`].
427    ///
428    /// [`[T]::rchunks_exact_mut`]: `slice::rchunks_exact_mut`
429    pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> {
430        // SAFETY: Any subslice of a unique slice is also unique.
431        unsafe {
432            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
433                self.0.rchunks_exact_mut(chunk_size),
434            )
435        }
436    }
437
438    /// Returns an iterator over the slice producing non-overlapping runs
439    /// of elements using the predicate to separate them.
440    ///
441    /// Equivalent to [`[T]::chunk_by`].
442    ///
443    /// [`[T]::chunk_by`]: `slice::chunk_by`
444    pub fn chunk_by<F>(&self, pred: F) -> ChunkBy<'_, F, T>
445    where
446        F: FnMut(&T, &T) -> bool,
447    {
448        // SAFETY: Any subslice of a unique slice is also unique.
449        unsafe {
450            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.chunk_by(pred))
451        }
452    }
453
454    /// Returns an iterator over the slice producing non-overlapping mutable
455    /// runs of elements using the predicate to separate them.
456    ///
457    /// Equivalent to [`[T]::chunk_by_mut`].
458    ///
459    /// [`[T]::chunk_by_mut`]: `slice::chunk_by_mut`
460    pub fn chunk_by_mut<F>(&mut self, pred: F) -> ChunkByMut<'_, F, T>
461    where
462        F: FnMut(&T, &T) -> bool,
463    {
464        // SAFETY: Any subslice of a unique slice is also unique.
465        unsafe {
466            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
467                self.0.chunk_by_mut(pred),
468            )
469        }
470    }
471
472    /// Divides one slice into two at an index.
473    ///
474    /// Equivalent to [`[T]::split_at`](slice::split_at).
475    pub const fn split_at(&self, mid: usize) -> (&Self, &Self) {
476        let (left, right) = self.0.split_at(mid);
477        // SAFETY: All elements in the original slice are unique.
478        unsafe {
479            (
480                Self::from_slice_unchecked(left),
481                Self::from_slice_unchecked(right),
482            )
483        }
484    }
485
486    /// Divides one mutable slice into two at an index.
487    ///
488    /// Equivalent to [`[T]::split_at_mut`](slice::split_at_mut).
489    pub const fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self) {
490        let (left, right) = self.0.split_at_mut(mid);
491        // SAFETY: All elements in the original slice are unique.
492        unsafe {
493            (
494                Self::from_slice_unchecked_mut(left),
495                Self::from_slice_unchecked_mut(right),
496            )
497        }
498    }
499
500    /// Divides one slice into two at an index, without doing bounds checking.
501    ///
502    /// Equivalent to [`[T]::split_at_unchecked`](slice::split_at_unchecked).
503    ///
504    /// # Safety
505    ///
506    /// `mid` must be safe to use in [`[T]::split_at_unchecked`].
507    ///
508    /// [`[T]::split_at_unchecked`]: `slice::split_at_unchecked`
509    pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&Self, &Self) {
510        // SAFETY: The safety contract is upheld by the caller.
511        let (left, right) = unsafe { self.0.split_at_unchecked(mid) };
512        // SAFETY: All elements in the original slice are unique.
513        unsafe {
514            (
515                Self::from_slice_unchecked(left),
516                Self::from_slice_unchecked(right),
517            )
518        }
519    }
520
521    /// Divides one mutable slice into two at an index, without doing bounds checking.
522    ///
523    /// Equivalent to [`[T]::split_at_mut_unchecked`](slice::split_at_mut_unchecked).
524    ///
525    /// # Safety
526    ///
527    /// `mid` must be safe to use in [`[T]::split_at_mut_unchecked`].
528    ///
529    /// [`[T]::split_at_mut_unchecked`]: `slice::split_at_mut_unchecked`
530    pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut Self, &mut Self) {
531        // SAFETY: The safety contract is upheld by the caller.
532        let (left, right) = unsafe { self.0.split_at_mut_unchecked(mid) };
533        // SAFETY: All elements in the original slice are unique.
534        unsafe {
535            (
536                Self::from_slice_unchecked_mut(left),
537                Self::from_slice_unchecked_mut(right),
538            )
539        }
540    }
541
542    /// Divides one slice into two at an index, returning `None` if the slice is
543    /// too short.
544    ///
545    /// Equivalent to [`[T]::split_at_checked`](slice::split_at_checked).
546    pub const fn split_at_checked(&self, mid: usize) -> Option<(&Self, &Self)> {
547        let Some((left, right)) = self.0.split_at_checked(mid) else {
548            return None;
549        };
550        // SAFETY: All elements in the original slice are unique.
551        unsafe {
552            Some((
553                Self::from_slice_unchecked(left),
554                Self::from_slice_unchecked(right),
555            ))
556        }
557    }
558
559    /// Divides one mutable slice into two at an index, returning `None` if the
560    /// slice is too short.
561    ///
562    /// Equivalent to [`[T]::split_at_mut_checked`](slice::split_at_mut_checked).
563    pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut Self, &mut Self)> {
564        let Some((left, right)) = self.0.split_at_mut_checked(mid) else {
565            return None;
566        };
567        // SAFETY: All elements in the original slice are unique.
568        unsafe {
569            Some((
570                Self::from_slice_unchecked_mut(left),
571                Self::from_slice_unchecked_mut(right),
572            ))
573        }
574    }
575
576    /// Returns an iterator over subslices separated by elements that match
577    /// `pred`.
578    ///
579    /// Equivalent to [`[T]::split`].
580    ///
581    /// [`[T]::split`]: `slice::split`
582    pub fn split<F>(&self, pred: F) -> Split<'_, F, T>
583    where
584        F: FnMut(&T) -> bool,
585    {
586        // SAFETY: Any subslice of a unique slice is also unique.
587        unsafe {
588            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.split(pred))
589        }
590    }
591
592    /// Returns an iterator over mutable subslices separated by elements that
593    /// match `pred`.
594    ///
595    /// Equivalent to [`[T]::split_mut`].
596    ///
597    /// [`[T]::split_mut`]: `slice::split_mut`
598    pub fn split_mut<F>(&mut self, pred: F) -> SplitMut<'_, F, T>
599    where
600        F: FnMut(&T) -> bool,
601    {
602        // SAFETY: Any subslice of a unique slice is also unique.
603        unsafe {
604            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
605                self.0.split_mut(pred),
606            )
607        }
608    }
609
610    /// Returns an iterator over subslices separated by elements that match
611    /// `pred`.
612    ///
613    /// Equivalent to [`[T]::split_inclusive`].
614    ///
615    /// [`[T]::split_inclusive`]: `slice::split_inclusive`
616    pub fn split_inclusive<F>(&self, pred: F) -> SplitInclusive<'_, F, T>
617    where
618        F: FnMut(&T) -> bool,
619    {
620        // SAFETY: Any subslice of a unique slice is also unique.
621        unsafe {
622            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(
623                self.0.split_inclusive(pred),
624            )
625        }
626    }
627
628    /// Returns an iterator over mutable subslices separated by elements that
629    /// match `pred`.
630    ///
631    /// Equivalent to [`[T]::split_inclusive_mut`].
632    ///
633    /// [`[T]::split_inclusive_mut`]: `slice::split_inclusive_mut`
634    pub fn split_inclusive_mut<F>(&mut self, pred: F) -> SplitInclusiveMut<'_, F, T>
635    where
636        F: FnMut(&T) -> bool,
637    {
638        // SAFETY: Any subslice of a unique slice is also unique.
639        unsafe {
640            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
641                self.0.split_inclusive_mut(pred),
642            )
643        }
644    }
645
646    /// Returns an iterator over subslices separated by elements that match
647    /// `pred`, starting at the end of the slice and working backwards.
648    ///
649    /// Equivalent to [`[T]::rsplit`].
650    ///
651    /// [`[T]::rsplit`]: `slice::rsplit`
652    pub fn rsplit<F>(&self, pred: F) -> RSplit<'_, F, T>
653    where
654        F: FnMut(&T) -> bool,
655    {
656        // SAFETY: Any subslice of a unique slice is also unique.
657        unsafe {
658            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplit(pred))
659        }
660    }
661
662    /// Returns an iterator over mutable subslices separated by elements that
663    /// match `pred`, starting at the end of the slice and working
664    /// backwards.
665    ///
666    /// Equivalent to [`[T]::rsplit_mut`].
667    ///
668    /// [`[T]::rsplit_mut`]: `slice::rsplit_mut`
669    pub fn rsplit_mut<F>(&mut self, pred: F) -> RSplitMut<'_, F, T>
670    where
671        F: FnMut(&T) -> bool,
672    {
673        // SAFETY: Any subslice of a unique slice is also unique.
674        unsafe {
675            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
676                self.0.rsplit_mut(pred),
677            )
678        }
679    }
680
681    /// Returns an iterator over subslices separated by elements that match
682    /// `pred`, limited to returning at most `n` items.
683    ///
684    /// Equivalent to [`[T]::splitn`].
685    ///
686    /// [`[T]::splitn`]: `slice::splitn`
687    pub fn splitn<F>(&self, n: usize, pred: F) -> SplitN<'_, F, T>
688    where
689        F: FnMut(&T) -> bool,
690    {
691        // SAFETY: Any subslice of a unique slice is also unique.
692        unsafe {
693            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.splitn(n, pred))
694        }
695    }
696
697    /// Returns an iterator over mutable subslices separated by elements that match
698    /// `pred`, limited to returning at most `n` items.
699    ///
700    /// Equivalent to [`[T]::splitn_mut`].
701    ///
702    /// [`[T]::splitn_mut`]: `slice::splitn_mut`
703    pub fn splitn_mut<F>(&mut self, n: usize, pred: F) -> SplitNMut<'_, F, T>
704    where
705        F: FnMut(&T) -> bool,
706    {
707        // SAFETY: Any subslice of a unique slice is also unique.
708        unsafe {
709            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
710                self.0.splitn_mut(n, pred),
711            )
712        }
713    }
714
715    /// Returns an iterator over subslices separated by elements that match
716    /// `pred` limited to returning at most `n` items.
717    ///
718    /// Equivalent to [`[T]::rsplitn`].
719    ///
720    /// [`[T]::rsplitn`]: `slice::rsplitn`
721    pub fn rsplitn<F>(&self, n: usize, pred: F) -> RSplitN<'_, F, T>
722    where
723        F: FnMut(&T) -> bool,
724    {
725        // SAFETY: Any subslice of a unique slice is also unique.
726        unsafe {
727            UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplitn(n, pred))
728        }
729    }
730
731    /// Returns an iterator over subslices separated by elements that match
732    /// `pred` limited to returning at most `n` items.
733    ///
734    /// Equivalent to [`[T]::rsplitn_mut`].
735    ///
736    /// [`[T]::rsplitn_mut`]: `slice::rsplitn_mut`
737    pub fn rsplitn_mut<F>(&mut self, n: usize, pred: F) -> RSplitNMut<'_, F, T>
738    where
739        F: FnMut(&T) -> bool,
740    {
741        // SAFETY: Any subslice of a unique slice is also unique.
742        unsafe {
743            UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked(
744                self.0.rsplitn_mut(n, pred),
745            )
746        }
747    }
748
749    /// Sorts the slice **without** preserving the initial order of equal elements.
750    ///
751    /// Equivalent to [`[T]::sort_unstable`](slice::sort_unstable).
752    pub fn sort_unstable(&mut self)
753    where
754        T: Ord,
755    {
756        self.0.sort_unstable();
757    }
758
759    /// Sorts the slice with a comparison function, **without** preserving the initial order of
760    /// equal elements.
761    ///
762    /// Equivalent to [`[T]::sort_unstable_by`](slice::sort_unstable_by).
763    pub fn sort_unstable_by<F>(&mut self, compare: F)
764    where
765        F: FnMut(&T, &T) -> Ordering,
766    {
767        self.0.sort_unstable_by(compare);
768    }
769
770    /// Sorts the slice with a key extraction function, **without** preserving the initial order of
771    /// equal elements.
772    ///
773    /// Equivalent to [`[T]::sort_unstable_by_key`](slice::sort_unstable_by_key).
774    pub fn sort_unstable_by_key<K, F>(&mut self, f: F)
775    where
776        F: FnMut(&T) -> K,
777        K: Ord,
778    {
779        self.0.sort_unstable_by_key(f);
780    }
781
782    /// Rotates the slice in-place such that the first `mid` elements of the
783    /// slice move to the end while the last `self.len() - mid` elements move to
784    /// the front.
785    ///
786    /// Equivalent to [`[T]::rotate_left`](slice::rotate_left).
787    pub fn rotate_left(&mut self, mid: usize) {
788        self.0.rotate_left(mid);
789    }
790
791    /// Rotates the slice in-place such that the first `self.len() - k`
792    /// elements of the slice move to the end while the last `k` elements move
793    /// to the front.
794    ///
795    /// Equivalent to [`[T]::rotate_right`](slice::rotate_right).
796    pub fn rotate_right(&mut self, mid: usize) {
797        self.0.rotate_right(mid);
798    }
799
800    /// Sorts the slice, preserving initial order of equal elements.
801    ///
802    /// Equivalent to [`[T]::sort`](slice::sort()).
803    pub fn sort(&mut self)
804    where
805        T: Ord,
806    {
807        self.0.sort();
808    }
809
810    /// Sorts the slice with a comparison function, preserving initial order of equal elements.
811    ///
812    /// Equivalent to [`[T]::sort_by`](slice::sort_by).
813    pub fn sort_by<F>(&mut self, compare: F)
814    where
815        F: FnMut(&T, &T) -> Ordering,
816    {
817        self.0.sort_by(compare);
818    }
819
820    /// Sorts the slice with a key extraction function, preserving initial order of equal elements.
821    ///
822    /// Equivalent to [`[T]::sort_by_key`](slice::sort_by_key).
823    pub fn sort_by_key<K, F>(&mut self, f: F)
824    where
825        F: FnMut(&T) -> K,
826        K: Ord,
827    {
828        self.0.sort_by_key(f);
829    }
830
831    // Sorts the slice with a key extraction function, preserving initial order of equal elements.
832    ///
833    /// Equivalent to [`[T]::sort_by_cached_key`](slice::sort_by_cached_key).
834    pub fn sort_by_cached_key<K, F>(&mut self, f: F)
835    where
836        F: FnMut(&T) -> K,
837        K: Ord,
838    {
839        self.0.sort_by_cached_key(f);
840    }
841
842    /// Copies self into a new `UniqueEntityEquivalentVec`.
843    pub fn to_vec(&self) -> UniqueEntityEquivalentVec<T>
844    where
845        T: Clone,
846    {
847        // SAFETY: All elements in the original slice are unique.
848        unsafe { UniqueEntityEquivalentVec::from_vec_unchecked(self.0.to_vec()) }
849    }
850
851    /// Converts `self` into a vector without clones or allocation.
852    ///
853    /// Equivalent to [`[T]::into_vec`](slice::into_vec).
854    pub fn into_vec(self: Box<Self>) -> UniqueEntityEquivalentVec<T> {
855        // SAFETY:
856        // This matches the implementation of `slice::into_vec`.
857        // All elements in the original slice are unique.
858        unsafe {
859            let len = self.len();
860            let vec = Vec::from_raw_parts(Box::into_raw(self).cast::<T>(), len, len);
861            UniqueEntityEquivalentVec::from_vec_unchecked(vec)
862        }
863    }
864}
865
866/// Converts a reference to T into a slice of length 1 (without copying).
867pub const fn from_ref<T: EntityEquivalent>(s: &T) -> &UniqueEntityEquivalentSlice<T> {
868    // SAFETY: A slice with a length of 1 is always unique.
869    unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice::from_ref(s)) }
870}
871
872/// Converts a reference to T into a slice of length 1 (without copying).
873pub const fn from_mut<T: EntityEquivalent>(s: &mut T) -> &mut UniqueEntityEquivalentSlice<T> {
874    // SAFETY: A slice with a length of 1 is always unique.
875    unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice::from_mut(s)) }
876}
877
878/// Forms a slice from a pointer and a length.
879///
880/// Equivalent to [`slice::from_raw_parts`].
881///
882/// # Safety
883///
884/// [`slice::from_raw_parts`] must be safe to call with `data` and `len`.
885/// Additionally, all elements in the resulting slice must be unique.
886pub const unsafe fn from_raw_parts<'a, T: EntityEquivalent>(
887    data: *const T,
888    len: usize,
889) -> &'a UniqueEntityEquivalentSlice<T> {
890    // SAFETY: The safety contract is upheld by the caller.
891    unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice::from_raw_parts(data, len)) }
892}
893
894/// Performs the same functionality as [`from_raw_parts`], except that a mutable slice is returned.
895///
896/// Equivalent to [`slice::from_raw_parts_mut`].
897///
898/// # Safety
899///
900/// [`slice::from_raw_parts_mut`] must be safe to call with `data` and `len`.
901/// Additionally, all elements in the resulting slice must be unique.
902pub const unsafe fn from_raw_parts_mut<'a, T: EntityEquivalent>(
903    data: *mut T,
904    len: usize,
905) -> &'a mut UniqueEntityEquivalentSlice<T> {
906    // SAFETY: The safety contract is upheld by the caller.
907    unsafe {
908        UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice::from_raw_parts_mut(data, len))
909    }
910}
911
912/// Casts a slice of entity slices to a slice of [`UniqueEntityEquivalentSlice`]s.
913///
914/// # Safety
915///
916/// All elements in each of the casted slices must be unique.
917pub unsafe fn cast_slice_of_unique_entity_slice<'a, 'b, T: EntityEquivalent + 'a>(
918    slice: &'b [&'a [T]],
919) -> &'b [&'a UniqueEntityEquivalentSlice<T>] {
920    // SAFETY: All elements in the original iterator are unique slices.
921    unsafe { &*(ptr::from_ref(slice) as *const [&UniqueEntityEquivalentSlice<T>]) }
922}
923
924/// Casts a mutable slice of entity slices to a slice of [`UniqueEntityEquivalentSlice`]s.
925///
926/// # Safety
927///
928/// All elements in each of the casted slices must be unique.
929pub unsafe fn cast_slice_of_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent + 'a>(
930    slice: &'b mut [&'a [T]],
931) -> &'b mut [&'a UniqueEntityEquivalentSlice<T>] {
932    // SAFETY: All elements in the original iterator are unique slices.
933    unsafe { &mut *(ptr::from_mut(slice) as *mut [&UniqueEntityEquivalentSlice<T>]) }
934}
935
936/// Casts a mutable slice of mutable entity slices to a slice of mutable [`UniqueEntityEquivalentSlice`]s.
937///
938/// # Safety
939///
940/// All elements in each of the casted slices must be unique.
941pub unsafe fn cast_slice_of_mut_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent + 'a>(
942    slice: &'b mut [&'a mut [T]],
943) -> &'b mut [&'a mut UniqueEntityEquivalentSlice<T>] {
944    // SAFETY: All elements in the original iterator are unique slices.
945    unsafe { &mut *(ptr::from_mut(slice) as *mut [&mut UniqueEntityEquivalentSlice<T>]) }
946}
947
948impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentSlice<T> {
949    type Item = &'a T;
950
951    type IntoIter = Iter<'a, T>;
952
953    fn into_iter(self) -> Self::IntoIter {
954        self.iter()
955    }
956}
957
958impl<'a, T: EntityEquivalent> IntoIterator for &'a Box<UniqueEntityEquivalentSlice<T>> {
959    type Item = &'a T;
960
961    type IntoIter = Iter<'a, T>;
962
963    fn into_iter(self) -> Self::IntoIter {
964        self.iter()
965    }
966}
967
968impl<T: EntityEquivalent> IntoIterator for Box<UniqueEntityEquivalentSlice<T>> {
969    type Item = T;
970
971    type IntoIter = unique_vec::IntoIter<T>;
972
973    fn into_iter(self) -> Self::IntoIter {
974        self.into_vec().into_iter()
975    }
976}
977
978impl<T: EntityEquivalent> Deref for UniqueEntityEquivalentSlice<T> {
979    type Target = [T];
980
981    fn deref(&self) -> &Self::Target {
982        &self.0
983    }
984}
985
986impl<T: EntityEquivalent> AsRef<[T]> for UniqueEntityEquivalentSlice<T> {
987    fn as_ref(&self) -> &[T] {
988        self
989    }
990}
991
992impl<T: EntityEquivalent> AsRef<Self> for UniqueEntityEquivalentSlice<T> {
993    fn as_ref(&self) -> &Self {
994        self
995    }
996}
997
998impl<T: EntityEquivalent> AsMut<Self> for UniqueEntityEquivalentSlice<T> {
999    fn as_mut(&mut self) -> &mut Self {
1000        self
1001    }
1002}
1003
1004impl<T: EntityEquivalent> Borrow<[T]> for UniqueEntityEquivalentSlice<T> {
1005    fn borrow(&self) -> &[T] {
1006        self
1007    }
1008}
1009
1010impl<T: EntityEquivalent + Clone> Clone for Box<UniqueEntityEquivalentSlice<T>> {
1011    fn clone(&self) -> Self {
1012        self.to_vec().into_boxed_slice()
1013    }
1014}
1015
1016impl<T: EntityEquivalent> Default for &UniqueEntityEquivalentSlice<T> {
1017    fn default() -> Self {
1018        // SAFETY: All elements in the original slice are unique.
1019        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(Default::default()) }
1020    }
1021}
1022
1023impl<T: EntityEquivalent> Default for &mut UniqueEntityEquivalentSlice<T> {
1024    fn default() -> Self {
1025        // SAFETY: All elements in the original slice are unique.
1026        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(Default::default()) }
1027    }
1028}
1029
1030impl<T: EntityEquivalent> Default for Box<UniqueEntityEquivalentSlice<T>> {
1031    fn default() -> Self {
1032        // SAFETY: All elements in the original slice are unique.
1033        unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(Default::default()) }
1034    }
1035}
1036
1037impl<T: EntityEquivalent + Clone> From<&UniqueEntityEquivalentSlice<T>>
1038    for Box<UniqueEntityEquivalentSlice<T>>
1039{
1040    fn from(value: &UniqueEntityEquivalentSlice<T>) -> Self {
1041        // SAFETY: All elements in the original slice are unique.
1042        unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(value.0.into()) }
1043    }
1044}
1045
1046impl<T: EntityEquivalent + Clone> From<&UniqueEntityEquivalentSlice<T>>
1047    for Arc<UniqueEntityEquivalentSlice<T>>
1048{
1049    fn from(value: &UniqueEntityEquivalentSlice<T>) -> Self {
1050        // SAFETY: All elements in the original slice are unique.
1051        unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(value.0.into()) }
1052    }
1053}
1054
1055impl<T: EntityEquivalent + Clone> From<&UniqueEntityEquivalentSlice<T>>
1056    for Rc<UniqueEntityEquivalentSlice<T>>
1057{
1058    fn from(value: &UniqueEntityEquivalentSlice<T>) -> Self {
1059        // SAFETY: All elements in the original slice are unique.
1060        unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(value.0.into()) }
1061    }
1062}
1063
1064impl<'a, T: EntityEquivalent + Clone> From<&'a UniqueEntityEquivalentSlice<T>>
1065    for Cow<'a, UniqueEntityEquivalentSlice<T>>
1066{
1067    fn from(value: &'a UniqueEntityEquivalentSlice<T>) -> Self {
1068        Cow::Borrowed(value)
1069    }
1070}
1071
1072impl<T: EntityEquivalent + Clone, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
1073    for Box<UniqueEntityEquivalentSlice<T>>
1074{
1075    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
1076        // SAFETY: All elements in the original slice are unique.
1077        unsafe {
1078            UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(Box::new(value.into_inner()))
1079        }
1080    }
1081}
1082
1083impl<'a, T: EntityEquivalent + Clone> From<Cow<'a, UniqueEntityEquivalentSlice<T>>>
1084    for Box<UniqueEntityEquivalentSlice<T>>
1085{
1086    fn from(value: Cow<'a, UniqueEntityEquivalentSlice<T>>) -> Self {
1087        match value {
1088            Cow::Borrowed(slice) => Box::from(slice),
1089            Cow::Owned(slice) => Box::from(slice),
1090        }
1091    }
1092}
1093
1094impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>>
1095    for Box<UniqueEntityEquivalentSlice<T>>
1096{
1097    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
1098        value.into_boxed_slice()
1099    }
1100}
1101
1102impl<T: EntityEquivalent> FromIterator<T> for Box<UniqueEntityEquivalentSlice<T>> {
1103    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
1104        iter.into_iter()
1105            .collect::<UniqueEntityEquivalentVec<T>>()
1106            .into_boxed_slice()
1107    }
1108}
1109
1110impl<T: EntityEquivalent> FromEntitySetIterator<T> for Box<UniqueEntityEquivalentSlice<T>> {
1111    fn from_entity_set_iter<I: EntitySet<Item = T>>(iter: I) -> Self {
1112        iter.into_iter()
1113            .collect_set::<UniqueEntityEquivalentVec<T>>()
1114            .into_boxed_slice()
1115    }
1116}
1117
1118impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
1119    PartialEq<UniqueEntityEquivalentVec<U>> for &UniqueEntityEquivalentSlice<T>
1120{
1121    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
1122        self.0.eq(other.as_vec())
1123    }
1124}
1125
1126impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
1127    PartialEq<UniqueEntityEquivalentVec<U>> for &mut UniqueEntityEquivalentSlice<T>
1128{
1129    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
1130        self.0.eq(other.as_vec())
1131    }
1132}
1133
1134impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
1135    PartialEq<UniqueEntityEquivalentVec<U>> for UniqueEntityEquivalentSlice<T>
1136{
1137    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
1138        self.0.eq(other.as_vec())
1139    }
1140}
1141
1142impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
1143    PartialEq<&UniqueEntityEquivalentSlice<U>> for [T; N]
1144{
1145    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
1146        self.eq(&other.0)
1147    }
1148}
1149
1150impl<T: PartialEq<U> + Clone, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice<U>>
1151    for Cow<'_, [T]>
1152{
1153    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
1154        self.eq(&&other.0)
1155    }
1156}
1157
1158impl<T: EntityEquivalent + PartialEq<U> + Clone, U: EntityEquivalent>
1159    PartialEq<&UniqueEntityEquivalentSlice<U>> for Cow<'_, UniqueEntityEquivalentSlice<T>>
1160{
1161    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
1162        self.0.eq(&other.0)
1163    }
1164}
1165
1166impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice<U>> for Vec<T> {
1167    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
1168        self.eq(&other.0)
1169    }
1170}
1171
1172impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice<U>>
1173    for VecDeque<T>
1174{
1175    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
1176        self.eq(&&other.0)
1177    }
1178}
1179
1180impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize>
1181    PartialEq<&mut UniqueEntityEquivalentSlice<U>> for [T; N]
1182{
1183    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
1184        self.eq(&other.0)
1185    }
1186}
1187
1188impl<T: PartialEq<U> + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice<U>>
1189    for Cow<'_, [T]>
1190{
1191    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
1192        self.eq(&&**other)
1193    }
1194}
1195
1196impl<T: EntityEquivalent + PartialEq<U> + Clone, U: EntityEquivalent>
1197    PartialEq<&mut UniqueEntityEquivalentSlice<U>> for Cow<'_, UniqueEntityEquivalentSlice<T>>
1198{
1199    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
1200        self.0.eq(&other.0)
1201    }
1202}
1203
1204impl<T: EntityEquivalent + PartialEq<U> + Clone, U: EntityEquivalent>
1205    PartialEq<UniqueEntityEquivalentVec<U>> for Cow<'_, UniqueEntityEquivalentSlice<T>>
1206{
1207    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
1208        self.0.eq(other.as_vec())
1209    }
1210}
1211
1212impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice<U>>
1213    for Vec<T>
1214{
1215    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
1216        self.eq(&other.0)
1217    }
1218}
1219
1220impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice<U>>
1221    for VecDeque<T>
1222{
1223    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
1224        self.eq(&&other.0)
1225    }
1226}
1227
1228impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
1229    PartialEq<UniqueEntityEquivalentSlice<U>> for [T]
1230{
1231    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
1232        self.eq(&other.0)
1233    }
1234}
1235
1236impl<T: PartialEq<U>, U: EntityEquivalent, const N: usize> PartialEq<UniqueEntityEquivalentSlice<U>>
1237    for [T; N]
1238{
1239    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
1240        self.eq(&other.0)
1241    }
1242}
1243
1244impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
1245    PartialEq<UniqueEntityEquivalentSlice<U>> for Vec<T>
1246{
1247    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
1248        self.eq(&other.0)
1249    }
1250}
1251
1252impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<[U; N]>
1253    for &UniqueEntityEquivalentSlice<T>
1254{
1255    fn eq(&self, other: &[U; N]) -> bool {
1256        self.0.eq(other)
1257    }
1258}
1259
1260impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<[U; N]>
1261    for &mut UniqueEntityEquivalentSlice<T>
1262{
1263    fn eq(&self, other: &[U; N]) -> bool {
1264        self.0.eq(other)
1265    }
1266}
1267
1268impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<[U; N]>
1269    for UniqueEntityEquivalentSlice<T>
1270{
1271    fn eq(&self, other: &[U; N]) -> bool {
1272        self.0.eq(other)
1273    }
1274}
1275
1276impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
1277    PartialEq<UniqueEntityEquivalentArray<U, N>> for &UniqueEntityEquivalentSlice<T>
1278{
1279    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
1280        self.0.eq(&other.0)
1281    }
1282}
1283
1284impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
1285    PartialEq<UniqueEntityEquivalentArray<U, N>> for &mut UniqueEntityEquivalentSlice<T>
1286{
1287    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
1288        self.0.eq(&other.0)
1289    }
1290}
1291
1292impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
1293    PartialEq<UniqueEntityEquivalentArray<U, N>> for UniqueEntityEquivalentSlice<T>
1294{
1295    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
1296        self.0.eq(&other.0)
1297    }
1298}
1299
1300impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<Vec<U>> for &UniqueEntityEquivalentSlice<T> {
1301    fn eq(&self, other: &Vec<U>) -> bool {
1302        self.0.eq(other)
1303    }
1304}
1305
1306impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<Vec<U>>
1307    for &mut UniqueEntityEquivalentSlice<T>
1308{
1309    fn eq(&self, other: &Vec<U>) -> bool {
1310        self.0.eq(other)
1311    }
1312}
1313
1314impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<Vec<U>> for UniqueEntityEquivalentSlice<T> {
1315    fn eq(&self, other: &Vec<U>) -> bool {
1316        self.0.eq(other)
1317    }
1318}
1319
1320impl<T: EntityEquivalent + Clone> ToOwned for UniqueEntityEquivalentSlice<T> {
1321    type Owned = UniqueEntityEquivalentVec<T>;
1322
1323    fn to_owned(&self) -> Self::Owned {
1324        // SAFETY: All elements in the original slice are unique.
1325        unsafe { UniqueEntityEquivalentVec::from_vec_unchecked(self.0.to_owned()) }
1326    }
1327}
1328
1329impl<'a, T: EntityEquivalent + Copy, const N: usize> TryFrom<&'a UniqueEntityEquivalentSlice<T>>
1330    for &'a UniqueEntityEquivalentArray<T, N>
1331{
1332    type Error = TryFromSliceError;
1333
1334    fn try_from(value: &'a UniqueEntityEquivalentSlice<T>) -> Result<Self, Self::Error> {
1335        <&[T; N]>::try_from(&value.0).map(|array|
1336                // SAFETY: All elements in the original slice are unique.
1337                unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(array) })
1338    }
1339}
1340
1341impl<T: EntityEquivalent + Copy, const N: usize> TryFrom<&UniqueEntityEquivalentSlice<T>>
1342    for UniqueEntityEquivalentArray<T, N>
1343{
1344    type Error = TryFromSliceError;
1345
1346    fn try_from(value: &UniqueEntityEquivalentSlice<T>) -> Result<Self, Self::Error> {
1347        <&Self>::try_from(value).copied()
1348    }
1349}
1350
1351impl<T: EntityEquivalent + Copy, const N: usize> TryFrom<&mut UniqueEntityEquivalentSlice<T>>
1352    for UniqueEntityEquivalentArray<T, N>
1353{
1354    type Error = TryFromSliceError;
1355
1356    fn try_from(value: &mut UniqueEntityEquivalentSlice<T>) -> Result<Self, Self::Error> {
1357        <Self>::try_from(&*value)
1358    }
1359}
1360
1361impl<T: EntityEquivalent> Index<(Bound<usize>, Bound<usize>)> for UniqueEntityEquivalentSlice<T> {
1362    type Output = Self;
1363    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self {
1364        // SAFETY: All elements in the original slice are unique.
1365        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1366    }
1367}
1368
1369impl<T: EntityEquivalent> Index<Range<usize>> for UniqueEntityEquivalentSlice<T> {
1370    type Output = Self;
1371    fn index(&self, key: Range<usize>) -> &Self {
1372        // SAFETY: All elements in the original slice are unique.
1373        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1374    }
1375}
1376
1377impl<T: EntityEquivalent> Index<RangeFrom<usize>> for UniqueEntityEquivalentSlice<T> {
1378    type Output = Self;
1379    fn index(&self, key: RangeFrom<usize>) -> &Self {
1380        // SAFETY: All elements in the original slice are unique.
1381        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1382    }
1383}
1384
1385impl<T: EntityEquivalent> Index<RangeFull> for UniqueEntityEquivalentSlice<T> {
1386    type Output = Self;
1387    fn index(&self, key: RangeFull) -> &Self {
1388        // SAFETY: All elements in the original slice are unique.
1389        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1390    }
1391}
1392
1393impl<T: EntityEquivalent> Index<RangeInclusive<usize>> for UniqueEntityEquivalentSlice<T> {
1394    type Output = UniqueEntityEquivalentSlice<T>;
1395    fn index(&self, key: RangeInclusive<usize>) -> &Self {
1396        // SAFETY: All elements in the original slice are unique.
1397        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1398    }
1399}
1400
1401impl<T: EntityEquivalent> Index<RangeTo<usize>> for UniqueEntityEquivalentSlice<T> {
1402    type Output = UniqueEntityEquivalentSlice<T>;
1403    fn index(&self, key: RangeTo<usize>) -> &Self {
1404        // SAFETY: All elements in the original slice are unique.
1405        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1406    }
1407}
1408
1409impl<T: EntityEquivalent> Index<RangeToInclusive<usize>> for UniqueEntityEquivalentSlice<T> {
1410    type Output = UniqueEntityEquivalentSlice<T>;
1411    fn index(&self, key: RangeToInclusive<usize>) -> &Self {
1412        // SAFETY: All elements in the original slice are unique.
1413        unsafe { Self::from_slice_unchecked(self.0.index(key)) }
1414    }
1415}
1416
1417impl<T: EntityEquivalent> Index<usize> for UniqueEntityEquivalentSlice<T> {
1418    type Output = T;
1419
1420    fn index(&self, index: usize) -> &T {
1421        &self.0[index]
1422    }
1423}
1424
1425impl<T: EntityEquivalent> IndexMut<(Bound<usize>, Bound<usize>)>
1426    for UniqueEntityEquivalentSlice<T>
1427{
1428    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self {
1429        // SAFETY: All elements in the original slice are unique.
1430        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1431    }
1432}
1433
1434impl<T: EntityEquivalent> IndexMut<Range<usize>> for UniqueEntityEquivalentSlice<T> {
1435    fn index_mut(&mut self, key: Range<usize>) -> &mut Self {
1436        // SAFETY: All elements in the original slice are unique.
1437        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1438    }
1439}
1440
1441impl<T: EntityEquivalent> IndexMut<RangeFrom<usize>> for UniqueEntityEquivalentSlice<T> {
1442    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self {
1443        // SAFETY: All elements in the original slice are unique.
1444        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1445    }
1446}
1447
1448impl<T: EntityEquivalent> IndexMut<RangeFull> for UniqueEntityEquivalentSlice<T> {
1449    fn index_mut(&mut self, key: RangeFull) -> &mut Self {
1450        // SAFETY: All elements in the original slice are unique.
1451        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1452    }
1453}
1454
1455impl<T: EntityEquivalent> IndexMut<RangeInclusive<usize>> for UniqueEntityEquivalentSlice<T> {
1456    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self {
1457        // SAFETY: All elements in the original slice are unique.
1458        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1459    }
1460}
1461
1462impl<T: EntityEquivalent> IndexMut<RangeTo<usize>> for UniqueEntityEquivalentSlice<T> {
1463    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self {
1464        // SAFETY: All elements in the original slice are unique.
1465        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1466    }
1467}
1468
1469impl<T: EntityEquivalent> IndexMut<RangeToInclusive<usize>> for UniqueEntityEquivalentSlice<T> {
1470    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self {
1471        // SAFETY: All elements in the original slice are unique.
1472        unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) }
1473    }
1474}
1475
1476/// Immutable slice iterator.
1477///
1478/// This struct is created by [`iter`] method on [`UniqueEntityEquivalentSlice`] and
1479/// the [`IntoIterator`] impls on it and [`UniqueEntityEquivalentVec`].
1480///
1481/// [`iter`]: `UniqueEntityEquivalentSlice::iter`
1482pub type Iter<'a, T> = UniqueEntityIter<slice::Iter<'a, T>>;
1483
1484impl<'a, T: EntityEquivalent> UniqueEntityIter<slice::Iter<'a, T>> {
1485    /// Views the underlying data as a subslice of the original data.
1486    ///
1487    /// Equivalent to [`slice::Iter::as_slice`].
1488    pub fn as_slice(&self) -> &'a UniqueEntityEquivalentSlice<T> {
1489        // SAFETY: All elements in the original slice are unique.
1490        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
1491    }
1492}
1493
1494/// Mutable slice iterator.
1495pub type IterMut<'a, T> = UniqueEntityIter<slice::IterMut<'a, T>>;
1496
1497impl<'a, T: EntityEquivalent> UniqueEntityIter<slice::IterMut<'a, T>> {
1498    /// Views the underlying data as a mutable subslice of the original data.
1499    ///
1500    /// Equivalent to [`slice::IterMut::into_slice`].
1501    pub fn into_slice(self) -> &'a mut UniqueEntityEquivalentSlice<T> {
1502        // SAFETY: All elements in the original slice are unique.
1503        unsafe {
1504            UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.into_inner().into_slice())
1505        }
1506    }
1507
1508    /// Views the underlying data as a subslice of the original data.
1509    ///
1510    /// Equivalent to [`slice::IterMut::as_slice`].
1511    pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
1512        // SAFETY: All elements in the original slice are unique.
1513        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
1514    }
1515}
1516
1517/// An iterator that yields `&UniqueEntityEquivalentSlice`. Note that an entity may appear
1518/// in multiple slices, depending on the wrapped iterator.
1519#[derive(Debug)]
1520pub struct UniqueEntityEquivalentSliceIter<
1521    'a,
1522    T: EntityEquivalent + 'a,
1523    I: Iterator<Item = &'a [T]>,
1524> {
1525    pub(crate) iter: I,
1526}
1527
1528impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]>>
1529    UniqueEntityEquivalentSliceIter<'a, T, I>
1530{
1531    /// Constructs a [`UniqueEntityEquivalentSliceIter`] from a slice iterator unsafely.
1532    ///
1533    /// # Safety
1534    ///
1535    /// All elements in each of the slices must be unique.
1536    pub unsafe fn from_slice_iterator_unchecked(iter: I) -> Self {
1537        Self { iter }
1538    }
1539
1540    /// Returns the inner `I`.
1541    pub fn into_inner(self) -> I {
1542        self.iter
1543    }
1544
1545    /// Returns a reference to the inner `I`.
1546    pub fn as_inner(&self) -> &I {
1547        &self.iter
1548    }
1549
1550    /// Returns a mutable reference to the inner `I`.
1551    ///
1552    /// # Safety
1553    ///
1554    /// `self` must always contain an iterator that yields unique elements,
1555    /// even while this reference is live.
1556    pub unsafe fn as_mut_inner(&mut self) -> &mut I {
1557        &mut self.iter
1558    }
1559}
1560
1561impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]>> Iterator
1562    for UniqueEntityEquivalentSliceIter<'a, T, I>
1563{
1564    type Item = &'a UniqueEntityEquivalentSlice<T>;
1565
1566    fn next(&mut self) -> Option<Self::Item> {
1567        self.iter.next().map(|slice|
1568        // SAFETY: All elements in the original iterator are unique slices.
1569        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice) })
1570    }
1571
1572    fn size_hint(&self) -> (usize, Option<usize>) {
1573        self.iter.size_hint()
1574    }
1575}
1576
1577impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator<Item = &'a [T]>> ExactSizeIterator
1578    for UniqueEntityEquivalentSliceIter<'a, T, I>
1579{
1580}
1581
1582impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator<Item = &'a [T]>> DoubleEndedIterator
1583    for UniqueEntityEquivalentSliceIter<'a, T, I>
1584{
1585    fn next_back(&mut self) -> Option<Self::Item> {
1586        self.iter.next_back().map(|slice|
1587            // SAFETY: All elements in the original iterator are unique slices.
1588            unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice) })
1589    }
1590}
1591
1592impl<'a, T: EntityEquivalent + 'a, I: FusedIterator<Item = &'a [T]>> FusedIterator
1593    for UniqueEntityEquivalentSliceIter<'a, T, I>
1594{
1595}
1596
1597impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]> + AsRef<[&'a [T]]>>
1598    AsRef<[&'a UniqueEntityEquivalentSlice<T>]> for UniqueEntityEquivalentSliceIter<'a, T, I>
1599{
1600    fn as_ref(&self) -> &[&'a UniqueEntityEquivalentSlice<T>] {
1601        // SAFETY:
1602        unsafe { cast_slice_of_unique_entity_slice(self.iter.as_ref()) }
1603    }
1604}
1605
1606/// An iterator over overlapping subslices of length `size`.
1607///
1608/// This struct is created by [`UniqueEntityEquivalentSlice::windows`].
1609pub type Windows<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Windows<'a, T>>;
1610
1611/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
1612/// time), starting at the beginning of the slice.
1613///
1614/// This struct is created by [`UniqueEntityEquivalentSlice::chunks`].
1615pub type Chunks<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Chunks<'a, T>>;
1616
1617/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
1618/// time), starting at the beginning of the slice.
1619///
1620/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_exact`].
1621pub type ChunksExact<'a, T = Entity> =
1622    UniqueEntityEquivalentSliceIter<'a, T, slice::ChunksExact<'a, T>>;
1623
1624impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::ChunksExact<'a, T>> {
1625    /// Returns the remainder of the original slice that is not going to be
1626    /// returned by the iterator.
1627    ///
1628    /// Equivalent to [`slice::ChunksExact::remainder`].
1629    pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice<T> {
1630        // SAFETY: All elements in the original iterator are unique slices.
1631        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) }
1632    }
1633}
1634
1635/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
1636/// time), starting at the end of the slice.
1637///
1638/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks`].
1639pub type RChunks<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::RChunks<'a, T>>;
1640
1641/// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a
1642/// time), starting at the end of the slice.
1643///
1644/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_exact`].
1645pub type RChunksExact<'a, T = Entity> =
1646    UniqueEntityEquivalentSliceIter<'a, T, slice::RChunksExact<'a, T>>;
1647
1648impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::RChunksExact<'a, T>> {
1649    /// Returns the remainder of the original slice that is not going to be
1650    /// returned by the iterator.
1651    ///
1652    /// Equivalent to [`slice::RChunksExact::remainder`].
1653    pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice<T> {
1654        // SAFETY: All elements in the original iterator are unique slices.
1655        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) }
1656    }
1657}
1658
1659/// An iterator over slice in (non-overlapping) chunks separated by a predicate.
1660///
1661/// This struct is created by [`UniqueEntityEquivalentSlice::chunk_by`].
1662pub type ChunkBy<'a, P, T = Entity> =
1663    UniqueEntityEquivalentSliceIter<'a, T, slice::ChunkBy<'a, T, P>>;
1664
1665/// An iterator over subslices separated by elements that match a predicate
1666/// function.
1667///
1668/// This struct is created by [`UniqueEntityEquivalentSlice::split`].
1669pub type Split<'a, P, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Split<'a, T, P>>;
1670
1671/// An iterator over subslices separated by elements that match a predicate
1672/// function.
1673///
1674/// This struct is created by [`UniqueEntityEquivalentSlice::split_inclusive`].
1675pub type SplitInclusive<'a, P, T = Entity> =
1676    UniqueEntityEquivalentSliceIter<'a, T, slice::SplitInclusive<'a, T, P>>;
1677
1678/// An iterator over subslices separated by elements that match a predicate
1679/// function, starting from the end of the slice.
1680///
1681/// This struct is created by [`UniqueEntityEquivalentSlice::rsplit`].
1682pub type RSplit<'a, P, T = Entity> =
1683    UniqueEntityEquivalentSliceIter<'a, T, slice::RSplit<'a, T, P>>;
1684
1685/// An iterator over subslices separated by elements that match a predicate
1686/// function, limited to a given number of splits.
1687///
1688/// This struct is created by [`UniqueEntityEquivalentSlice::splitn`].
1689pub type SplitN<'a, P, T = Entity> =
1690    UniqueEntityEquivalentSliceIter<'a, T, slice::SplitN<'a, T, P>>;
1691
1692/// An iterator over subslices separated by elements that match a
1693/// predicate function, limited to a given number of splits, starting
1694/// from the end of the slice.
1695///
1696/// This struct is created by [`UniqueEntityEquivalentSlice::rsplitn`].
1697pub type RSplitN<'a, P, T = Entity> =
1698    UniqueEntityEquivalentSliceIter<'a, T, slice::RSplitN<'a, T, P>>;
1699
1700/// An iterator that yields `&mut UniqueEntityEquivalentSlice`. Note that an entity may appear
1701/// in multiple slices, depending on the wrapped iterator.
1702#[derive(Debug)]
1703pub struct UniqueEntityEquivalentSliceIterMut<
1704    'a,
1705    T: EntityEquivalent + 'a,
1706    I: Iterator<Item = &'a mut [T]>,
1707> {
1708    pub(crate) iter: I,
1709}
1710
1711impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]>>
1712    UniqueEntityEquivalentSliceIterMut<'a, T, I>
1713{
1714    /// Constructs a [`UniqueEntityEquivalentSliceIterMut`] from a mutable slice iterator unsafely.
1715    ///
1716    /// # Safety
1717    ///
1718    /// All elements in each of the slices must be unique.
1719    pub unsafe fn from_mut_slice_iterator_unchecked(iter: I) -> Self {
1720        Self { iter }
1721    }
1722
1723    /// Returns the inner `I`.
1724    pub fn into_inner(self) -> I {
1725        self.iter
1726    }
1727
1728    /// Returns a reference to the inner `I`.
1729    pub fn as_inner(&self) -> &I {
1730        &self.iter
1731    }
1732
1733    /// Returns a mutable reference to the inner `I`.
1734    ///
1735    /// # Safety
1736    ///
1737    /// `self` must always contain an iterator that yields unique elements,
1738    /// even while this reference is live.
1739    pub unsafe fn as_mut_inner(&mut self) -> &mut I {
1740        &mut self.iter
1741    }
1742}
1743
1744impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]>> Iterator
1745    for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1746{
1747    type Item = &'a mut UniqueEntityEquivalentSlice<T>;
1748
1749    fn next(&mut self) -> Option<Self::Item> {
1750        self.iter.next().map(|slice|
1751            // SAFETY: All elements in the original iterator are unique slices.
1752            unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice) })
1753    }
1754
1755    fn size_hint(&self) -> (usize, Option<usize>) {
1756        self.iter.size_hint()
1757    }
1758}
1759
1760impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator<Item = &'a mut [T]>> ExactSizeIterator
1761    for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1762{
1763}
1764
1765impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator<Item = &'a mut [T]>> DoubleEndedIterator
1766    for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1767{
1768    fn next_back(&mut self) -> Option<Self::Item> {
1769        self.iter.next_back().map(|slice|
1770            // SAFETY: All elements in the original iterator are unique slices.
1771            unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice) })
1772    }
1773}
1774
1775impl<'a, T: EntityEquivalent + 'a, I: FusedIterator<Item = &'a mut [T]>> FusedIterator
1776    for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1777{
1778}
1779
1780impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]> + AsRef<[&'a [T]]>>
1781    AsRef<[&'a UniqueEntityEquivalentSlice<T>]> for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1782{
1783    fn as_ref(&self) -> &[&'a UniqueEntityEquivalentSlice<T>] {
1784        // SAFETY: All elements in the original iterator are unique slices.
1785        unsafe { cast_slice_of_unique_entity_slice(self.iter.as_ref()) }
1786    }
1787}
1788
1789impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]> + AsMut<[&'a mut [T]]>>
1790    AsMut<[&'a mut UniqueEntityEquivalentSlice<T>]>
1791    for UniqueEntityEquivalentSliceIterMut<'a, T, I>
1792{
1793    fn as_mut(&mut self) -> &mut [&'a mut UniqueEntityEquivalentSlice<T>] {
1794        // SAFETY: All elements in the original iterator are unique slices.
1795        unsafe { cast_slice_of_mut_unique_entity_slice_mut(self.iter.as_mut()) }
1796    }
1797}
1798
1799/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
1800/// elements at a time), starting at the beginning of the slice.
1801///
1802/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_mut`].
1803pub type ChunksMut<'a, T = Entity> =
1804    UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksMut<'a, T>>;
1805
1806/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
1807/// elements at a time), starting at the beginning of the slice.
1808///
1809/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_exact_mut`].
1810pub type ChunksExactMut<'a, T = Entity> =
1811    UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksExactMut<'a, T>>;
1812
1813impl<'a, T: EntityEquivalent>
1814    UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksExactMut<'a, T>>
1815{
1816    /// Returns the remainder of the original slice that is not going to be
1817    /// returned by the iterator.
1818    ///
1819    /// Equivalent to [`slice::ChunksExactMut::into_remainder`].
1820    pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice<T> {
1821        // SAFETY: All elements in the original iterator are unique slices.
1822        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) }
1823    }
1824}
1825
1826/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
1827/// elements at a time), starting at the end of the slice.
1828///
1829/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_mut`].
1830pub type RChunksMut<'a, T = Entity> =
1831    UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksMut<'a, T>>;
1832
1833/// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size`
1834/// elements at a time), starting at the end of the slice.
1835///
1836/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_exact_mut`].
1837pub type RChunksExactMut<'a, T = Entity> =
1838    UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksExactMut<'a, T>>;
1839
1840impl<'a, T: EntityEquivalent>
1841    UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksExactMut<'a, T>>
1842{
1843    /// Returns the remainder of the original slice that is not going to be
1844    /// returned by the iterator.
1845    ///
1846    /// Equivalent to [`slice::RChunksExactMut::into_remainder`].
1847    pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice<T> {
1848        // SAFETY: All elements in the original iterator are unique slices.
1849        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) }
1850    }
1851}
1852
1853/// An iterator over slice in (non-overlapping) mutable chunks separated
1854/// by a predicate.
1855///
1856/// This struct is created by [`UniqueEntityEquivalentSlice::chunk_by_mut`].
1857pub type ChunkByMut<'a, P, T = Entity> =
1858    UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunkByMut<'a, T, P>>;
1859
1860/// An iterator over the mutable subslices of the vector which are separated
1861/// by elements that match `pred`.
1862///
1863/// This struct is created by [`UniqueEntityEquivalentSlice::split_mut`].
1864pub type SplitMut<'a, P, T = Entity> =
1865    UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitMut<'a, T, P>>;
1866
1867/// An iterator over the mutable subslices of the vector which are separated
1868/// by elements that match `pred`. Unlike `SplitMut`, it contains the matched
1869/// parts in the ends of the subslices.
1870///
1871/// This struct is created by [`UniqueEntityEquivalentSlice::split_inclusive_mut`].
1872pub type SplitInclusiveMut<'a, P, T = Entity> =
1873    UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitInclusiveMut<'a, T, P>>;
1874
1875/// An iterator over the subslices of the vector which are separated
1876/// by elements that match `pred`, starting from the end of the slice.
1877///
1878/// This struct is created by [`UniqueEntityEquivalentSlice::rsplit_mut`].
1879pub type RSplitMut<'a, P, T = Entity> =
1880    UniqueEntityEquivalentSliceIterMut<'a, T, slice::RSplitMut<'a, T, P>>;
1881
1882/// An iterator over subslices separated by elements that match a predicate
1883/// function, limited to a given number of splits.
1884///
1885/// This struct is created by [`UniqueEntityEquivalentSlice::splitn_mut`].
1886pub type SplitNMut<'a, P, T = Entity> =
1887    UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitNMut<'a, T, P>>;
1888
1889/// An iterator over subslices separated by elements that match a
1890/// predicate function, limited to a given number of splits, starting
1891/// from the end of the slice.
1892///
1893/// This struct is created by [`UniqueEntityEquivalentSlice::rsplitn_mut`].
1894pub type RSplitNMut<'a, P, T = Entity> =
1895    UniqueEntityEquivalentSliceIterMut<'a, T, slice::RSplitNMut<'a, T, P>>;