Skip to main content

bevy_ecs/entity/
unique_vec.rs

1//! A wrapper around entity [`Vec`]s with a uniqueness invariant.
2
3use core::{
4    borrow::{Borrow, BorrowMut},
5    mem::MaybeUninit,
6    ops::{
7        Bound, Deref, DerefMut, Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull,
8        RangeInclusive, RangeTo, RangeToInclusive,
9    },
10    ptr,
11};
12
13use alloc::{
14    borrow::{Cow, ToOwned},
15    boxed::Box,
16    collections::{BTreeSet, BinaryHeap, TryReserveError, VecDeque},
17    rc::Rc,
18    vec::{self, Vec},
19};
20
21use bevy_platform::sync::Arc;
22
23use super::{
24    unique_slice::{self, UniqueEntityEquivalentSlice},
25    Entity, EntityEquivalent, EntitySet, FromEntitySetIterator, UniqueEntityEquivalentArray,
26    UniqueEntityIter,
27};
28
29/// A `Vec` that contains only unique entities.
30///
31/// "Unique" means that `x != y` holds for any 2 entities in this collection.
32/// This is always true when less than 2 entities are present.
33///
34/// This type is best obtained by its `FromEntitySetIterator` impl, via either
35/// `EntityIterator::collect_set` or `UniqueEntityEquivalentVec::from_entity_iter`.
36///
37/// While this type can be constructed via `Iterator::collect`, doing so is inefficient,
38/// and not recommended.
39///
40/// When `T` is [`Entity`], use the [`UniqueEntityVec`] alias.
41#[repr(transparent)]
42#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
43pub struct UniqueEntityEquivalentVec<T: EntityEquivalent>(Vec<T>);
44
45/// A `Vec` that contains only unique [`Entity`].
46///
47/// This is the default case of a [`UniqueEntityEquivalentVec`].
48pub type UniqueEntityVec = UniqueEntityEquivalentVec<Entity>;
49
50impl<T: EntityEquivalent> UniqueEntityEquivalentVec<T> {
51    /// Constructs a new, empty `UniqueEntityEquivalentVec<T>`.
52    ///
53    /// Equivalent to [`Vec::new`].
54    pub const fn new() -> Self {
55        // SAFETY: Any empty Vec cannot contain duplicates.
56        unsafe { Self::from_vec_unchecked(Vec::new()) }
57    }
58
59    /// Constructs a new, empty `UniqueEntityEquivalentVec<T>` with at least the specified capacity.
60    ///
61    /// Equivalent to [`Vec::with_capacity`].
62    pub fn with_capacity(capacity: usize) -> Self {
63        // SAFETY: Any empty Vec cannot contain duplicates.
64        unsafe { Self::from_vec_unchecked(Vec::with_capacity(capacity)) }
65    }
66
67    /// Creates a `UniqueEntityEquivalentVec<T>` directly from a pointer, a length, and a capacity.
68    ///
69    /// Equivalent to [`Vec::from_raw_parts`].
70    ///
71    /// # Safety
72    ///
73    /// It must be safe to call [`Vec::from_raw_parts`] with these inputs,
74    /// and the resulting [`Vec`] must only contain unique elements.
75    pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
76        // SAFETY: Caller ensures it is safe to call `Vec::from_raw_parts`, and that
77        // the resulting `Vec` only contains unique elements.
78        unsafe { Self::from_vec_unchecked(Vec::from_raw_parts(ptr, length, capacity)) }
79    }
80
81    /// Constructs a `UniqueEntityEquivalentVec` from a [`Vec<T>`] unsafely.
82    ///
83    /// # Safety
84    ///
85    /// `vec` must contain only unique elements.
86    pub const unsafe fn from_vec_unchecked(vec: Vec<T>) -> Self {
87        Self(vec)
88    }
89
90    /// Constructs a `UniqueEntityEquivalentVec` from a [`&Vec<T>`](Vec) unsafely.
91    ///
92    /// # Safety
93    ///
94    /// `vec` must contain only unique elements.
95    pub const unsafe fn from_vec_ref_unchecked(vec: &Vec<T>) -> &Self {
96        // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec.
97        unsafe { &*ptr::from_ref(vec).cast() }
98    }
99
100    /// Constructs a `UniqueEntityEquivalentVec` from a [`&mut Vec<T>`](Vec) unsafely.
101    ///
102    /// # Safety
103    ///
104    /// `vec` must contain only unique elements.
105    pub const unsafe fn from_vec_mut_unchecked(vec: &mut Vec<T>) -> &mut Self {
106        // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec.
107        unsafe { &mut *ptr::from_mut(vec).cast() }
108    }
109
110    /// Returns the inner [`Vec<T>`].
111    pub fn into_inner(self) -> Vec<T> {
112        self.0
113    }
114
115    /// Returns a reference to the inner [`Vec<T>`].
116    pub const fn as_vec(&self) -> &Vec<T> {
117        &self.0
118    }
119
120    /// Returns a mutable reference to the inner [`Vec<T>`].
121    ///
122    /// # Safety
123    ///
124    /// The elements of this `Vec` must always remain unique, even while
125    /// this mutable reference is live.
126    pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec<T> {
127        &mut self.0
128    }
129
130    /// Returns the total number of elements the vector can hold without
131    /// reallocating.
132    ///
133    /// Equivalent to [`Vec::capacity`].
134    pub const fn capacity(&self) -> usize {
135        self.0.capacity()
136    }
137
138    /// Reserves capacity for at least `additional` more elements to be inserted
139    /// in the given `Vec<T>`.
140    ///
141    /// Equivalent to [`Vec::reserve`].
142    pub fn reserve(&mut self, additional: usize) {
143        self.0.reserve(additional);
144    }
145
146    /// Reserves the minimum capacity for at least `additional` more elements to
147    /// be inserted in the given `UniqueEntityEquivalentVec<T>`.
148    ///
149    /// Equivalent to [`Vec::reserve_exact`].
150    pub fn reserve_exact(&mut self, additional: usize) {
151        self.0.reserve_exact(additional);
152    }
153
154    /// Tries to reserve capacity for at least `additional` more elements to be inserted
155    /// in the given `Vec<T>`.
156    ///
157    /// Equivalent to [`Vec::try_reserve`].
158    pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
159        self.0.try_reserve(additional)
160    }
161
162    /// Tries to reserve the minimum capacity for at least `additional`
163    /// elements to be inserted in the given `Vec<T>`.
164    ///
165    /// Equivalent to [`Vec::try_reserve_exact`].
166    pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {
167        self.0.try_reserve_exact(additional)
168    }
169
170    /// Shrinks the capacity of the vector as much as possible.
171    ///
172    /// Equivalent to [`Vec::shrink_to_fit`].
173    pub fn shrink_to_fit(&mut self) {
174        self.0.shrink_to_fit();
175    }
176
177    /// Shrinks the capacity of the vector with a lower bound.
178    ///
179    /// Equivalent to [`Vec::shrink_to`].
180    pub fn shrink_to(&mut self, min_capacity: usize) {
181        self.0.shrink_to(min_capacity);
182    }
183
184    /// Converts the vector into `Box<UniqueEntityEquivalentSlice<T>>`.
185    pub fn into_boxed_slice(self) -> Box<UniqueEntityEquivalentSlice<T>> {
186        // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T].
187        unsafe {
188            UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(self.0.into_boxed_slice())
189        }
190    }
191
192    /// Extracts a slice containing the entire vector.
193    pub const fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
194        // SAFETY: All elements in the original slice are unique.
195        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.as_slice()) }
196    }
197
198    /// Extracts a mutable slice of the entire vector.
199    pub const fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
200        // SAFETY: All elements in the original slice are unique.
201        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
202    }
203
204    /// Shortens the vector, keeping the first `len` elements and dropping
205    /// the rest.
206    ///
207    /// Equivalent to [`Vec::truncate`].
208    pub fn truncate(&mut self, len: usize) {
209        self.0.truncate(len);
210    }
211
212    /// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
213    /// valid for zero sized reads if the vector didn't allocate.
214    ///
215    /// Equivalent to [`Vec::as_ptr`].
216    pub const fn as_ptr(&self) -> *const T {
217        self.0.as_ptr()
218    }
219    /// Returns a raw mutable pointer to the vector's buffer, or a dangling
220    /// raw pointer valid for zero sized reads if the vector didn't allocate.
221    ///
222    /// Equivalent to [`Vec::as_mut_ptr`].
223    pub const fn as_mut_ptr(&mut self) -> *mut T {
224        self.0.as_mut_ptr()
225    }
226
227    /// Forces the length of the vector to `new_len`.
228    ///
229    /// Equivalent to [`Vec::set_len`].
230    ///
231    /// # Safety
232    ///
233    /// It must be safe to call [`Vec::set_len`] with these inputs,
234    /// and the resulting [`Vec`] must only contain unique elements.
235    pub unsafe fn set_len(&mut self, new_len: usize) {
236        // SAFETY: Caller ensures it's safe to call `Vec::set_len`
237        unsafe { self.0.set_len(new_len) };
238    }
239
240    /// Removes an element from the vector and returns it.
241    ///
242    /// Equivalent to [`Vec::swap_remove`].
243    pub fn swap_remove(&mut self, index: usize) -> T {
244        self.0.swap_remove(index)
245    }
246
247    /// Inserts an element at position `index` within the vector, shifting all
248    /// elements after it to the right.
249    ///
250    /// Equivalent to [`Vec::insert`].
251    ///
252    /// # Safety
253    ///
254    /// No `T` contained by `self` may equal `element`.
255    pub unsafe fn insert(&mut self, index: usize, element: T) {
256        self.0.insert(index, element);
257    }
258
259    /// Removes and returns the element at position `index` within the vector,
260    /// shifting all elements after it to the left.
261    ///
262    /// Equivalent to [`Vec::remove`].
263    pub fn remove(&mut self, index: usize) -> T {
264        self.0.remove(index)
265    }
266
267    /// Retains only the elements specified by the predicate.
268    ///
269    /// Equivalent to [`Vec::retain`].
270    pub fn retain<F>(&mut self, f: F)
271    where
272        F: FnMut(&T) -> bool,
273    {
274        self.0.retain(f);
275    }
276
277    /// Retains only the elements specified by the predicate, passing a mutable reference to it.
278    ///
279    /// Equivalent to [`Vec::retain_mut`].
280    ///
281    /// # Safety
282    ///
283    /// `self` must only contain unique elements after each individual execution of `f`.
284    pub unsafe fn retain_mut<F>(&mut self, f: F)
285    where
286        F: FnMut(&mut T) -> bool,
287    {
288        self.0.retain_mut(f);
289    }
290
291    /// Removes all but the first of consecutive elements in the vector that resolve to the same
292    /// key.
293    ///
294    /// Equivalent to [`Vec::dedup_by_key`].
295    ///
296    /// # Safety
297    ///
298    /// `self` must only contain unique elements after each individual execution of `key`.
299    pub unsafe fn dedup_by_key<F, K>(&mut self, key: F)
300    where
301        F: FnMut(&mut T) -> K,
302        K: PartialEq,
303    {
304        self.0.dedup_by_key(key);
305    }
306
307    /// Removes all but the first of consecutive elements in the vector satisfying a given equality
308    /// relation.
309    ///
310    /// Equivalent to [`Vec::dedup_by`].
311    ///
312    /// # Safety
313    ///
314    /// `self` must only contain unique elements after each individual execution of `same_bucket`.
315    pub unsafe fn dedup_by<F>(&mut self, same_bucket: F)
316    where
317        F: FnMut(&mut T, &mut T) -> bool,
318    {
319        self.0.dedup_by(same_bucket);
320    }
321
322    /// Appends an element to the back of a collection.
323    ///
324    /// Equivalent to [`Vec::push`].
325    ///
326    /// # Safety
327    ///
328    /// No `T` contained by `self` may equal `element`.
329    pub unsafe fn push(&mut self, value: T) {
330        self.0.push(value);
331    }
332
333    /// Moves all the elements of `other` into `self`, leaving `other` empty.
334    ///
335    /// Equivalent to [`Vec::append`].
336    ///
337    /// # Safety
338    ///
339    /// `other` must contain no elements that equal any element in `self`.
340    pub unsafe fn append(&mut self, other: &mut UniqueEntityEquivalentVec<T>) {
341        self.0.append(&mut other.0);
342    }
343
344    /// Removes the last element from a vector and returns it, or [`None`] if it
345    /// is empty.
346    ///
347    /// Equivalent to [`Vec::pop`].
348    pub fn pop(&mut self) -> Option<T> {
349        self.0.pop()
350    }
351
352    /// Removes the specified range from the vector in bulk, returning all
353    /// removed elements as an iterator.
354    ///
355    /// Equivalent to [`Vec::drain`].
356    pub fn drain<R>(&mut self, range: R) -> Drain<'_, T>
357    where
358        R: RangeBounds<usize>,
359    {
360        // SAFETY: `self` and thus `range` contains only unique elements.
361        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.drain(range)) }
362    }
363
364    /// Clears the vector, removing all values.
365    ///
366    /// Equivalent to [`Vec::clear`].
367    pub fn clear(&mut self) {
368        self.0.clear();
369    }
370
371    /// Returns the number of elements in the vector, also referred to
372    /// as its 'length'.
373    ///
374    /// Equivalent to [`Vec::len`].
375    pub const fn len(&self) -> usize {
376        self.0.len()
377    }
378
379    /// Returns `true` if the vector contains no elements.
380    ///
381    /// Equivalent to [`Vec::is_empty`].
382    pub const fn is_empty(&self) -> bool {
383        self.0.is_empty()
384    }
385
386    /// Splits the collection into two at the given index.
387    ///
388    /// Equivalent to [`Vec::split_off`].
389    pub fn split_off(&mut self, at: usize) -> Self {
390        // SAFETY: Any subslice/subsection of a `UniqueEntityVec` is also unique.
391        unsafe { Self::from_vec_unchecked(self.0.split_off(at)) }
392    }
393
394    /// Resizes the `Vec` in-place so that `len` is equal to `new_len`.
395    ///
396    /// Equivalent to [`Vec::resize_with`].
397    ///
398    /// # Safety
399    ///
400    /// `f` must only produce unique `T`, and none of these may equal any `T` in `self`.
401    pub unsafe fn resize_with<F>(&mut self, new_len: usize, f: F)
402    where
403        F: FnMut() -> T,
404    {
405        self.0.resize_with(new_len, f);
406    }
407
408    /// Consumes and leaks the Vec, returning a mutable reference to the contents, `&'a mut UniqueEntityEquivalentSlice<T>`.
409    pub fn leak<'a>(self) -> &'a mut UniqueEntityEquivalentSlice<T> {
410        // SAFETY: All elements in the original slice are unique.
411        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.leak()) }
412    }
413
414    /// Returns the remaining spare capacity of the vector as a slice of
415    /// [`MaybeUninit<T>`].
416    ///
417    /// Equivalent to [`Vec::spare_capacity_mut`].
418    pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
419        self.0.spare_capacity_mut()
420    }
421
422    /// Creates a splicing iterator that replaces the specified range in the vector
423    /// with the given `replace_with` iterator and yields the removed items.
424    ///
425    /// Equivalent to [`Vec::splice`].
426    ///
427    /// # Safety
428    ///
429    /// `replace_with` must not yield any elements that equal any elements in `self`,
430    /// except for those in `range`.
431    pub unsafe fn splice<R, I>(
432        &mut self,
433        range: R,
434        replace_with: I,
435    ) -> Splice<'_, <I as IntoIterator>::IntoIter>
436    where
437        R: RangeBounds<usize>,
438        I: EntitySet<Item = T>,
439    {
440        // SAFETY: `self` and thus `range` contains only unique elements.
441        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.splice(range, replace_with)) }
442    }
443}
444
445impl<T: EntityEquivalent> Default for UniqueEntityEquivalentVec<T> {
446    fn default() -> Self {
447        // SAFETY: An empty `Vec` cannot contain duplicates.
448        unsafe { Self::from_vec_unchecked(Vec::default()) }
449    }
450}
451
452impl<T: EntityEquivalent> Deref for UniqueEntityEquivalentVec<T> {
453    type Target = UniqueEntityEquivalentSlice<T>;
454
455    fn deref(&self) -> &Self::Target {
456        self.as_slice()
457    }
458}
459
460impl<T: EntityEquivalent> DerefMut for UniqueEntityEquivalentVec<T> {
461    fn deref_mut(&mut self) -> &mut Self::Target {
462        self.as_mut_slice()
463    }
464}
465
466impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentVec<T>
467where
468    &'a T: EntityEquivalent,
469{
470    type Item = &'a T;
471
472    type IntoIter = unique_slice::Iter<'a, T>;
473
474    fn into_iter(self) -> Self::IntoIter {
475        // SAFETY: `self` contains only unique elements.
476        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) }
477    }
478}
479
480impl<T: EntityEquivalent> IntoIterator for UniqueEntityEquivalentVec<T> {
481    type Item = T;
482
483    type IntoIter = IntoIter<T>;
484
485    fn into_iter(self) -> Self::IntoIter {
486        // SAFETY: `self` contains only unique elements.
487        unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) }
488    }
489}
490
491impl<T: EntityEquivalent> AsMut<Self> for UniqueEntityEquivalentVec<T> {
492    fn as_mut(&mut self) -> &mut UniqueEntityEquivalentVec<T> {
493        self
494    }
495}
496
497impl<T: EntityEquivalent> AsMut<UniqueEntityEquivalentSlice<T>> for UniqueEntityEquivalentVec<T> {
498    fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
499        self
500    }
501}
502
503impl<T: EntityEquivalent> AsRef<Self> for UniqueEntityEquivalentVec<T> {
504    fn as_ref(&self) -> &Self {
505        self
506    }
507}
508
509impl<T: EntityEquivalent> AsRef<Vec<T>> for UniqueEntityEquivalentVec<T> {
510    fn as_ref(&self) -> &Vec<T> {
511        &self.0
512    }
513}
514
515impl<T: EntityEquivalent> Borrow<Vec<T>> for UniqueEntityEquivalentVec<T> {
516    fn borrow(&self) -> &Vec<T> {
517        &self.0
518    }
519}
520
521impl<T: EntityEquivalent> AsRef<[T]> for UniqueEntityEquivalentVec<T> {
522    fn as_ref(&self) -> &[T] {
523        &self.0
524    }
525}
526
527impl<T: EntityEquivalent> AsRef<UniqueEntityEquivalentSlice<T>> for UniqueEntityEquivalentVec<T> {
528    fn as_ref(&self) -> &UniqueEntityEquivalentSlice<T> {
529        self
530    }
531}
532
533impl<T: EntityEquivalent> Borrow<[T]> for UniqueEntityEquivalentVec<T> {
534    fn borrow(&self) -> &[T] {
535        &self.0
536    }
537}
538
539impl<T: EntityEquivalent> Borrow<UniqueEntityEquivalentSlice<T>> for UniqueEntityEquivalentVec<T> {
540    fn borrow(&self) -> &UniqueEntityEquivalentSlice<T> {
541        self
542    }
543}
544
545impl<T: EntityEquivalent> BorrowMut<UniqueEntityEquivalentSlice<T>>
546    for UniqueEntityEquivalentVec<T>
547{
548    fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
549        self
550    }
551}
552
553impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<Vec<U>> for UniqueEntityEquivalentVec<T> {
554    fn eq(&self, other: &Vec<U>) -> bool {
555        self.0.eq(other)
556    }
557}
558
559impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<&[U]> for UniqueEntityEquivalentVec<T> {
560    fn eq(&self, other: &&[U]) -> bool {
561        self.0.eq(other)
562    }
563}
564
565impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
566    PartialEq<&UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentVec<T>
567{
568    fn eq(&self, other: &&UniqueEntityEquivalentSlice<U>) -> bool {
569        self.0.eq(other)
570    }
571}
572
573impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<&mut [U]> for UniqueEntityEquivalentVec<T> {
574    fn eq(&self, other: &&mut [U]) -> bool {
575        self.0.eq(other)
576    }
577}
578
579impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
580    PartialEq<&mut UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentVec<T>
581{
582    fn eq(&self, other: &&mut UniqueEntityEquivalentSlice<U>) -> bool {
583        self.0.eq(other)
584    }
585}
586
587impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<&[U; N]>
588    for UniqueEntityEquivalentVec<T>
589{
590    fn eq(&self, other: &&[U; N]) -> bool {
591        self.0.eq(other)
592    }
593}
594
595impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
596    PartialEq<&UniqueEntityEquivalentArray<U, N>> for UniqueEntityEquivalentVec<T>
597{
598    fn eq(&self, other: &&UniqueEntityEquivalentArray<U, N>) -> bool {
599        self.0.eq(&other.as_inner())
600    }
601}
602
603impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<&mut [U; N]>
604    for UniqueEntityEquivalentVec<T>
605{
606    fn eq(&self, other: &&mut [U; N]) -> bool {
607        self.0.eq(&**other)
608    }
609}
610
611impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
612    PartialEq<&mut UniqueEntityEquivalentArray<U, N>> for UniqueEntityEquivalentVec<T>
613{
614    fn eq(&self, other: &&mut UniqueEntityEquivalentArray<U, N>) -> bool {
615        self.0.eq(other.as_inner())
616    }
617}
618
619impl<T: EntityEquivalent + PartialEq<U>, U> PartialEq<[U]> for UniqueEntityEquivalentVec<T> {
620    fn eq(&self, other: &[U]) -> bool {
621        self.0.eq(other)
622    }
623}
624
625impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
626    PartialEq<UniqueEntityEquivalentSlice<U>> for UniqueEntityEquivalentVec<T>
627{
628    fn eq(&self, other: &UniqueEntityEquivalentSlice<U>) -> bool {
629        self.0.eq(&**other)
630    }
631}
632
633impl<T: EntityEquivalent + PartialEq<U>, U, const N: usize> PartialEq<[U; N]>
634    for UniqueEntityEquivalentVec<T>
635{
636    fn eq(&self, other: &[U; N]) -> bool {
637        self.0.eq(other)
638    }
639}
640
641impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent, const N: usize>
642    PartialEq<UniqueEntityEquivalentArray<U, N>> for UniqueEntityEquivalentVec<T>
643{
644    fn eq(&self, other: &UniqueEntityEquivalentArray<U, N>) -> bool {
645        self.0.eq(other.as_inner())
646    }
647}
648
649impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<UniqueEntityEquivalentVec<U>> for Vec<T> {
650    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
651        self.eq(&other.0)
652    }
653}
654
655impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<UniqueEntityEquivalentVec<U>> for &[T] {
656    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
657        self.eq(&other.0)
658    }
659}
660
661impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<UniqueEntityEquivalentVec<U>> for &mut [T] {
662    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
663        self.eq(&other.0)
664    }
665}
666
667impl<T: EntityEquivalent + PartialEq<U>, U: EntityEquivalent>
668    PartialEq<UniqueEntityEquivalentVec<U>> for [T]
669{
670    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
671        self.eq(&other.0)
672    }
673}
674
675impl<T: PartialEq<U> + Clone, U: EntityEquivalent> PartialEq<UniqueEntityEquivalentVec<U>>
676    for Cow<'_, [T]>
677{
678    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
679        self.eq(&other.0)
680    }
681}
682
683impl<T: PartialEq<U>, U: EntityEquivalent> PartialEq<UniqueEntityEquivalentVec<U>> for VecDeque<T> {
684    fn eq(&self, other: &UniqueEntityEquivalentVec<U>) -> bool {
685        self.eq(&other.0)
686    }
687}
688
689impl<T: EntityEquivalent + Clone> From<&UniqueEntityEquivalentSlice<T>>
690    for UniqueEntityEquivalentVec<T>
691{
692    fn from(value: &UniqueEntityEquivalentSlice<T>) -> Self {
693        value.to_vec()
694    }
695}
696
697impl<T: EntityEquivalent + Clone> From<&mut UniqueEntityEquivalentSlice<T>>
698    for UniqueEntityEquivalentVec<T>
699{
700    fn from(value: &mut UniqueEntityEquivalentSlice<T>) -> Self {
701        value.to_vec()
702    }
703}
704
705impl<T: EntityEquivalent> From<Box<UniqueEntityEquivalentSlice<T>>>
706    for UniqueEntityEquivalentVec<T>
707{
708    fn from(value: Box<UniqueEntityEquivalentSlice<T>>) -> Self {
709        value.into_vec()
710    }
711}
712
713impl<T: EntityEquivalent> From<Cow<'_, UniqueEntityEquivalentSlice<T>>>
714    for UniqueEntityEquivalentVec<T>
715where
716    UniqueEntityEquivalentSlice<T>: ToOwned<Owned = UniqueEntityEquivalentVec<T>>,
717{
718    fn from(value: Cow<UniqueEntityEquivalentSlice<T>>) -> Self {
719        value.into_owned()
720    }
721}
722
723impl<T: EntityEquivalent + Clone> From<&[T; 1]> for UniqueEntityEquivalentVec<T> {
724    fn from(value: &[T; 1]) -> Self {
725        // SAFETY: An array with 1 element cannot contain duplicates.
726        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
727    }
728}
729
730impl<T: EntityEquivalent + Clone> From<&[T; 0]> for UniqueEntityEquivalentVec<T> {
731    fn from(value: &[T; 0]) -> Self {
732        // SAFETY: An empty array cannot contain duplicates.
733        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
734    }
735}
736
737impl<T: EntityEquivalent + Clone> From<&mut [T; 1]> for UniqueEntityEquivalentVec<T> {
738    fn from(value: &mut [T; 1]) -> Self {
739        // SAFETY: An array with 1 element cannot contain duplicates.
740        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
741    }
742}
743
744impl<T: EntityEquivalent + Clone> From<&mut [T; 0]> for UniqueEntityEquivalentVec<T> {
745    fn from(value: &mut [T; 0]) -> Self {
746        // SAFETY: An empty array cannot contain duplicates.
747        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
748    }
749}
750
751impl<T: EntityEquivalent> From<[T; 1]> for UniqueEntityEquivalentVec<T> {
752    fn from(value: [T; 1]) -> Self {
753        // SAFETY: An array with 1 element cannot contain duplicates.
754        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
755    }
756}
757
758impl<T: EntityEquivalent> From<[T; 0]> for UniqueEntityEquivalentVec<T> {
759    fn from(value: [T; 0]) -> Self {
760        // SAFETY: An empty array cannot contain duplicates.
761        unsafe { Self::from_vec_unchecked(Vec::from(value)) }
762    }
763}
764
765impl<T: EntityEquivalent + Clone, const N: usize> From<&UniqueEntityEquivalentArray<T, N>>
766    for UniqueEntityEquivalentVec<T>
767{
768    fn from(value: &UniqueEntityEquivalentArray<T, N>) -> Self {
769        // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements.
770        unsafe { Self::from_vec_unchecked(Vec::from(value.as_inner().clone())) }
771    }
772}
773
774impl<T: EntityEquivalent + Clone, const N: usize> From<&mut UniqueEntityEquivalentArray<T, N>>
775    for UniqueEntityEquivalentVec<T>
776{
777    fn from(value: &mut UniqueEntityEquivalentArray<T, N>) -> Self {
778        // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements.
779        unsafe { Self::from_vec_unchecked(Vec::from(value.as_inner().clone())) }
780    }
781}
782
783impl<T: EntityEquivalent, const N: usize> From<UniqueEntityEquivalentArray<T, N>>
784    for UniqueEntityEquivalentVec<T>
785{
786    fn from(value: UniqueEntityEquivalentArray<T, N>) -> Self {
787        // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements.
788        unsafe { Self::from_vec_unchecked(Vec::from(value.into_inner())) }
789    }
790}
791
792impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>> for Vec<T> {
793    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
794        value.0
795    }
796}
797
798impl<'a, T: EntityEquivalent + Clone> From<UniqueEntityEquivalentVec<T>> for Cow<'a, [T]> {
799    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
800        Cow::from(value.0)
801    }
802}
803
804impl<'a, T: EntityEquivalent + Clone> From<UniqueEntityEquivalentVec<T>>
805    for Cow<'a, UniqueEntityEquivalentSlice<T>>
806{
807    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
808        Cow::Owned(value)
809    }
810}
811
812impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>> for Arc<[T]> {
813    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
814        Arc::from(value.0)
815    }
816}
817
818impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>>
819    for Arc<UniqueEntityEquivalentSlice<T>>
820{
821    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
822        // SAFETY: All elements in the original slice are unique.
823        unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(Arc::from(value.0)) }
824    }
825}
826
827impl<T: EntityEquivalent + Ord> From<UniqueEntityEquivalentVec<T>> for BinaryHeap<T> {
828    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
829        BinaryHeap::from(value.0)
830    }
831}
832
833impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>> for Box<[T]> {
834    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
835        Box::from(value.0)
836    }
837}
838
839impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>> for Rc<[T]> {
840    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
841        Rc::from(value.0)
842    }
843}
844
845impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>>
846    for Rc<UniqueEntityEquivalentSlice<T>>
847{
848    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
849        // SAFETY: All elements in the original slice are unique.
850        unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(Rc::from(value.0)) }
851    }
852}
853
854impl<T: EntityEquivalent> From<UniqueEntityEquivalentVec<T>> for VecDeque<T> {
855    fn from(value: UniqueEntityEquivalentVec<T>) -> Self {
856        VecDeque::from(value.0)
857    }
858}
859
860impl<T: EntityEquivalent, const N: usize> TryFrom<UniqueEntityEquivalentVec<T>> for Box<[T; N]> {
861    type Error = UniqueEntityEquivalentVec<T>;
862
863    fn try_from(value: UniqueEntityEquivalentVec<T>) -> Result<Self, Self::Error> {
864        Box::try_from(value.0).map_err(UniqueEntityEquivalentVec)
865    }
866}
867
868impl<T: EntityEquivalent, const N: usize> TryFrom<UniqueEntityEquivalentVec<T>>
869    for Box<UniqueEntityEquivalentArray<T, N>>
870{
871    type Error = UniqueEntityEquivalentVec<T>;
872
873    fn try_from(value: UniqueEntityEquivalentVec<T>) -> Result<Self, Self::Error> {
874        Box::try_from(value.0)
875            .map(|v|
876                // SAFETY: All elements in the original Vec are unique.
877                unsafe { UniqueEntityEquivalentArray::from_boxed_array_unchecked(v) })
878            .map_err(UniqueEntityEquivalentVec)
879    }
880}
881
882impl<T: EntityEquivalent, const N: usize> TryFrom<UniqueEntityEquivalentVec<T>> for [T; N] {
883    type Error = UniqueEntityEquivalentVec<T>;
884
885    fn try_from(value: UniqueEntityEquivalentVec<T>) -> Result<Self, Self::Error> {
886        <[T; N] as TryFrom<Vec<T>>>::try_from(value.0).map_err(UniqueEntityEquivalentVec)
887    }
888}
889
890impl<T: EntityEquivalent, const N: usize> TryFrom<UniqueEntityEquivalentVec<T>>
891    for UniqueEntityEquivalentArray<T, N>
892{
893    type Error = UniqueEntityEquivalentVec<T>;
894
895    fn try_from(value: UniqueEntityEquivalentVec<T>) -> Result<Self, Self::Error> {
896        <[T; N] as TryFrom<Vec<T>>>::try_from(value.0)
897            .map(|v|
898            // SAFETY: All elements in the original Vec are unique.
899            unsafe { UniqueEntityEquivalentArray::from_array_unchecked(v) })
900            .map_err(UniqueEntityEquivalentVec)
901    }
902}
903
904impl<T: EntityEquivalent> From<BTreeSet<T>> for UniqueEntityEquivalentVec<T> {
905    fn from(value: BTreeSet<T>) -> Self {
906        // SAFETY: A `BTreeSet` over an `EntityEquivalent` T only contains unique elements.
907        unsafe { Self::from_vec_unchecked(value.into_iter().collect::<Vec<T>>()) }
908    }
909}
910
911impl<T: EntityEquivalent> FromIterator<T> for UniqueEntityEquivalentVec<T> {
912    /// This impl only uses `Eq` to validate uniqueness, resulting in O(n^2) complexity.
913    /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`.
914    /// When possible, use `FromEntitySetIterator::from_entity_iter` instead.
915    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
916        // Matches the `HashSet::from_iter` reservation logic.
917        let iter = iter.into_iter();
918        let unique_vec = Self::with_capacity(iter.size_hint().0);
919        // Internal iteration (fold/for_each) is known to result in better code generation
920        // over a for loop.
921        iter.fold(unique_vec, |mut unique_vec, item| {
922            if !unique_vec.0.contains(&item) {
923                unique_vec.0.push(item);
924            }
925            unique_vec
926        })
927    }
928}
929
930impl<T: EntityEquivalent> FromEntitySetIterator<T> for UniqueEntityEquivalentVec<T> {
931    fn from_entity_set_iter<I: EntitySet<Item = T>>(iter: I) -> Self {
932        // SAFETY: `iter` is an `EntitySet`.
933        unsafe { Self::from_vec_unchecked(Vec::from_iter(iter)) }
934    }
935}
936
937impl<T: EntityEquivalent> Extend<T> for UniqueEntityEquivalentVec<T> {
938    /// Use with caution, because this impl only uses `Eq` to validate uniqueness,
939    /// resulting in O(n^2) complexity.
940    /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`.
941    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
942        // Matches the `HashSet::extend` reservation logic. Their reasoning:
943        //  "Keys may be already present or show multiple times in the iterator.
944        //  Reserve the entire hint lower bound if the map is empty.
945        //  Otherwise reserve half the hint (rounded up), so the map
946        //  will only resize twice in the worst case."
947        let iter = iter.into_iter();
948        let reserve = if self.is_empty() {
949            iter.size_hint().0
950        } else {
951            iter.size_hint().0.div_ceil(2)
952        };
953        self.reserve(reserve);
954        // Internal iteration (fold/for_each) is known to result in better code generation
955        // over a for loop.
956        iter.for_each(move |item| {
957            if !self.0.contains(&item) {
958                self.0.push(item);
959            }
960        });
961    }
962}
963
964impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityEquivalentVec<T> {
965    /// Use with caution, because this impl only uses `Eq` to validate uniqueness,
966    /// resulting in O(n^2) complexity.
967    /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`.
968    fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
969        // Matches the `HashSet::extend` reservation logic. Their reasoning:
970        //  "Keys may be already present or show multiple times in the iterator.
971        //  Reserve the entire hint lower bound if the map is empty.
972        //  Otherwise reserve half the hint (rounded up), so the map
973        //  will only resize twice in the worst case."
974        let iter = iter.into_iter();
975        let reserve = if self.is_empty() {
976            iter.size_hint().0
977        } else {
978            iter.size_hint().0.div_ceil(2)
979        };
980        self.reserve(reserve);
981        // Internal iteration (fold/for_each) is known to result in better code generation
982        // over a for loop.
983        iter.for_each(move |item| {
984            if !self.0.contains(item) {
985                self.0.push(*item);
986            }
987        });
988    }
989}
990
991impl<T: EntityEquivalent> Index<(Bound<usize>, Bound<usize>)> for UniqueEntityEquivalentVec<T> {
992    type Output = UniqueEntityEquivalentSlice<T>;
993
994    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {
995        // SAFETY: All elements in the original slice are unique.
996        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
997    }
998}
999
1000impl<T: EntityEquivalent> Index<Range<usize>> for UniqueEntityEquivalentVec<T> {
1001    type Output = UniqueEntityEquivalentSlice<T>;
1002
1003    fn index(&self, key: Range<usize>) -> &Self::Output {
1004        // SAFETY: All elements in the original slice are unique.
1005        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1006    }
1007}
1008
1009impl<T: EntityEquivalent> Index<RangeFrom<usize>> for UniqueEntityEquivalentVec<T> {
1010    type Output = UniqueEntityEquivalentSlice<T>;
1011
1012    fn index(&self, key: RangeFrom<usize>) -> &Self::Output {
1013        // SAFETY: All elements in the original slice are unique.
1014        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1015    }
1016}
1017
1018impl<T: EntityEquivalent> Index<RangeFull> for UniqueEntityEquivalentVec<T> {
1019    type Output = UniqueEntityEquivalentSlice<T>;
1020
1021    fn index(&self, key: RangeFull) -> &Self::Output {
1022        // SAFETY: All elements in the original slice are unique.
1023        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1024    }
1025}
1026
1027impl<T: EntityEquivalent> Index<RangeInclusive<usize>> for UniqueEntityEquivalentVec<T> {
1028    type Output = UniqueEntityEquivalentSlice<T>;
1029
1030    fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {
1031        // SAFETY: All elements in the original slice are unique.
1032        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1033    }
1034}
1035
1036impl<T: EntityEquivalent> Index<RangeTo<usize>> for UniqueEntityEquivalentVec<T> {
1037    type Output = UniqueEntityEquivalentSlice<T>;
1038
1039    fn index(&self, key: RangeTo<usize>) -> &Self::Output {
1040        // SAFETY: All elements in the original slice are unique.
1041        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1042    }
1043}
1044
1045impl<T: EntityEquivalent> Index<RangeToInclusive<usize>> for UniqueEntityEquivalentVec<T> {
1046    type Output = UniqueEntityEquivalentSlice<T>;
1047
1048    fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {
1049        // SAFETY: All elements in the original slice are unique.
1050        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) }
1051    }
1052}
1053
1054impl<T: EntityEquivalent> Index<usize> for UniqueEntityEquivalentVec<T> {
1055    type Output = T;
1056
1057    fn index(&self, key: usize) -> &T {
1058        self.0.index(key)
1059    }
1060}
1061
1062impl<T: EntityEquivalent> IndexMut<(Bound<usize>, Bound<usize>)> for UniqueEntityEquivalentVec<T> {
1063    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {
1064        // SAFETY: All elements in the original slice are unique.
1065        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1066    }
1067}
1068
1069impl<T: EntityEquivalent> IndexMut<Range<usize>> for UniqueEntityEquivalentVec<T> {
1070    fn index_mut(&mut self, key: Range<usize>) -> &mut Self::Output {
1071        // SAFETY: All elements in the original slice are unique.
1072        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1073    }
1074}
1075
1076impl<T: EntityEquivalent> IndexMut<RangeFrom<usize>> for UniqueEntityEquivalentVec<T> {
1077    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self::Output {
1078        // SAFETY: All elements in the original slice are unique.
1079        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1080    }
1081}
1082
1083impl<T: EntityEquivalent> IndexMut<RangeFull> for UniqueEntityEquivalentVec<T> {
1084    fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output {
1085        // SAFETY: All elements in the original slice are unique.
1086        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1087    }
1088}
1089
1090impl<T: EntityEquivalent> IndexMut<RangeInclusive<usize>> for UniqueEntityEquivalentVec<T> {
1091    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self::Output {
1092        // SAFETY: All elements in the original slice are unique.
1093        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1094    }
1095}
1096
1097impl<T: EntityEquivalent> IndexMut<RangeTo<usize>> for UniqueEntityEquivalentVec<T> {
1098    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self::Output {
1099        // SAFETY: All elements in the original slice are unique.
1100        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1101    }
1102}
1103
1104impl<T: EntityEquivalent> IndexMut<RangeToInclusive<usize>> for UniqueEntityEquivalentVec<T> {
1105    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self::Output {
1106        // SAFETY: All elements in the original slice are unique.
1107        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) }
1108    }
1109}
1110
1111/// An iterator that moves out of a vector.
1112///
1113/// This `struct` is created by the [`IntoIterator::into_iter`] trait
1114/// method on [`UniqueEntityEquivalentVec`].
1115pub type IntoIter<T = Entity> = UniqueEntityIter<vec::IntoIter<T>>;
1116
1117impl<T: EntityEquivalent> UniqueEntityIter<vec::IntoIter<T>> {
1118    /// Returns the remaining items of this iterator as a slice.
1119    ///
1120    /// Equivalent to [`vec::IntoIter::as_slice`].
1121    pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
1122        // SAFETY: All elements in the original slice are unique.
1123        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
1124    }
1125
1126    /// Returns the remaining items of this iterator as a mutable slice.
1127    ///
1128    /// Equivalent to [`vec::IntoIter::as_mut_slice`].
1129    pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
1130        // SAFETY: All elements in the original slice are unique.
1131        unsafe {
1132            UniqueEntityEquivalentSlice::from_slice_unchecked_mut(
1133                self.as_mut_inner().as_mut_slice(),
1134            )
1135        }
1136    }
1137}
1138
1139/// A draining iterator for [`UniqueEntityEquivalentVec<T>`].
1140///
1141/// This struct is created by [`UniqueEntityEquivalentVec::drain`].
1142/// See its documentation for more.
1143pub type Drain<'a, T = Entity> = UniqueEntityIter<vec::Drain<'a, T>>;
1144
1145impl<'a, T: EntityEquivalent> UniqueEntityIter<vec::Drain<'a, T>> {
1146    /// Returns the remaining items of this iterator as a slice.
1147    ///
1148    /// Equivalent to [`vec::Drain::as_slice`].
1149    pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice<T> {
1150        // SAFETY: All elements in the original slice are unique.
1151        unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) }
1152    }
1153}
1154
1155/// A splicing iterator for [`UniqueEntityEquivalentVec`].
1156///
1157/// This struct is created by [`UniqueEntityEquivalentVec::splice`].
1158/// See its documentation for more.
1159pub type Splice<'a, I> = UniqueEntityIter<vec::Splice<'a, I>>;