From c9fb956058efa648e7299ba94e80cc58028cac6e Mon Sep 17 00:00:00 2001 From: Vic <59878206+Victoronz@users.noreply.github.com> Date: Thu, 3 Apr 2025 05:59:04 +0200 Subject: [PATCH] use entity set collections type aliases instead of defaults (#18695) # Objective Newest installment of the #16547 series. In #18319 we introduced `Entity` defaults to accomodate the most common use case for these types, however that resulted in the switch of the `T` and `N` generics of `UniqueEntityArray`. Swapping generics might be somewhat acceptable for `UniqueEntityArray`, it is not at all acceptable for map and set types, which we would make generic over `T: EntityEquivalent` in #18408. Leaving these defaults in place would result in a glaring inconsistency between these set collections and the others. Additionally, the current standard in the engine is for "entity" to mean `Entity`. APIs could be changed to accept `EntityEquivalent`, however that is a separate and contentious discussion. ## Solution Name these set collections `UniqueEntityEquivalent*`, and retain the `UniqueEntity*` name for an alias of the `Entity` case. While more verbose, this allows for all generics to be in proper order, full consistency between all set types*, and the "entity" name to be restricted to `Entity`. On top of that, `UniqueEntity*` now always have 1 generic less, when previously this was not enforced for the default case. *`UniqueEntityIter>` is the sole exception to this. Aliases are unable to enforce bounds (`lazy_type_alias` is needed for this), so for this type, doing this split would be a mere suggestion, and in no way enforced. Iterator types are rarely ever named, and this specific one is intended to be aliased when it sees more use, like we do for the corresponding set collection iterators. Furthermore, the `EntityEquivalent` precursor `Borrow` was used exactly because of such iterator bounds! Because of that, we leave it as is. While no migration guide for 0.15 users, for those that upgrade from main: `UniqueEntityVec` -> `UniqueEntityEquivalentVec` `UniqueEntitySlice` -> `UniqueEntityEquivalentSlice` `UniqueEntityArray` -> `UniqueEntityEquivalentArray` --- crates/bevy_ecs/src/entity/entity_set.rs | 14 +- crates/bevy_ecs/src/entity/mod.rs | 6 +- crates/bevy_ecs/src/entity/unique_array.rs | 347 +++++----- crates/bevy_ecs/src/entity/unique_slice.rs | 697 ++++++++++++--------- crates/bevy_ecs/src/entity/unique_vec.rs | 422 +++++++------ crates/bevy_ecs/src/query/par_iter.rs | 6 +- crates/bevy_ecs/src/query/state.rs | 4 +- crates/bevy_ecs/src/system/query.rs | 2 +- 8 files changed, 839 insertions(+), 659 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index 7608a7af5c..56f597ceba 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -13,7 +13,7 @@ use core::{ option, result, }; -use super::{Entity, UniqueEntitySlice}; +use super::{Entity, UniqueEntityEquivalentSlice}; use bevy_platform_support::sync::Arc; @@ -432,20 +432,20 @@ impl + AsRef<[T]>> AsRef<[T]> for UniqueE } impl + AsRef<[T]>> - AsRef> for UniqueEntityIter + AsRef> for UniqueEntityIter { - fn as_ref(&self) -> &UniqueEntitySlice { + fn as_ref(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.iter.as_ref()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.as_ref()) } } } impl + AsMut<[T]>> - AsMut> for UniqueEntityIter + AsMut> for UniqueEntityIter { - fn as_mut(&mut self) -> &mut UniqueEntitySlice { + fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.iter.as_mut()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.as_mut()) } } } diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index 38cda15eaa..faa97083ba 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -67,9 +67,9 @@ pub mod unique_array; pub mod unique_slice; pub mod unique_vec; -pub use unique_array::UniqueEntityArray; -pub use unique_slice::UniqueEntitySlice; -pub use unique_vec::UniqueEntityVec; +pub use unique_array::{UniqueEntityArray, UniqueEntityEquivalentArray}; +pub use unique_slice::{UniqueEntityEquivalentSlice, UniqueEntitySlice}; +pub use unique_vec::{UniqueEntityEquivalentVec, UniqueEntityVec}; use crate::{ archetype::{ArchetypeId, ArchetypeRow}, diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index 7cfee9cb4d..fa528c253a 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -21,19 +21,26 @@ use alloc::{ use bevy_platform_support::sync::Arc; use super::{ - unique_slice::{self, UniqueEntitySlice}, + unique_slice::{self, UniqueEntityEquivalentSlice}, Entity, EntityEquivalent, UniqueEntityIter, }; /// An array that contains only unique entities. /// -/// It can be obtained through certain methods on [`UniqueEntitySlice`], +/// It can be obtained through certain methods on [`UniqueEntityEquivalentSlice`], /// and some [`TryFrom`] implementations. +/// +/// When `T` is [`Entity`], use [`UniqueEntityArray`]. #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct UniqueEntityArray([T; N]); +pub struct UniqueEntityEquivalentArray([T; N]); -impl UniqueEntityArray { - /// Constructs a `UniqueEntityArray` from a [`[T; N]`] unsafely. +/// An array that contains only unique [`Entity`]. +/// +/// This is the default case of a [`UniqueEntityEquivalentArray`]. +pub type UniqueEntityArray = UniqueEntityEquivalentArray; + +impl UniqueEntityEquivalentArray { + /// Constructs a `UniqueEntityEquivalentArray` from a [`[T; N]`] unsafely. /// /// # Safety /// @@ -42,61 +49,61 @@ impl UniqueEntityArray { Self(array) } - /// Constructs a `&UniqueEntityArray` from a [`&[T; N]`] unsafely. + /// Constructs a `&UniqueEntityEquivalentArray` from a [`&[T; N]`] unsafely. /// /// # Safety /// /// `array` must contain only unique elements. pub const unsafe fn from_array_ref_unchecked(array: &[T; N]) -> &Self { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { &*(ptr::from_ref(array).cast()) } } - /// Constructs a `Box` from a [`Box<[T; N]>`] unsafely. + /// Constructs a `Box` from a [`Box<[T; N]>`] unsafely. /// /// # Safety /// /// `array` must contain only unique elements. pub unsafe fn from_boxed_array_unchecked(array: Box<[T; N]>) -> Box { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(array).cast()) } } /// Casts `self` into the inner array. pub fn into_boxed_inner(self: Box) -> Box<[T; N]> { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(self).cast()) } } - /// Constructs a `Arc` from a [`Arc<[T; N]>`] unsafely. + /// Constructs a `Arc` from a [`Arc<[T; N]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_arc_array_unchecked(slice: Arc<[T; N]>) -> Arc { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. pub fn into_arc_inner(this: Arc) -> Arc<[T; N]> { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(this).cast()) } } - // Constructs a `Rc` from a [`Rc<[T; N]>`] unsafely. + // Constructs a `Rc` from a [`Rc<[T; N]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_rc_array_unchecked(slice: Rc<[T; N]>) -> Rc { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. pub fn into_rc_inner(self: Rc) -> Rc<[T; N]> { - // SAFETY: UniqueEntityArray is a transparent wrapper around [T; N]. + // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(self).cast()) } } @@ -111,49 +118,51 @@ impl UniqueEntityArray { } /// Returns a slice containing the entire array. Equivalent to `&s[..]`. - pub const fn as_slice(&self) -> &UniqueEntitySlice { + pub const fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.as_slice()) } } /// Returns a mutable slice containing the entire array. Equivalent to /// `&mut s[..]`. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { + pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } } /// Borrows each element and returns an array of references with the same /// size as `self`. /// /// Equivalent to [`[T; N]::as_ref`](array::each_ref). - pub fn each_ref(&self) -> UniqueEntityArray { - UniqueEntityArray(self.0.each_ref()) + pub fn each_ref(&self) -> UniqueEntityEquivalentArray<&T, N> { + UniqueEntityEquivalentArray(self.0.each_ref()) } } -impl Deref for UniqueEntityArray { - type Target = UniqueEntitySlice; +impl Deref for UniqueEntityEquivalentArray { + type Target = UniqueEntityEquivalentSlice; fn deref(&self) -> &Self::Target { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(&self.0) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } } } -impl DerefMut for UniqueEntityArray { +impl DerefMut for UniqueEntityEquivalentArray { fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(&mut self.0) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } } } -impl Default for UniqueEntityArray<0, T> { +impl Default for UniqueEntityEquivalentArray { fn default() -> Self { Self(Default::default()) } } -impl<'a, T: EntityEquivalent, const N: usize> IntoIterator for &'a UniqueEntityArray { +impl<'a, T: EntityEquivalent, const N: usize> IntoIterator + for &'a UniqueEntityEquivalentArray +{ type Item = &'a T; type IntoIter = unique_slice::Iter<'a, T>; @@ -164,7 +173,7 @@ impl<'a, T: EntityEquivalent, const N: usize> IntoIterator for &'a UniqueEntityA } } -impl IntoIterator for UniqueEntityArray { +impl IntoIterator for UniqueEntityEquivalentArray { type Item = T; type IntoIter = IntoIter; @@ -175,93 +184,107 @@ impl IntoIterator for UniqueEntityArray AsRef> for UniqueEntityArray { - fn as_ref(&self) -> &UniqueEntitySlice { - self - } -} - -impl AsMut> for UniqueEntityArray { - fn as_mut(&mut self) -> &mut UniqueEntitySlice { - self - } -} - -impl Borrow> for UniqueEntityArray { - fn borrow(&self) -> &UniqueEntitySlice { - self - } -} - -impl BorrowMut> - for UniqueEntityArray +impl AsRef> + for UniqueEntityEquivalentArray { - fn borrow_mut(&mut self) -> &mut UniqueEntitySlice { + fn as_ref(&self) -> &UniqueEntityEquivalentSlice { + self + } +} + +impl AsMut> + for UniqueEntityEquivalentArray +{ + fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { + self + } +} + +impl Borrow> + for UniqueEntityEquivalentArray +{ + fn borrow(&self) -> &UniqueEntityEquivalentSlice { + self + } +} + +impl BorrowMut> + for UniqueEntityEquivalentArray +{ + fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } } impl Index<(Bound, Bound)> - for UniqueEntityArray + for UniqueEntityEquivalentArray { - type Output = UniqueEntitySlice; + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityArray { - type Output = UniqueEntitySlice; +impl Index> + for UniqueEntityEquivalentArray +{ + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: Range) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityArray { - type Output = UniqueEntitySlice; +impl Index> + for UniqueEntityEquivalentArray +{ + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index for UniqueEntityArray { - type Output = UniqueEntitySlice; +impl Index for UniqueEntityEquivalentArray { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityArray { - type Output = UniqueEntitySlice; +impl Index> + for UniqueEntityEquivalentArray +{ + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityArray { - type Output = UniqueEntitySlice; +impl Index> + for UniqueEntityEquivalentArray +{ + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } impl Index> - for UniqueEntityArray + for UniqueEntityEquivalentArray { - type Output = UniqueEntitySlice; + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index for UniqueEntityArray { +impl Index for UniqueEntityEquivalentArray { type Output = T; fn index(&self, key: usize) -> &T { self.0.index(key) @@ -269,248 +292,268 @@ impl Index for UniqueEntityArray IndexMut<(Bound, Bound)> - for UniqueEntityArray + for UniqueEntityEquivalentArray { fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityArray { +impl IndexMut> + for UniqueEntityEquivalentArray +{ fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityArray { +impl IndexMut> + for UniqueEntityEquivalentArray +{ fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut for UniqueEntityArray { +impl IndexMut + for UniqueEntityEquivalentArray +{ fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> - for UniqueEntityArray + for UniqueEntityEquivalentArray { fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityArray { +impl IndexMut> + for UniqueEntityEquivalentArray +{ fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } impl IndexMut> - for UniqueEntityArray + for UniqueEntityEquivalentArray { fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl From<&[T; 1]> for UniqueEntityArray<1, T> { +impl From<&[T; 1]> for UniqueEntityEquivalentArray { fn from(value: &[T; 1]) -> Self { Self(value.clone()) } } -impl From<&[T; 0]> for UniqueEntityArray<0, T> { +impl From<&[T; 0]> for UniqueEntityEquivalentArray { fn from(value: &[T; 0]) -> Self { Self(value.clone()) } } -impl From<&mut [T; 1]> for UniqueEntityArray<1, T> { +impl From<&mut [T; 1]> for UniqueEntityEquivalentArray { fn from(value: &mut [T; 1]) -> Self { Self(value.clone()) } } -impl From<&mut [T; 0]> for UniqueEntityArray<0, T> { +impl From<&mut [T; 0]> for UniqueEntityEquivalentArray { fn from(value: &mut [T; 0]) -> Self { Self(value.clone()) } } -impl From<[T; 1]> for UniqueEntityArray<1, T> { +impl From<[T; 1]> for UniqueEntityEquivalentArray { fn from(value: [T; 1]) -> Self { Self(value) } } -impl From<[T; 0]> for UniqueEntityArray<0, T> { +impl From<[T; 0]> for UniqueEntityEquivalentArray { fn from(value: [T; 0]) -> Self { Self(value) } } -impl From> for (T,) { - fn from(array: UniqueEntityArray<1, T>) -> Self { +impl From> for (T,) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T) { - fn from(array: UniqueEntityArray<2, T>) -> Self { +impl From> for (T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T) { - fn from(array: UniqueEntityArray<3, T>) -> Self { +impl From> for (T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T) { - fn from(array: UniqueEntityArray<4, T>) -> Self { +impl From> for (T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T) { - fn from(array: UniqueEntityArray<5, T>) -> Self { +impl From> for (T, T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<6, T>) -> Self { +impl From> for (T, T, T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<7, T>) -> Self { +impl From> for (T, T, T, T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<8, T>) -> Self { +impl From> for (T, T, T, T, T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<9, T>) -> Self { +impl From> for (T, T, T, T, T, T, T, T, T) { + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<10, T>) -> Self { +impl From> + for (T, T, T, T, T, T, T, T, T, T) +{ + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<11, T>) -> Self { +impl From> + for (T, T, T, T, T, T, T, T, T, T, T) +{ + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for (T, T, T, T, T, T, T, T, T, T, T, T) { - fn from(array: UniqueEntityArray<12, T>) -> Self { +impl From> + for (T, T, T, T, T, T, T, T, T, T, T, T) +{ + fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } -impl From> for BTreeSet { - fn from(value: UniqueEntityArray) -> Self { +impl From> + for BTreeSet +{ + fn from(value: UniqueEntityEquivalentArray) -> Self { BTreeSet::from(value.0) } } -impl From> for BinaryHeap { - fn from(value: UniqueEntityArray) -> Self { +impl From> + for BinaryHeap +{ + fn from(value: UniqueEntityEquivalentArray) -> Self { BinaryHeap::from(value.0) } } -impl From> for LinkedList { - fn from(value: UniqueEntityArray) -> Self { +impl From> + for LinkedList +{ + fn from(value: UniqueEntityEquivalentArray) -> Self { LinkedList::from(value.0) } } -impl From> for Vec { - fn from(value: UniqueEntityArray) -> Self { +impl From> for Vec { + fn from(value: UniqueEntityEquivalentArray) -> Self { Vec::from(value.0) } } -impl From> for VecDeque { - fn from(value: UniqueEntityArray) -> Self { +impl From> for VecDeque { + fn from(value: UniqueEntityEquivalentArray) -> Self { VecDeque::from(value.0) } } impl, U: EntityEquivalent, const N: usize> - PartialEq<&UniqueEntitySlice> for UniqueEntityArray + PartialEq<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentArray { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.as_inner()) } } impl, U: EntityEquivalent, const N: usize> - PartialEq> for UniqueEntityArray + PartialEq> for UniqueEntityEquivalentArray { - fn eq(&self, other: &UniqueEntitySlice) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.0.eq(other.as_inner()) } } -impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityArray> - for Vec +impl, U: EntityEquivalent, const N: usize> + PartialEq<&UniqueEntityEquivalentArray> for Vec { - fn eq(&self, other: &&UniqueEntityArray) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityArray> - for VecDeque +impl, U: EntityEquivalent, const N: usize> + PartialEq<&UniqueEntityEquivalentArray> for VecDeque { - fn eq(&self, other: &&UniqueEntityArray) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq<&mut UniqueEntityArray> - for VecDeque +impl, U: EntityEquivalent, const N: usize> + PartialEq<&mut UniqueEntityEquivalentArray> for VecDeque { - fn eq(&self, other: &&mut UniqueEntityArray) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq> - for Vec +impl, U: EntityEquivalent, const N: usize> + PartialEq> for Vec { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq> - for VecDeque +impl, U: EntityEquivalent, const N: usize> + PartialEq> for VecDeque { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } } @@ -525,16 +568,20 @@ impl UniqueEntityIter /// yet. /// /// Equivalent to [`array::IntoIter::as_slice`]. - pub fn as_slice(&self) -> &UniqueEntitySlice { + pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } } /// Returns a mutable slice of all elements that have not been yielded yet. /// /// Equivalent to [`array::IntoIter::as_mut_slice`]. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { + pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.as_mut_inner().as_mut_slice()) } + unsafe { + UniqueEntityEquivalentSlice::from_slice_unchecked_mut( + self.as_mut_inner().as_mut_slice(), + ) + } } } diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index d4a7186a7c..6672d761cc 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -25,36 +25,43 @@ use alloc::{ use bevy_platform_support::sync::Arc; use super::{ - unique_vec::{self, UniqueEntityVec}, + unique_vec::{self, UniqueEntityEquivalentVec}, Entity, EntityEquivalent, EntitySet, EntitySetIterator, FromEntitySetIterator, - UniqueEntityArray, UniqueEntityIter, + UniqueEntityEquivalentArray, UniqueEntityIter, }; /// A slice that contains only unique entities. /// -/// It can be obtained by slicing [`UniqueEntityVec`]. +/// This can be obtained by slicing [`UniqueEntityEquivalentVec`]. +/// +/// When `T` is [`Entity`], use [`UniqueEntitySlice`]. #[repr(transparent)] #[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct UniqueEntitySlice([T]); +pub struct UniqueEntityEquivalentSlice([T]); -impl UniqueEntitySlice { - /// Constructs a `UniqueEntitySlice` from a [`&[T]`] unsafely. +/// A slice that contains only unique [`Entity`]. +/// +/// This is the default case of a [`UniqueEntityEquivalentSlice`]. +pub type UniqueEntitySlice = UniqueEntityEquivalentSlice; + +impl UniqueEntityEquivalentSlice { + /// Constructs a `UniqueEntityEquivalentSlice` from a [`&[T]`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub const unsafe fn from_slice_unchecked(slice: &[T]) -> &Self { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { &*(ptr::from_ref(slice) as *const Self) } } - /// Constructs a `UniqueEntitySlice` from a [`&mut [T]`] unsafely. + /// Constructs a `UniqueEntityEquivalentSlice` from a [`&mut [T]`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub const unsafe fn from_slice_unchecked_mut(slice: &mut [T]) -> &mut Self { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { &mut *(ptr::from_mut(slice) as *mut Self) } } @@ -63,51 +70,51 @@ impl UniqueEntitySlice { &self.0 } - /// Constructs a `UniqueEntitySlice` from a [`Box<[T]>`] unsafely. + /// Constructs a `UniqueEntityEquivalentSlice` from a [`Box<[T]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_boxed_slice_unchecked(slice: Box<[T]>) -> Box { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) } } /// Casts `self` to the inner slice. pub fn into_boxed_inner(self: Box) -> Box<[T]> { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Box::from_raw(Box::into_raw(self) as *mut [T]) } } - /// Constructs a `UniqueEntitySlice` from a [`Arc<[T]>`] unsafely. + /// Constructs a `UniqueEntityEquivalentSlice` from a [`Arc<[T]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_arc_slice_unchecked(slice: Arc<[T]>) -> Arc { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Arc::from_raw(Arc::into_raw(slice) as *mut Self) } } /// Casts `self` to the inner slice. pub fn into_arc_inner(this: Arc) -> Arc<[T]> { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Arc::from_raw(Arc::into_raw(this) as *mut [T]) } } - // Constructs a `UniqueEntitySlice` from a [`Rc<[T]>`] unsafely. + // Constructs a `UniqueEntityEquivalentSlice` from a [`Rc<[T]>`] unsafely. /// /// # Safety /// /// `slice` must contain only unique elements. pub unsafe fn from_rc_slice_unchecked(slice: Rc<[T]>) -> Rc { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Rc::from_raw(Rc::into_raw(slice) as *mut Self) } } /// Casts `self` to the inner slice. pub fn into_rc_inner(self: Rc) -> Rc<[T]> { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Rc::from_raw(Rc::into_raw(self) as *mut [T]) } } @@ -136,12 +143,12 @@ impl UniqueEntitySlice { /// Returns an array reference to the first `N` items in the slice. /// /// Equivalent to [`[T]::first_chunk`](slice::first_chunk). - pub const fn first_chunk(&self) -> Option<&UniqueEntityArray> { + pub const fn first_chunk(&self) -> Option<&UniqueEntityEquivalentArray> { let Some(chunk) = self.0.first_chunk() else { return None; }; // SAFETY: All elements in the original slice are unique. - Some(unsafe { UniqueEntityArray::from_array_ref_unchecked(chunk) }) + Some(unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk) }) } /// Returns an array reference to the first `N` items in the slice and the remaining slice. @@ -149,14 +156,17 @@ impl UniqueEntitySlice { /// Equivalent to [`[T]::split_first_chunk`](slice::split_first_chunk). pub const fn split_first_chunk( &self, - ) -> Option<(&UniqueEntityArray, &UniqueEntitySlice)> { + ) -> Option<( + &UniqueEntityEquivalentArray, + &UniqueEntityEquivalentSlice, + )> { let Some((chunk, rest)) = self.0.split_first_chunk() else { return None; }; // SAFETY: All elements in the original slice are unique. unsafe { Some(( - UniqueEntityArray::from_array_ref_unchecked(chunk), + UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk), Self::from_slice_unchecked(rest), )) } @@ -167,7 +177,10 @@ impl UniqueEntitySlice { /// Equivalent to [`[T]::split_last_chunk`](slice::split_last_chunk). pub const fn split_last_chunk( &self, - ) -> Option<(&UniqueEntitySlice, &UniqueEntityArray)> { + ) -> Option<( + &UniqueEntityEquivalentSlice, + &UniqueEntityEquivalentArray, + )> { let Some((rest, chunk)) = self.0.split_last_chunk() else { return None; }; @@ -175,7 +188,7 @@ impl UniqueEntitySlice { unsafe { Some(( Self::from_slice_unchecked(rest), - UniqueEntityArray::from_array_ref_unchecked(chunk), + UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk), )) } } @@ -183,12 +196,12 @@ impl UniqueEntitySlice { /// Returns an array reference to the last `N` items in the slice. /// /// Equivalent to [`[T]::last_chunk`](slice::last_chunk). - pub const fn last_chunk(&self) -> Option<&UniqueEntityArray> { + pub const fn last_chunk(&self) -> Option<&UniqueEntityEquivalentArray> { let Some(chunk) = self.0.last_chunk() else { return None; }; // SAFETY: All elements in the original slice are unique. - Some(unsafe { UniqueEntityArray::from_array_ref_unchecked(chunk) }) + Some(unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(chunk) }) } /// Returns a reference to a subslice. @@ -212,7 +225,7 @@ impl UniqueEntitySlice { /// /// Equivalent to the range functionality of [`[T]::get_mut`]. /// - /// Note that `UniqueEntitySlice::get_mut` cannot be called with a [`usize`]. + /// Note that `UniqueEntityEquivalentSlice::get_mut` cannot be called with a [`usize`]. /// /// [`[T]::get_mut`]: `slice::get_mut`s pub fn get_mut(&mut self, index: I) -> Option<&mut Self> @@ -248,7 +261,7 @@ impl UniqueEntitySlice { /// /// Equivalent to the range functionality of [`[T]::get_unchecked_mut`]. /// - /// Note that `UniqueEntitySlice::get_unchecked_mut` cannot be called with an index. + /// Note that `UniqueEntityEquivalentSlice::get_unchecked_mut` cannot be called with an index. /// /// # Safety /// @@ -298,7 +311,9 @@ impl UniqueEntitySlice { /// [`[T]::windows`]: `slice::windows` pub fn windows(&self, size: usize) -> Windows<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.windows(size)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.windows(size)) + } } /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the @@ -309,7 +324,11 @@ impl UniqueEntitySlice { /// [`[T]::chunks`]: `slice::chunks` pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.chunks(chunk_size)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + self.0.chunks(chunk_size), + ) + } } /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the @@ -321,7 +340,7 @@ impl UniqueEntitySlice { pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( self.0.chunks_mut(chunk_size), ) } @@ -335,7 +354,9 @@ impl UniqueEntitySlice { pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.chunks_exact(chunk_size)) + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + self.0.chunks_exact(chunk_size), + ) } } @@ -348,7 +369,7 @@ impl UniqueEntitySlice { pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( self.0.chunks_exact_mut(chunk_size), ) } @@ -362,7 +383,11 @@ impl UniqueEntitySlice { /// [`[T]::rchunks`]: `slice::rchunks` pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.rchunks(chunk_size)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + self.0.rchunks(chunk_size), + ) + } } /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the end @@ -374,7 +399,7 @@ impl UniqueEntitySlice { pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( self.0.rchunks_mut(chunk_size), ) } @@ -389,7 +414,9 @@ impl UniqueEntitySlice { pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.rchunks_exact(chunk_size)) + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + self.0.rchunks_exact(chunk_size), + ) } } @@ -402,7 +429,7 @@ impl UniqueEntitySlice { pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( self.0.rchunks_exact_mut(chunk_size), ) } @@ -419,7 +446,9 @@ impl UniqueEntitySlice { F: FnMut(&T, &T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.chunk_by(pred)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.chunk_by(pred)) + } } /// Returns an iterator over the slice producing non-overlapping mutable @@ -434,7 +463,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked(self.0.chunk_by_mut(pred)) + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + self.0.chunk_by_mut(pred), + ) } } @@ -553,7 +584,9 @@ impl UniqueEntitySlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.split(pred)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.split(pred)) + } } /// Returns an iterator over mutable subslices separated by elements that @@ -568,7 +601,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked(self.0.split_mut(pred)) + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + self.0.split_mut(pred), + ) } } @@ -584,7 +619,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.split_inclusive(pred)) + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + self.0.split_inclusive(pred), + ) } } @@ -600,7 +637,7 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( self.0.split_inclusive_mut(pred), ) } @@ -617,7 +654,9 @@ impl UniqueEntitySlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.rsplit(pred)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplit(pred)) + } } /// Returns an iterator over mutable subslices separated by elements that @@ -633,7 +672,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked(self.0.rsplit_mut(pred)) + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + self.0.rsplit_mut(pred), + ) } } @@ -648,7 +689,9 @@ impl UniqueEntitySlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.splitn(n, pred)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.splitn(n, pred)) + } } /// Returns an iterator over mutable subslices separated by elements that match @@ -663,7 +706,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked(self.0.splitn_mut(n, pred)) + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + self.0.splitn_mut(n, pred), + ) } } @@ -678,7 +723,9 @@ impl UniqueEntitySlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { UniqueEntitySliceIter::from_slice_iterator_unchecked(self.0.rsplitn(n, pred)) } + unsafe { + UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplitn(n, pred)) + } } /// Returns an iterator over subslices separated by elements that match @@ -693,7 +740,9 @@ impl UniqueEntitySlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntitySliceIterMut::from_mut_slice_iterator_unchecked(self.0.rsplitn_mut(n, pred)) + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + self.0.rsplitn_mut(n, pred), + ) } } @@ -790,40 +839,40 @@ impl UniqueEntitySlice { self.0.sort_by_cached_key(f); } - /// Copies self into a new `UniqueEntityVec`. - pub fn to_vec(&self) -> UniqueEntityVec + /// Copies self into a new `UniqueEntityEquivalentVec`. + pub fn to_vec(&self) -> UniqueEntityEquivalentVec where T: Clone, { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityVec::from_vec_unchecked(self.0.to_vec()) } + unsafe { UniqueEntityEquivalentVec::from_vec_unchecked(self.0.to_vec()) } } /// Converts `self` into a vector without clones or allocation. /// /// Equivalent to [`[T]::into_vec`](slice::into_vec). - pub fn into_vec(self: Box) -> UniqueEntityVec { + pub fn into_vec(self: Box) -> UniqueEntityEquivalentVec { // SAFETY: // This matches the implementation of `slice::into_vec`. // All elements in the original slice are unique. unsafe { let len = self.len(); let vec = Vec::from_raw_parts(Box::into_raw(self).cast::(), len, len); - UniqueEntityVec::from_vec_unchecked(vec) + UniqueEntityEquivalentVec::from_vec_unchecked(vec) } } } /// Converts a reference to T into a slice of length 1 (without copying). -pub const fn from_ref(s: &T) -> &UniqueEntitySlice { +pub const fn from_ref(s: &T) -> &UniqueEntityEquivalentSlice { // SAFETY: A slice with a length of 1 is always unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(slice::from_ref(s)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice::from_ref(s)) } } /// Converts a reference to T into a slice of length 1 (without copying). -pub const fn from_mut(s: &mut T) -> &mut UniqueEntitySlice { +pub const fn from_mut(s: &mut T) -> &mut UniqueEntityEquivalentSlice { // SAFETY: A slice with a length of 1 is always unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(slice::from_mut(s)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice::from_mut(s)) } } /// Forms a slice from a pointer and a length. @@ -837,9 +886,9 @@ pub const fn from_mut(s: &mut T) -> &mut UniqueEntitySlice< pub const unsafe fn from_raw_parts<'a, T: EntityEquivalent>( data: *const T, len: usize, -) -> &'a UniqueEntitySlice { +) -> &'a UniqueEntityEquivalentSlice { // SAFETY: The safety contract is upheld by the caller. - unsafe { UniqueEntitySlice::from_slice_unchecked(slice::from_raw_parts(data, len)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice::from_raw_parts(data, len)) } } /// Performs the same functionality as [`from_raw_parts`], except that a mutable slice is returned. @@ -853,48 +902,50 @@ pub const unsafe fn from_raw_parts<'a, T: EntityEquivalent>( pub const unsafe fn from_raw_parts_mut<'a, T: EntityEquivalent>( data: *mut T, len: usize, -) -> &'a mut UniqueEntitySlice { +) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: The safety contract is upheld by the caller. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(slice::from_raw_parts_mut(data, len)) } + unsafe { + UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice::from_raw_parts_mut(data, len)) + } } -/// Casts a slice of entity slices to a slice of [`UniqueEntitySlice`]s. +/// Casts a slice of entity slices to a slice of [`UniqueEntityEquivalentSlice`]s. /// /// # Safety /// /// All elements in each of the casted slices must be unique. pub unsafe fn cast_slice_of_unique_entity_slice<'a, 'b, T: EntityEquivalent + 'a>( slice: &'b [&'a [T]], -) -> &'b [&'a UniqueEntitySlice] { +) -> &'b [&'a UniqueEntityEquivalentSlice] { // SAFETY: All elements in the original iterator are unique slices. - unsafe { &*(ptr::from_ref(slice) as *const [&UniqueEntitySlice]) } + unsafe { &*(ptr::from_ref(slice) as *const [&UniqueEntityEquivalentSlice]) } } -/// Casts a mutable slice of entity slices to a slice of [`UniqueEntitySlice`]s. +/// Casts a mutable slice of entity slices to a slice of [`UniqueEntityEquivalentSlice`]s. /// /// # Safety /// /// All elements in each of the casted slices must be unique. pub unsafe fn cast_slice_of_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent + 'a>( slice: &'b mut [&'a [T]], -) -> &'b mut [&'a UniqueEntitySlice] { +) -> &'b mut [&'a UniqueEntityEquivalentSlice] { // SAFETY: All elements in the original iterator are unique slices. - unsafe { &mut *(ptr::from_mut(slice) as *mut [&UniqueEntitySlice]) } + unsafe { &mut *(ptr::from_mut(slice) as *mut [&UniqueEntityEquivalentSlice]) } } -/// Casts a mutable slice of mutable entity slices to a slice of mutable [`UniqueEntitySlice`]s. +/// Casts a mutable slice of mutable entity slices to a slice of mutable [`UniqueEntityEquivalentSlice`]s. /// /// # Safety /// /// All elements in each of the casted slices must be unique. pub unsafe fn cast_slice_of_mut_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent + 'a>( slice: &'b mut [&'a mut [T]], -) -> &'b mut [&'a mut UniqueEntitySlice] { +) -> &'b mut [&'a mut UniqueEntityEquivalentSlice] { // SAFETY: All elements in the original iterator are unique slices. - unsafe { &mut *(ptr::from_mut(slice) as *mut [&mut UniqueEntitySlice]) } + unsafe { &mut *(ptr::from_mut(slice) as *mut [&mut UniqueEntityEquivalentSlice]) } } -impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntitySlice { +impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentSlice { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -904,7 +955,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntitySlice { } } -impl<'a, T: EntityEquivalent> IntoIterator for &'a Box> { +impl<'a, T: EntityEquivalent> IntoIterator for &'a Box> { type Item = &'a T; type IntoIter = Iter<'a, T>; @@ -914,7 +965,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a Box> { } } -impl IntoIterator for Box> { +impl IntoIterator for Box> { type Item = T; type IntoIter = unique_vec::IntoIter; @@ -924,7 +975,7 @@ impl IntoIterator for Box> { } } -impl Deref for UniqueEntitySlice { +impl Deref for UniqueEntityEquivalentSlice { type Target = [T]; fn deref(&self) -> &Self::Target { @@ -932,99 +983,107 @@ impl Deref for UniqueEntitySlice { } } -impl AsRef<[T]> for UniqueEntitySlice { +impl AsRef<[T]> for UniqueEntityEquivalentSlice { fn as_ref(&self) -> &[T] { self } } -impl AsRef for UniqueEntitySlice { +impl AsRef for UniqueEntityEquivalentSlice { fn as_ref(&self) -> &Self { self } } -impl AsMut for UniqueEntitySlice { +impl AsMut for UniqueEntityEquivalentSlice { fn as_mut(&mut self) -> &mut Self { self } } -impl Borrow<[T]> for UniqueEntitySlice { +impl Borrow<[T]> for UniqueEntityEquivalentSlice { fn borrow(&self) -> &[T] { self } } -impl Clone for Box> { +impl Clone for Box> { fn clone(&self) -> Self { self.to_vec().into_boxed_slice() } } -impl Default for &UniqueEntitySlice { +impl Default for &UniqueEntityEquivalentSlice { fn default() -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(Default::default()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(Default::default()) } } } -impl Default for &mut UniqueEntitySlice { +impl Default for &mut UniqueEntityEquivalentSlice { fn default() -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(Default::default()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(Default::default()) } } } -impl Default for Box> { +impl Default for Box> { fn default() -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_boxed_slice_unchecked(Default::default()) } + unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(Default::default()) } } } -impl From<&UniqueEntitySlice> for Box> { - fn from(value: &UniqueEntitySlice) -> Self { - // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_boxed_slice_unchecked(value.0.into()) } - } -} - -impl From<&UniqueEntitySlice> for Arc> { - fn from(value: &UniqueEntitySlice) -> Self { - // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_arc_slice_unchecked(value.0.into()) } - } -} - -impl From<&UniqueEntitySlice> for Rc> { - fn from(value: &UniqueEntitySlice) -> Self { - // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_rc_slice_unchecked(value.0.into()) } - } -} - -impl<'a, T: EntityEquivalent + Clone> From<&'a UniqueEntitySlice> - for Cow<'a, UniqueEntitySlice> +impl From<&UniqueEntityEquivalentSlice> + for Box> { - fn from(value: &'a UniqueEntitySlice) -> Self { + fn from(value: &UniqueEntityEquivalentSlice) -> Self { + // SAFETY: All elements in the original slice are unique. + unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(value.0.into()) } + } +} + +impl From<&UniqueEntityEquivalentSlice> + for Arc> +{ + fn from(value: &UniqueEntityEquivalentSlice) -> Self { + // SAFETY: All elements in the original slice are unique. + unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(value.0.into()) } + } +} + +impl From<&UniqueEntityEquivalentSlice> + for Rc> +{ + fn from(value: &UniqueEntityEquivalentSlice) -> Self { + // SAFETY: All elements in the original slice are unique. + unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(value.0.into()) } + } +} + +impl<'a, T: EntityEquivalent + Clone> From<&'a UniqueEntityEquivalentSlice> + for Cow<'a, UniqueEntityEquivalentSlice> +{ + fn from(value: &'a UniqueEntityEquivalentSlice) -> Self { Cow::Borrowed(value) } } -impl From> - for Box> +impl From> + for Box> { - fn from(value: UniqueEntityArray) -> Self { + fn from(value: UniqueEntityEquivalentArray) -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_boxed_slice_unchecked(Box::new(value.into_inner())) } + unsafe { + UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(Box::new(value.into_inner())) + } } } -impl<'a, T: EntityEquivalent + Clone> From>> - for Box> +impl<'a, T: EntityEquivalent + Clone> From>> + for Box> { - fn from(value: Cow<'a, UniqueEntitySlice>) -> Self { + fn from(value: Cow<'a, UniqueEntityEquivalentSlice>) -> Self { match value { Cow::Borrowed(slice) => Box::from(slice), Cow::Owned(slice) => Box::from(slice), @@ -1032,158 +1091,166 @@ impl<'a, T: EntityEquivalent + Clone> From>> } } -impl From> for Box> { - fn from(value: UniqueEntityVec) -> Self { +impl From> + for Box> +{ + fn from(value: UniqueEntityEquivalentVec) -> Self { value.into_boxed_slice() } } -impl FromIterator for Box> { +impl FromIterator for Box> { fn from_iter>(iter: I) -> Self { iter.into_iter() - .collect::>() + .collect::>() .into_boxed_slice() } } -impl FromEntitySetIterator for Box> { +impl FromEntitySetIterator for Box> { fn from_entity_set_iter>(iter: I) -> Self { iter.into_iter() - .collect_set::>() + .collect_set::>() .into_boxed_slice() } } -impl, U: EntityEquivalent> PartialEq> - for &UniqueEntitySlice +impl, U: EntityEquivalent> + PartialEq> for &UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityVec) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } } -impl, U: EntityEquivalent> PartialEq> - for &mut UniqueEntitySlice +impl, U: EntityEquivalent> + PartialEq> for &mut UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityVec) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } } -impl, U: EntityEquivalent> PartialEq> - for UniqueEntitySlice +impl, U: EntityEquivalent> + PartialEq> for UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityVec) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } } -impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntitySlice> - for [T; N] +impl, U: EntityEquivalent, const N: usize> + PartialEq<&UniqueEntityEquivalentSlice> for [T; N] { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl + Clone, U: EntityEquivalent> PartialEq<&UniqueEntitySlice> +impl + Clone, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for Cow<'_, [T]> { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } } impl + Clone, U: EntityEquivalent> - PartialEq<&UniqueEntitySlice> for Cow<'_, UniqueEntitySlice> + PartialEq<&UniqueEntityEquivalentSlice> for Cow<'_, UniqueEntityEquivalentSlice> { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq<&UniqueEntitySlice> for Vec { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { +impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for Vec { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq<&UniqueEntitySlice> for VecDeque { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { +impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> + for VecDeque +{ + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq<&mut UniqueEntitySlice> - for [T; N] +impl, U: EntityEquivalent, const N: usize> + PartialEq<&mut UniqueEntityEquivalentSlice> for [T; N] { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntitySlice> +impl + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for Cow<'_, [T]> { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&&**other) } } impl + Clone, U: EntityEquivalent> - PartialEq<&mut UniqueEntitySlice> for Cow<'_, UniqueEntitySlice> + PartialEq<&mut UniqueEntityEquivalentSlice> for Cow<'_, UniqueEntityEquivalentSlice> { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.0) } } -impl + Clone, U: EntityEquivalent> PartialEq> - for Cow<'_, UniqueEntitySlice> +impl + Clone, U: EntityEquivalent> + PartialEq> for Cow<'_, UniqueEntityEquivalentSlice> { - fn eq(&self, other: &UniqueEntityVec) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } } -impl, U: EntityEquivalent> PartialEq<&mut UniqueEntitySlice> for Vec { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { +impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> + for Vec +{ + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq<&mut UniqueEntitySlice> for VecDeque { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { +impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> + for VecDeque +{ + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } } -impl, U: EntityEquivalent> PartialEq> - for [T] +impl, U: EntityEquivalent> + PartialEq> for [T] { - fn eq(&self, other: &UniqueEntitySlice) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent, const N: usize> PartialEq> +impl, U: EntityEquivalent, const N: usize> PartialEq> for [T; N] { - fn eq(&self, other: &UniqueEntitySlice) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq> - for Vec +impl, U: EntityEquivalent> + PartialEq> for Vec { - fn eq(&self, other: &UniqueEntitySlice) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } } impl, U, const N: usize> PartialEq<[U; N]> - for &UniqueEntitySlice + for &UniqueEntityEquivalentSlice { fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) @@ -1191,7 +1258,7 @@ impl, U, const N: usize> PartialEq<[U; N]> } impl, U, const N: usize> PartialEq<[U; N]> - for &mut UniqueEntitySlice + for &mut UniqueEntityEquivalentSlice { fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) @@ -1199,7 +1266,7 @@ impl, U, const N: usize> PartialEq<[U; N]> } impl, U, const N: usize> PartialEq<[U; N]> - for UniqueEntitySlice + for UniqueEntityEquivalentSlice { fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) @@ -1207,89 +1274,91 @@ impl, U, const N: usize> PartialEq<[U; N]> } impl, U: EntityEquivalent, const N: usize> - PartialEq> for &UniqueEntitySlice + PartialEq> for &UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } } impl, U: EntityEquivalent, const N: usize> - PartialEq> for &mut UniqueEntitySlice + PartialEq> for &mut UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } } impl, U: EntityEquivalent, const N: usize> - PartialEq> for UniqueEntitySlice + PartialEq> for UniqueEntityEquivalentSlice { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } } -impl, U> PartialEq> for &UniqueEntitySlice { +impl, U> PartialEq> for &UniqueEntityEquivalentSlice { fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } -impl, U> PartialEq> for &mut UniqueEntitySlice { +impl, U> PartialEq> + for &mut UniqueEntityEquivalentSlice +{ fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } -impl, U> PartialEq> for UniqueEntitySlice { +impl, U> PartialEq> for UniqueEntityEquivalentSlice { fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } -impl ToOwned for UniqueEntitySlice { - type Owned = UniqueEntityVec; +impl ToOwned for UniqueEntityEquivalentSlice { + type Owned = UniqueEntityEquivalentVec; fn to_owned(&self) -> Self::Owned { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityVec::from_vec_unchecked(self.0.to_owned()) } + unsafe { UniqueEntityEquivalentVec::from_vec_unchecked(self.0.to_owned()) } } } -impl<'a, T: EntityEquivalent + Copy, const N: usize> TryFrom<&'a UniqueEntitySlice> - for &'a UniqueEntityArray +impl<'a, T: EntityEquivalent + Copy, const N: usize> TryFrom<&'a UniqueEntityEquivalentSlice> + for &'a UniqueEntityEquivalentArray { type Error = TryFromSliceError; - fn try_from(value: &'a UniqueEntitySlice) -> Result { + fn try_from(value: &'a UniqueEntityEquivalentSlice) -> Result { <&[T; N]>::try_from(&value.0).map(|array| // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityArray::from_array_ref_unchecked(array) }) + unsafe { UniqueEntityEquivalentArray::from_array_ref_unchecked(array) }) } } -impl TryFrom<&UniqueEntitySlice> - for UniqueEntityArray +impl TryFrom<&UniqueEntityEquivalentSlice> + for UniqueEntityEquivalentArray { type Error = TryFromSliceError; - fn try_from(value: &UniqueEntitySlice) -> Result { + fn try_from(value: &UniqueEntityEquivalentSlice) -> Result { <&Self>::try_from(value).copied() } } -impl TryFrom<&mut UniqueEntitySlice> - for UniqueEntityArray +impl TryFrom<&mut UniqueEntityEquivalentSlice> + for UniqueEntityEquivalentArray { type Error = TryFromSliceError; - fn try_from(value: &mut UniqueEntitySlice) -> Result { + fn try_from(value: &mut UniqueEntityEquivalentSlice) -> Result { ::try_from(&*value) } } -impl Index<(Bound, Bound)> for UniqueEntitySlice { +impl Index<(Bound, Bound)> for UniqueEntityEquivalentSlice { type Output = Self; fn index(&self, key: (Bound, Bound)) -> &Self { // SAFETY: All elements in the original slice are unique. @@ -1297,7 +1366,7 @@ impl Index<(Bound, Bound)> for UniqueEntitySl } } -impl Index> for UniqueEntitySlice { +impl Index> for UniqueEntityEquivalentSlice { type Output = Self; fn index(&self, key: Range) -> &Self { // SAFETY: All elements in the original slice are unique. @@ -1305,7 +1374,7 @@ impl Index> for UniqueEntitySlice { } } -impl Index> for UniqueEntitySlice { +impl Index> for UniqueEntityEquivalentSlice { type Output = Self; fn index(&self, key: RangeFrom) -> &Self { // SAFETY: All elements in the original slice are unique. @@ -1313,7 +1382,7 @@ impl Index> for UniqueEntitySlice { } } -impl Index for UniqueEntitySlice { +impl Index for UniqueEntityEquivalentSlice { type Output = Self; fn index(&self, key: RangeFull) -> &Self { // SAFETY: All elements in the original slice are unique. @@ -1321,31 +1390,31 @@ impl Index for UniqueEntitySlice { } } -impl Index> for UniqueEntitySlice { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentSlice { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeInclusive) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntitySlice { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentSlice { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeTo) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntitySlice { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentSlice { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeToInclusive) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } } } -impl Index for UniqueEntitySlice { +impl Index for UniqueEntityEquivalentSlice { type Output = T; fn index(&self, index: usize) -> &T { @@ -1353,49 +1422,51 @@ impl Index for UniqueEntitySlice { } } -impl IndexMut<(Bound, Bound)> for UniqueEntitySlice { +impl IndexMut<(Bound, Bound)> + for UniqueEntityEquivalentSlice +{ fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntitySlice { +impl IndexMut> for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: Range) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntitySlice { +impl IndexMut> for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: RangeFrom) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut for UniqueEntitySlice { +impl IndexMut for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: RangeFull) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntitySlice { +impl IndexMut> for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: RangeInclusive) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntitySlice { +impl IndexMut> for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: RangeTo) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntitySlice { +impl IndexMut> for UniqueEntityEquivalentSlice { fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1404,19 +1475,19 @@ impl IndexMut> for UniqueEntitySlic /// Immutable slice iterator. /// -/// This struct is created by [`iter`] method on [`UniqueEntitySlice`] and -/// the [`IntoIterator`] impls on it and [`UniqueEntityVec`]. +/// This struct is created by [`iter`] method on [`UniqueEntityEquivalentSlice`] and +/// the [`IntoIterator`] impls on it and [`UniqueEntityEquivalentVec`]. /// -/// [`iter`]: `UniqueEntitySlice::iter` +/// [`iter`]: `UniqueEntityEquivalentSlice::iter` pub type Iter<'a, T> = UniqueEntityIter>; impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Views the underlying data as a subslice of the original data. /// /// Equivalent to [`slice::Iter::as_slice`]. - pub fn as_slice(&self) -> &'a UniqueEntitySlice { + pub fn as_slice(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } } } @@ -1427,29 +1498,37 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Views the underlying data as a mutable subslice of the original data. /// /// Equivalent to [`slice::IterMut::into_slice`]. - pub fn into_slice(self) -> &'a mut UniqueEntitySlice { + pub fn into_slice(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.into_inner().into_slice()) } + unsafe { + UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.into_inner().into_slice()) + } } /// Views the underlying data as a subslice of the original data. /// /// Equivalent to [`slice::IterMut::as_slice`]. - pub fn as_slice(&self) -> &UniqueEntitySlice { + pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } } } -/// An iterator that yields `&UniqueEntitySlice`. Note that an entity may appear +/// An iterator that yields `&UniqueEntityEquivalentSlice`. Note that an entity may appear /// in multiple slices, depending on the wrapped iterator. #[derive(Debug)] -pub struct UniqueEntitySliceIter<'a, T: EntityEquivalent + 'a, I: Iterator> { +pub struct UniqueEntityEquivalentSliceIter< + 'a, + T: EntityEquivalent + 'a, + I: Iterator, +> { pub(crate) iter: I, } -impl<'a, T: EntityEquivalent + 'a, I: Iterator> UniqueEntitySliceIter<'a, T, I> { - /// Constructs a [`UniqueEntitySliceIter`] from a slice iterator unsafely. +impl<'a, T: EntityEquivalent + 'a, I: Iterator> + UniqueEntityEquivalentSliceIter<'a, T, I> +{ + /// Constructs a [`UniqueEntityEquivalentSliceIter`] from a slice iterator unsafely. /// /// # Safety /// @@ -1480,14 +1559,14 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> UniqueEntitySlic } impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator - for UniqueEntitySliceIter<'a, T, I> + for UniqueEntityEquivalentSliceIter<'a, T, I> { - type Item = &'a UniqueEntitySlice; + type Item = &'a UniqueEntityEquivalentSlice; fn next(&mut self) -> Option { self.iter.next().map(|slice| // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked(slice) }) + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice) }) } fn size_hint(&self) -> (usize, Option) { @@ -1496,29 +1575,29 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator } impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator> ExactSizeIterator - for UniqueEntitySliceIter<'a, T, I> + for UniqueEntityEquivalentSliceIter<'a, T, I> { } impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator> DoubleEndedIterator - for UniqueEntitySliceIter<'a, T, I> + for UniqueEntityEquivalentSliceIter<'a, T, I> { fn next_back(&mut self) -> Option { self.iter.next_back().map(|slice| // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked(slice) }) + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice) }) } } impl<'a, T: EntityEquivalent + 'a, I: FusedIterator> FusedIterator - for UniqueEntitySliceIter<'a, T, I> + for UniqueEntityEquivalentSliceIter<'a, T, I> { } impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsRef<[&'a [T]]>> - AsRef<[&'a UniqueEntitySlice]> for UniqueEntitySliceIter<'a, T, I> + AsRef<[&'a UniqueEntityEquivalentSlice]> for UniqueEntityEquivalentSliceIter<'a, T, I> { - fn as_ref(&self) -> &[&'a UniqueEntitySlice] { + fn as_ref(&self) -> &[&'a UniqueEntityEquivalentSlice] { // SAFETY: unsafe { cast_slice_of_unique_entity_slice(self.iter.as_ref()) } } @@ -1526,103 +1605,113 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsRef<[&'a [T]] /// An iterator over overlapping subslices of length `size`. /// -/// This struct is created by [`UniqueEntitySlice::windows`]. -pub type Windows<'a, T = Entity> = UniqueEntitySliceIter<'a, T, slice::Windows<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::windows`]. +pub type Windows<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Windows<'a, T>>; /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a /// time), starting at the beginning of the slice. /// -/// This struct is created by [`UniqueEntitySlice::chunks`]. -pub type Chunks<'a, T = Entity> = UniqueEntitySliceIter<'a, T, slice::Chunks<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::chunks`]. +pub type Chunks<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Chunks<'a, T>>; /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a /// time), starting at the beginning of the slice. /// -/// This struct is created by [`UniqueEntitySlice::chunks_exact`]. -pub type ChunksExact<'a, T = Entity> = UniqueEntitySliceIter<'a, T, slice::ChunksExact<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_exact`]. +pub type ChunksExact<'a, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::ChunksExact<'a, T>>; -impl<'a, T: EntityEquivalent> UniqueEntitySliceIter<'a, T, slice::ChunksExact<'a, T>> { +impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::ChunksExact<'a, T>> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. /// /// Equivalent to [`slice::ChunksExact::remainder`]. - pub fn remainder(&self) -> &'a UniqueEntitySlice { + pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.iter.remainder()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) } } } /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a /// time), starting at the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rchunks`]. -pub type RChunks<'a, T = Entity> = UniqueEntitySliceIter<'a, T, slice::RChunks<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks`]. +pub type RChunks<'a, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::RChunks<'a, T>>; /// An iterator over a slice in (non-overlapping) chunks (`chunk_size` elements at a /// time), starting at the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rchunks_exact`]. -pub type RChunksExact<'a, T = Entity> = UniqueEntitySliceIter<'a, T, slice::RChunksExact<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_exact`]. +pub type RChunksExact<'a, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::RChunksExact<'a, T>>; -impl<'a, T: EntityEquivalent> UniqueEntitySliceIter<'a, T, slice::RChunksExact<'a, T>> { +impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::RChunksExact<'a, T>> { /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. /// /// Equivalent to [`slice::RChunksExact::remainder`]. - pub fn remainder(&self) -> &'a UniqueEntitySlice { + pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.iter.remainder()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) } } } /// An iterator over slice in (non-overlapping) chunks separated by a predicate. /// -/// This struct is created by [`UniqueEntitySlice::chunk_by`]. -pub type ChunkBy<'a, P, T = Entity> = UniqueEntitySliceIter<'a, T, slice::ChunkBy<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::chunk_by`]. +pub type ChunkBy<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::ChunkBy<'a, T, P>>; /// An iterator over subslices separated by elements that match a predicate /// function. /// -/// This struct is created by [`UniqueEntitySlice::split`]. -pub type Split<'a, P, T = Entity> = UniqueEntitySliceIter<'a, T, slice::Split<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::split`]. +pub type Split<'a, P, T = Entity> = UniqueEntityEquivalentSliceIter<'a, T, slice::Split<'a, T, P>>; /// An iterator over subslices separated by elements that match a predicate /// function. /// -/// This struct is created by [`UniqueEntitySlice::split_inclusive`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::split_inclusive`]. pub type SplitInclusive<'a, P, T = Entity> = - UniqueEntitySliceIter<'a, T, slice::SplitInclusive<'a, T, P>>; + UniqueEntityEquivalentSliceIter<'a, T, slice::SplitInclusive<'a, T, P>>; /// An iterator over subslices separated by elements that match a predicate /// function, starting from the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rsplit`]. -pub type RSplit<'a, P, T = Entity> = UniqueEntitySliceIter<'a, T, slice::RSplit<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rsplit`]. +pub type RSplit<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::RSplit<'a, T, P>>; /// An iterator over subslices separated by elements that match a predicate /// function, limited to a given number of splits. /// -/// This struct is created by [`UniqueEntitySlice::splitn`]. -pub type SplitN<'a, P, T = Entity> = UniqueEntitySliceIter<'a, T, slice::SplitN<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::splitn`]. +pub type SplitN<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::SplitN<'a, T, P>>; /// An iterator over subslices separated by elements that match a /// predicate function, limited to a given number of splits, starting /// from the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rsplitn`]. -pub type RSplitN<'a, P, T = Entity> = UniqueEntitySliceIter<'a, T, slice::RSplitN<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rsplitn`]. +pub type RSplitN<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIter<'a, T, slice::RSplitN<'a, T, P>>; -/// An iterator that yields `&mut UniqueEntitySlice`. Note that an entity may appear +/// An iterator that yields `&mut UniqueEntityEquivalentSlice`. Note that an entity may appear /// in multiple slices, depending on the wrapped iterator. #[derive(Debug)] -pub struct UniqueEntitySliceIterMut<'a, T: EntityEquivalent + 'a, I: Iterator> { +pub struct UniqueEntityEquivalentSliceIterMut< + 'a, + T: EntityEquivalent + 'a, + I: Iterator, +> { pub(crate) iter: I, } impl<'a, T: EntityEquivalent + 'a, I: Iterator> - UniqueEntitySliceIterMut<'a, T, I> + UniqueEntityEquivalentSliceIterMut<'a, T, I> { - /// Constructs a [`UniqueEntitySliceIterMut`] from a mutable slice iterator unsafely. + /// Constructs a [`UniqueEntityEquivalentSliceIterMut`] from a mutable slice iterator unsafely. /// /// # Safety /// @@ -1653,14 +1742,14 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> } impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator - for UniqueEntitySliceIterMut<'a, T, I> + for UniqueEntityEquivalentSliceIterMut<'a, T, I> { - type Item = &'a mut UniqueEntitySlice; + type Item = &'a mut UniqueEntityEquivalentSlice; fn next(&mut self) -> Option { self.iter.next().map(|slice| // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(slice) }) + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice) }) } fn size_hint(&self) -> (usize, Option) { @@ -1669,38 +1758,39 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator } impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator> ExactSizeIterator - for UniqueEntitySliceIterMut<'a, T, I> + for UniqueEntityEquivalentSliceIterMut<'a, T, I> { } impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator> DoubleEndedIterator - for UniqueEntitySliceIterMut<'a, T, I> + for UniqueEntityEquivalentSliceIterMut<'a, T, I> { fn next_back(&mut self) -> Option { self.iter.next_back().map(|slice| // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(slice) }) + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice) }) } } impl<'a, T: EntityEquivalent + 'a, I: FusedIterator> FusedIterator - for UniqueEntitySliceIterMut<'a, T, I> + for UniqueEntityEquivalentSliceIterMut<'a, T, I> { } impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsRef<[&'a [T]]>> - AsRef<[&'a UniqueEntitySlice]> for UniqueEntitySliceIterMut<'a, T, I> + AsRef<[&'a UniqueEntityEquivalentSlice]> for UniqueEntityEquivalentSliceIterMut<'a, T, I> { - fn as_ref(&self) -> &[&'a UniqueEntitySlice] { + fn as_ref(&self) -> &[&'a UniqueEntityEquivalentSlice] { // SAFETY: All elements in the original iterator are unique slices. unsafe { cast_slice_of_unique_entity_slice(self.iter.as_ref()) } } } impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsMut<[&'a mut [T]]>> - AsMut<[&'a mut UniqueEntitySlice]> for UniqueEntitySliceIterMut<'a, T, I> + AsMut<[&'a mut UniqueEntityEquivalentSlice]> + for UniqueEntityEquivalentSliceIterMut<'a, T, I> { - fn as_mut(&mut self) -> &mut [&'a mut UniqueEntitySlice] { + fn as_mut(&mut self) -> &mut [&'a mut UniqueEntityEquivalentSlice] { // SAFETY: All elements in the original iterator are unique slices. unsafe { cast_slice_of_mut_unique_entity_slice_mut(self.iter.as_mut()) } } @@ -1709,88 +1799,97 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsMut<[&'a /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size` /// elements at a time), starting at the beginning of the slice. /// -/// This struct is created by [`UniqueEntitySlice::chunks_mut`]. -pub type ChunksMut<'a, T = Entity> = UniqueEntitySliceIterMut<'a, T, slice::ChunksMut<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_mut`]. +pub type ChunksMut<'a, T = Entity> = + UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksMut<'a, T>>; /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size` /// elements at a time), starting at the beginning of the slice. /// -/// This struct is created by [`UniqueEntitySlice::chunks_exact_mut`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::chunks_exact_mut`]. pub type ChunksExactMut<'a, T = Entity> = - UniqueEntitySliceIterMut<'a, T, slice::ChunksExactMut<'a, T>>; + UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksExactMut<'a, T>>; -impl<'a, T: EntityEquivalent> UniqueEntitySliceIterMut<'a, T, slice::ChunksExactMut<'a, T>> { +impl<'a, T: EntityEquivalent> + UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunksExactMut<'a, T>> +{ /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. /// /// Equivalent to [`slice::ChunksExactMut::into_remainder`]. - pub fn into_remainder(self) -> &'a mut UniqueEntitySlice { + pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.iter.into_remainder()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) } } } /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size` /// elements at a time), starting at the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rchunks_mut`]. -pub type RChunksMut<'a, T = Entity> = UniqueEntitySliceIterMut<'a, T, slice::RChunksMut<'a, T>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_mut`]. +pub type RChunksMut<'a, T = Entity> = + UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksMut<'a, T>>; /// An iterator over a slice in (non-overlapping) mutable chunks (`chunk_size` /// elements at a time), starting at the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rchunks_exact_mut`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::rchunks_exact_mut`]. pub type RChunksExactMut<'a, T = Entity> = - UniqueEntitySliceIterMut<'a, T, slice::RChunksExactMut<'a, T>>; + UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksExactMut<'a, T>>; -impl<'a, T: EntityEquivalent> UniqueEntitySliceIterMut<'a, T, slice::RChunksExactMut<'a, T>> { +impl<'a, T: EntityEquivalent> + UniqueEntityEquivalentSliceIterMut<'a, T, slice::RChunksExactMut<'a, T>> +{ /// Returns the remainder of the original slice that is not going to be /// returned by the iterator. /// /// Equivalent to [`slice::RChunksExactMut::into_remainder`]. - pub fn into_remainder(self) -> &'a mut UniqueEntitySlice { + pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.iter.into_remainder()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) } } } /// An iterator over slice in (non-overlapping) mutable chunks separated /// by a predicate. /// -/// This struct is created by [`UniqueEntitySlice::chunk_by_mut`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::chunk_by_mut`]. pub type ChunkByMut<'a, P, T = Entity> = - UniqueEntitySliceIterMut<'a, T, slice::ChunkByMut<'a, T, P>>; + UniqueEntityEquivalentSliceIterMut<'a, T, slice::ChunkByMut<'a, T, P>>; /// An iterator over the mutable subslices of the vector which are separated /// by elements that match `pred`. /// -/// This struct is created by [`UniqueEntitySlice::split_mut`]. -pub type SplitMut<'a, P, T = Entity> = UniqueEntitySliceIterMut<'a, T, slice::SplitMut<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::split_mut`]. +pub type SplitMut<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitMut<'a, T, P>>; /// An iterator over the mutable subslices of the vector which are separated /// by elements that match `pred`. Unlike `SplitMut`, it contains the matched /// parts in the ends of the subslices. /// -/// This struct is created by [`UniqueEntitySlice::split_inclusive_mut`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::split_inclusive_mut`]. pub type SplitInclusiveMut<'a, P, T = Entity> = - UniqueEntitySliceIterMut<'a, T, slice::SplitInclusiveMut<'a, T, P>>; + UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitInclusiveMut<'a, T, P>>; /// An iterator over the subslices of the vector which are separated /// by elements that match `pred`, starting from the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rsplit_mut`]. -pub type RSplitMut<'a, P, T = Entity> = UniqueEntitySliceIterMut<'a, T, slice::RSplitMut<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::rsplit_mut`]. +pub type RSplitMut<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIterMut<'a, T, slice::RSplitMut<'a, T, P>>; /// An iterator over subslices separated by elements that match a predicate /// function, limited to a given number of splits. /// -/// This struct is created by [`UniqueEntitySlice::splitn_mut`]. -pub type SplitNMut<'a, P, T = Entity> = UniqueEntitySliceIterMut<'a, T, slice::SplitNMut<'a, T, P>>; +/// This struct is created by [`UniqueEntityEquivalentSlice::splitn_mut`]. +pub type SplitNMut<'a, P, T = Entity> = + UniqueEntityEquivalentSliceIterMut<'a, T, slice::SplitNMut<'a, T, P>>; /// An iterator over subslices separated by elements that match a /// predicate function, limited to a given number of splits, starting /// from the end of the slice. /// -/// This struct is created by [`UniqueEntitySlice::rsplitn_mut`]. +/// This struct is created by [`UniqueEntityEquivalentSlice::rsplitn_mut`]. pub type RSplitNMut<'a, P, T = Entity> = - UniqueEntitySliceIterMut<'a, T, slice::RSplitNMut<'a, T, P>>; + UniqueEntityEquivalentSliceIterMut<'a, T, slice::RSplitNMut<'a, T, P>>; diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index 4c4816b9ba..a397777087 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -20,8 +20,8 @@ use alloc::{ use bevy_platform_support::sync::Arc; use super::{ - unique_slice::{self, UniqueEntitySlice}, - Entity, EntityEquivalent, EntitySet, FromEntitySetIterator, UniqueEntityArray, + unique_slice::{self, UniqueEntityEquivalentSlice}, + Entity, EntityEquivalent, EntitySet, FromEntitySetIterator, UniqueEntityEquivalentArray, UniqueEntityIter, }; @@ -31,29 +31,36 @@ use super::{ /// This is always true when less than 2 entities are present. /// /// This type is best obtained by its `FromEntitySetIterator` impl, via either -/// `EntityIterator::collect_set` or `UniqueEntityVec::from_entity_iter`. +/// `EntityIterator::collect_set` or `UniqueEntityEquivalentVec::from_entity_iter`. /// /// While this type can be constructed via `Iterator::collect`, doing so is inefficient, /// and not recommended. +/// +/// When `T` is [`Entity`], use the [`UniqueEntityVec`] alias. #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct UniqueEntityVec(Vec); +pub struct UniqueEntityEquivalentVec(Vec); -impl UniqueEntityVec { - /// Constructs a new, empty `UniqueEntityVec`. +/// A `Vec` that contains only unique [`Entity`]. +/// +/// This is the default case of a [`UniqueEntityEquivalentVec`]. +pub type UniqueEntityVec = UniqueEntityEquivalentVec; + +impl UniqueEntityEquivalentVec { + /// Constructs a new, empty `UniqueEntityEquivalentVec`. /// /// Equivalent to [`Vec::new`]. pub const fn new() -> Self { Self(Vec::new()) } - /// Constructs a new, empty `UniqueEntityVec` with at least the specified capacity. + /// Constructs a new, empty `UniqueEntityEquivalentVec` with at least the specified capacity. /// /// Equivalent to [`Vec::with_capacity`] pub fn with_capacity(capacity: usize) -> Self { Self(Vec::with_capacity(capacity)) } - /// Creates a `UniqueEntityVec` directly from a pointer, a length, and a capacity. + /// Creates a `UniqueEntityEquivalentVec` directly from a pointer, a length, and a capacity. /// /// Equivalent to [`Vec::from_raw_parts`]. /// @@ -66,7 +73,7 @@ impl UniqueEntityVec { Self(unsafe { Vec::from_raw_parts(ptr, length, capacity) }) } - /// Constructs a `UniqueEntityVec` from a [`Vec`] unsafely. + /// Constructs a `UniqueEntityEquivalentVec` from a [`Vec`] unsafely. /// /// # Safety /// @@ -112,7 +119,7 @@ impl UniqueEntityVec { } /// Reserves the minimum capacity for at least `additional` more elements to - /// be inserted in the given `UniqueEntityVec`. + /// be inserted in the given `UniqueEntityEquivalentVec`. /// /// Equivalent to [`Vec::reserve_exact`]. pub fn reserve_exact(&mut self, additional: usize) { @@ -149,19 +156,21 @@ impl UniqueEntityVec { self.0.shrink_to(min_capacity); } - /// Converts the vector into `Box>`. - pub fn into_boxed_slice(self) -> Box> { - // SAFETY: UniqueEntitySlice is a transparent wrapper around [T]. - unsafe { UniqueEntitySlice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) } + /// Converts the vector into `Box>`. + pub fn into_boxed_slice(self) -> Box> { + // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. + unsafe { + UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) + } } /// Extracts a slice containing the entire vector. - pub fn as_slice(&self) -> &UniqueEntitySlice { + pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { self } /// Extracts a mutable slice of the entire vector. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { + pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -301,7 +310,7 @@ impl UniqueEntityVec { /// # Safety /// /// `other` must contain no elements that equal any element in `self`. - pub unsafe fn append(&mut self, other: &mut UniqueEntityVec) { + pub unsafe fn append(&mut self, other: &mut UniqueEntityEquivalentVec) { self.0.append(&mut other.0); } @@ -368,10 +377,10 @@ impl UniqueEntityVec { self.0.resize_with(new_len, f); } - /// Consumes and leaks the Vec, returning a mutable reference to the contents, `&'a mut UniqueEntitySlice`. - pub fn leak<'a>(self) -> &'a mut UniqueEntitySlice { + /// Consumes and leaks the Vec, returning a mutable reference to the contents, `&'a mut UniqueEntityEquivalentSlice`. + pub fn leak<'a>(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.leak()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.leak()) } } /// Returns the remaining spare capacity of the vector as a slice of @@ -405,29 +414,29 @@ impl UniqueEntityVec { } } -impl Default for UniqueEntityVec { +impl Default for UniqueEntityEquivalentVec { fn default() -> Self { Self(Vec::default()) } } -impl Deref for UniqueEntityVec { - type Target = UniqueEntitySlice; +impl Deref for UniqueEntityEquivalentVec { + type Target = UniqueEntityEquivalentSlice; fn deref(&self) -> &Self::Target { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(&self.0) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } } } -impl DerefMut for UniqueEntityVec { +impl DerefMut for UniqueEntityEquivalentVec { fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(&mut self.0) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } } } -impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityVec +impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentVec where &'a T: EntityEquivalent, { @@ -441,7 +450,7 @@ where } } -impl IntoIterator for UniqueEntityVec { +impl IntoIterator for UniqueEntityEquivalentVec { type Item = T; type IntoIter = IntoIter; @@ -452,102 +461,104 @@ impl IntoIterator for UniqueEntityVec { } } -impl AsMut for UniqueEntityVec { - fn as_mut(&mut self) -> &mut UniqueEntityVec { +impl AsMut for UniqueEntityEquivalentVec { + fn as_mut(&mut self) -> &mut UniqueEntityEquivalentVec { self } } -impl AsMut> for UniqueEntityVec { - fn as_mut(&mut self) -> &mut UniqueEntitySlice { +impl AsMut> for UniqueEntityEquivalentVec { + fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } } -impl AsRef for UniqueEntityVec { +impl AsRef for UniqueEntityEquivalentVec { fn as_ref(&self) -> &Self { self } } -impl AsRef> for UniqueEntityVec { +impl AsRef> for UniqueEntityEquivalentVec { fn as_ref(&self) -> &Vec { &self.0 } } -impl Borrow> for UniqueEntityVec { +impl Borrow> for UniqueEntityEquivalentVec { fn borrow(&self) -> &Vec { &self.0 } } -impl AsRef<[T]> for UniqueEntityVec { +impl AsRef<[T]> for UniqueEntityEquivalentVec { fn as_ref(&self) -> &[T] { &self.0 } } -impl AsRef> for UniqueEntityVec { - fn as_ref(&self) -> &UniqueEntitySlice { +impl AsRef> for UniqueEntityEquivalentVec { + fn as_ref(&self) -> &UniqueEntityEquivalentSlice { self } } -impl Borrow<[T]> for UniqueEntityVec { +impl Borrow<[T]> for UniqueEntityEquivalentVec { fn borrow(&self) -> &[T] { &self.0 } } -impl Borrow> for UniqueEntityVec { - fn borrow(&self) -> &UniqueEntitySlice { +impl Borrow> for UniqueEntityEquivalentVec { + fn borrow(&self) -> &UniqueEntityEquivalentSlice { self } } -impl BorrowMut> for UniqueEntityVec { - fn borrow_mut(&mut self) -> &mut UniqueEntitySlice { +impl BorrowMut> + for UniqueEntityEquivalentVec +{ + fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } } -impl, U> PartialEq> for UniqueEntityVec { +impl, U> PartialEq> for UniqueEntityEquivalentVec { fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } -impl, U> PartialEq<&[U]> for UniqueEntityVec { +impl, U> PartialEq<&[U]> for UniqueEntityEquivalentVec { fn eq(&self, other: &&[U]) -> bool { self.0.eq(other) } } -impl, U: EntityEquivalent> PartialEq<&UniqueEntitySlice> - for UniqueEntityVec +impl, U: EntityEquivalent> + PartialEq<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { - fn eq(&self, other: &&UniqueEntitySlice) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(other) } } -impl, U> PartialEq<&mut [U]> for UniqueEntityVec { +impl, U> PartialEq<&mut [U]> for UniqueEntityEquivalentVec { fn eq(&self, other: &&mut [U]) -> bool { self.0.eq(other) } } -impl, U: EntityEquivalent> PartialEq<&mut UniqueEntitySlice> - for UniqueEntityVec +impl, U: EntityEquivalent> + PartialEq<&mut UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { - fn eq(&self, other: &&mut UniqueEntitySlice) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.0.eq(other) } } impl, U, const N: usize> PartialEq<&[U; N]> - for UniqueEntityVec + for UniqueEntityEquivalentVec { fn eq(&self, other: &&[U; N]) -> bool { self.0.eq(other) @@ -555,15 +566,15 @@ impl, U, const N: usize> PartialEq<&[U; N]> } impl, U: EntityEquivalent, const N: usize> - PartialEq<&UniqueEntityArray> for UniqueEntityVec + PartialEq<&UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { - fn eq(&self, other: &&UniqueEntityArray) -> bool { + fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.as_inner()) } } impl, U, const N: usize> PartialEq<&mut [U; N]> - for UniqueEntityVec + for UniqueEntityEquivalentVec { fn eq(&self, other: &&mut [U; N]) -> bool { self.0.eq(&**other) @@ -571,29 +582,29 @@ impl, U, const N: usize> PartialEq<&mut [U; N } impl, U: EntityEquivalent, const N: usize> - PartialEq<&mut UniqueEntityArray> for UniqueEntityVec + PartialEq<&mut UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { - fn eq(&self, other: &&mut UniqueEntityArray) -> bool { + fn eq(&self, other: &&mut UniqueEntityEquivalentArray) -> bool { self.0.eq(other.as_inner()) } } -impl, U> PartialEq<[U]> for UniqueEntityVec { +impl, U> PartialEq<[U]> for UniqueEntityEquivalentVec { fn eq(&self, other: &[U]) -> bool { self.0.eq(other) } } -impl, U: EntityEquivalent> PartialEq> - for UniqueEntityVec +impl, U: EntityEquivalent> + PartialEq> for UniqueEntityEquivalentVec { - fn eq(&self, other: &UniqueEntitySlice) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.0.eq(&**other) } } impl, U, const N: usize> PartialEq<[U; N]> - for UniqueEntityVec + for UniqueEntityEquivalentVec { fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) @@ -601,247 +612,266 @@ impl, U, const N: usize> PartialEq<[U; N]> } impl, U: EntityEquivalent, const N: usize> - PartialEq> for UniqueEntityVec + PartialEq> for UniqueEntityEquivalentVec { - fn eq(&self, other: &UniqueEntityArray) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(other.as_inner()) } } -impl, U: EntityEquivalent> PartialEq> for Vec { - fn eq(&self, other: &UniqueEntityVec) -> bool { +impl, U: EntityEquivalent> PartialEq> for Vec { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq> for &[T] { - fn eq(&self, other: &UniqueEntityVec) -> bool { +impl, U: EntityEquivalent> PartialEq> for &[T] { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq> for &mut [T] { - fn eq(&self, other: &UniqueEntityVec) -> bool { +impl, U: EntityEquivalent> PartialEq> for &mut [T] { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq> - for [T] +impl, U: EntityEquivalent> + PartialEq> for [T] { - fn eq(&self, other: &UniqueEntityVec) -> bool { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl + Clone, U: EntityEquivalent> PartialEq> for Cow<'_, [T]> { - fn eq(&self, other: &UniqueEntityVec) -> bool { +impl + Clone, U: EntityEquivalent> PartialEq> + for Cow<'_, [T]> +{ + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl, U: EntityEquivalent> PartialEq> for VecDeque { - fn eq(&self, other: &UniqueEntityVec) -> bool { +impl, U: EntityEquivalent> PartialEq> for VecDeque { + fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } -impl From<&UniqueEntitySlice> for UniqueEntityVec { - fn from(value: &UniqueEntitySlice) -> Self { +impl From<&UniqueEntityEquivalentSlice> + for UniqueEntityEquivalentVec +{ + fn from(value: &UniqueEntityEquivalentSlice) -> Self { value.to_vec() } } -impl From<&mut UniqueEntitySlice> for UniqueEntityVec { - fn from(value: &mut UniqueEntitySlice) -> Self { +impl From<&mut UniqueEntityEquivalentSlice> + for UniqueEntityEquivalentVec +{ + fn from(value: &mut UniqueEntityEquivalentSlice) -> Self { value.to_vec() } } -impl From>> for UniqueEntityVec { - fn from(value: Box>) -> Self { +impl From>> + for UniqueEntityEquivalentVec +{ + fn from(value: Box>) -> Self { value.into_vec() } } -impl From>> for UniqueEntityVec +impl From>> + for UniqueEntityEquivalentVec where - UniqueEntitySlice: ToOwned>, + UniqueEntityEquivalentSlice: ToOwned>, { - fn from(value: Cow>) -> Self { + fn from(value: Cow>) -> Self { value.into_owned() } } -impl From<&[T; 1]> for UniqueEntityVec { +impl From<&[T; 1]> for UniqueEntityEquivalentVec { fn from(value: &[T; 1]) -> Self { Self(Vec::from(value)) } } -impl From<&[T; 0]> for UniqueEntityVec { +impl From<&[T; 0]> for UniqueEntityEquivalentVec { fn from(value: &[T; 0]) -> Self { Self(Vec::from(value)) } } -impl From<&mut [T; 1]> for UniqueEntityVec { +impl From<&mut [T; 1]> for UniqueEntityEquivalentVec { fn from(value: &mut [T; 1]) -> Self { Self(Vec::from(value)) } } -impl From<&mut [T; 0]> for UniqueEntityVec { +impl From<&mut [T; 0]> for UniqueEntityEquivalentVec { fn from(value: &mut [T; 0]) -> Self { Self(Vec::from(value)) } } -impl From<[T; 1]> for UniqueEntityVec { +impl From<[T; 1]> for UniqueEntityEquivalentVec { fn from(value: [T; 1]) -> Self { Self(Vec::from(value)) } } -impl From<[T; 0]> for UniqueEntityVec { +impl From<[T; 0]> for UniqueEntityEquivalentVec { fn from(value: [T; 0]) -> Self { Self(Vec::from(value)) } } -impl From<&UniqueEntityArray> - for UniqueEntityVec +impl From<&UniqueEntityEquivalentArray> + for UniqueEntityEquivalentVec { - fn from(value: &UniqueEntityArray) -> Self { + fn from(value: &UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.as_inner().clone())) } } -impl From<&mut UniqueEntityArray> - for UniqueEntityVec +impl From<&mut UniqueEntityEquivalentArray> + for UniqueEntityEquivalentVec { - fn from(value: &mut UniqueEntityArray) -> Self { + fn from(value: &mut UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.as_inner().clone())) } } -impl From> for UniqueEntityVec { - fn from(value: UniqueEntityArray) -> Self { +impl From> + for UniqueEntityEquivalentVec +{ + fn from(value: UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.into_inner())) } } -impl From> for Vec { - fn from(value: UniqueEntityVec) -> Self { +impl From> for Vec { + fn from(value: UniqueEntityEquivalentVec) -> Self { value.0 } } -impl<'a, T: EntityEquivalent + Clone> From> for Cow<'a, [T]> { - fn from(value: UniqueEntityVec) -> Self { +impl<'a, T: EntityEquivalent + Clone> From> for Cow<'a, [T]> { + fn from(value: UniqueEntityEquivalentVec) -> Self { Cow::from(value.0) } } -impl<'a, T: EntityEquivalent + Clone> From> for Cow<'a, UniqueEntitySlice> { - fn from(value: UniqueEntityVec) -> Self { +impl<'a, T: EntityEquivalent + Clone> From> + for Cow<'a, UniqueEntityEquivalentSlice> +{ + fn from(value: UniqueEntityEquivalentVec) -> Self { Cow::Owned(value) } } -impl From> for Arc<[T]> { - fn from(value: UniqueEntityVec) -> Self { +impl From> for Arc<[T]> { + fn from(value: UniqueEntityEquivalentVec) -> Self { Arc::from(value.0) } } -impl From> for Arc> { - fn from(value: UniqueEntityVec) -> Self { +impl From> + for Arc> +{ + fn from(value: UniqueEntityEquivalentVec) -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_arc_slice_unchecked(Arc::from(value.0)) } + unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(Arc::from(value.0)) } } } -impl From> for BinaryHeap { - fn from(value: UniqueEntityVec) -> Self { +impl From> for BinaryHeap { + fn from(value: UniqueEntityEquivalentVec) -> Self { BinaryHeap::from(value.0) } } -impl From> for Box<[T]> { - fn from(value: UniqueEntityVec) -> Self { +impl From> for Box<[T]> { + fn from(value: UniqueEntityEquivalentVec) -> Self { Box::from(value.0) } } -impl From> for Rc<[T]> { - fn from(value: UniqueEntityVec) -> Self { +impl From> for Rc<[T]> { + fn from(value: UniqueEntityEquivalentVec) -> Self { Rc::from(value.0) } } -impl From> for Rc> { - fn from(value: UniqueEntityVec) -> Self { +impl From> + for Rc> +{ + fn from(value: UniqueEntityEquivalentVec) -> Self { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_rc_slice_unchecked(Rc::from(value.0)) } + unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(Rc::from(value.0)) } } } -impl From> for VecDeque { - fn from(value: UniqueEntityVec) -> Self { +impl From> for VecDeque { + fn from(value: UniqueEntityEquivalentVec) -> Self { VecDeque::from(value.0) } } -impl TryFrom> for Box<[T; N]> { - type Error = UniqueEntityVec; +impl TryFrom> for Box<[T; N]> { + type Error = UniqueEntityEquivalentVec; - fn try_from(value: UniqueEntityVec) -> Result { - Box::try_from(value.0).map_err(UniqueEntityVec) + fn try_from(value: UniqueEntityEquivalentVec) -> Result { + Box::try_from(value.0).map_err(UniqueEntityEquivalentVec) } } -impl TryFrom> - for Box> +impl TryFrom> + for Box> { - type Error = UniqueEntityVec; + type Error = UniqueEntityEquivalentVec; - fn try_from(value: UniqueEntityVec) -> Result { + fn try_from(value: UniqueEntityEquivalentVec) -> Result { Box::try_from(value.0) .map(|v| // SAFETY: All elements in the original Vec are unique. - unsafe { UniqueEntityArray::from_boxed_array_unchecked(v) }) - .map_err(UniqueEntityVec) + unsafe { UniqueEntityEquivalentArray::from_boxed_array_unchecked(v) }) + .map_err(UniqueEntityEquivalentVec) } } -impl TryFrom> for [T; N] { - type Error = UniqueEntityVec; +impl TryFrom> for [T; N] { + type Error = UniqueEntityEquivalentVec; - fn try_from(value: UniqueEntityVec) -> Result { - <[T; N] as TryFrom>>::try_from(value.0).map_err(UniqueEntityVec) + fn try_from(value: UniqueEntityEquivalentVec) -> Result { + <[T; N] as TryFrom>>::try_from(value.0).map_err(UniqueEntityEquivalentVec) } } -impl TryFrom> for UniqueEntityArray { - type Error = UniqueEntityVec; +impl TryFrom> + for UniqueEntityEquivalentArray +{ + type Error = UniqueEntityEquivalentVec; - fn try_from(value: UniqueEntityVec) -> Result { + fn try_from(value: UniqueEntityEquivalentVec) -> Result { <[T; N] as TryFrom>>::try_from(value.0) .map(|v| // SAFETY: All elements in the original Vec are unique. - unsafe { UniqueEntityArray::from_array_unchecked(v) }) - .map_err(UniqueEntityVec) + unsafe { UniqueEntityEquivalentArray::from_array_unchecked(v) }) + .map_err(UniqueEntityEquivalentVec) } } -impl From> for UniqueEntityVec { +impl From> for UniqueEntityEquivalentVec { fn from(value: BTreeSet) -> Self { Self(value.into_iter().collect::>()) } } -impl FromIterator for UniqueEntityVec { +impl FromIterator for UniqueEntityEquivalentVec { /// This impl only uses `Eq` to validate uniqueness, resulting in O(n^2) complexity. /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`. /// When possible, use `FromEntitySetIterator::from_entity_iter` instead. @@ -860,14 +890,14 @@ impl FromIterator for UniqueEntityVec { } } -impl FromEntitySetIterator for UniqueEntityVec { +impl FromEntitySetIterator for UniqueEntityEquivalentVec { fn from_entity_set_iter>(iter: I) -> Self { // SAFETY: `iter` is an `EntitySet`. unsafe { Self::from_vec_unchecked(Vec::from_iter(iter)) } } } -impl Extend for UniqueEntityVec { +impl Extend for UniqueEntityEquivalentVec { /// Use with caution, because this impl only uses `Eq` to validate uniqueness, /// resulting in O(n^2) complexity. /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`. @@ -894,7 +924,7 @@ impl Extend for UniqueEntityVec { } } -impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityVec { +impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityEquivalentVec { /// Use with caution, because this impl only uses `Eq` to validate uniqueness, /// resulting in O(n^2) complexity. /// It can make sense for very low N, or if `T` implements neither `Ord` nor `Hash`. @@ -921,145 +951,149 @@ impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityVec { } } -impl Index<(Bound, Bound)> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index<(Bound, Bound)> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: Range) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index> for UniqueEntityVec { - type Output = UniqueEntitySlice; +impl Index> for UniqueEntityEquivalentVec { + type Output = UniqueEntityEquivalentSlice; fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.0.index(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } } } -impl Index for UniqueEntityVec { +impl Index for UniqueEntityEquivalentVec { type Output = T; fn index(&self, key: usize) -> &T { self.0.index(key) } } -impl IndexMut<(Bound, Bound)> for UniqueEntityVec { +impl IndexMut<(Bound, Bound)> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityVec { +impl IndexMut> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityVec { +impl IndexMut> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut for UniqueEntityVec { +impl IndexMut for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityVec { +impl IndexMut> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityVec { +impl IndexMut> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } -impl IndexMut> for UniqueEntityVec { +impl IndexMut> for UniqueEntityEquivalentVec { fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.0.index_mut(key)) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } } } /// An iterator that moves out of a vector. /// /// This `struct` is created by the [`IntoIterator::into_iter`] trait -/// method on [`UniqueEntityVec`]. +/// method on [`UniqueEntityEquivalentVec`]. pub type IntoIter = UniqueEntityIter>; impl UniqueEntityIter> { /// Returns the remaining items of this iterator as a slice. /// /// Equivalent to [`vec::IntoIter::as_slice`]. - pub fn as_slice(&self) -> &UniqueEntitySlice { + pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } } /// Returns the remaining items of this iterator as a mutable slice. /// /// Equivalent to [`vec::IntoIter::as_mut_slice`]. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntitySlice { + pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked_mut(self.as_mut_inner().as_mut_slice()) } + unsafe { + UniqueEntityEquivalentSlice::from_slice_unchecked_mut( + self.as_mut_inner().as_mut_slice(), + ) + } } } -/// A draining iterator for [`UniqueEntityVec`]. +/// A draining iterator for [`UniqueEntityEquivalentVec`]. /// -/// This struct is created by [`UniqueEntityVec::drain`]. +/// This struct is created by [`UniqueEntityEquivalentVec::drain`]. /// See its documentation for more. pub type Drain<'a, T = Entity> = UniqueEntityIter>; @@ -1067,14 +1101,14 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Returns the remaining items of this iterator as a slice. /// /// Equivalent to [`vec::Drain::as_slice`]. - pub fn as_slice(&self) -> &UniqueEntitySlice { + pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntitySlice::from_slice_unchecked(self.as_inner().as_slice()) } + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } } } -/// A splicing iterator for [`UniqueEntityVec`]. +/// A splicing iterator for [`UniqueEntityEquivalentVec`]. /// -/// This struct is created by [`UniqueEntityVec::splice`]. +/// This struct is created by [`UniqueEntityEquivalentVec::splice`]. /// See its documentation for more. pub type Splice<'a, I> = UniqueEntityIter>; diff --git a/crates/bevy_ecs/src/query/par_iter.rs b/crates/bevy_ecs/src/query/par_iter.rs index 2e9824ef87..41a46cf174 100644 --- a/crates/bevy_ecs/src/query/par_iter.rs +++ b/crates/bevy_ecs/src/query/par_iter.rs @@ -1,7 +1,7 @@ use crate::{ batching::BatchingStrategy, component::Tick, - entity::{EntityEquivalent, UniqueEntityVec}, + entity::{EntityEquivalent, UniqueEntityEquivalentVec}, world::unsafe_world_cell::UnsafeWorldCell, }; @@ -318,7 +318,7 @@ pub struct QueryParManyUniqueIter<'w, 's, D: QueryData, F: QueryFilter, E: Entit { pub(crate) world: UnsafeWorldCell<'w>, pub(crate) state: &'s QueryState, - pub(crate) entity_list: UniqueEntityVec, + pub(crate) entity_list: UniqueEntityEquivalentVec, pub(crate) last_run: Tick, pub(crate) this_run: Tick, pub(crate) batching_strategy: BatchingStrategy, @@ -369,7 +369,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter, E: EntityEquivalent + Sync> /// struct T; /// /// #[derive(Resource)] - /// struct V(UniqueEntityVec); + /// struct V(UniqueEntityVec); /// /// impl<'a> IntoIterator for &'a V { /// // ... diff --git a/crates/bevy_ecs/src/query/state.rs b/crates/bevy_ecs/src/query/state.rs index 7a875b6532..e9a00f4646 100644 --- a/crates/bevy_ecs/src/query/state.rs +++ b/crates/bevy_ecs/src/query/state.rs @@ -11,7 +11,7 @@ use crate::{ }; #[cfg(all(not(target_arch = "wasm32"), feature = "multi_threaded"))] -use crate::entity::UniqueEntitySlice; +use crate::entity::UniqueEntityEquivalentSlice; use alloc::vec::Vec; use core::{fmt, ptr}; @@ -1606,7 +1606,7 @@ impl QueryState { &self, init_accum: INIT, world: UnsafeWorldCell<'w>, - entity_list: &UniqueEntitySlice, + entity_list: &UniqueEntityEquivalentSlice, batch_size: usize, mut func: FN, last_run: Tick, diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index 55f771ee96..aa28903c8e 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -1733,7 +1733,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// let mut world = World::new(); /// - /// let entity_set: UniqueEntityVec<_> = world.spawn_batch((0..3).map(A)).collect_set(); + /// let entity_set: UniqueEntityVec = world.spawn_batch((0..3).map(A)).collect_set(); /// let entity_set: UniqueEntityArray<3> = entity_set.try_into().unwrap(); /// /// world.spawn(A(73));