From 513da58cc9d53f1a2176a9ef7ccdb984e264c1b8 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Fri, 10 Oct 2025 18:50:01 +0200 Subject: [PATCH 1/8] privatize entity set type fields --- crates/bevy_ecs/src/entity/hash_map.rs | 8 +++++++- crates/bevy_ecs/src/entity/hash_set.rs | 8 +++++++- crates/bevy_ecs/src/entity/index_map.rs | 2 +- crates/bevy_ecs/src/entity/index_set.rs | 2 +- crates/bevy_ecs/src/entity/unique_slice.rs | 4 ++-- .../src/relationship/relationship_source_collection.rs | 10 ++++------ 6 files changed, 22 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index d83ea7bae1dcc..1cca4f2eeb5d8 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -19,7 +19,7 @@ use super::{Entity, EntityEquivalent, EntityHash, EntitySetIterator}; #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] #[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] #[derive(Debug, Clone, PartialEq, Eq)] -pub struct EntityHashMap(pub(crate) HashMap); +pub struct EntityHashMap(HashMap); impl EntityHashMap { /// Creates an empty `EntityHashMap`. @@ -113,6 +113,12 @@ impl FromIterator<(Entity, V)> for EntityHashMap { } } +impl From> for EntityHashMap { + fn from(value: HashMap) -> Self { + Self(value) + } +} + impl Index<&Q> for EntityHashMap { type Output = V; fn index(&self, key: &Q) -> &V { diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index 7fd1ae9011273..c42da76bacd43 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -22,7 +22,7 @@ use super::{Entity, EntityHash, EntitySet, EntitySetIterator, FromEntitySetItera #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] #[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] #[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct EntityHashSet(pub(crate) HashSet); +pub struct EntityHashSet(HashSet); impl EntityHashSet { /// Creates an empty `EntityHashSet`. @@ -210,6 +210,12 @@ impl FromEntitySetIterator for EntityHashSet { } } +impl From> for EntityHashSet { + fn from(value: HashSet) -> Self { + Self(value) + } +} + /// An iterator over the items of an [`EntityHashSet`]. /// /// This struct is created by the [`iter`] method on [`EntityHashSet`]. See its documentation for more. diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index 9067b12ca28b2..3330d1e58d25a 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -28,7 +28,7 @@ use bevy_platform::prelude::Box; #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] #[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] #[derive(Debug, Clone)] -pub struct EntityIndexMap(pub(crate) IndexMap); +pub struct EntityIndexMap(IndexMap); impl EntityIndexMap { /// Creates an empty `EntityIndexMap`. diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index 42f420a211a23..6741a41c2c679 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -25,7 +25,7 @@ use bevy_platform::prelude::Box; /// An [`IndexSet`] pre-configured to use [`EntityHash`] hashing. #[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))] #[derive(Debug, Clone, Default)] -pub struct EntityIndexSet(pub(crate) IndexSet); +pub struct EntityIndexSet(IndexSet); impl EntityIndexSet { /// Creates an empty `EntityIndexSet`. diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index 26ebe8674f498..9910b1b7befb6 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -1522,7 +1522,7 @@ pub struct UniqueEntityEquivalentSliceIter< T: EntityEquivalent + 'a, I: Iterator, > { - pub(crate) iter: I, + iter: I, } impl<'a, T: EntityEquivalent + 'a, I: Iterator> @@ -1705,7 +1705,7 @@ pub struct UniqueEntityEquivalentSliceIterMut< T: EntityEquivalent + 'a, I: Iterator, > { - pub(crate) iter: I, + iter: I, } impl<'a, T: EntityEquivalent + 'a, I: Iterator> diff --git a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs index 5664fef34068c..4f3fdb2c036c2 100644 --- a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs +++ b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs @@ -242,7 +242,7 @@ impl RelationshipSourceCollection for EntityHashSet { } fn reserve(&mut self, additional: usize) { - self.0.reserve(additional); + self.deref_mut().reserve(additional); } fn with_capacity(capacity: usize) -> Self { @@ -254,9 +254,7 @@ impl RelationshipSourceCollection for EntityHashSet { } fn remove(&mut self, entity: Entity) -> bool { - // We need to call the remove method on the underlying hash set, - // which takes its argument by reference - self.0.remove(&entity) + self.deref_mut().remove(&entity) } fn iter(&self) -> Self::SourceIter<'_> { @@ -268,11 +266,11 @@ impl RelationshipSourceCollection for EntityHashSet { } fn clear(&mut self) { - self.0.clear(); + self.deref_mut().clear(); } fn shrink_to_fit(&mut self) { - self.0.shrink_to_fit(); + self.deref_mut().shrink_to_fit(); } fn extend_from_iter(&mut self, entities: impl IntoIterator) { From 4e08725b6dd917aca64e5d351989f8b7a85bc9c6 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Fri, 10 Oct 2025 20:40:17 +0200 Subject: [PATCH 2/8] entity set constructor consistency --- crates/bevy_ecs/src/entity/entity_set.rs | 29 +++++++-- crates/bevy_ecs/src/entity/hash_map.rs | 27 ++++++++ crates/bevy_ecs/src/entity/hash_set.rs | 49 +++++++++++++++ crates/bevy_ecs/src/entity/index_map.rs | 71 ++++++++++++++++++++++ crates/bevy_ecs/src/entity/index_set.rs | 35 +++++++++++ crates/bevy_ecs/src/entity/unique_array.rs | 5 +- crates/bevy_ecs/src/entity/unique_slice.rs | 64 ++++++++----------- crates/bevy_ecs/src/entity/unique_vec.rs | 30 +++++++-- crates/bevy_transform/src/systems.rs | 6 +- 9 files changed, 264 insertions(+), 52 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index e4860685fe07f..319601b1f9c6f 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -10,7 +10,7 @@ use core::{ fmt::{Debug, Formatter}, hash::{BuildHasher, Hash}, iter::{self, FusedIterator}, - option, result, + option, ptr, result, }; use super::{Entity, UniqueEntityEquivalentSlice}; @@ -358,13 +358,14 @@ impl FromEntitySetIterator /// An iterator that yields unique entities. /// /// This wrapper can provide an [`EntitySetIterator`] implementation when an instance of `I` is known to uphold uniqueness. +#[repr(transparent)] pub struct UniqueEntityIter> { iter: I, } impl UniqueEntityIter { /// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`]. - pub fn from_entity_set_iterator(iter: I) -> Self { + pub fn from_entity_set_iter(iter: I) -> Self { Self { iter } } } @@ -375,10 +376,30 @@ impl> UniqueEntityIter { /// # Safety /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. - pub unsafe fn from_iterator_unchecked(iter: I) -> Self { + pub unsafe fn from_iter_unchecked(iter: I) -> Self { Self { iter } } + /// Constructs a [`UniqueEntityIter`] from an iterator unsafely. + /// + /// # Safety + /// `iter` must only yield unique elements. + /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. + pub unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self { + // SAFETY: UniqueEntityIter is a transparent wrapper around I. + unsafe { &*ptr::from_ref(iter).cast() } + } + + /// Constructs a [`UniqueEntityIter`] from an iterator unsafely. + /// + /// # Safety + /// `iter` must only yield unique elements. + /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. + pub unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self { + // SAFETY: UniqueEntityIter is a transparent wrapper around I. + unsafe { &mut *ptr::from_mut(iter).cast() } + } + /// Returns the inner `I`. pub fn into_inner(self) -> I { self.iter @@ -506,7 +527,7 @@ mod tests { // SAFETY: SpawnBatchIter is `EntitySetIterator`, let mut unique_entity_iter = - unsafe { UniqueEntityIter::from_iterator_unchecked(spawn_batch.iter()) }; + unsafe { UniqueEntityIter::from_iter_unchecked(spawn_batch.iter()) }; let entity_set = unique_entity_iter .by_ref() diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index 1cca4f2eeb5d8..4f767a52d6f4c 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -40,6 +40,11 @@ impl EntityHashMap { Self(HashMap::with_capacity_and_hasher(n, EntityHash)) } + /// Constructs an `EntityHashMap` from an [`HashMap`]. + pub fn from_index_map(set: HashMap) -> Self { + Self(set) + } + /// Returns the inner [`HashMap`]. pub fn into_inner(self) -> HashMap { self.0 @@ -162,6 +167,16 @@ impl IntoIterator for EntityHashMap { pub struct Keys<'a, V, S = EntityHash>(hash_map::Keys<'a, Entity, V>, PhantomData); impl<'a, V> Keys<'a, V> { + /// Constructs a [`Keys<'a, V, S>`] from a [`hash_map::Keys<'a, V>`] unsafely. + /// + /// # Safety + /// + /// `keys` must either be empty, or have been obtained from a + /// [`hash_map::HashMap`] using the `S` hasher. + pub unsafe fn from_keys_unchecked(keys: hash_map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { + Keys::<'_, _, S>(keys, PhantomData) + } + /// Returns the inner [`Keys`](hash_map::Keys). pub fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { self.0 @@ -220,6 +235,18 @@ unsafe impl EntitySetIterator for Keys<'_, V> {} pub struct IntoKeys(hash_map::IntoKeys, PhantomData); impl IntoKeys { + /// Constructs a [`IntoKeys`] from a [`hash_map::IntoKeys`] unsafely. + /// + /// # Safety + /// + /// `into_keys` must either be empty, or have been obtained from a + /// [`hash_map::HashMap`] using the `S` hasher. + pub unsafe fn from_into_keys_unchecked( + into_keys: hash_map::IntoKeys, + ) -> IntoKeys { + IntoKeys::<_, S>(into_keys, PhantomData) + } + /// Returns the inner [`IntoKeys`](hash_map::IntoKeys). pub fn into_inner(self) -> hash_map::IntoKeys { self.0 diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index c42da76bacd43..e2b16b2e9079f 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -53,6 +53,11 @@ impl EntityHashSet { self.0.is_empty() } + /// Constructs an `EntityHashSet` from an [`HashSet`]. + pub fn from_hash_set(set: HashSet) -> Self { + Self(set) + } + /// Returns the inner [`HashSet`]. pub fn into_inner(self) -> HashSet { self.0 @@ -224,6 +229,16 @@ impl From> for EntityHashSet { pub struct Iter<'a, S = EntityHash>(hash_set::Iter<'a, Entity>, PhantomData); impl<'a> Iter<'a> { + /// Constructs a [`Iter<'a, S>`] from a [`hash_set::Iter<'a>`] unsafely. + /// + /// # Safety + /// + /// `iter` must either be empty, or have been obtained from a + /// [`hash_set::HashSet`] using the `S` hasher. + pub unsafe fn from_iter_unchecked(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> { + Iter::<'_, S>(iter, PhantomData) + } + /// Returns the inner [`Iter`](hash_set::Iter). pub fn into_inner(self) -> hash_set::Iter<'a, Entity> { self.0 @@ -279,6 +294,18 @@ unsafe impl EntitySetIterator for Iter<'_> {} pub struct IntoIter(hash_set::IntoIter, PhantomData); impl IntoIter { + /// Constructs a [`IntoIter`] from a [`hash_set::IntoIter`] unsafely. + /// + /// # Safety + /// + /// `into_iter` must either be empty, or have been obtained from a + /// [`hash_set::HashSet`] using the `S` hasher. + pub unsafe fn from_into_iter_unchecked( + into_iter: hash_set::IntoIter, + ) -> IntoIter { + IntoIter::(into_iter, PhantomData) + } + /// Returns the inner [`IntoIter`](hash_set::IntoIter). pub fn into_inner(self) -> hash_set::IntoIter { self.0 @@ -331,6 +358,16 @@ unsafe impl EntitySetIterator for IntoIter {} pub struct Drain<'a, S = EntityHash>(hash_set::Drain<'a, Entity>, PhantomData); impl<'a> Drain<'a> { + /// Constructs a [`Drain<'a, S>`] from a [`hash_set::Drain<'a>`] unsafely. + /// + /// # Safety + /// + /// `drain` must either be empty, or have been obtained from a + /// [`hash_set::HashSet`] using the `S` hasher. + pub unsafe fn from_drain_unchecked(drain: hash_set::Drain<'a, Entity>) -> Drain<'a, S> { + Drain::<'_, S>(drain, PhantomData) + } + /// Returns the inner [`Drain`](hash_set::Drain). pub fn into_inner(self) -> hash_set::Drain<'a, Entity> { self.0 @@ -380,6 +417,18 @@ pub struct ExtractIf<'a, F: FnMut(&Entity) -> bool, S = EntityHash>( ); impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> { + /// Constructs a [`ExtractIf<'a, F, S>`] from a [`hash_set::ExtractIf<'a, F>`] unsafely. + /// + /// # Safety + /// + /// `extract_if` must either be empty, or have been obtained from a + /// [`hash_set::HashSet`] using the `S` hasher. + pub unsafe fn from_extract_if_unchecked( + extract_if: hash_set::ExtractIf<'a, Entity, F>, + ) -> ExtractIf<'a, F, S> { + ExtractIf::<'_, _, S>(extract_if, PhantomData) + } + /// Returns the inner [`ExtractIf`](hash_set::ExtractIf). pub fn into_inner(self) -> hash_set::ExtractIf<'a, Entity, F> { self.0 diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index 3330d1e58d25a..420ec40f5d92d 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -49,6 +49,11 @@ impl EntityIndexMap { Self(IndexMap::with_capacity_and_hasher(n, EntityHash)) } + /// Constructs an `EntityIndexMap` from an [`IndexMap`]. + pub fn from_index_map(set: IndexMap) -> Self { + Self(set) + } + /// Returns the inner [`IndexMap`]. pub fn into_inner(self) -> IndexMap { self.0 @@ -833,6 +838,16 @@ impl IndexMut for Slice { pub struct Iter<'a, V, S = EntityHash>(map::Iter<'a, Entity, V>, PhantomData); impl<'a, V> Iter<'a, V> { + /// Constructs a [`Iter<'a, V, S>`] from a [`map::Iter<'a, V>`] unsafely. + /// + /// # Safety + /// + /// `iter` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_iter_unchecked(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> { + Iter::<'_, _, S>(iter, PhantomData) + } + /// Returns the inner [`Iter`](map::Iter). pub fn into_inner(self) -> map::Iter<'a, Entity, V> { self.0 @@ -898,6 +913,18 @@ impl Default for Iter<'_, V> { pub struct IterMut<'a, V, S = EntityHash>(map::IterMut<'a, Entity, V>, PhantomData); impl<'a, V> IterMut<'a, V> { + /// Constructs a [`IterMut<'a, V, S>`] from a [`map::IterMut<'a, V>`] unsafely. + /// + /// # Safety + /// + /// `iter_mut` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_iter_mut_unchecked( + iter_mut: map::IterMut<'a, Entity, V>, + ) -> IterMut<'a, V, S> { + IterMut::<'_, _, S>(iter_mut, PhantomData) + } + /// Returns the inner [`IterMut`](map::IterMut). pub fn into_inner(self) -> map::IterMut<'a, Entity, V> { self.0 @@ -968,6 +995,18 @@ impl Default for IterMut<'_, V> { pub struct IntoIter(map::IntoIter, PhantomData); impl IntoIter { + /// Constructs a [`IntoIter`] from a [`map::IntoIter`] unsafely. + /// + /// # Safety + /// + /// `into_iter` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_into_iter_unchecked( + into_iter: map::IntoIter, + ) -> IntoIter { + IntoIter::<_, S>(into_iter, PhantomData) + } + /// Returns the inner [`IntoIter`](map::IntoIter). pub fn into_inner(self) -> map::IntoIter { self.0 @@ -1044,6 +1083,16 @@ impl Default for IntoIter { pub struct Drain<'a, V, S = EntityHash>(map::Drain<'a, Entity, V>, PhantomData); impl<'a, V> Drain<'a, V> { + /// Constructs a [`Drain<'a, V, S>`] from a [`map::Drain<'a, V>`] unsafely. + /// + /// # Safety + /// + /// `drain` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_drain_unchecked(drain: map::Drain<'a, Entity, V>) -> Drain<'a, V, S> { + Drain::<'_, _, S>(drain, PhantomData) + } + /// Returns the inner [`Drain`](map::Drain). pub fn into_inner(self) -> map::Drain<'a, Entity, V> { self.0 @@ -1100,6 +1149,16 @@ impl Debug for Drain<'_, V> { pub struct Keys<'a, V, S = EntityHash>(map::Keys<'a, Entity, V>, PhantomData); impl<'a, V> Keys<'a, V> { + /// Constructs a [`Keys<'a, V, S>`] from a [`map::Keys<'a, V>`] unsafely. + /// + /// # Safety + /// + /// `keys` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_keys_unchecked(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { + Keys::<'_, _, S>(keys, PhantomData) + } + /// Returns the inner [`Keys`](map::Keys). pub fn into_inner(self) -> map::Keys<'a, Entity, V> { self.0 @@ -1168,6 +1227,18 @@ unsafe impl EntitySetIterator for Keys<'_, V> {} pub struct IntoKeys(map::IntoKeys, PhantomData); impl IntoKeys { + /// Constructs a [`IntoKeys`] from a [`map::IntoKeys`] unsafely. + /// + /// # Safety + /// + /// `into_keys` must either be empty, or have been obtained from a + /// [`IndexMap`] using the `S` hasher. + pub unsafe fn from_into_keys_unchecked( + into_keys: map::IntoKeys, + ) -> IntoKeys { + IntoKeys::<_, S>(into_keys, PhantomData) + } + /// Returns the inner [`IntoKeys`](map::IntoKeys). pub fn into_inner(self) -> map::IntoKeys { self.0 diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index 6741a41c2c679..2fe94534f7bd5 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -46,6 +46,11 @@ impl EntityIndexSet { Self(IndexSet::with_capacity_and_hasher(n, EntityHash)) } + /// Constructs an `EntityIndexSet` from an [`IndexSet`]. + pub fn from_index_set(set: IndexSet) -> Self { + Self(set) + } + /// Returns the inner [`IndexSet`]. pub fn into_inner(self) -> IndexSet { self.0 @@ -554,6 +559,16 @@ impl Index for Slice { pub struct Iter<'a, S = EntityHash>(set::Iter<'a, Entity>, PhantomData); impl<'a> Iter<'a> { + /// Constructs a [`Iter<'a, S>`] from a [`set::Iter<'a>`] unsafely. + /// + /// # Safety + /// + /// `iter` must either be empty, or have been obtained from a + /// [`IndexSet`] using the `S` hasher. + pub unsafe fn from_iter_unchecked(iter: set::Iter<'a, Entity>) -> Iter<'a, S> { + Iter::<'_, S>(iter, PhantomData) + } + /// Returns the inner [`Iter`](set::Iter). pub fn into_inner(self) -> set::Iter<'a, Entity> { self.0 @@ -623,6 +638,16 @@ unsafe impl EntitySetIterator for Iter<'_> {} pub struct IntoIter(set::IntoIter, PhantomData); impl IntoIter { + /// Constructs a [`IntoIter`] from a [`set::IntoIter`] unsafely. + /// + /// # Safety + /// + /// `into_iter` must either be empty, or have been obtained from a + /// [`IndexSet`] using the `S` hasher. + pub unsafe fn from_into_iter_unchecked(into_iter: set::IntoIter) -> IntoIter { + IntoIter::(into_iter, PhantomData) + } + /// Returns the inner [`IntoIter`](set::IntoIter). pub fn into_inner(self) -> set::IntoIter { self.0 @@ -695,6 +720,16 @@ unsafe impl EntitySetIterator for IntoIter {} pub struct Drain<'a, S = EntityHash>(set::Drain<'a, Entity>, PhantomData); impl<'a> Drain<'a> { + /// Constructs a [`Drain<'a, S>`] from a [`set::Drain<'a>`] unsafely. + /// + /// # Safety + /// + /// `drain` must either be empty, or have been obtained from a + /// [`IndexSet`] using the `S` hasher. + pub unsafe fn from_drain_unchecked(drain: set::Drain<'a, Entity>) -> Drain<'a, S> { + Drain::<'_, S>(drain, PhantomData) + } + /// Returns the inner [`Drain`](set::Drain). pub fn into_inner(self) -> set::Drain<'a, Entity> { self.0 diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index 71df33ec5f42c..49e62dad4d909 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -31,6 +31,7 @@ use super::{ /// and some [`TryFrom`] implementations. /// /// When `T` is [`Entity`], use [`UniqueEntityArray`]. +#[repr(transparent)] #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct UniqueEntityEquivalentArray([T; N]); @@ -170,7 +171,7 @@ impl<'a, T: EntityEquivalent, const N: usize> IntoIterator fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } } } @@ -181,7 +182,7 @@ impl IntoIterator for UniqueEntityEquivalen fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.into_iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) } } } diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index 9910b1b7befb6..dd1b42903fa24 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -300,7 +300,7 @@ impl UniqueEntityEquivalentSlice { /// Returns an iterator over the slice. pub fn iter(&self) -> Iter<'_, T> { // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } } /// Returns an iterator over all contiguous windows of length @@ -311,9 +311,7 @@ impl UniqueEntityEquivalentSlice { /// [`[T]::windows`]: `slice::windows` pub fn windows(&self, size: usize) -> Windows<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.windows(size)) - } + unsafe { UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.windows(size)) } } /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the @@ -325,9 +323,7 @@ impl UniqueEntityEquivalentSlice { pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( - self.0.chunks(chunk_size), - ) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.chunks(chunk_size)) } } @@ -340,7 +336,7 @@ impl UniqueEntityEquivalentSlice { pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.chunks_mut(chunk_size), ) } @@ -354,7 +350,7 @@ impl UniqueEntityEquivalentSlice { pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked( self.0.chunks_exact(chunk_size), ) } @@ -369,7 +365,7 @@ impl UniqueEntityEquivalentSlice { pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.chunks_exact_mut(chunk_size), ) } @@ -384,9 +380,7 @@ impl UniqueEntityEquivalentSlice { pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( - self.0.rchunks(chunk_size), - ) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.rchunks(chunk_size)) } } @@ -399,7 +393,7 @@ impl UniqueEntityEquivalentSlice { pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rchunks_mut(chunk_size), ) } @@ -414,7 +408,7 @@ impl UniqueEntityEquivalentSlice { pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked( self.0.rchunks_exact(chunk_size), ) } @@ -429,7 +423,7 @@ impl UniqueEntityEquivalentSlice { pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rchunks_exact_mut(chunk_size), ) } @@ -446,9 +440,7 @@ impl UniqueEntityEquivalentSlice { F: FnMut(&T, &T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.chunk_by(pred)) - } + unsafe { UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.chunk_by(pred)) } } /// Returns an iterator over the slice producing non-overlapping mutable @@ -463,7 +455,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.chunk_by_mut(pred), ) } @@ -584,9 +576,7 @@ impl UniqueEntityEquivalentSlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.split(pred)) - } + unsafe { UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.split(pred)) } } /// Returns an iterator over mutable subslices separated by elements that @@ -601,7 +591,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.split_mut(pred), ) } @@ -619,9 +609,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( - self.0.split_inclusive(pred), - ) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.split_inclusive(pred)) } } @@ -637,7 +625,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.split_inclusive_mut(pred), ) } @@ -654,9 +642,7 @@ impl UniqueEntityEquivalentSlice { F: FnMut(&T) -> bool, { // SAFETY: Any subslice of a unique slice is also unique. - unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplit(pred)) - } + unsafe { UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.rsplit(pred)) } } /// Returns an iterator over mutable subslices separated by elements that @@ -672,7 +658,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rsplit_mut(pred), ) } @@ -690,7 +676,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.splitn(n, pred)) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.splitn(n, pred)) } } @@ -706,7 +692,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.splitn_mut(n, pred), ) } @@ -724,7 +710,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked(self.0.rsplitn(n, pred)) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.rsplitn(n, pred)) } } @@ -740,7 +726,7 @@ impl UniqueEntityEquivalentSlice { { // SAFETY: Any subslice of a unique slice is also unique. unsafe { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rsplitn_mut(n, pred), ) } @@ -1516,6 +1502,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// An iterator that yields `&UniqueEntityEquivalentSlice`. Note that an entity may appear /// in multiple slices, depending on the wrapped iterator. +#[repr(transparent)] #[derive(Debug)] pub struct UniqueEntityEquivalentSliceIter< 'a, @@ -1533,7 +1520,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// # Safety /// /// All elements in each of the slices must be unique. - pub unsafe fn from_slice_iterator_unchecked(iter: I) -> Self { + pub unsafe fn from_slice_iter_unchecked(iter: I) -> Self { Self { iter } } @@ -1699,6 +1686,7 @@ pub type RSplitN<'a, P, T = Entity> = /// An iterator that yields `&mut UniqueEntityEquivalentSlice`. Note that an entity may appear /// in multiple slices, depending on the wrapped iterator. +#[repr(transparent)] #[derive(Debug)] pub struct UniqueEntityEquivalentSliceIterMut< 'a, @@ -1716,7 +1704,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// # Safety /// /// All elements in each of the slices must be unique. - pub unsafe fn from_mut_slice_iterator_unchecked(iter: I) -> Self { + pub unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self { Self { iter } } diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index 30f9984e70be7..51e4632be8478 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -7,6 +7,7 @@ use core::{ Bound, Deref, DerefMut, Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive, }, + ptr, }; use alloc::{ @@ -37,6 +38,7 @@ use super::{ /// and not recommended. /// /// When `T` is [`Entity`], use the [`UniqueEntityVec`] alias. +#[repr(transparent)] #[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub struct UniqueEntityEquivalentVec(Vec); @@ -82,6 +84,26 @@ impl UniqueEntityEquivalentVec { Self(vec) } + /// Constructs a `UniqueEntityEquivalentVec` from a [`&Vec`] unsafely. + /// + /// # Safety + /// + /// `vec` must contain only unique elements. + pub unsafe fn from_vec_ref_unchecked(vec: &Vec) -> &Self { + // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. + unsafe { &*ptr::from_ref(vec).cast() } + } + + /// Constructs a `UniqueEntityEquivalentVec` from a [`&mut Vec`] unsafely. + /// + /// # Safety + /// + /// `vec` must contain only unique elements. + pub unsafe fn from_vec_mut_unchecked(vec: &mut Vec) -> &mut Self { + // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. + unsafe { &mut *ptr::from_mut(vec).cast() } + } + /// Returns the inner [`Vec`]. pub fn into_inner(self) -> Vec { self.0 @@ -331,7 +353,7 @@ impl UniqueEntityEquivalentVec { R: RangeBounds, { // SAFETY: `self` and thus `range` contains only unique elements. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.drain(range)) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.drain(range)) } } /// Clears the vector, removing all values. @@ -410,7 +432,7 @@ impl UniqueEntityEquivalentVec { I: EntitySet, { // SAFETY: `self` and thus `range` contains only unique elements. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.splice(range, replace_with)) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.splice(range, replace_with)) } } } @@ -446,7 +468,7 @@ where fn into_iter(self) -> Self::IntoIter { // SAFETY: `self` contains only unique elements. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } } } @@ -457,7 +479,7 @@ impl IntoIterator for UniqueEntityEquivalentVec { fn into_iter(self) -> Self::IntoIter { // SAFETY: `self` contains only unique elements. - unsafe { UniqueEntityIter::from_iterator_unchecked(self.0.into_iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) } } } diff --git a/crates/bevy_transform/src/systems.rs b/crates/bevy_transform/src/systems.rs index 62038b37ed90f..a49d575bc8df4 100644 --- a/crates/bevy_transform/src/systems.rs +++ b/crates/bevy_transform/src/systems.rs @@ -251,7 +251,7 @@ mod parallel { use crate::prelude::*; // TODO: this implementation could be used in no_std if there are equivalents of these. use alloc::{sync::Arc, vec::Vec}; - use bevy_ecs::{entity::UniqueEntityIter, prelude::*, system::lifetimeless::Read}; + use bevy_ecs::{entity::UniqueEntitySlice, prelude::*, system::lifetimeless::Read}; use bevy_tasks::{ComputeTaskPool, TaskPool}; use bevy_utils::Parallel; use core::sync::atomic::{AtomicI32, Ordering}; @@ -440,9 +440,7 @@ mod parallel { // visiting disjoint entities in parallel, which is safe. #[expect(unsafe_code, reason = "Mutating disjoint entities in parallel")] let children_iter = unsafe { - nodes.iter_many_unique_unsafe(UniqueEntityIter::from_iterator_unchecked( - p_children.iter(), - )) + nodes.iter_many_unique_unsafe(UniqueEntitySlice::from_slice_unchecked(p_children)) }; let mut last_child = None; From bb953550f4481c6834572c0eab96142a8075745a Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Fri, 10 Oct 2025 20:43:34 +0200 Subject: [PATCH 3/8] inline entity set logic --- crates/bevy_ecs/src/entity/entity_set.rs | 22 +++ crates/bevy_ecs/src/entity/hash.rs | 1 + crates/bevy_ecs/src/entity/hash_map.rs | 30 ++++ crates/bevy_ecs/src/entity/hash_set.rs | 38 +++++ crates/bevy_ecs/src/entity/index_map.rs | 143 ++++++++++++++++++ crates/bevy_ecs/src/entity/index_set.rs | 80 +++++++++++ crates/bevy_ecs/src/entity/unique_array.rs | 69 +++++++++ crates/bevy_ecs/src/entity/unique_slice.rs | 159 +++++++++++++++++++++ crates/bevy_ecs/src/entity/unique_vec.rs | 126 ++++++++++++++++ 9 files changed, 668 insertions(+) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index 319601b1f9c6f..49bfd4e9819c9 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -68,6 +68,7 @@ pub trait ContainsEntity { pub unsafe trait EntityEquivalent: ContainsEntity + Eq {} impl ContainsEntity for Entity { + #[inline] fn entity(&self) -> Entity { *self } @@ -78,6 +79,7 @@ impl ContainsEntity for Entity { unsafe impl EntityEquivalent for Entity {} impl ContainsEntity for &T { + #[inline] fn entity(&self) -> Entity { (**self).entity() } @@ -90,6 +92,7 @@ impl ContainsEntity for &T { unsafe impl EntityEquivalent for &T {} impl ContainsEntity for &mut T { + #[inline] fn entity(&self) -> Entity { (**self).entity() } @@ -102,6 +105,7 @@ impl ContainsEntity for &mut T { unsafe impl EntityEquivalent for &mut T {} impl ContainsEntity for Box { + #[inline] fn entity(&self) -> Entity { (**self).entity() } @@ -114,6 +118,7 @@ impl ContainsEntity for Box { unsafe impl EntityEquivalent for Box {} impl ContainsEntity for Rc { + #[inline] fn entity(&self) -> Entity { (**self).entity() } @@ -126,6 +131,7 @@ impl ContainsEntity for Rc { unsafe impl EntityEquivalent for Rc {} impl ContainsEntity for Arc { + #[inline] fn entity(&self) -> Entity { (**self).entity() } @@ -342,6 +348,7 @@ pub trait FromEntitySetIterator: FromIterator { impl FromEntitySetIterator for HashSet { + #[inline] fn from_entity_set_iter>(set_iter: I) -> Self { let iter = set_iter.into_iter(); let set = HashSet::::with_capacity_and_hasher(iter.size_hint().0, S::default()); @@ -365,6 +372,7 @@ pub struct UniqueEntityIter> { impl UniqueEntityIter { /// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`]. + #[inline] pub fn from_entity_set_iter(iter: I) -> Self { Self { iter } } @@ -376,6 +384,7 @@ impl> UniqueEntityIter { /// # Safety /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. + #[inline] pub unsafe fn from_iter_unchecked(iter: I) -> Self { Self { iter } } @@ -385,6 +394,7 @@ impl> UniqueEntityIter { /// # Safety /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. + #[inline] pub unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self { // SAFETY: UniqueEntityIter is a transparent wrapper around I. unsafe { &*ptr::from_ref(iter).cast() } @@ -395,17 +405,20 @@ impl> UniqueEntityIter { /// # Safety /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. + #[inline] pub unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self { // SAFETY: UniqueEntityIter is a transparent wrapper around I. unsafe { &mut *ptr::from_mut(iter).cast() } } /// Returns the inner `I`. + #[inline] pub fn into_inner(self) -> I { self.iter } /// Returns a reference to the inner `I`. + #[inline] pub fn as_inner(&self) -> &I { &self.iter } @@ -416,6 +429,7 @@ impl> UniqueEntityIter { /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. + #[inline] pub unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } @@ -424,10 +438,12 @@ impl> UniqueEntityIter { impl> Iterator for UniqueEntityIter { type Item = I::Item; + #[inline] fn next(&mut self) -> Option { self.iter.next() } + #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } @@ -436,6 +452,7 @@ impl> Iterator for UniqueEntityIter { impl> ExactSizeIterator for UniqueEntityIter {} impl> DoubleEndedIterator for UniqueEntityIter { + #[inline] fn next_back(&mut self) -> Option { self.iter.next_back() } @@ -447,6 +464,7 @@ impl> FusedIterator for UniqueEntityIte unsafe impl> EntitySetIterator for UniqueEntityIter {} impl + AsRef<[T]>> AsRef<[T]> for UniqueEntityIter { + #[inline] fn as_ref(&self) -> &[T] { self.iter.as_ref() } @@ -455,6 +473,7 @@ impl + AsRef<[T]>> AsRef<[T]> for UniqueE impl + AsRef<[T]>> AsRef> for UniqueEntityIter { + #[inline] fn as_ref(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.as_ref()) } @@ -464,6 +483,7 @@ impl + AsRef<[T]>> impl + AsMut<[T]>> AsMut> for UniqueEntityIter { + #[inline] fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.as_mut()) } @@ -472,6 +492,7 @@ impl + AsMut<[T]>> // Default does not guarantee uniqueness, meaning `I` needs to be EntitySetIterator. impl Default for UniqueEntityIter { + #[inline] fn default() -> Self { Self { iter: Default::default(), @@ -481,6 +502,7 @@ impl Default for UniqueEntityIter { // Clone does not guarantee to maintain uniqueness, meaning `I` needs to be EntitySetIterator. impl Clone for UniqueEntityIter { + #[inline] fn clone(&self) -> Self { Self { iter: self.iter.clone(), diff --git a/crates/bevy_ecs/src/entity/hash.rs b/crates/bevy_ecs/src/entity/hash.rs index a53847343952a..179443fb0b20b 100644 --- a/crates/bevy_ecs/src/entity/hash.rs +++ b/crates/bevy_ecs/src/entity/hash.rs @@ -11,6 +11,7 @@ pub struct EntityHash; impl BuildHasher for EntityHash { type Hasher = EntityHasher; + #[inline] fn build_hasher(&self) -> Self::Hasher { Self::Hasher::default() } diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index 4f767a52d6f4c..10a32f91a623e 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -27,6 +27,7 @@ impl EntityHashMap { /// Equivalent to [`HashMap::with_hasher(EntityHash)`]. /// /// [`HashMap::with_hasher(EntityHash)`]: HashMap::with_hasher + #[inline] pub const fn new() -> Self { Self(HashMap::with_hasher(EntityHash)) } @@ -36,16 +37,19 @@ impl EntityHashMap { /// Equivalent to [`HashMap::with_capacity_and_hasher(n, EntityHash)`]. /// /// [`HashMap:with_capacity_and_hasher(n, EntityHash)`]: HashMap::with_capacity_and_hasher + #[inline] pub fn with_capacity(n: usize) -> Self { Self(HashMap::with_capacity_and_hasher(n, EntityHash)) } /// Constructs an `EntityHashMap` from an [`HashMap`]. + #[inline] pub fn from_index_map(set: HashMap) -> Self { Self(set) } /// Returns the inner [`HashMap`]. + #[inline] pub fn into_inner(self) -> HashMap { self.0 } @@ -54,6 +58,7 @@ impl EntityHashMap { /// The iterator element type is `&'a Entity`. /// /// Equivalent to [`HashMap::keys`]. + #[inline] pub fn keys(&self) -> Keys<'_, V> { Keys(self.0.keys(), PhantomData) } @@ -63,12 +68,14 @@ impl EntityHashMap { /// The iterator element type is [`Entity`]. /// /// Equivalent to [`HashMap::into_keys`]. + #[inline] pub fn into_keys(self) -> IntoKeys { IntoKeys(self.0.into_keys(), PhantomData) } } impl Default for EntityHashMap { + #[inline] fn default() -> Self { Self(Default::default()) } @@ -77,48 +84,56 @@ impl Default for EntityHashMap { impl Deref for EntityHashMap { type Target = HashMap; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for EntityHashMap { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl<'a, V: Copy> Extend<&'a (Entity, V)> for EntityHashMap { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl<'a, V: Copy> Extend<(&'a Entity, &'a V)> for EntityHashMap { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl Extend<(Entity, V)> for EntityHashMap { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl From<[(Entity, V); N]> for EntityHashMap { + #[inline] fn from(value: [(Entity, V); N]) -> Self { Self(HashMap::from_iter(value)) } } impl FromIterator<(Entity, V)> for EntityHashMap { + #[inline] fn from_iter>(iterable: I) -> Self { Self(HashMap::from_iter(iterable)) } } impl From> for EntityHashMap { + #[inline] fn from(value: HashMap) -> Self { Self(value) } @@ -126,6 +141,7 @@ impl From> for EntityHashMap { impl Index<&Q> for EntityHashMap { type Output = V; + #[inline] fn index(&self, key: &Q) -> &V { self.0.index(&key.entity()) } @@ -135,6 +151,7 @@ impl<'a, V> IntoIterator for &'a EntityHashMap { type Item = (&'a Entity, &'a V); type IntoIter = hash_map::Iter<'a, Entity, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { self.0.iter() } @@ -144,6 +161,7 @@ impl<'a, V> IntoIterator for &'a mut EntityHashMap { type Item = (&'a Entity, &'a mut V); type IntoIter = hash_map::IterMut<'a, Entity, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { self.0.iter_mut() } @@ -153,6 +171,7 @@ impl IntoIterator for EntityHashMap { type Item = (Entity, V); type IntoIter = hash_map::IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { self.0.into_iter() } @@ -173,11 +192,13 @@ impl<'a, V> Keys<'a, V> { /// /// `keys` must either be empty, or have been obtained from a /// [`hash_map::HashMap`] using the `S` hasher. + #[inline] pub unsafe fn from_keys_unchecked(keys: hash_map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { Keys::<'_, _, S>(keys, PhantomData) } /// Returns the inner [`Keys`](hash_map::Keys). + #[inline] pub fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { self.0 } @@ -186,6 +207,7 @@ impl<'a, V> Keys<'a, V> { impl<'a, V> Deref for Keys<'a, V> { type Target = hash_map::Keys<'a, Entity, V>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -194,6 +216,7 @@ impl<'a, V> Deref for Keys<'a, V> { impl<'a, V> Iterator for Keys<'a, V> { type Item = &'a Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -204,6 +227,7 @@ impl ExactSizeIterator for Keys<'_, V> {} impl FusedIterator for Keys<'_, V> {} impl Clone for Keys<'_, V> { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -216,6 +240,7 @@ impl Debug for Keys<'_, V> { } impl Default for Keys<'_, V> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -241,6 +266,7 @@ impl IntoKeys { /// /// `into_keys` must either be empty, or have been obtained from a /// [`hash_map::HashMap`] using the `S` hasher. + #[inline] pub unsafe fn from_into_keys_unchecked( into_keys: hash_map::IntoKeys, ) -> IntoKeys { @@ -248,6 +274,7 @@ impl IntoKeys { } /// Returns the inner [`IntoKeys`](hash_map::IntoKeys). + #[inline] pub fn into_inner(self) -> hash_map::IntoKeys { self.0 } @@ -256,6 +283,7 @@ impl IntoKeys { impl Deref for IntoKeys { type Target = hash_map::IntoKeys; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -264,6 +292,7 @@ impl Deref for IntoKeys { impl Iterator for IntoKeys { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -283,6 +312,7 @@ impl Debug for IntoKeys { } impl Default for IntoKeys { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index e2b16b2e9079f..a958c834714b6 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -30,6 +30,7 @@ impl EntityHashSet { /// Equivalent to [`HashSet::with_hasher(EntityHash)`]. /// /// [`HashSet::with_hasher(EntityHash)`]: HashSet::with_hasher + #[inline] pub const fn new() -> Self { Self(HashSet::with_hasher(EntityHash)) } @@ -39,26 +40,31 @@ impl EntityHashSet { /// Equivalent to [`HashSet::with_capacity_and_hasher(n, EntityHash)`]. /// /// [`HashSet::with_capacity_and_hasher(n, EntityHash)`]: HashSet::with_capacity_and_hasher + #[inline] pub fn with_capacity(n: usize) -> Self { Self(HashSet::with_capacity_and_hasher(n, EntityHash)) } /// Returns the number of elements in the set. + #[inline] pub fn len(&self) -> usize { self.0.len() } /// Returns `true` if the set contains no elements. + #[inline] pub fn is_empty(&self) -> bool { self.0.is_empty() } /// Constructs an `EntityHashSet` from an [`HashSet`]. + #[inline] pub fn from_hash_set(set: HashSet) -> Self { Self(set) } /// Returns the inner [`HashSet`]. + #[inline] pub fn into_inner(self) -> HashSet { self.0 } @@ -66,6 +72,7 @@ impl EntityHashSet { /// Clears the set, returning all elements in an iterator. /// /// Equivalent to [`HashSet::drain`]. + #[inline] pub fn drain(&mut self) -> Drain<'_> { Drain(self.0.drain(), PhantomData) } @@ -74,6 +81,7 @@ impl EntityHashSet { /// The iterator element type is `&'a Entity`. /// /// Equivalent to [`HashSet::iter`]. + #[inline] pub fn iter(&self) -> Iter<'_> { Iter(self.0.iter(), PhantomData) } @@ -82,6 +90,7 @@ impl EntityHashSet { /// and returns an iterator over the removed items. /// /// Equivalent to [`HashSet::extract_if`]. + #[inline] pub fn extract_if bool>(&mut self, f: F) -> ExtractIf<'_, F> { ExtractIf(self.0.extract_if(f), PhantomData) } @@ -90,12 +99,14 @@ impl EntityHashSet { impl Deref for EntityHashSet { type Target = HashSet; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for EntityHashSet { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -106,6 +117,7 @@ impl<'a> IntoIterator for &'a EntityHashSet { type IntoIter = Iter<'a>; + #[inline] fn into_iter(self) -> Self::IntoIter { Iter((&self.0).into_iter(), PhantomData) } @@ -116,6 +128,7 @@ impl IntoIterator for EntityHashSet { type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter(), PhantomData) } @@ -178,30 +191,35 @@ impl SubAssign<&EntityHashSet> for EntityHashSet { } impl<'a> Extend<&'a Entity> for EntityHashSet { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl Extend for EntityHashSet { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl From<[Entity; N]> for EntityHashSet { + #[inline] fn from(value: [Entity; N]) -> Self { Self(HashSet::from_iter(value)) } } impl FromIterator for EntityHashSet { + #[inline] fn from_iter>(iterable: I) -> Self { Self(HashSet::from_iter(iterable)) } } impl FromEntitySetIterator for EntityHashSet { + #[inline] fn from_entity_set_iter>(set_iter: I) -> Self { let iter = set_iter.into_iter(); let set = EntityHashSet::with_capacity(iter.size_hint().0); @@ -216,6 +234,7 @@ impl FromEntitySetIterator for EntityHashSet { } impl From> for EntityHashSet { + #[inline] fn from(value: HashSet) -> Self { Self(value) } @@ -235,11 +254,13 @@ impl<'a> Iter<'a> { /// /// `iter` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. + #[inline] pub unsafe fn from_iter_unchecked(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> { Iter::<'_, S>(iter, PhantomData) } /// Returns the inner [`Iter`](hash_set::Iter). + #[inline] pub fn into_inner(self) -> hash_set::Iter<'a, Entity> { self.0 } @@ -248,6 +269,7 @@ impl<'a> Iter<'a> { impl<'a> Deref for Iter<'a> { type Target = hash_set::Iter<'a, Entity>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -256,6 +278,7 @@ impl<'a> Deref for Iter<'a> { impl<'a> Iterator for Iter<'a> { type Item = &'a Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -266,6 +289,7 @@ impl ExactSizeIterator for Iter<'_> {} impl FusedIterator for Iter<'_> {} impl Clone for Iter<'_> { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -278,6 +302,7 @@ impl Debug for Iter<'_> { } impl Default for Iter<'_> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -300,6 +325,7 @@ impl IntoIter { /// /// `into_iter` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. + #[inline] pub unsafe fn from_into_iter_unchecked( into_iter: hash_set::IntoIter, ) -> IntoIter { @@ -307,6 +333,7 @@ impl IntoIter { } /// Returns the inner [`IntoIter`](hash_set::IntoIter). + #[inline] pub fn into_inner(self) -> hash_set::IntoIter { self.0 } @@ -315,6 +342,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = hash_set::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -323,6 +351,7 @@ impl Deref for IntoIter { impl Iterator for IntoIter { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -342,6 +371,7 @@ impl Debug for IntoIter { } impl Default for IntoIter { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -364,11 +394,13 @@ impl<'a> Drain<'a> { /// /// `drain` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. + #[inline] pub unsafe fn from_drain_unchecked(drain: hash_set::Drain<'a, Entity>) -> Drain<'a, S> { Drain::<'_, S>(drain, PhantomData) } /// Returns the inner [`Drain`](hash_set::Drain). + #[inline] pub fn into_inner(self) -> hash_set::Drain<'a, Entity> { self.0 } @@ -377,6 +409,7 @@ impl<'a> Drain<'a> { impl<'a> Deref for Drain<'a> { type Target = hash_set::Drain<'a, Entity>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -385,6 +418,7 @@ impl<'a> Deref for Drain<'a> { impl<'a> Iterator for Drain<'a> { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -423,6 +457,7 @@ impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> { /// /// `extract_if` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. + #[inline] pub unsafe fn from_extract_if_unchecked( extract_if: hash_set::ExtractIf<'a, Entity, F>, ) -> ExtractIf<'a, F, S> { @@ -430,6 +465,7 @@ impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> { } /// Returns the inner [`ExtractIf`](hash_set::ExtractIf). + #[inline] pub fn into_inner(self) -> hash_set::ExtractIf<'a, Entity, F> { self.0 } @@ -438,6 +474,7 @@ impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> { impl<'a, F: FnMut(&Entity) -> bool> Deref for ExtractIf<'a, F> { type Target = hash_set::ExtractIf<'a, Entity, F>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -446,6 +483,7 @@ impl<'a, F: FnMut(&Entity) -> bool> Deref for ExtractIf<'a, F> { impl<'a, F: FnMut(&Entity) -> bool> Iterator for ExtractIf<'a, F> { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index 420ec40f5d92d..e41ab08e7d899 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -36,6 +36,7 @@ impl EntityIndexMap { /// Equivalent to [`IndexMap::with_hasher(EntityHash)`]. /// /// [`IndexMap::with_hasher(EntityHash)`]: IndexMap::with_hasher + #[inline] pub const fn new() -> Self { Self(IndexMap::with_hasher(EntityHash)) } @@ -45,16 +46,19 @@ impl EntityIndexMap { /// Equivalent to [`IndexMap::with_capacity_and_hasher(n, EntityHash)`]. /// /// [`IndexMap:with_capacity_and_hasher(n, EntityHash)`]: IndexMap::with_capacity_and_hasher + #[inline] pub fn with_capacity(n: usize) -> Self { Self(IndexMap::with_capacity_and_hasher(n, EntityHash)) } /// Constructs an `EntityIndexMap` from an [`IndexMap`]. + #[inline] pub fn from_index_map(set: IndexMap) -> Self { Self(set) } /// Returns the inner [`IndexMap`]. + #[inline] pub fn into_inner(self) -> IndexMap { self.0 } @@ -62,6 +66,7 @@ impl EntityIndexMap { /// Returns a slice of all the key-value pairs in the map. /// /// Equivalent to [`IndexMap::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -70,6 +75,7 @@ impl EntityIndexMap { /// Returns a mutable slice of all the key-value pairs in the map. /// /// Equivalent to [`IndexMap::as_mut_slice`]. + #[inline] pub fn as_mut_slice(&mut self) -> &mut Slice { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) } @@ -78,6 +84,7 @@ impl EntityIndexMap { /// Converts into a boxed slice of all the key-value pairs in the map. /// /// Equivalent to [`IndexMap::into_boxed_slice`]. + #[inline] pub fn into_boxed_slice(self) -> Box> { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) } @@ -86,6 +93,7 @@ impl EntityIndexMap { /// Returns a slice of key-value pairs in the given range of indices. /// /// Equivalent to [`IndexMap::get_range`]. + #[inline] pub fn get_range>(&self, range: R) -> Option<&Slice> { self.0.get_range(range).map(|slice| // SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice. @@ -95,6 +103,7 @@ impl EntityIndexMap { /// Returns a mutable slice of key-value pairs in the given range of indices. /// /// Equivalent to [`IndexMap::get_range_mut`]. + #[inline] pub fn get_range_mut>(&mut self, range: R) -> Option<&mut Slice> { self.0.get_range_mut(range).map(|slice| // SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice. @@ -104,6 +113,7 @@ impl EntityIndexMap { /// Return an iterator over the key-value pairs of the map, in their order. /// /// Equivalent to [`IndexMap::iter`]. + #[inline] pub fn iter(&self) -> Iter<'_, V> { Iter(self.0.iter(), PhantomData) } @@ -111,6 +121,7 @@ impl EntityIndexMap { /// Return a mutable iterator over the key-value pairs of the map, in their order. /// /// Equivalent to [`IndexMap::iter_mut`]. + #[inline] pub fn iter_mut(&mut self) -> IterMut<'_, V> { IterMut(self.0.iter_mut(), PhantomData) } @@ -119,6 +130,7 @@ impl EntityIndexMap { /// key-value pairs as a drain iterator. /// /// Equivalent to [`IndexMap::drain`]. + #[inline] pub fn drain>(&mut self, range: R) -> Drain<'_, V> { Drain(self.0.drain(range), PhantomData) } @@ -126,6 +138,7 @@ impl EntityIndexMap { /// Return an iterator over the keys of the map, in their order. /// /// Equivalent to [`IndexMap::keys`]. + #[inline] pub fn keys(&self) -> Keys<'_, V> { Keys(self.0.keys(), PhantomData) } @@ -133,12 +146,14 @@ impl EntityIndexMap { /// Return an owning iterator over the keys of the map, in their order. /// /// Equivalent to [`IndexMap::into_keys`]. + #[inline] pub fn into_keys(self) -> IntoKeys { IntoKeys(self.0.into_keys(), PhantomData) } } impl Default for EntityIndexMap { + #[inline] fn default() -> Self { Self(Default::default()) } @@ -147,36 +162,42 @@ impl Default for EntityIndexMap { impl Deref for EntityIndexMap { type Target = IndexMap; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for EntityIndexMap { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } impl<'a, V: Copy> Extend<(&'a Entity, &'a V)> for EntityIndexMap { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl Extend<(Entity, V)> for EntityIndexMap { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl From<[(Entity, V); N]> for EntityIndexMap { + #[inline] fn from(value: [(Entity, V); N]) -> Self { Self(IndexMap::from_iter(value)) } } impl FromIterator<(Entity, V)> for EntityIndexMap { + #[inline] fn from_iter>(iterable: I) -> Self { Self(IndexMap::from_iter(iterable)) } @@ -184,6 +205,7 @@ impl FromIterator<(Entity, V)> for EntityIndexMap { impl Index<&Q> for EntityIndexMap { type Output = V; + #[inline] fn index(&self, key: &Q) -> &V { self.0.index(&key.entity()) } @@ -191,6 +213,7 @@ impl Index<&Q> for EntityIndexMap { impl Index<(Bound, Bound)> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -199,6 +222,7 @@ impl Index<(Bound, Bound)> for EntityIndexMap { impl Index> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: Range) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -207,6 +231,7 @@ impl Index> for EntityIndexMap { impl Index> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -215,6 +240,7 @@ impl Index> for EntityIndexMap { impl Index for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -223,6 +249,7 @@ impl Index for EntityIndexMap { impl Index> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -231,6 +258,7 @@ impl Index> for EntityIndexMap { impl Index> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -239,6 +267,7 @@ impl Index> for EntityIndexMap { impl Index> for EntityIndexMap { type Output = Slice; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -247,18 +276,21 @@ impl Index> for EntityIndexMap { impl Index for EntityIndexMap { type Output = V; + #[inline] fn index(&self, key: usize) -> &V { self.0.index(key) } } impl IndexMut<&Q> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: &Q) -> &mut V { self.0.index_mut(&key.entity()) } } impl IndexMut<(Bound, Bound)> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -266,6 +298,7 @@ impl IndexMut<(Bound, Bound)> for EntityIndexMap { } impl IndexMut> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -273,6 +306,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -280,6 +314,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -287,6 +322,7 @@ impl IndexMut for EntityIndexMap { } impl IndexMut> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -294,6 +330,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -301,6 +338,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut> for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -308,6 +346,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: usize) -> &mut V { self.0.index_mut(key) } @@ -317,6 +356,7 @@ impl<'a, V> IntoIterator for &'a EntityIndexMap { type Item = (&'a Entity, &'a V); type IntoIter = Iter<'a, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { Iter(self.0.iter(), PhantomData) } @@ -326,6 +366,7 @@ impl<'a, V> IntoIterator for &'a mut EntityIndexMap { type Item = (&'a Entity, &'a mut V); type IntoIter = IterMut<'a, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { IterMut(self.0.iter_mut(), PhantomData) } @@ -335,6 +376,7 @@ impl IntoIterator for EntityIndexMap { type Item = (Entity, V); type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter(), PhantomData) } @@ -345,6 +387,7 @@ where V1: PartialEq, S2: BuildHasher, { + #[inline] fn eq(&self, other: &IndexMap) -> bool { self.0.eq(other) } @@ -354,6 +397,7 @@ impl PartialEq> for EntityIndexMap where V1: PartialEq, { + #[inline] fn eq(&self, other: &EntityIndexMap) -> bool { self.0.eq(other) } @@ -372,6 +416,7 @@ impl Slice { /// Returns an empty slice. /// /// Equivalent to [`map::Slice::new`]. + #[inline] pub const fn new<'a>() -> &'a Self { // SAFETY: The source slice is empty. unsafe { Self::from_slice_unchecked(map::Slice::new()) } @@ -380,6 +425,7 @@ impl Slice { /// Returns an empty mutable slice. /// /// Equivalent to [`map::Slice::new_mut`]. + #[inline] pub fn new_mut<'a>() -> &'a mut Self { // SAFETY: The source slice is empty. unsafe { Self::from_slice_unchecked_mut(map::Slice::new_mut()) } @@ -392,6 +438,7 @@ impl Slice { /// `slice` must stem from an [`IndexMap`] using [`EntityHash`]. /// /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice` + #[inline] pub const unsafe fn from_slice_unchecked(slice: &map::Slice) -> &Self { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { &*(ptr::from_ref(slice) as *const Self) } @@ -404,12 +451,14 @@ impl Slice { /// `slice` must stem from an [`IndexMap`] using [`EntityHash`]. /// /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice` + #[inline] pub const unsafe fn from_slice_unchecked_mut(slice: &mut map::Slice) -> &mut Self { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { &mut *(ptr::from_mut(slice) as *mut Self) } } /// Casts `self` to the inner slice. + #[inline] pub const fn as_inner(&self) -> &map::Slice { &self.1 } @@ -421,6 +470,7 @@ impl Slice { /// `slice` must stem from an [`IndexMap`] using [`EntityHash`]. /// /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice` + #[inline] pub unsafe fn from_boxed_slice_unchecked(slice: Box>) -> Box { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) } @@ -431,12 +481,14 @@ impl Slice { clippy::borrowed_box, reason = "We wish to access the Box API of the inner type, without consuming it." )] + #[inline] pub fn as_boxed_inner(self: &Box) -> &Box> { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { &*(ptr::from_ref(self).cast::>>()) } } /// Casts `self` to the inner slice. + #[inline] pub fn into_boxed_inner(self: Box) -> Box> { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { Box::from_raw(Box::into_raw(self) as *mut map::Slice) } @@ -445,6 +497,7 @@ impl Slice { /// Get a key-value pair by index, with mutable access to the value. /// /// Equivalent to [`map::Slice::get_index_mut`]. + #[inline] pub fn get_index_mut(&mut self, index: usize) -> Option<(&Entity, &mut V)> { self.1.get_index_mut(index) } @@ -452,6 +505,7 @@ impl Slice { /// Returns a slice of key-value pairs in the given range of indices. /// /// Equivalent to [`map::Slice::get_range`]. + #[inline] pub fn get_range>(&self, range: R) -> Option<&Self> { self.1.get_range(range).map(|slice| // SAFETY: This a subslice of a valid slice. @@ -461,6 +515,7 @@ impl Slice { /// Returns a mutable slice of key-value pairs in the given range of indices. /// /// Equivalent to [`map::Slice::get_range_mut`]. + #[inline] pub fn get_range_mut>(&mut self, range: R) -> Option<&mut Self> { self.1.get_range_mut(range).map(|slice| // SAFETY: This a subslice of a valid slice. @@ -470,6 +525,7 @@ impl Slice { /// Get the first key-value pair, with mutable access to the value. /// /// Equivalent to [`map::Slice::first_mut`]. + #[inline] pub fn first_mut(&mut self) -> Option<(&Entity, &mut V)> { self.1.first_mut() } @@ -477,6 +533,7 @@ impl Slice { /// Get the last key-value pair, with mutable access to the value. /// /// Equivalent to [`map::Slice::last_mut`]. + #[inline] pub fn last_mut(&mut self) -> Option<(&Entity, &mut V)> { self.1.last_mut() } @@ -484,6 +541,7 @@ impl Slice { /// Divides one slice into two at an index. /// /// Equivalent to [`map::Slice::split_at`]. + #[inline] pub fn split_at(&self, index: usize) -> (&Self, &Self) { let (slice_1, slice_2) = self.1.split_at(index); // SAFETY: These are subslices of a valid slice. @@ -498,6 +556,7 @@ impl Slice { /// Divides one mutable slice into two at an index. /// /// Equivalent to [`map::Slice::split_at_mut`]. + #[inline] pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) { let (slice_1, slice_2) = self.1.split_at_mut(index); // SAFETY: These are subslices of a valid slice. @@ -513,6 +572,7 @@ impl Slice { /// or `None` if it is empty. /// /// Equivalent to [`map::Slice::split_first`]. + #[inline] pub fn split_first(&self) -> Option<((&Entity, &V), &Self)> { self.1.split_first().map(|(first, rest)| { ( @@ -527,6 +587,7 @@ impl Slice { /// with mutable access to the value, or `None` if it is empty. /// /// Equivalent to [`map::Slice::split_first_mut`]. + #[inline] pub fn split_first_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> { self.1.split_first_mut().map(|(first, rest)| { ( @@ -541,6 +602,7 @@ impl Slice { /// or `None` if it is empty. /// /// Equivalent to [`map::Slice::split_last`]. + #[inline] pub fn split_last(&self) -> Option<((&Entity, &V), &Self)> { self.1.split_last().map(|(last, rest)| { ( @@ -555,6 +617,7 @@ impl Slice { /// with mutable access to the value, or `None` if it is empty. /// /// Equivalent to [`map::Slice::split_last_mut`]. + #[inline] pub fn split_last_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> { self.1.split_last_mut().map(|(last, rest)| { ( @@ -568,6 +631,7 @@ impl Slice { /// Return an iterator over the key-value pairs of the map slice. /// /// Equivalent to [`map::Slice::iter`]. + #[inline] pub fn iter(&self) -> Iter<'_, V> { Iter(self.1.iter(), PhantomData) } @@ -575,6 +639,7 @@ impl Slice { /// Return an iterator over the key-value pairs of the map slice. /// /// Equivalent to [`map::Slice::iter_mut`]. + #[inline] pub fn iter_mut(&mut self) -> IterMut<'_, V> { IterMut(self.1.iter_mut(), PhantomData) } @@ -582,6 +647,7 @@ impl Slice { /// Return an iterator over the keys of the map slice. /// /// Equivalent to [`map::Slice::keys`]. + #[inline] pub fn keys(&self) -> Keys<'_, V> { Keys(self.1.keys(), PhantomData) } @@ -589,6 +655,7 @@ impl Slice { /// Return an owning iterator over the keys of the map slice. /// /// Equivalent to [`map::Slice::into_keys`]. + #[inline] pub fn into_keys(self: Box) -> IntoKeys { IntoKeys(self.into_boxed_inner().into_keys(), PhantomData) } @@ -596,6 +663,7 @@ impl Slice { /// Return an iterator over mutable references to the the values of the map slice. /// /// Equivalent to [`map::Slice::values_mut`]. + #[inline] pub fn values_mut(&mut self) -> ValuesMut<'_, Entity, V> { self.1.values_mut() } @@ -603,6 +671,7 @@ impl Slice { /// Return an owning iterator over the values of the map slice. /// /// Equivalent to [`map::Slice::into_values`]. + #[inline] pub fn into_values(self: Box) -> IntoValues { self.into_boxed_inner().into_values() } @@ -611,6 +680,7 @@ impl Slice { impl Deref for Slice { type Target = map::Slice; + #[inline] fn deref(&self) -> &Self::Target { &self.1 } @@ -626,6 +696,7 @@ impl Debug for Slice { } impl Clone for Box> { + #[inline] fn clone(&self) -> Self { // SAFETY: This a clone of a valid slice. unsafe { Slice::from_boxed_slice_unchecked(self.as_boxed_inner().clone()) } @@ -633,6 +704,7 @@ impl Clone for Box> { } impl Default for &Slice { + #[inline] fn default() -> Self { // SAFETY: The source slice is empty. unsafe { Slice::from_slice_unchecked(<&map::Slice>::default()) } @@ -640,6 +712,7 @@ impl Default for &Slice { } impl Default for &mut Slice { + #[inline] fn default() -> Self { // SAFETY: The source slice is empty. unsafe { Slice::from_slice_unchecked_mut(<&mut map::Slice>::default()) } @@ -647,6 +720,7 @@ impl Default for &mut Slice { } impl Default for Box> { + #[inline] fn default() -> Self { // SAFETY: The source slice is empty. unsafe { Slice::from_boxed_slice_unchecked(>>::default()) } @@ -654,6 +728,7 @@ impl Default for Box> { } impl From<&Slice> for Box> { + #[inline] fn from(value: &Slice) -> Self { // SAFETY: This slice is a copy of a valid slice. unsafe { Slice::from_boxed_slice_unchecked(value.1.into()) } @@ -661,6 +736,7 @@ impl From<&Slice> for Box> { } impl Hash for Slice { + #[inline] fn hash(&self, state: &mut H) { self.1.hash(state); } @@ -670,6 +746,7 @@ impl<'a, V> IntoIterator for &'a Slice { type Item = (&'a Entity, &'a V); type IntoIter = Iter<'a, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { Iter(self.1.iter(), PhantomData) } @@ -679,6 +756,7 @@ impl<'a, V> IntoIterator for &'a mut Slice { type Item = (&'a Entity, &'a mut V); type IntoIter = IterMut<'a, V>; + #[inline] fn into_iter(self) -> Self::IntoIter { IterMut(self.1.iter_mut(), PhantomData) } @@ -688,24 +766,28 @@ impl IntoIterator for Box> { type Item = (Entity, V); type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.into_boxed_inner().into_iter(), PhantomData) } } impl PartialOrd for Slice { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.1.partial_cmp(&other.1) } } impl Ord for Slice { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.1.cmp(other) } } impl PartialEq for Slice { + #[inline] fn eq(&self, other: &Self) -> bool { self.1 == other.1 } @@ -715,6 +797,7 @@ impl Eq for Slice {} impl Index<(Bound, Bound)> for Slice { type Output = Self; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -723,6 +806,7 @@ impl Index<(Bound, Bound)> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: Range) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -731,6 +815,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeFrom) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -739,6 +824,7 @@ impl Index> for Slice { impl Index for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeFull) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -747,6 +833,7 @@ impl Index for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeInclusive) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -755,6 +842,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeTo) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -763,6 +851,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -771,12 +860,14 @@ impl Index> for Slice { impl Index for Slice { type Output = V; + #[inline] fn index(&self, key: usize) -> &V { self.1.index(key) } } impl IndexMut<(Bound, Bound)> for Slice { + #[inline] fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -784,6 +875,7 @@ impl IndexMut<(Bound, Bound)> for Slice { } impl IndexMut> for Slice { + #[inline] fn index_mut(&mut self, key: Range) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -791,6 +883,7 @@ impl IndexMut> for Slice { } impl IndexMut> for Slice { + #[inline] fn index_mut(&mut self, key: RangeFrom) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -798,6 +891,7 @@ impl IndexMut> for Slice { } impl IndexMut for Slice { + #[inline] fn index_mut(&mut self, key: RangeFull) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -805,6 +899,7 @@ impl IndexMut for Slice { } impl IndexMut> for Slice { + #[inline] fn index_mut(&mut self, key: RangeInclusive) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -812,6 +907,7 @@ impl IndexMut> for Slice { } impl IndexMut> for Slice { + #[inline] fn index_mut(&mut self, key: RangeTo) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -819,6 +915,7 @@ impl IndexMut> for Slice { } impl IndexMut> for Slice { + #[inline] fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) } @@ -826,6 +923,7 @@ impl IndexMut> for Slice { } impl IndexMut for Slice { + #[inline] fn index_mut(&mut self, key: usize) -> &mut V { self.1.index_mut(key) } @@ -844,11 +942,13 @@ impl<'a, V> Iter<'a, V> { /// /// `iter` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_iter_unchecked(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> { Iter::<'_, _, S>(iter, PhantomData) } /// Returns the inner [`Iter`](map::Iter). + #[inline] pub fn into_inner(self) -> map::Iter<'a, Entity, V> { self.0 } @@ -856,6 +956,7 @@ impl<'a, V> Iter<'a, V> { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`map::Iter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -865,6 +966,7 @@ impl<'a, V> Iter<'a, V> { impl<'a, V> Deref for Iter<'a, V> { type Target = map::Iter<'a, Entity, V>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -873,12 +975,14 @@ impl<'a, V> Deref for Iter<'a, V> { impl<'a, V> Iterator for Iter<'a, V> { type Item = (&'a Entity, &'a V); + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for Iter<'_, V> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -889,6 +993,7 @@ impl ExactSizeIterator for Iter<'_, V> {} impl FusedIterator for Iter<'_, V> {} impl Clone for Iter<'_, V> { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -901,6 +1006,7 @@ impl Debug for Iter<'_, V> { } impl Default for Iter<'_, V> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -919,6 +1025,7 @@ impl<'a, V> IterMut<'a, V> { /// /// `iter_mut` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_iter_mut_unchecked( iter_mut: map::IterMut<'a, Entity, V>, ) -> IterMut<'a, V, S> { @@ -926,6 +1033,7 @@ impl<'a, V> IterMut<'a, V> { } /// Returns the inner [`IterMut`](map::IterMut). + #[inline] pub fn into_inner(self) -> map::IterMut<'a, Entity, V> { self.0 } @@ -933,6 +1041,7 @@ impl<'a, V> IterMut<'a, V> { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`map::IterMut::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -941,6 +1050,7 @@ impl<'a, V> IterMut<'a, V> { /// Returns a mutable slice of the remaining entries in the iterator. /// /// Equivalent to [`map::IterMut::into_slice`]. + #[inline] pub fn into_slice(self) -> &'a mut Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.into_slice()) } @@ -950,6 +1060,7 @@ impl<'a, V> IterMut<'a, V> { impl<'a, V> Deref for IterMut<'a, V> { type Target = map::IterMut<'a, Entity, V>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -958,12 +1069,14 @@ impl<'a, V> Deref for IterMut<'a, V> { impl<'a, V> Iterator for IterMut<'a, V> { type Item = (&'a Entity, &'a mut V); + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for IterMut<'_, V> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -983,6 +1096,7 @@ impl Debug for IterMut<'_, V> { } impl Default for IterMut<'_, V> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -1001,6 +1115,7 @@ impl IntoIter { /// /// `into_iter` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_into_iter_unchecked( into_iter: map::IntoIter, ) -> IntoIter { @@ -1008,6 +1123,7 @@ impl IntoIter { } /// Returns the inner [`IntoIter`](map::IntoIter). + #[inline] pub fn into_inner(self) -> map::IntoIter { self.0 } @@ -1015,6 +1131,7 @@ impl IntoIter { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`map::IntoIter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -1023,6 +1140,7 @@ impl IntoIter { /// Returns a mutable slice of the remaining entries in the iterator. /// /// Equivalent to [`map::IntoIter::as_mut_slice`]. + #[inline] pub fn as_mut_slice(&mut self) -> &mut Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) } @@ -1032,6 +1150,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = map::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1040,12 +1159,14 @@ impl Deref for IntoIter { impl Iterator for IntoIter { type Item = (Entity, V); + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for IntoIter { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -1056,6 +1177,7 @@ impl ExactSizeIterator for IntoIter {} impl FusedIterator for IntoIter {} impl Clone for IntoIter { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -1071,6 +1193,7 @@ impl Debug for IntoIter { } impl Default for IntoIter { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -1089,11 +1212,13 @@ impl<'a, V> Drain<'a, V> { /// /// `drain` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_drain_unchecked(drain: map::Drain<'a, Entity, V>) -> Drain<'a, V, S> { Drain::<'_, _, S>(drain, PhantomData) } /// Returns the inner [`Drain`](map::Drain). + #[inline] pub fn into_inner(self) -> map::Drain<'a, Entity, V> { self.0 } @@ -1101,6 +1226,7 @@ impl<'a, V> Drain<'a, V> { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`map::Drain::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexMap uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -1110,6 +1236,7 @@ impl<'a, V> Drain<'a, V> { impl<'a, V> Deref for Drain<'a, V> { type Target = map::Drain<'a, Entity, V>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1118,12 +1245,14 @@ impl<'a, V> Deref for Drain<'a, V> { impl Iterator for Drain<'_, V> { type Item = (Entity, V); + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for Drain<'_, V> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -1155,11 +1284,13 @@ impl<'a, V> Keys<'a, V> { /// /// `keys` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_keys_unchecked(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { Keys::<'_, _, S>(keys, PhantomData) } /// Returns the inner [`Keys`](map::Keys). + #[inline] pub fn into_inner(self) -> map::Keys<'a, Entity, V> { self.0 } @@ -1168,6 +1299,7 @@ impl<'a, V> Keys<'a, V> { impl<'a, V, S> Deref for Keys<'a, V, S> { type Target = map::Keys<'a, Entity, V>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1176,12 +1308,14 @@ impl<'a, V, S> Deref for Keys<'a, V, S> { impl<'a, V> Iterator for Keys<'a, V> { type Item = &'a Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for Keys<'_, V> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -1194,12 +1328,14 @@ impl FusedIterator for Keys<'_, V> {} impl Index for Keys<'_, V> { type Output = Entity; + #[inline] fn index(&self, index: usize) -> &Entity { self.0.index(index) } } impl Clone for Keys<'_, V> { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -1212,6 +1348,7 @@ impl Debug for Keys<'_, V> { } impl Default for Keys<'_, V> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -1233,6 +1370,7 @@ impl IntoKeys { /// /// `into_keys` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. + #[inline] pub unsafe fn from_into_keys_unchecked( into_keys: map::IntoKeys, ) -> IntoKeys { @@ -1240,6 +1378,7 @@ impl IntoKeys { } /// Returns the inner [`IntoKeys`](map::IntoKeys). + #[inline] pub fn into_inner(self) -> map::IntoKeys { self.0 } @@ -1248,6 +1387,7 @@ impl IntoKeys { impl Deref for IntoKeys { type Target = map::IntoKeys; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1256,12 +1396,14 @@ impl Deref for IntoKeys { impl Iterator for IntoKeys { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for IntoKeys { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -1281,6 +1423,7 @@ impl Debug for IntoKeys { } impl Default for IntoKeys { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index 2fe94534f7bd5..3bcf69263e89d 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -33,6 +33,7 @@ impl EntityIndexSet { /// Equivalent to [`IndexSet::with_hasher(EntityHash)`]. /// /// [`IndexSet::with_hasher(EntityHash)`]: IndexSet::with_hasher + #[inline] pub const fn new() -> Self { Self(IndexSet::with_hasher(EntityHash)) } @@ -42,16 +43,19 @@ impl EntityIndexSet { /// Equivalent to [`IndexSet::with_capacity_and_hasher(n, EntityHash)`]. /// /// [`IndexSet::with_capacity_and_hasher(n, EntityHash)`]: IndexSet::with_capacity_and_hasher + #[inline] pub fn with_capacity(n: usize) -> Self { Self(IndexSet::with_capacity_and_hasher(n, EntityHash)) } /// Constructs an `EntityIndexSet` from an [`IndexSet`]. + #[inline] pub fn from_index_set(set: IndexSet) -> Self { Self(set) } /// Returns the inner [`IndexSet`]. + #[inline] pub fn into_inner(self) -> IndexSet { self.0 } @@ -59,6 +63,7 @@ impl EntityIndexSet { /// Returns a slice of all the values in the set. /// /// Equivalent to [`IndexSet::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -68,6 +73,7 @@ impl EntityIndexSet { /// as a drain iterator. /// /// Equivalent to [`IndexSet::drain`]. + #[inline] pub fn drain>(&mut self, range: R) -> Drain<'_> { Drain(self.0.drain(range), PhantomData) } @@ -75,6 +81,7 @@ impl EntityIndexSet { /// Returns a slice of values in the given range of indices. /// /// Equivalent to [`IndexSet::get_range`]. + #[inline] pub fn get_range>(&self, range: R) -> Option<&Slice> { self.0.get_range(range).map(|slice| // SAFETY: The source IndexSet uses EntityHash. @@ -84,6 +91,7 @@ impl EntityIndexSet { /// Return an iterator over the values of the set, in their order. /// /// Equivalent to [`IndexSet::iter`]. + #[inline] pub fn iter(&self) -> Iter<'_> { Iter(self.0.iter(), PhantomData) } @@ -91,6 +99,7 @@ impl EntityIndexSet { /// Converts into a boxed slice of all the values in the set. /// /// Equivalent to [`IndexSet::into_boxed_slice`]. + #[inline] pub fn into_boxed_slice(self) -> Box { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) } @@ -100,12 +109,14 @@ impl EntityIndexSet { impl Deref for EntityIndexSet { type Target = IndexSet; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for EntityIndexSet { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } @@ -116,6 +127,7 @@ impl<'a> IntoIterator for &'a EntityIndexSet { type IntoIter = Iter<'a>; + #[inline] fn into_iter(self) -> Self::IntoIter { Iter((&self.0).into_iter(), PhantomData) } @@ -126,6 +138,7 @@ impl IntoIterator for EntityIndexSet { type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter(), PhantomData) } @@ -164,24 +177,28 @@ impl Sub for &EntityIndexSet { } impl<'a> Extend<&'a Entity> for EntityIndexSet { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl Extend for EntityIndexSet { + #[inline] fn extend>(&mut self, iter: T) { self.0.extend(iter); } } impl From<[Entity; N]> for EntityIndexSet { + #[inline] fn from(value: [Entity; N]) -> Self { Self(IndexSet::from_iter(value)) } } impl FromIterator for EntityIndexSet { + #[inline] fn from_iter>(iterable: I) -> Self { Self(IndexSet::from_iter(iterable)) } @@ -191,12 +208,14 @@ impl PartialEq> for EntityIndexSet where S2: BuildHasher, { + #[inline] fn eq(&self, other: &IndexSet) -> bool { self.0.eq(other) } } impl PartialEq for EntityIndexSet { + #[inline] fn eq(&self, other: &EntityIndexSet) -> bool { self.0.eq(other) } @@ -206,6 +225,7 @@ impl Eq for EntityIndexSet {} impl Index<(Bound, Bound)> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -214,6 +234,7 @@ impl Index<(Bound, Bound)> for EntityIndexSet { impl Index> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: Range) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -222,6 +243,7 @@ impl Index> for EntityIndexSet { impl Index> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -230,6 +252,7 @@ impl Index> for EntityIndexSet { impl Index for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -238,6 +261,7 @@ impl Index for EntityIndexSet { impl Index> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -246,6 +270,7 @@ impl Index> for EntityIndexSet { impl Index> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -254,6 +279,7 @@ impl Index> for EntityIndexSet { impl Index> for EntityIndexSet { type Output = Slice; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.index(key)) } @@ -262,6 +288,7 @@ impl Index> for EntityIndexSet { impl Index for EntityIndexSet { type Output = Entity; + #[inline] fn index(&self, key: usize) -> &Entity { self.0.index(key) } @@ -278,6 +305,7 @@ impl Slice { /// Returns an empty slice. /// /// Equivalent to [`set::Slice::new`]. + #[inline] pub const fn new<'a>() -> &'a Self { // SAFETY: The source slice is empty. unsafe { Self::from_slice_unchecked(set::Slice::new()) } @@ -290,6 +318,7 @@ impl Slice { /// `slice` must stem from an [`IndexSet`] using [`EntityHash`]. /// /// [`entity::index_set::Slice`]: `crate::entity::index_set::Slice` + #[inline] pub const unsafe fn from_slice_unchecked(slice: &set::Slice) -> &Self { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { &*(ptr::from_ref(slice) as *const Self) } @@ -302,12 +331,14 @@ impl Slice { /// `slice` must stem from an [`IndexSet`] using [`EntityHash`]. /// /// [`entity::index_set::Slice`]: `crate::entity::index_set::Slice` + #[inline] pub const unsafe fn from_slice_unchecked_mut(slice: &mut set::Slice) -> &mut Self { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { &mut *(ptr::from_mut(slice) as *mut Self) } } /// Casts `self` to the inner slice. + #[inline] pub const fn as_inner(&self) -> &set::Slice { &self.1 } @@ -319,6 +350,7 @@ impl Slice { /// `slice` must stem from an [`IndexSet`] using [`EntityHash`]. /// /// [`entity::index_set::Slice`]: `crate::entity::index_set::Slice` + #[inline] pub unsafe fn from_boxed_slice_unchecked(slice: Box>) -> Box { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) } @@ -329,12 +361,14 @@ impl Slice { clippy::borrowed_box, reason = "We wish to access the Box API of the inner type, without consuming it." )] + #[inline] pub fn as_boxed_inner(self: &Box) -> &Box> { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { &*(ptr::from_ref(self).cast::>>()) } } /// Casts `self` to the inner slice. + #[inline] pub fn into_boxed_inner(self: Box) -> Box> { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { Box::from_raw(Box::into_raw(self) as *mut set::Slice) } @@ -343,6 +377,7 @@ impl Slice { /// Returns a slice of values in the given range of indices. /// /// Equivalent to [`set::Slice::get_range`]. + #[inline] pub fn get_range>(&self, range: R) -> Option<&Self> { self.1.get_range(range).map(|slice| // SAFETY: This a subslice of a valid slice. @@ -352,6 +387,7 @@ impl Slice { /// Divides one slice into two at an index. /// /// Equivalent to [`set::Slice::split_at`]. + #[inline] pub fn split_at(&self, index: usize) -> (&Self, &Self) { let (slice_1, slice_2) = self.1.split_at(index); // SAFETY: These are subslices of a valid slice. @@ -367,6 +403,7 @@ impl Slice { /// or `None` if it is empty. /// /// Equivalent to [`set::Slice::split_first`]. + #[inline] pub fn split_first(&self) -> Option<(&Entity, &Self)> { self.1.split_first().map(|(first, rest)| { ( @@ -381,6 +418,7 @@ impl Slice { /// or `None` if it is empty. /// /// Equivalent to [`set::Slice::split_last`]. + #[inline] pub fn split_last(&self) -> Option<(&Entity, &Self)> { self.1.split_last().map(|(last, rest)| { ( @@ -394,6 +432,7 @@ impl Slice { /// Return an iterator over the values of the set slice. /// /// Equivalent to [`set::Slice::iter`]. + #[inline] pub fn iter(&self) -> Iter<'_> { Iter(self.1.iter(), PhantomData) } @@ -402,6 +441,7 @@ impl Slice { impl Deref for Slice { type Target = set::Slice; + #[inline] fn deref(&self) -> &Self::Target { &self.1 } @@ -411,6 +451,7 @@ impl<'a> IntoIterator for &'a Slice { type IntoIter = Iter<'a>; type Item = &'a Entity; + #[inline] fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -420,12 +461,14 @@ impl IntoIterator for Box { type IntoIter = IntoIter; type Item = Entity; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.into_boxed_inner().into_iter(), PhantomData) } } impl Clone for Box { + #[inline] fn clone(&self) -> Self { // SAFETY: This is a clone of a valid slice. unsafe { Slice::from_boxed_slice_unchecked(self.as_boxed_inner().clone()) } @@ -433,6 +476,7 @@ impl Clone for Box { } impl Default for &Slice { + #[inline] fn default() -> Self { // SAFETY: The source slice is empty. unsafe { Slice::from_slice_unchecked(<&set::Slice>::default()) } @@ -440,6 +484,7 @@ impl Default for &Slice { } impl Default for Box { + #[inline] fn default() -> Self { // SAFETY: The source slice is empty. unsafe { Slice::from_boxed_slice_unchecked(>>::default()) } @@ -456,6 +501,7 @@ impl Debug for Slice { } impl From<&Slice> for Box { + #[inline] fn from(value: &Slice) -> Self { // SAFETY: This slice is a copy of a valid slice. unsafe { Slice::from_boxed_slice_unchecked(value.1.into()) } @@ -463,24 +509,28 @@ impl From<&Slice> for Box { } impl Hash for Slice { + #[inline] fn hash(&self, state: &mut H) { self.1.hash(state); } } impl PartialOrd for Slice { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for Slice { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.1.cmp(other) } } impl PartialEq for Slice { + #[inline] fn eq(&self, other: &Self) -> bool { self.1 == other.1 } @@ -490,6 +540,7 @@ impl Eq for Slice {} impl Index<(Bound, Bound)> for Slice { type Output = Self; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -498,6 +549,7 @@ impl Index<(Bound, Bound)> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: Range) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -506,6 +558,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Slice; + #[inline] fn index(&self, key: RangeFrom) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -514,6 +567,7 @@ impl Index> for Slice { impl Index for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeFull) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -522,6 +576,7 @@ impl Index for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeInclusive) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -530,6 +585,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeTo) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -538,6 +594,7 @@ impl Index> for Slice { impl Index> for Slice { type Output = Self; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self { // SAFETY: This a subslice of a valid slice. unsafe { Self::from_slice_unchecked(self.1.index(key)) } @@ -546,6 +603,7 @@ impl Index> for Slice { impl Index for Slice { type Output = Entity; + #[inline] fn index(&self, key: usize) -> &Entity { self.1.index(key) } @@ -565,11 +623,13 @@ impl<'a> Iter<'a> { /// /// `iter` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. + #[inline] pub unsafe fn from_iter_unchecked(iter: set::Iter<'a, Entity>) -> Iter<'a, S> { Iter::<'_, S>(iter, PhantomData) } /// Returns the inner [`Iter`](set::Iter). + #[inline] pub fn into_inner(self) -> set::Iter<'a, Entity> { self.0 } @@ -577,6 +637,7 @@ impl<'a> Iter<'a> { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`set::Iter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -586,6 +647,7 @@ impl<'a> Iter<'a> { impl<'a> Deref for Iter<'a> { type Target = set::Iter<'a, Entity>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -594,12 +656,14 @@ impl<'a> Deref for Iter<'a> { impl<'a> Iterator for Iter<'a> { type Item = &'a Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for Iter<'_> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -610,6 +674,7 @@ impl ExactSizeIterator for Iter<'_> {} impl FusedIterator for Iter<'_> {} impl Clone for Iter<'_> { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -622,6 +687,7 @@ impl Debug for Iter<'_> { } impl Default for Iter<'_> { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -644,11 +710,13 @@ impl IntoIter { /// /// `into_iter` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. + #[inline] pub unsafe fn from_into_iter_unchecked(into_iter: set::IntoIter) -> IntoIter { IntoIter::(into_iter, PhantomData) } /// Returns the inner [`IntoIter`](set::IntoIter). + #[inline] pub fn into_inner(self) -> set::IntoIter { self.0 } @@ -656,6 +724,7 @@ impl IntoIter { /// Returns a slice of the remaining entries in the iterator. /// /// Equivalent to [`set::IntoIter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -665,6 +734,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = set::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -673,12 +743,14 @@ impl Deref for IntoIter { impl Iterator for IntoIter { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for IntoIter { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } @@ -689,6 +761,7 @@ impl ExactSizeIterator for IntoIter {} impl FusedIterator for IntoIter {} impl Clone for IntoIter { + #[inline] fn clone(&self) -> Self { Self(self.0.clone(), PhantomData) } @@ -704,6 +777,7 @@ impl Debug for IntoIter { } impl Default for IntoIter { + #[inline] fn default() -> Self { Self(Default::default(), PhantomData) } @@ -726,11 +800,13 @@ impl<'a> Drain<'a> { /// /// `drain` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. + #[inline] pub unsafe fn from_drain_unchecked(drain: set::Drain<'a, Entity>) -> Drain<'a, S> { Drain::<'_, S>(drain, PhantomData) } /// Returns the inner [`Drain`](set::Drain). + #[inline] pub fn into_inner(self) -> set::Drain<'a, Entity> { self.0 } @@ -738,6 +814,7 @@ impl<'a> Drain<'a> { /// Returns a slice of the remaining entries in the iterator.$ /// /// Equivalent to [`set::Drain::as_slice`]. + #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexSet uses EntityHash. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -747,6 +824,7 @@ impl<'a> Drain<'a> { impl<'a> Deref for Drain<'a> { type Target = set::Drain<'a, Entity>; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -755,12 +833,14 @@ impl<'a> Deref for Drain<'a> { impl<'a> Iterator for Drain<'a> { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } } impl DoubleEndedIterator for Drain<'_> { + #[inline] fn next_back(&mut self) -> Option { self.0.next_back() } diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index 49e62dad4d909..e375b45f56fb4 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -46,6 +46,7 @@ impl UniqueEntityEquivalentArray { /// # Safety /// /// `array` must contain only unique elements. + #[inline] pub const unsafe fn from_array_unchecked(array: [T; N]) -> Self { Self(array) } @@ -55,6 +56,7 @@ impl UniqueEntityEquivalentArray { /// # Safety /// /// `array` must contain only unique elements. + #[inline] pub const unsafe fn from_array_ref_unchecked(array: &[T; N]) -> &Self { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { &*(ptr::from_ref(array).cast()) } @@ -65,12 +67,14 @@ impl UniqueEntityEquivalentArray { /// # Safety /// /// `array` must contain only unique elements. + #[inline] pub unsafe fn from_boxed_array_unchecked(array: Box<[T; N]>) -> Box { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(array).cast()) } } /// Casts `self` into the inner array. + #[inline] pub fn into_boxed_inner(self: Box) -> Box<[T; N]> { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Box::from_raw(Box::into_raw(self).cast()) } @@ -81,12 +85,14 @@ impl UniqueEntityEquivalentArray { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub unsafe fn from_arc_array_unchecked(slice: Arc<[T; N]>) -> Arc { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. + #[inline] pub fn into_arc_inner(this: Arc) -> Arc<[T; N]> { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Arc::from_raw(Arc::into_raw(this).cast()) } @@ -97,28 +103,33 @@ impl UniqueEntityEquivalentArray { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub unsafe fn from_rc_array_unchecked(slice: Rc<[T; N]>) -> Rc { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(slice).cast()) } } /// Casts `self` to the inner array. + #[inline] pub fn into_rc_inner(self: Rc) -> Rc<[T; N]> { // SAFETY: UniqueEntityEquivalentArray is a transparent wrapper around [T; N]. unsafe { Rc::from_raw(Rc::into_raw(self).cast()) } } /// Return the inner array. + #[inline] pub fn into_inner(self) -> [T; N] { self.0 } /// Returns a reference to the inner array. + #[inline] pub fn as_inner(&self) -> &[T; N] { &self.0 } /// Returns a slice containing the entire array. Equivalent to `&s[..]`. + #[inline] pub const fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.as_slice()) } @@ -126,6 +137,7 @@ impl UniqueEntityEquivalentArray { /// Returns a mutable slice containing the entire array. Equivalent to /// `&mut s[..]`. + #[inline] pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } @@ -135,6 +147,7 @@ impl UniqueEntityEquivalentArray { /// size as `self`. /// /// Equivalent to [`[T; N]::as_ref`](array::each_ref). + #[inline] pub fn each_ref(&self) -> UniqueEntityEquivalentArray<&T, N> { UniqueEntityEquivalentArray(self.0.each_ref()) } @@ -143,6 +156,7 @@ impl UniqueEntityEquivalentArray { impl Deref for UniqueEntityEquivalentArray { type Target = UniqueEntityEquivalentSlice; + #[inline] fn deref(&self) -> &Self::Target { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } @@ -150,6 +164,7 @@ impl Deref for UniqueEntityEquivalentArray< } impl DerefMut for UniqueEntityEquivalentArray { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } @@ -157,6 +172,7 @@ impl DerefMut for UniqueEntityEquivalentArr } impl Default for UniqueEntityEquivalentArray { + #[inline] fn default() -> Self { Self(Default::default()) } @@ -169,6 +185,7 @@ impl<'a, T: EntityEquivalent, const N: usize> IntoIterator type IntoIter = unique_slice::Iter<'a, T>; + #[inline] fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } @@ -180,6 +197,7 @@ impl IntoIterator for UniqueEntityEquivalen type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) } @@ -189,6 +207,7 @@ impl IntoIterator for UniqueEntityEquivalen impl AsRef> for UniqueEntityEquivalentArray { + #[inline] fn as_ref(&self) -> &UniqueEntityEquivalentSlice { self } @@ -197,6 +216,7 @@ impl AsRef> impl AsMut> for UniqueEntityEquivalentArray { + #[inline] fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -205,6 +225,7 @@ impl AsMut> impl Borrow> for UniqueEntityEquivalentArray { + #[inline] fn borrow(&self) -> &UniqueEntityEquivalentSlice { self } @@ -213,6 +234,7 @@ impl Borrow> impl BorrowMut> for UniqueEntityEquivalentArray { + #[inline] fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -222,6 +244,7 @@ impl Index<(Bound, Bound)> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -232,6 +255,7 @@ impl Index> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: Range) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -242,6 +266,7 @@ impl Index> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -250,6 +275,7 @@ impl Index> impl Index for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -260,6 +286,7 @@ impl Index> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -270,6 +297,7 @@ impl Index> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -280,6 +308,7 @@ impl Index> for UniqueEntityEquivalentArray { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -288,6 +317,7 @@ impl Index> impl Index for UniqueEntityEquivalentArray { type Output = T; + #[inline] fn index(&self, key: usize) -> &T { self.0.index(key) } @@ -296,6 +326,7 @@ impl Index for UniqueEntityEquivalen impl IndexMut<(Bound, Bound)> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -305,6 +336,7 @@ impl IndexMut<(Bound, Bound)> impl IndexMut> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -314,6 +346,7 @@ impl IndexMut> impl IndexMut> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -323,6 +356,7 @@ impl IndexMut> impl IndexMut for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -332,6 +366,7 @@ impl IndexMut impl IndexMut> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -341,6 +376,7 @@ impl IndexMut> impl IndexMut> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -350,6 +386,7 @@ impl IndexMut> impl IndexMut> for UniqueEntityEquivalentArray { + #[inline] fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -357,90 +394,105 @@ impl IndexMut> } impl From<&[T; 1]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: &[T; 1]) -> Self { Self(value.clone()) } } impl From<&[T; 0]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: &[T; 0]) -> Self { Self(value.clone()) } } impl From<&mut [T; 1]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: &mut [T; 1]) -> Self { Self(value.clone()) } } impl From<&mut [T; 0]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: &mut [T; 0]) -> Self { Self(value.clone()) } } impl From<[T; 1]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: [T; 1]) -> Self { Self(value) } } impl From<[T; 0]> for UniqueEntityEquivalentArray { + #[inline] fn from(value: [T; 0]) -> Self { Self(value) } } impl From> for (T,) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } } impl From> for (T, T, T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } @@ -449,6 +501,7 @@ impl From> for (T, T, T, impl From> for (T, T, T, T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } @@ -457,6 +510,7 @@ impl From> impl From> for (T, T, T, T, T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } @@ -465,6 +519,7 @@ impl From> impl From> for (T, T, T, T, T, T, T, T, T, T, T, T) { + #[inline] fn from(array: UniqueEntityEquivalentArray) -> Self { Self::from(array.into_inner()) } @@ -473,6 +528,7 @@ impl From> impl From> for BTreeSet { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { BTreeSet::from(value.0) } @@ -481,6 +537,7 @@ impl From From> for BinaryHeap { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { BinaryHeap::from(value.0) } @@ -489,18 +546,21 @@ impl From From> for LinkedList { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { LinkedList::from(value.0) } } impl From> for Vec { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { Vec::from(value.0) } } impl From> for VecDeque { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { VecDeque::from(value.0) } @@ -509,6 +569,7 @@ impl From impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentArray { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.as_inner()) } @@ -517,6 +578,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq> for UniqueEntityEquivalentArray { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.0.eq(other.as_inner()) } @@ -525,6 +587,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityEquivalentArray> for Vec { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } @@ -533,6 +596,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityEquivalentArray> for VecDeque { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } @@ -541,6 +605,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq<&mut UniqueEntityEquivalentArray> for VecDeque { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } @@ -549,6 +614,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq> for Vec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } @@ -557,6 +623,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq> for VecDeque { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.eq(&other.0) } @@ -572,6 +639,7 @@ impl UniqueEntityIter /// yet. /// /// Equivalent to [`array::IntoIter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } @@ -580,6 +648,7 @@ impl UniqueEntityIter /// Returns a mutable slice of all elements that have not been yielded yet. /// /// Equivalent to [`array::IntoIter::as_mut_slice`]. + #[inline] pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index dd1b42903fa24..a279bc676359d 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -50,6 +50,7 @@ impl UniqueEntityEquivalentSlice { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub const unsafe fn from_slice_unchecked(slice: &[T]) -> &Self { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { &*(ptr::from_ref(slice) as *const Self) } @@ -60,12 +61,14 @@ impl UniqueEntityEquivalentSlice { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub const unsafe fn from_slice_unchecked_mut(slice: &mut [T]) -> &mut Self { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { &mut *(ptr::from_mut(slice) as *mut Self) } } /// Casts to `self` to a standard slice. + #[inline] pub const fn as_inner(&self) -> &[T] { &self.0 } @@ -75,12 +78,14 @@ impl UniqueEntityEquivalentSlice { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub unsafe fn from_boxed_slice_unchecked(slice: Box<[T]>) -> Box { // 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. + #[inline] pub fn into_boxed_inner(self: Box) -> Box<[T]> { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Box::from_raw(Box::into_raw(self) as *mut [T]) } @@ -91,12 +96,14 @@ impl UniqueEntityEquivalentSlice { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub unsafe fn from_arc_slice_unchecked(slice: Arc<[T]>) -> Arc { // 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. + #[inline] pub fn into_arc_inner(this: Arc) -> Arc<[T]> { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Arc::from_raw(Arc::into_raw(this) as *mut [T]) } @@ -107,12 +114,14 @@ impl UniqueEntityEquivalentSlice { /// # Safety /// /// `slice` must contain only unique elements. + #[inline] pub unsafe fn from_rc_slice_unchecked(slice: Rc<[T]>) -> Rc { // 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. + #[inline] pub fn into_rc_inner(self: Rc) -> Rc<[T]> { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { Rc::from_raw(Rc::into_raw(self) as *mut [T]) } @@ -121,6 +130,7 @@ impl UniqueEntityEquivalentSlice { /// Returns the first and all the rest of the elements of the slice, or `None` if it is empty. /// /// Equivalent to [`[T]::split_first`](slice::split_first). + #[inline] pub const fn split_first(&self) -> Option<(&T, &Self)> { let Some((first, rest)) = self.0.split_first() else { return None; @@ -132,6 +142,7 @@ impl UniqueEntityEquivalentSlice { /// Returns the last and all the rest of the elements of the slice, or `None` if it is empty. /// /// Equivalent to [`[T]::split_last`](slice::split_last). + #[inline] pub const fn split_last(&self) -> Option<(&T, &Self)> { let Some((last, rest)) = self.0.split_last() else { return None; @@ -143,6 +154,7 @@ impl UniqueEntityEquivalentSlice { /// Returns an array reference to the first `N` items in the slice. /// /// Equivalent to [`[T]::first_chunk`](slice::first_chunk). + #[inline] pub const fn first_chunk(&self) -> Option<&UniqueEntityEquivalentArray> { let Some(chunk) = self.0.first_chunk() else { return None; @@ -154,6 +166,7 @@ impl UniqueEntityEquivalentSlice { /// Returns an array reference to the first `N` items in the slice and the remaining slice. /// /// Equivalent to [`[T]::split_first_chunk`](slice::split_first_chunk). + #[inline] pub const fn split_first_chunk( &self, ) -> Option<( @@ -175,6 +188,7 @@ impl UniqueEntityEquivalentSlice { /// Returns an array reference to the last `N` items in the slice and the remaining slice. /// /// Equivalent to [`[T]::split_last_chunk`](slice::split_last_chunk). + #[inline] pub const fn split_last_chunk( &self, ) -> Option<( @@ -196,6 +210,7 @@ impl UniqueEntityEquivalentSlice { /// Returns an array reference to the last `N` items in the slice. /// /// Equivalent to [`[T]::last_chunk`](slice::last_chunk). + #[inline] pub const fn last_chunk(&self) -> Option<&UniqueEntityEquivalentArray> { let Some(chunk) = self.0.last_chunk() else { return None; @@ -211,6 +226,7 @@ impl UniqueEntityEquivalentSlice { /// Note that only the inner [`[T]::get`] supports indexing with a [`usize`]. /// /// [`[T]::get`]: `slice::get` + #[inline] pub fn get(&self, index: I) -> Option<&Self> where Self: Index, @@ -228,6 +244,7 @@ impl UniqueEntityEquivalentSlice { /// Note that `UniqueEntityEquivalentSlice::get_mut` cannot be called with a [`usize`]. /// /// [`[T]::get_mut`]: `slice::get_mut`s + #[inline] pub fn get_mut(&mut self, index: I) -> Option<&mut Self> where Self: Index, @@ -249,6 +266,7 @@ impl UniqueEntityEquivalentSlice { /// `index` must be safe to use with [`[T]::get_unchecked`] /// /// [`[T]::get_unchecked`]: `slice::get_unchecked` + #[inline] pub unsafe fn get_unchecked(&self, index: I) -> &Self where Self: Index, @@ -268,6 +286,7 @@ impl UniqueEntityEquivalentSlice { /// `index` must be safe to use with [`[T]::get_unchecked_mut`] /// /// [`[T]::get_unchecked_mut`]: `slice::get_unchecked_mut` + #[inline] pub unsafe fn get_unchecked_mut(&mut self, index: I) -> &mut Self where Self: Index, @@ -278,26 +297,31 @@ impl UniqueEntityEquivalentSlice { } /// Returns an unsafe mutable pointer to the slice's buffer. + #[inline] pub const fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr() } /// Returns the two unsafe mutable pointers spanning the slice. + #[inline] pub const fn as_mut_ptr_range(&mut self) -> Range<*mut T> { self.0.as_mut_ptr_range() } /// Swaps two elements in the slice. + #[inline] pub fn swap(&mut self, a: usize, b: usize) { self.0.swap(a, b); } /// Reverses the order of elements in the slice, in place. + #[inline] pub fn reverse(&mut self) { self.0.reverse(); } /// Returns an iterator over the slice. + #[inline] pub fn iter(&self) -> Iter<'_, T> { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } @@ -309,6 +333,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::windows`]. /// /// [`[T]::windows`]: `slice::windows` + #[inline] pub fn windows(&self, size: usize) -> Windows<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.windows(size)) } @@ -320,6 +345,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunks`]. /// /// [`[T]::chunks`]: `slice::chunks` + #[inline] pub fn chunks(&self, chunk_size: usize) -> Chunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -333,6 +359,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunks_mut`]. /// /// [`[T]::chunks_mut`]: `slice::chunks_mut` + #[inline] pub fn chunks_mut(&mut self, chunk_size: usize) -> ChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -347,6 +374,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunks_exact`]. /// /// [`[T]::chunks_exact`]: `slice::chunks_exact` + #[inline] pub fn chunks_exact(&self, chunk_size: usize) -> ChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -362,6 +390,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunks_exact_mut`]. /// /// [`[T]::chunks_exact_mut`]: `slice::chunks_exact_mut` + #[inline] pub fn chunks_exact_mut(&mut self, chunk_size: usize) -> ChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -377,6 +406,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rchunks`]. /// /// [`[T]::rchunks`]: `slice::rchunks` + #[inline] pub fn rchunks(&self, chunk_size: usize) -> RChunks<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -390,6 +420,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rchunks_mut`]. /// /// [`[T]::rchunks_mut`]: `slice::rchunks_mut` + #[inline] pub fn rchunks_mut(&mut self, chunk_size: usize) -> RChunksMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -405,6 +436,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rchunks_exact`]. /// /// [`[T]::rchunks_exact`]: `slice::rchunks_exact` + #[inline] pub fn rchunks_exact(&self, chunk_size: usize) -> RChunksExact<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -420,6 +452,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rchunks_exact_mut`]. /// /// [`[T]::rchunks_exact_mut`]: `slice::rchunks_exact_mut` + #[inline] pub fn rchunks_exact_mut(&mut self, chunk_size: usize) -> RChunksExactMut<'_, T> { // SAFETY: Any subslice of a unique slice is also unique. unsafe { @@ -435,6 +468,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunk_by`]. /// /// [`[T]::chunk_by`]: `slice::chunk_by` + #[inline] pub fn chunk_by(&self, pred: F) -> ChunkBy<'_, F, T> where F: FnMut(&T, &T) -> bool, @@ -449,6 +483,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::chunk_by_mut`]. /// /// [`[T]::chunk_by_mut`]: `slice::chunk_by_mut` + #[inline] pub fn chunk_by_mut(&mut self, pred: F) -> ChunkByMut<'_, F, T> where F: FnMut(&T, &T) -> bool, @@ -464,6 +499,7 @@ impl UniqueEntityEquivalentSlice { /// Divides one slice into two at an index. /// /// Equivalent to [`[T]::split_at`](slice::split_at). + #[inline] pub const fn split_at(&self, mid: usize) -> (&Self, &Self) { let (left, right) = self.0.split_at(mid); // SAFETY: All elements in the original slice are unique. @@ -478,6 +514,7 @@ impl UniqueEntityEquivalentSlice { /// Divides one mutable slice into two at an index. /// /// Equivalent to [`[T]::split_at_mut`](slice::split_at_mut). + #[inline] pub const fn split_at_mut(&mut self, mid: usize) -> (&mut Self, &mut Self) { let (left, right) = self.0.split_at_mut(mid); // SAFETY: All elements in the original slice are unique. @@ -498,6 +535,7 @@ impl UniqueEntityEquivalentSlice { /// `mid` must be safe to use in [`[T]::split_at_unchecked`]. /// /// [`[T]::split_at_unchecked`]: `slice::split_at_unchecked` + #[inline] pub const unsafe fn split_at_unchecked(&self, mid: usize) -> (&Self, &Self) { // SAFETY: The safety contract is upheld by the caller. let (left, right) = unsafe { self.0.split_at_unchecked(mid) }; @@ -519,6 +557,7 @@ impl UniqueEntityEquivalentSlice { /// `mid` must be safe to use in [`[T]::split_at_mut_unchecked`]. /// /// [`[T]::split_at_mut_unchecked`]: `slice::split_at_mut_unchecked` + #[inline] pub const unsafe fn split_at_mut_unchecked(&mut self, mid: usize) -> (&mut Self, &mut Self) { // SAFETY: The safety contract is upheld by the caller. let (left, right) = unsafe { self.0.split_at_mut_unchecked(mid) }; @@ -535,6 +574,7 @@ impl UniqueEntityEquivalentSlice { /// too short. /// /// Equivalent to [`[T]::split_at_checked`](slice::split_at_checked). + #[inline] pub const fn split_at_checked(&self, mid: usize) -> Option<(&Self, &Self)> { let Some((left, right)) = self.0.split_at_checked(mid) else { return None; @@ -552,6 +592,7 @@ impl UniqueEntityEquivalentSlice { /// slice is too short. /// /// Equivalent to [`[T]::split_at_mut_checked`](slice::split_at_mut_checked). + #[inline] pub const fn split_at_mut_checked(&mut self, mid: usize) -> Option<(&mut Self, &mut Self)> { let Some((left, right)) = self.0.split_at_mut_checked(mid) else { return None; @@ -571,6 +612,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::split`]. /// /// [`[T]::split`]: `slice::split` + #[inline] pub fn split(&self, pred: F) -> Split<'_, F, T> where F: FnMut(&T) -> bool, @@ -585,6 +627,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::split_mut`]. /// /// [`[T]::split_mut`]: `slice::split_mut` + #[inline] pub fn split_mut(&mut self, pred: F) -> SplitMut<'_, F, T> where F: FnMut(&T) -> bool, @@ -603,6 +646,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::split_inclusive`]. /// /// [`[T]::split_inclusive`]: `slice::split_inclusive` + #[inline] pub fn split_inclusive(&self, pred: F) -> SplitInclusive<'_, F, T> where F: FnMut(&T) -> bool, @@ -619,6 +663,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::split_inclusive_mut`]. /// /// [`[T]::split_inclusive_mut`]: `slice::split_inclusive_mut` + #[inline] pub fn split_inclusive_mut(&mut self, pred: F) -> SplitInclusiveMut<'_, F, T> where F: FnMut(&T) -> bool, @@ -637,6 +682,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rsplit`]. /// /// [`[T]::rsplit`]: `slice::rsplit` + #[inline] pub fn rsplit(&self, pred: F) -> RSplit<'_, F, T> where F: FnMut(&T) -> bool, @@ -652,6 +698,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rsplit_mut`]. /// /// [`[T]::rsplit_mut`]: `slice::rsplit_mut` + #[inline] pub fn rsplit_mut(&mut self, pred: F) -> RSplitMut<'_, F, T> where F: FnMut(&T) -> bool, @@ -670,6 +717,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::splitn`]. /// /// [`[T]::splitn`]: `slice::splitn` + #[inline] pub fn splitn(&self, n: usize, pred: F) -> SplitN<'_, F, T> where F: FnMut(&T) -> bool, @@ -686,6 +734,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::splitn_mut`]. /// /// [`[T]::splitn_mut`]: `slice::splitn_mut` + #[inline] pub fn splitn_mut(&mut self, n: usize, pred: F) -> SplitNMut<'_, F, T> where F: FnMut(&T) -> bool, @@ -704,6 +753,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rsplitn`]. /// /// [`[T]::rsplitn`]: `slice::rsplitn` + #[inline] pub fn rsplitn(&self, n: usize, pred: F) -> RSplitN<'_, F, T> where F: FnMut(&T) -> bool, @@ -720,6 +770,7 @@ impl UniqueEntityEquivalentSlice { /// Equivalent to [`[T]::rsplitn_mut`]. /// /// [`[T]::rsplitn_mut`]: `slice::rsplitn_mut` + #[inline] pub fn rsplitn_mut(&mut self, n: usize, pred: F) -> RSplitNMut<'_, F, T> where F: FnMut(&T) -> bool, @@ -735,6 +786,7 @@ impl UniqueEntityEquivalentSlice { /// Sorts the slice **without** preserving the initial order of equal elements. /// /// Equivalent to [`[T]::sort_unstable`](slice::sort_unstable). + #[inline] pub fn sort_unstable(&mut self) where T: Ord, @@ -746,6 +798,7 @@ impl UniqueEntityEquivalentSlice { /// equal elements. /// /// Equivalent to [`[T]::sort_unstable_by`](slice::sort_unstable_by). + #[inline] pub fn sort_unstable_by(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering, @@ -757,6 +810,7 @@ impl UniqueEntityEquivalentSlice { /// equal elements. /// /// Equivalent to [`[T]::sort_unstable_by_key`](slice::sort_unstable_by_key). + #[inline] pub fn sort_unstable_by_key(&mut self, f: F) where F: FnMut(&T) -> K, @@ -770,6 +824,7 @@ impl UniqueEntityEquivalentSlice { /// the front. /// /// Equivalent to [`[T]::rotate_left`](slice::rotate_left). + #[inline] pub fn rotate_left(&mut self, mid: usize) { self.0.rotate_left(mid); } @@ -779,6 +834,7 @@ impl UniqueEntityEquivalentSlice { /// to the front. /// /// Equivalent to [`[T]::rotate_right`](slice::rotate_right). + #[inline] pub fn rotate_right(&mut self, mid: usize) { self.0.rotate_right(mid); } @@ -786,6 +842,7 @@ impl UniqueEntityEquivalentSlice { /// Sorts the slice, preserving initial order of equal elements. /// /// Equivalent to [`[T]::sort`](slice::sort()). + #[inline] pub fn sort(&mut self) where T: Ord, @@ -796,6 +853,7 @@ impl UniqueEntityEquivalentSlice { /// Sorts the slice with a comparison function, preserving initial order of equal elements. /// /// Equivalent to [`[T]::sort_by`](slice::sort_by). + #[inline] pub fn sort_by(&mut self, compare: F) where F: FnMut(&T, &T) -> Ordering, @@ -806,6 +864,7 @@ impl UniqueEntityEquivalentSlice { /// Sorts the slice with a key extraction function, preserving initial order of equal elements. /// /// Equivalent to [`[T]::sort_by_key`](slice::sort_by_key). + #[inline] pub fn sort_by_key(&mut self, f: F) where F: FnMut(&T) -> K, @@ -817,6 +876,7 @@ impl UniqueEntityEquivalentSlice { // Sorts the slice with a key extraction function, preserving initial order of equal elements. /// /// Equivalent to [`[T]::sort_by_cached_key`](slice::sort_by_cached_key). + #[inline] pub fn sort_by_cached_key(&mut self, f: F) where F: FnMut(&T) -> K, @@ -826,6 +886,7 @@ impl UniqueEntityEquivalentSlice { } /// Copies self into a new `UniqueEntityEquivalentVec`. + #[inline] pub fn to_vec(&self) -> UniqueEntityEquivalentVec where T: Clone, @@ -837,6 +898,7 @@ impl UniqueEntityEquivalentSlice { /// Converts `self` into a vector without clones or allocation. /// /// Equivalent to [`[T]::into_vec`](slice::into_vec). + #[inline] pub fn into_vec(self: Box) -> UniqueEntityEquivalentVec { // SAFETY: // This matches the implementation of `slice::into_vec`. @@ -850,12 +912,14 @@ impl UniqueEntityEquivalentSlice { } /// Converts a reference to T into a slice of length 1 (without copying). +#[inline] pub const fn from_ref(s: &T) -> &UniqueEntityEquivalentSlice { // SAFETY: A slice with a length of 1 is always unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice::from_ref(s)) } } /// Converts a reference to T into a slice of length 1 (without copying). +#[inline] pub const fn from_mut(s: &mut T) -> &mut UniqueEntityEquivalentSlice { // SAFETY: A slice with a length of 1 is always unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice::from_mut(s)) } @@ -869,6 +933,7 @@ pub const fn from_mut(s: &mut T) -> &mut UniqueEntityEquiva /// /// [`slice::from_raw_parts`] must be safe to call with `data` and `len`. /// Additionally, all elements in the resulting slice must be unique. +#[inline] pub const unsafe fn from_raw_parts<'a, T: EntityEquivalent>( data: *const T, len: usize, @@ -885,6 +950,7 @@ pub const unsafe fn from_raw_parts<'a, T: EntityEquivalent>( /// /// [`slice::from_raw_parts_mut`] must be safe to call with `data` and `len`. /// Additionally, all elements in the resulting slice must be unique. +#[inline] pub const unsafe fn from_raw_parts_mut<'a, T: EntityEquivalent>( data: *mut T, len: usize, @@ -900,6 +966,7 @@ pub const unsafe fn from_raw_parts_mut<'a, T: EntityEquivalent>( /// # Safety /// /// All elements in each of the cast slices must be unique. +#[inline] pub unsafe fn cast_slice_of_unique_entity_slice<'a, 'b, T: EntityEquivalent + 'a>( slice: &'b [&'a [T]], ) -> &'b [&'a UniqueEntityEquivalentSlice] { @@ -912,6 +979,7 @@ pub unsafe fn cast_slice_of_unique_entity_slice<'a, 'b, T: EntityEquivalent + 'a /// # Safety /// /// All elements in each of the cast slices must be unique. +#[inline] pub unsafe fn cast_slice_of_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent + 'a>( slice: &'b mut [&'a [T]], ) -> &'b mut [&'a UniqueEntityEquivalentSlice] { @@ -924,6 +992,7 @@ pub unsafe fn cast_slice_of_unique_entity_slice_mut<'a, 'b, T: EntityEquivalent /// # Safety /// /// All elements in each of the cast slices must be unique. +#[inline] 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 UniqueEntityEquivalentSlice] { @@ -936,6 +1005,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentSlice; + #[inline] fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -946,6 +1016,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a Box; + #[inline] fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -956,6 +1027,7 @@ impl IntoIterator for Box> { type IntoIter = unique_vec::IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { self.into_vec().into_iter() } @@ -964,42 +1036,49 @@ impl IntoIterator for Box> { impl Deref for UniqueEntityEquivalentSlice { type Target = [T]; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl AsRef<[T]> for UniqueEntityEquivalentSlice { + #[inline] fn as_ref(&self) -> &[T] { self } } impl AsRef for UniqueEntityEquivalentSlice { + #[inline] fn as_ref(&self) -> &Self { self } } impl AsMut for UniqueEntityEquivalentSlice { + #[inline] fn as_mut(&mut self) -> &mut Self { self } } impl Borrow<[T]> for UniqueEntityEquivalentSlice { + #[inline] fn borrow(&self) -> &[T] { self } } impl Clone for Box> { + #[inline] fn clone(&self) -> Self { self.to_vec().into_boxed_slice() } } impl Default for &UniqueEntityEquivalentSlice { + #[inline] fn default() -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(Default::default()) } @@ -1007,6 +1086,7 @@ impl Default for &UniqueEntityEquivalentSlice { } impl Default for &mut UniqueEntityEquivalentSlice { + #[inline] fn default() -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(Default::default()) } @@ -1014,6 +1094,7 @@ impl Default for &mut UniqueEntityEquivalentSlice { } impl Default for Box> { + #[inline] fn default() -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(Default::default()) } @@ -1023,6 +1104,7 @@ impl Default for Box> { impl From<&UniqueEntityEquivalentSlice> for Box> { + #[inline] fn from(value: &UniqueEntityEquivalentSlice) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_boxed_slice_unchecked(value.0.into()) } @@ -1032,6 +1114,7 @@ impl From<&UniqueEntityEquivalentSlice> impl From<&UniqueEntityEquivalentSlice> for Arc> { + #[inline] fn from(value: &UniqueEntityEquivalentSlice) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(value.0.into()) } @@ -1041,6 +1124,7 @@ impl From<&UniqueEntityEquivalentSlice> impl From<&UniqueEntityEquivalentSlice> for Rc> { + #[inline] fn from(value: &UniqueEntityEquivalentSlice) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(value.0.into()) } @@ -1050,6 +1134,7 @@ impl From<&UniqueEntityEquivalentSlice> impl<'a, T: EntityEquivalent + Clone> From<&'a UniqueEntityEquivalentSlice> for Cow<'a, UniqueEntityEquivalentSlice> { + #[inline] fn from(value: &'a UniqueEntityEquivalentSlice) -> Self { Cow::Borrowed(value) } @@ -1058,6 +1143,7 @@ impl<'a, T: EntityEquivalent + Clone> From<&'a UniqueEntityEquivalentSlice> impl From> for Box> { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { @@ -1069,6 +1155,7 @@ impl From From>> for Box> { + #[inline] fn from(value: Cow<'a, UniqueEntityEquivalentSlice>) -> Self { match value { Cow::Borrowed(slice) => Box::from(slice), @@ -1080,12 +1167,14 @@ impl<'a, T: EntityEquivalent + Clone> From From> for Box> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { value.into_boxed_slice() } } impl FromIterator for Box> { + #[inline] fn from_iter>(iter: I) -> Self { iter.into_iter() .collect::>() @@ -1094,6 +1183,7 @@ impl FromIterator for Box } impl FromEntitySetIterator for Box> { + #[inline] fn from_entity_set_iter>(iter: I) -> Self { iter.into_iter() .collect_set::>() @@ -1104,6 +1194,7 @@ impl FromEntitySetIterator for Box, U: EntityEquivalent> PartialEq> for &UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1112,6 +1203,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent> PartialEq> for &mut UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1120,6 +1212,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent> PartialEq> for UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1128,6 +1221,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityEquivalentSlice> for [T; N] { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1136,6 +1230,7 @@ impl, U: EntityEquivalent, const N: usize> impl + Clone, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for Cow<'_, [T]> { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } @@ -1144,12 +1239,14 @@ impl + Clone, U: EntityEquivalent> PartialEq<&UniqueEntityEquiva impl + Clone, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for Cow<'_, UniqueEntityEquivalentSlice> { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.0) } } impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for Vec { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1158,6 +1255,7 @@ impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlic impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for VecDeque { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } @@ -1166,6 +1264,7 @@ impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlic impl, U: EntityEquivalent, const N: usize> PartialEq<&mut UniqueEntityEquivalentSlice> for [T; N] { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1174,6 +1273,7 @@ impl, U: EntityEquivalent, const N: usize> impl + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for Cow<'_, [T]> { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&&**other) } @@ -1182,6 +1282,7 @@ impl + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntityEq impl + Clone, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for Cow<'_, UniqueEntityEquivalentSlice> { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.0.eq(&other.0) } @@ -1190,6 +1291,7 @@ impl + Clone, U: EntityEquivalent> impl + Clone, U: EntityEquivalent> PartialEq> for Cow<'_, UniqueEntityEquivalentSlice> { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1198,6 +1300,7 @@ impl + Clone, U: EntityEquivalent> impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for Vec { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1206,6 +1309,7 @@ impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalent impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for VecDeque { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } @@ -1214,6 +1318,7 @@ impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalent impl, U: EntityEquivalent> PartialEq> for [T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1222,6 +1327,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent, const N: usize> PartialEq> for [T; N] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1230,6 +1336,7 @@ impl, U: EntityEquivalent, const N: usize> PartialEq, U: EntityEquivalent> PartialEq> for Vec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1238,6 +1345,7 @@ impl, U: EntityEquivalent> impl, U, const N: usize> PartialEq<[U; N]> for &UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) } @@ -1246,6 +1354,7 @@ impl, U, const N: usize> PartialEq<[U; N]> impl, U, const N: usize> PartialEq<[U; N]> for &mut UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) } @@ -1254,6 +1363,7 @@ impl, U, const N: usize> PartialEq<[U; N]> impl, U, const N: usize> PartialEq<[U; N]> for UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) } @@ -1262,6 +1372,7 @@ impl, U, const N: usize> PartialEq<[U; N]> impl, U: EntityEquivalent, const N: usize> PartialEq> for &UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } @@ -1270,6 +1381,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq> for &mut UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } @@ -1278,12 +1390,14 @@ impl, U: EntityEquivalent, const N: usize> impl, U: EntityEquivalent, const N: usize> PartialEq> for UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.0) } } impl, U> PartialEq> for &UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } @@ -1292,12 +1406,14 @@ impl, U> PartialEq> for &UniqueEntityE impl, U> PartialEq> for &mut UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } impl, U> PartialEq> for UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } @@ -1306,6 +1422,7 @@ impl, U> PartialEq> for UniqueEntityEq impl ToOwned for UniqueEntityEquivalentSlice { type Owned = UniqueEntityEquivalentVec; + #[inline] fn to_owned(&self) -> Self::Owned { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentVec::from_vec_unchecked(self.0.to_owned()) } @@ -1317,6 +1434,7 @@ impl<'a, T: EntityEquivalent + Copy, const N: usize> TryFrom<&'a UniqueEntityEqu { type Error = TryFromSliceError; + #[inline] fn try_from(value: &'a UniqueEntityEquivalentSlice) -> Result { <&[T; N]>::try_from(&value.0).map(|array| // SAFETY: All elements in the original slice are unique. @@ -1329,6 +1447,7 @@ impl TryFrom<&UniqueEntityEquivalent { type Error = TryFromSliceError; + #[inline] fn try_from(value: &UniqueEntityEquivalentSlice) -> Result { <&Self>::try_from(value).copied() } @@ -1339,6 +1458,7 @@ impl TryFrom<&mut UniqueEntityEquiva { type Error = TryFromSliceError; + #[inline] fn try_from(value: &mut UniqueEntityEquivalentSlice) -> Result { ::try_from(&*value) } @@ -1346,6 +1466,7 @@ impl TryFrom<&mut UniqueEntityEquiva impl Index<(Bound, Bound)> for UniqueEntityEquivalentSlice { type Output = Self; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1354,6 +1475,7 @@ impl Index<(Bound, Bound)> for UniqueEntityEq impl Index> for UniqueEntityEquivalentSlice { type Output = Self; + #[inline] fn index(&self, key: Range) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1362,6 +1484,7 @@ impl Index> for UniqueEntityEquivalentSlice impl Index> for UniqueEntityEquivalentSlice { type Output = Self; + #[inline] fn index(&self, key: RangeFrom) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1370,6 +1493,7 @@ impl Index> for UniqueEntityEquivalentSlic impl Index for UniqueEntityEquivalentSlice { type Output = Self; + #[inline] fn index(&self, key: RangeFull) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1378,6 +1502,7 @@ impl Index for UniqueEntityEquivalentSlice { impl Index> for UniqueEntityEquivalentSlice { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeInclusive) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1386,6 +1511,7 @@ impl Index> for UniqueEntityEquivalen impl Index> for UniqueEntityEquivalentSlice { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeTo) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1394,6 +1520,7 @@ impl Index> for UniqueEntityEquivalentSlice< impl Index> for UniqueEntityEquivalentSlice { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self { // SAFETY: All elements in the original slice are unique. unsafe { Self::from_slice_unchecked(self.0.index(key)) } @@ -1403,6 +1530,7 @@ impl Index> for UniqueEntityEquival impl Index for UniqueEntityEquivalentSlice { type Output = T; + #[inline] fn index(&self, index: usize) -> &T { &self.0[index] } @@ -1411,6 +1539,7 @@ impl Index for UniqueEntityEquivalentSlice { impl IndexMut<(Bound, Bound)> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1418,6 +1547,7 @@ impl IndexMut<(Bound, Bound)> } impl IndexMut> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1425,6 +1555,7 @@ impl IndexMut> for UniqueEntityEquivalentSlice } impl IndexMut> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1432,6 +1563,7 @@ impl IndexMut> for UniqueEntityEquivalentS } impl IndexMut for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1439,6 +1571,7 @@ impl IndexMut for UniqueEntityEquivalentSlice } impl IndexMut> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1446,6 +1579,7 @@ impl IndexMut> for UniqueEntityEquiva } impl IndexMut> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1453,6 +1587,7 @@ impl IndexMut> for UniqueEntityEquivalentSli } impl IndexMut> for UniqueEntityEquivalentSlice { + #[inline] 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)) } @@ -1471,6 +1606,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Views the underlying data as a subslice of the original data. /// /// Equivalent to [`slice::Iter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } @@ -1484,6 +1620,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Views the underlying data as a mutable subslice of the original data. /// /// Equivalent to [`slice::IterMut::into_slice`]. + #[inline] pub fn into_slice(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { @@ -1494,6 +1631,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Views the underlying data as a subslice of the original data. /// /// Equivalent to [`slice::IterMut::as_slice`]. + #[inline] pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } @@ -1520,16 +1658,19 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// # Safety /// /// All elements in each of the slices must be unique. + #[inline] pub unsafe fn from_slice_iter_unchecked(iter: I) -> Self { Self { iter } } /// Returns the inner `I`. + #[inline] pub fn into_inner(self) -> I { self.iter } /// Returns a reference to the inner `I`. + #[inline] pub fn as_inner(&self) -> &I { &self.iter } @@ -1540,6 +1681,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. + #[inline] pub unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } @@ -1550,12 +1692,14 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator { type Item = &'a UniqueEntityEquivalentSlice; + #[inline] fn next(&mut self) -> Option { self.iter.next().map(|slice| // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(slice) }) } + #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } @@ -1569,6 +1713,7 @@ impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator> ExactSi impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator> DoubleEndedIterator for UniqueEntityEquivalentSliceIter<'a, T, I> { + #[inline] fn next_back(&mut self) -> Option { self.iter.next_back().map(|slice| // SAFETY: All elements in the original iterator are unique slices. @@ -1584,6 +1729,7 @@ impl<'a, T: EntityEquivalent + 'a, I: FusedIterator> FusedIterat impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsRef<[&'a [T]]>> AsRef<[&'a UniqueEntityEquivalentSlice]> for UniqueEntityEquivalentSliceIter<'a, T, I> { + #[inline] fn as_ref(&self) -> &[&'a UniqueEntityEquivalentSlice] { // SAFETY: unsafe { cast_slice_of_unique_entity_slice(self.iter.as_ref()) } @@ -1613,6 +1759,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::Chun /// returned by the iterator. /// /// Equivalent to [`slice::ChunksExact::remainder`]. + #[inline] pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) } @@ -1637,6 +1784,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityEquivalentSliceIter<'a, T, slice::RChu /// returned by the iterator. /// /// Equivalent to [`slice::RChunksExact::remainder`]. + #[inline] pub fn remainder(&self) -> &'a UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.iter.remainder()) } @@ -1704,16 +1852,19 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// # Safety /// /// All elements in each of the slices must be unique. + #[inline] pub unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self { Self { iter } } /// Returns the inner `I`. + #[inline] pub fn into_inner(self) -> I { self.iter } /// Returns a reference to the inner `I`. + #[inline] pub fn as_inner(&self) -> &I { &self.iter } @@ -1724,6 +1875,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. + #[inline] pub unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } @@ -1734,12 +1886,14 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> Iterator { type Item = &'a mut UniqueEntityEquivalentSlice; + #[inline] fn next(&mut self) -> Option { self.iter.next().map(|slice| // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(slice) }) } + #[inline] fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } @@ -1753,6 +1907,7 @@ impl<'a, T: EntityEquivalent + 'a, I: ExactSizeIterator> Exa impl<'a, T: EntityEquivalent + 'a, I: DoubleEndedIterator> DoubleEndedIterator for UniqueEntityEquivalentSliceIterMut<'a, T, I> { + #[inline] fn next_back(&mut self) -> Option { self.iter.next_back().map(|slice| // SAFETY: All elements in the original iterator are unique slices. @@ -1768,6 +1923,7 @@ impl<'a, T: EntityEquivalent + 'a, I: FusedIterator> FusedIt impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsRef<[&'a [T]]>> AsRef<[&'a UniqueEntityEquivalentSlice]> for UniqueEntityEquivalentSliceIterMut<'a, T, I> { + #[inline] 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()) } @@ -1778,6 +1934,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator + AsMut<[&'a AsMut<[&'a mut UniqueEntityEquivalentSlice]> for UniqueEntityEquivalentSliceIterMut<'a, T, I> { + #[inline] 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()) } @@ -1805,6 +1962,7 @@ impl<'a, T: EntityEquivalent> /// returned by the iterator. /// /// Equivalent to [`slice::ChunksExactMut::into_remainder`]. + #[inline] pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) } @@ -1832,6 +1990,7 @@ impl<'a, T: EntityEquivalent> /// returned by the iterator. /// /// Equivalent to [`slice::RChunksExactMut::into_remainder`]. + #[inline] pub fn into_remainder(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original iterator are unique slices. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.iter.into_remainder()) } diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index 51e4632be8478..a40ec61e4f85c 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -51,6 +51,7 @@ impl UniqueEntityEquivalentVec { /// Constructs a new, empty `UniqueEntityEquivalentVec`. /// /// Equivalent to [`Vec::new`]. + #[inline] pub const fn new() -> Self { Self(Vec::new()) } @@ -58,6 +59,7 @@ impl UniqueEntityEquivalentVec { /// Constructs a new, empty `UniqueEntityEquivalentVec` with at least the specified capacity. /// /// Equivalent to [`Vec::with_capacity`] + #[inline] pub fn with_capacity(capacity: usize) -> Self { Self(Vec::with_capacity(capacity)) } @@ -70,6 +72,7 @@ impl UniqueEntityEquivalentVec { /// /// It must be safe to call [`Vec::from_raw_parts`] with these inputs, /// and the resulting [`Vec`] must only contain unique elements. + #[inline] pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self { // SAFETY: Caller ensures it's safe to call `Vec::from_raw_parts` Self(unsafe { Vec::from_raw_parts(ptr, length, capacity) }) @@ -80,6 +83,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `vec` must contain only unique elements. + #[inline] pub unsafe fn from_vec_unchecked(vec: Vec) -> Self { Self(vec) } @@ -89,6 +93,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `vec` must contain only unique elements. + #[inline] pub unsafe fn from_vec_ref_unchecked(vec: &Vec) -> &Self { // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. unsafe { &*ptr::from_ref(vec).cast() } @@ -99,17 +104,20 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `vec` must contain only unique elements. + #[inline] pub unsafe fn from_vec_mut_unchecked(vec: &mut Vec) -> &mut Self { // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. unsafe { &mut *ptr::from_mut(vec).cast() } } /// Returns the inner [`Vec`]. + #[inline] pub fn into_inner(self) -> Vec { self.0 } /// Returns a reference to the inner [`Vec`]. + #[inline] pub fn as_vec(&self) -> &Vec { &self.0 } @@ -120,6 +128,7 @@ impl UniqueEntityEquivalentVec { /// /// The elements of this `Vec` must always remain unique, even while /// this mutable reference is live. + #[inline] pub unsafe fn as_mut_vec(&mut self) -> &mut Vec { &mut self.0 } @@ -128,6 +137,7 @@ impl UniqueEntityEquivalentVec { /// reallocating. /// /// Equivalent to [`Vec::capacity`]. + #[inline] pub fn capacity(&self) -> usize { self.0.capacity() } @@ -136,6 +146,7 @@ impl UniqueEntityEquivalentVec { /// in the given `Vec`. /// /// Equivalent to [`Vec::reserve`]. + #[inline] pub fn reserve(&mut self, additional: usize) { self.0.reserve(additional); } @@ -144,6 +155,7 @@ impl UniqueEntityEquivalentVec { /// be inserted in the given `UniqueEntityEquivalentVec`. /// /// Equivalent to [`Vec::reserve_exact`]. + #[inline] pub fn reserve_exact(&mut self, additional: usize) { self.0.reserve_exact(additional); } @@ -152,6 +164,7 @@ impl UniqueEntityEquivalentVec { /// in the given `Vec`. /// /// Equivalent to [`Vec::try_reserve`]. + #[inline] pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { self.0.try_reserve(additional) } @@ -160,6 +173,7 @@ impl UniqueEntityEquivalentVec { /// elements to be inserted in the given `Vec`. /// /// Equivalent to [`Vec::try_reserve_exact`]. + #[inline] pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> { self.0.try_reserve_exact(additional) } @@ -167,6 +181,7 @@ impl UniqueEntityEquivalentVec { /// Shrinks the capacity of the vector as much as possible. /// /// Equivalent to [`Vec::shrink_to_fit`]. + #[inline] pub fn shrink_to_fit(&mut self) { self.0.shrink_to_fit(); } @@ -174,11 +189,13 @@ impl UniqueEntityEquivalentVec { /// Shrinks the capacity of the vector with a lower bound. /// /// Equivalent to [`Vec::shrink_to`]. + #[inline] pub fn shrink_to(&mut self, min_capacity: usize) { self.0.shrink_to(min_capacity); } /// Converts the vector into `Box>`. + #[inline] pub fn into_boxed_slice(self) -> Box> { // SAFETY: UniqueEntityEquivalentSlice is a transparent wrapper around [T]. unsafe { @@ -187,11 +204,13 @@ impl UniqueEntityEquivalentVec { } /// Extracts a slice containing the entire vector. + #[inline] pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { self } /// Extracts a mutable slice of the entire vector. + #[inline] pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -200,6 +219,7 @@ impl UniqueEntityEquivalentVec { /// the rest. /// /// Equivalent to [`Vec::truncate`]. + #[inline] pub fn truncate(&mut self, len: usize) { self.0.truncate(len); } @@ -208,6 +228,7 @@ impl UniqueEntityEquivalentVec { /// valid for zero sized reads if the vector didn't allocate. /// /// Equivalent to [`Vec::as_ptr`]. + #[inline] pub fn as_ptr(&self) -> *const T { self.0.as_ptr() } @@ -215,6 +236,7 @@ impl UniqueEntityEquivalentVec { /// raw pointer valid for zero sized reads if the vector didn't allocate. /// /// Equivalent to [`Vec::as_mut_ptr`]. + #[inline] pub fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr() } @@ -227,6 +249,7 @@ impl UniqueEntityEquivalentVec { /// /// It must be safe to call [`Vec::set_len`] with these inputs, /// and the resulting [`Vec`] must only contain unique elements. + #[inline] pub unsafe fn set_len(&mut self, new_len: usize) { // SAFETY: Caller ensures it's safe to call `Vec::set_len` unsafe { self.0.set_len(new_len) }; @@ -235,6 +258,7 @@ impl UniqueEntityEquivalentVec { /// Removes an element from the vector and returns it. /// /// Equivalent to [`Vec::swap_remove`]. + #[inline] pub fn swap_remove(&mut self, index: usize) -> T { self.0.swap_remove(index) } @@ -247,6 +271,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// No `T` contained by `self` may equal `element`. + #[inline] pub unsafe fn insert(&mut self, index: usize, element: T) { self.0.insert(index, element); } @@ -255,6 +280,7 @@ impl UniqueEntityEquivalentVec { /// shifting all elements after it to the left. /// /// Equivalent to [`Vec::remove`]. + #[inline] pub fn remove(&mut self, index: usize) -> T { self.0.remove(index) } @@ -262,6 +288,7 @@ impl UniqueEntityEquivalentVec { /// Retains only the elements specified by the predicate. /// /// Equivalent to [`Vec::retain`]. + #[inline] pub fn retain(&mut self, f: F) where F: FnMut(&T) -> bool, @@ -276,6 +303,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `self` must only contain unique elements after each individual execution of `f`. + #[inline] pub unsafe fn retain_mut(&mut self, f: F) where F: FnMut(&mut T) -> bool, @@ -291,6 +319,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `self` must only contain unique elements after each individual execution of `key`. + #[inline] pub unsafe fn dedup_by_key(&mut self, key: F) where F: FnMut(&mut T) -> K, @@ -307,6 +336,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `self` must only contain unique elements after each individual execution of `same_bucket`. + #[inline] pub unsafe fn dedup_by(&mut self, same_bucket: F) where F: FnMut(&mut T, &mut T) -> bool, @@ -321,6 +351,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// No `T` contained by `self` may equal `element`. + #[inline] pub unsafe fn push(&mut self, value: T) { self.0.push(value); } @@ -332,6 +363,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `other` must contain no elements that equal any element in `self`. + #[inline] pub unsafe fn append(&mut self, other: &mut UniqueEntityEquivalentVec) { self.0.append(&mut other.0); } @@ -340,6 +372,7 @@ impl UniqueEntityEquivalentVec { /// is empty. /// /// Equivalent to [`Vec::pop`]. + #[inline] pub fn pop(&mut self) -> Option { self.0.pop() } @@ -348,6 +381,7 @@ impl UniqueEntityEquivalentVec { /// removed elements as an iterator. /// /// Equivalent to [`Vec::drain`]. + #[inline] pub fn drain(&mut self, range: R) -> Drain<'_, T> where R: RangeBounds, @@ -359,6 +393,7 @@ impl UniqueEntityEquivalentVec { /// Clears the vector, removing all values. /// /// Equivalent to [`Vec::clear`]. + #[inline] pub fn clear(&mut self) { self.0.clear(); } @@ -367,6 +402,7 @@ impl UniqueEntityEquivalentVec { /// as its 'length'. /// /// Equivalent to [`Vec::len`]. + #[inline] pub fn len(&self) -> usize { self.0.len() } @@ -374,6 +410,7 @@ impl UniqueEntityEquivalentVec { /// Returns `true` if the vector contains no elements. /// /// Equivalent to [`Vec::is_empty`]. + #[inline] pub fn is_empty(&self) -> bool { self.0.is_empty() } @@ -381,6 +418,7 @@ impl UniqueEntityEquivalentVec { /// Splits the collection into two at the given index. /// /// Equivalent to [`Vec::split_off`]. + #[inline] pub fn split_off(&mut self, at: usize) -> Self { Self(self.0.split_off(at)) } @@ -392,6 +430,7 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `f` must only produce unique `T`, and none of these may equal any `T` in `self`. + #[inline] pub unsafe fn resize_with(&mut self, new_len: usize, f: F) where F: FnMut() -> T, @@ -400,6 +439,7 @@ impl UniqueEntityEquivalentVec { } /// Consumes and leaks the Vec, returning a mutable reference to the contents, `&'a mut UniqueEntityEquivalentSlice`. + #[inline] pub fn leak<'a>(self) -> &'a mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.leak()) } @@ -409,6 +449,7 @@ impl UniqueEntityEquivalentVec { /// [`MaybeUninit`]. /// /// Equivalent to [`Vec::spare_capacity_mut`]. + #[inline] pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit] { self.0.spare_capacity_mut() } @@ -422,6 +463,7 @@ impl UniqueEntityEquivalentVec { /// /// `replace_with` must not yield any elements that equal any elements in `self`, /// except for those in `range`. + #[inline] pub unsafe fn splice( &mut self, range: R, @@ -437,6 +479,7 @@ impl UniqueEntityEquivalentVec { } impl Default for UniqueEntityEquivalentVec { + #[inline] fn default() -> Self { Self(Vec::default()) } @@ -445,6 +488,7 @@ impl Default for UniqueEntityEquivalentVec { impl Deref for UniqueEntityEquivalentVec { type Target = UniqueEntityEquivalentSlice; + #[inline] fn deref(&self) -> &Self::Target { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } @@ -452,6 +496,7 @@ impl Deref for UniqueEntityEquivalentVec { } impl DerefMut for UniqueEntityEquivalentVec { + #[inline] fn deref_mut(&mut self) -> &mut Self::Target { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } @@ -466,6 +511,7 @@ where type IntoIter = unique_slice::Iter<'a, T>; + #[inline] fn into_iter(self) -> Self::IntoIter { // SAFETY: `self` contains only unique elements. unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } @@ -477,6 +523,7 @@ impl IntoIterator for UniqueEntityEquivalentVec { type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { // SAFETY: `self` contains only unique elements. unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) } @@ -484,54 +531,63 @@ impl IntoIterator for UniqueEntityEquivalentVec { } impl AsMut for UniqueEntityEquivalentVec { + #[inline] fn as_mut(&mut self) -> &mut UniqueEntityEquivalentVec { self } } impl AsMut> for UniqueEntityEquivalentVec { + #[inline] fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } } impl AsRef for UniqueEntityEquivalentVec { + #[inline] fn as_ref(&self) -> &Self { self } } impl AsRef> for UniqueEntityEquivalentVec { + #[inline] fn as_ref(&self) -> &Vec { &self.0 } } impl Borrow> for UniqueEntityEquivalentVec { + #[inline] fn borrow(&self) -> &Vec { &self.0 } } impl AsRef<[T]> for UniqueEntityEquivalentVec { + #[inline] fn as_ref(&self) -> &[T] { &self.0 } } impl AsRef> for UniqueEntityEquivalentVec { + #[inline] fn as_ref(&self) -> &UniqueEntityEquivalentSlice { self } } impl Borrow<[T]> for UniqueEntityEquivalentVec { + #[inline] fn borrow(&self) -> &[T] { &self.0 } } impl Borrow> for UniqueEntityEquivalentVec { + #[inline] fn borrow(&self) -> &UniqueEntityEquivalentSlice { self } @@ -540,18 +596,21 @@ impl Borrow> for UniqueEntit impl BorrowMut> for UniqueEntityEquivalentVec { + #[inline] fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } } impl, U> PartialEq> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &Vec) -> bool { self.0.eq(other) } } impl, U> PartialEq<&[U]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&[U]) -> bool { self.0.eq(other) } @@ -560,12 +619,14 @@ impl, U> PartialEq<&[U]> for UniqueEntityEqui impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.0.eq(other) } } impl, U> PartialEq<&mut [U]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&mut [U]) -> bool { self.0.eq(other) } @@ -574,6 +635,7 @@ impl, U> PartialEq<&mut [U]> for UniqueEntity impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentSlice) -> bool { self.0.eq(other) } @@ -582,6 +644,7 @@ impl, U: EntityEquivalent> impl, U, const N: usize> PartialEq<&[U; N]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&[U; N]) -> bool { self.0.eq(other) } @@ -590,6 +653,7 @@ impl, U, const N: usize> PartialEq<&[U; N]> impl, U: EntityEquivalent, const N: usize> PartialEq<&UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentArray) -> bool { self.0.eq(&other.as_inner()) } @@ -598,6 +662,7 @@ impl, U: EntityEquivalent, const N: usize> impl, U, const N: usize> PartialEq<&mut [U; N]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&mut [U; N]) -> bool { self.0.eq(&**other) } @@ -606,12 +671,14 @@ impl, U, const N: usize> PartialEq<&mut [U; N impl, U: EntityEquivalent, const N: usize> PartialEq<&mut UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &&mut UniqueEntityEquivalentArray) -> bool { self.0.eq(other.as_inner()) } } impl, U> PartialEq<[U]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &[U]) -> bool { self.0.eq(other) } @@ -620,6 +687,7 @@ impl, U> PartialEq<[U]> for UniqueEntityEquiv impl, U: EntityEquivalent> PartialEq> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.0.eq(&**other) } @@ -628,6 +696,7 @@ impl, U: EntityEquivalent> impl, U, const N: usize> PartialEq<[U; N]> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &[U; N]) -> bool { self.0.eq(other) } @@ -636,24 +705,28 @@ impl, U, const N: usize> PartialEq<[U; N]> impl, U: EntityEquivalent, const N: usize> PartialEq> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentArray) -> bool { self.0.eq(other.as_inner()) } } impl, U: EntityEquivalent> PartialEq> for Vec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } impl, U: EntityEquivalent> PartialEq> for &[T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } impl, U: EntityEquivalent> PartialEq> for &mut [T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } @@ -662,6 +735,7 @@ impl, U: EntityEquivalent> PartialEq, U: EntityEquivalent> PartialEq> for [T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } @@ -670,12 +744,14 @@ impl, U: EntityEquivalent> impl + Clone, U: EntityEquivalent> PartialEq> for Cow<'_, [T]> { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } } impl, U: EntityEquivalent> PartialEq> for VecDeque { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } @@ -684,6 +760,7 @@ impl, U: EntityEquivalent> PartialEq From<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &UniqueEntityEquivalentSlice) -> Self { value.to_vec() } @@ -692,6 +769,7 @@ impl From<&UniqueEntityEquivalentSlice> impl From<&mut UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &mut UniqueEntityEquivalentSlice) -> Self { value.to_vec() } @@ -700,6 +778,7 @@ impl From<&mut UniqueEntityEquivalentSlice> impl From>> for UniqueEntityEquivalentVec { + #[inline] fn from(value: Box>) -> Self { value.into_vec() } @@ -710,42 +789,49 @@ impl From>> where UniqueEntityEquivalentSlice: ToOwned>, { + #[inline] fn from(value: Cow>) -> Self { value.into_owned() } } impl From<&[T; 1]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &[T; 1]) -> Self { Self(Vec::from(value)) } } impl From<&[T; 0]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &[T; 0]) -> Self { Self(Vec::from(value)) } } impl From<&mut [T; 1]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &mut [T; 1]) -> Self { Self(Vec::from(value)) } } impl From<&mut [T; 0]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &mut [T; 0]) -> Self { Self(Vec::from(value)) } } impl From<[T; 1]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: [T; 1]) -> Self { Self(Vec::from(value)) } } impl From<[T; 0]> for UniqueEntityEquivalentVec { + #[inline] fn from(value: [T; 0]) -> Self { Self(Vec::from(value)) } @@ -754,6 +840,7 @@ impl From<[T; 0]> for UniqueEntityEquivalentVec { impl From<&UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.as_inner().clone())) } @@ -762,6 +849,7 @@ impl From<&UniqueEntityEquivalentAr impl From<&mut UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &mut UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.as_inner().clone())) } @@ -770,18 +858,21 @@ impl From<&mut UniqueEntityEquivale impl From> for UniqueEntityEquivalentVec { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { Self(Vec::from(value.into_inner())) } } impl From> for Vec { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { value.0 } } impl<'a, T: EntityEquivalent + Clone> From> for Cow<'a, [T]> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { Cow::from(value.0) } @@ -790,12 +881,14 @@ impl<'a, T: EntityEquivalent + Clone> From> for Cow impl<'a, T: EntityEquivalent + Clone> From> for Cow<'a, UniqueEntityEquivalentSlice> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { Cow::Owned(value) } } impl From> for Arc<[T]> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { Arc::from(value.0) } @@ -804,6 +897,7 @@ impl From> for Arc<[T]> { impl From> for Arc> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_arc_slice_unchecked(Arc::from(value.0)) } @@ -811,18 +905,21 @@ impl From> } impl From> for BinaryHeap { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { BinaryHeap::from(value.0) } } impl From> for Box<[T]> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { Box::from(value.0) } } impl From> for Rc<[T]> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { Rc::from(value.0) } @@ -831,6 +928,7 @@ impl From> for Rc<[T]> { impl From> for Rc> { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_rc_slice_unchecked(Rc::from(value.0)) } @@ -838,6 +936,7 @@ impl From> } impl From> for VecDeque { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { VecDeque::from(value.0) } @@ -846,6 +945,7 @@ impl From> for VecDeque { impl TryFrom> for Box<[T; N]> { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { Box::try_from(value.0).map_err(UniqueEntityEquivalentVec) } @@ -856,6 +956,7 @@ impl TryFrom> { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { Box::try_from(value.0) .map(|v| @@ -868,6 +969,7 @@ impl TryFrom> impl TryFrom> for [T; N] { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { <[T; N] as TryFrom>>::try_from(value.0).map_err(UniqueEntityEquivalentVec) } @@ -878,6 +980,7 @@ impl TryFrom> { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { <[T; N] as TryFrom>>::try_from(value.0) .map(|v| @@ -888,6 +991,7 @@ impl TryFrom> } impl From> for UniqueEntityEquivalentVec { + #[inline] fn from(value: BTreeSet) -> Self { Self(value.into_iter().collect::>()) } @@ -897,6 +1001,7 @@ 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. + #[inline] fn from_iter>(iter: I) -> Self { // Matches the `HashSet::from_iter` reservation logic. let iter = iter.into_iter(); @@ -913,6 +1018,7 @@ impl FromIterator for UniqueEntityEquivalentVec { } impl FromEntitySetIterator for UniqueEntityEquivalentVec { + #[inline] fn from_entity_set_iter>(iter: I) -> Self { // SAFETY: `iter` is an `EntitySet`. unsafe { Self::from_vec_unchecked(Vec::from_iter(iter)) } @@ -923,6 +1029,7 @@ 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`. + #[inline] fn extend>(&mut self, iter: I) { // Matches the `HashSet::extend` reservation logic. Their reasoning: // "Keys may be already present or show multiple times in the iterator. @@ -950,6 +1057,7 @@ impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityEquivale /// 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`. + #[inline] fn extend>(&mut self, iter: I) { // Matches the `HashSet::extend` reservation logic. Their reasoning: // "Keys may be already present or show multiple times in the iterator. @@ -975,6 +1083,7 @@ impl<'a, T: EntityEquivalent + Copy + 'a> Extend<&'a T> for UniqueEntityEquivale impl Index<(Bound, Bound)> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: (Bound, Bound)) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -983,6 +1092,7 @@ impl Index<(Bound, Bound)> for UniqueEntityEq impl Index> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: Range) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -991,6 +1101,7 @@ impl Index> for UniqueEntityEquivalentVec { impl Index> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeFrom) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -999,6 +1110,7 @@ impl Index> for UniqueEntityEquivalentVec< impl Index for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeFull) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -1007,6 +1119,7 @@ impl Index for UniqueEntityEquivalentVec { impl Index> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -1015,6 +1128,7 @@ impl Index> for UniqueEntityEquivalen impl Index> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeTo) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -1023,6 +1137,7 @@ impl Index> for UniqueEntityEquivalentVec impl Index> for UniqueEntityEquivalentVec { type Output = UniqueEntityEquivalentSlice; + #[inline] fn index(&self, key: RangeToInclusive) -> &Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.index(key)) } @@ -1031,12 +1146,14 @@ impl Index> for UniqueEntityEquival impl Index for UniqueEntityEquivalentVec { type Output = T; + #[inline] fn index(&self, key: usize) -> &T { self.0.index(key) } } impl IndexMut<(Bound, Bound)> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: (Bound, Bound)) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1044,6 +1161,7 @@ impl IndexMut<(Bound, Bound)> for UniqueEntit } impl IndexMut> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: Range) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1051,6 +1169,7 @@ impl IndexMut> for UniqueEntityEquivalentVec IndexMut> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: RangeFrom) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1058,6 +1177,7 @@ impl IndexMut> for UniqueEntityEquivalentV } impl IndexMut for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1065,6 +1185,7 @@ impl IndexMut for UniqueEntityEquivalentVec { } impl IndexMut> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: RangeInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1072,6 +1193,7 @@ impl IndexMut> for UniqueEntityEquiva } impl IndexMut> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: RangeTo) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1079,6 +1201,7 @@ impl IndexMut> for UniqueEntityEquivalentVec } impl IndexMut> for UniqueEntityEquivalentVec { + #[inline] fn index_mut(&mut self, key: RangeToInclusive) -> &mut Self::Output { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.index_mut(key)) } @@ -1095,6 +1218,7 @@ impl UniqueEntityIter> { /// Returns the remaining items of this iterator as a slice. /// /// Equivalent to [`vec::IntoIter::as_slice`]. + #[inline] pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } @@ -1103,6 +1227,7 @@ impl UniqueEntityIter> { /// Returns the remaining items of this iterator as a mutable slice. /// /// Equivalent to [`vec::IntoIter::as_mut_slice`]. + #[inline] pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { @@ -1123,6 +1248,7 @@ impl<'a, T: EntityEquivalent> UniqueEntityIter> { /// Returns the remaining items of this iterator as a slice. /// /// Equivalent to [`vec::Drain::as_slice`]. + #[inline] pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { // SAFETY: All elements in the original slice are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.as_inner().as_slice()) } From dceec1703b8f4c2cdab2ae4a7d9bc79d9b583b35 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Fri, 10 Oct 2025 22:27:18 +0200 Subject: [PATCH 4/8] stop internally skipping entity set type construction safety --- crates/bevy_ecs/src/entity/entity_set.rs | 3 +- crates/bevy_ecs/src/entity/hash_map.rs | 9 +++-- crates/bevy_ecs/src/entity/hash_set.rs | 9 +++-- crates/bevy_ecs/src/entity/index_map.rs | 24 +++++++---- crates/bevy_ecs/src/entity/index_set.rs | 12 ++++-- crates/bevy_ecs/src/entity/unique_array.rs | 21 ++++++---- crates/bevy_ecs/src/entity/unique_vec.rs | 47 ++++++++++++++-------- 7 files changed, 83 insertions(+), 42 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index 49bfd4e9819c9..68f0e72fc4f55 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -374,7 +374,8 @@ impl UniqueEntityIter { /// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`]. #[inline] pub fn from_entity_set_iter(iter: I) -> Self { - Self { iter } + // SAFETY: iter implements `EntitySetIterator`. + unsafe { Self::from_iter_unchecked(iter) } } } diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index 10a32f91a623e..02273e0163bf0 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -229,7 +229,8 @@ impl FusedIterator for Keys<'_, V> {} impl Clone for Keys<'_, V> { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `Keys`. + unsafe { Self::from_keys_unchecked(self.0.clone()) } } } @@ -242,7 +243,8 @@ impl Debug for Keys<'_, V> { impl Default for Keys<'_, V> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `Keys` is empty. + unsafe { Self::from_keys_unchecked(Default::default()) } } } @@ -314,7 +316,8 @@ impl Debug for IntoKeys { impl Default for IntoKeys { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IntoKeys` is empty. + unsafe { Self::from_into_keys_unchecked(Default::default()) } } } diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index a958c834714b6..ce6bfdbb10577 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -291,7 +291,8 @@ impl FusedIterator for Iter<'_> {} impl Clone for Iter<'_> { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `Iter`. + unsafe { Self::from_iter_unchecked(self.0.clone()) } } } @@ -304,7 +305,8 @@ impl Debug for Iter<'_> { impl Default for Iter<'_> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `Iter` is empty. + unsafe { Self::from_iter_unchecked(Default::default()) } } } @@ -373,7 +375,8 @@ impl Debug for IntoIter { impl Default for IntoIter { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IntoIter` is empty. + unsafe { Self::from_into_iter_unchecked(Default::default()) } } } diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index e41ab08e7d899..fd471583214ed 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -995,7 +995,8 @@ impl FusedIterator for Iter<'_, V> {} impl Clone for Iter<'_, V> { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `Iter`. + unsafe { Self::from_iter_unchecked(self.0.clone()) } } } @@ -1008,7 +1009,8 @@ impl Debug for Iter<'_, V> { impl Default for Iter<'_, V> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `Iter` is empty. + unsafe { Self::from_iter_unchecked(Default::default()) } } } @@ -1098,7 +1100,8 @@ impl Debug for IterMut<'_, V> { impl Default for IterMut<'_, V> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IterMut` is empty. + unsafe { Self::from_iter_mut_unchecked(Default::default()) } } } @@ -1179,7 +1182,8 @@ impl FusedIterator for IntoIter {} impl Clone for IntoIter { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `IntoIter`. + unsafe { Self::from_into_iter_unchecked(self.0.clone()) } } } @@ -1195,7 +1199,8 @@ impl Debug for IntoIter { impl Default for IntoIter { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IntoIter` is empty. + unsafe { Self::from_into_iter_unchecked(Default::default()) } } } @@ -1337,7 +1342,8 @@ impl Index for Keys<'_, V> { impl Clone for Keys<'_, V> { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `Keys`. + unsafe { Self::from_keys_unchecked(self.0.clone()) } } } @@ -1350,7 +1356,8 @@ impl Debug for Keys<'_, V> { impl Default for Keys<'_, V> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `Keys` is empty. + unsafe { Self::from_keys_unchecked(Default::default()) } } } @@ -1425,7 +1432,8 @@ impl Debug for IntoKeys { impl Default for IntoKeys { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IntoKeys` is empty. + unsafe { Self::from_into_keys_unchecked(Default::default()) } } } diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index 3bcf69263e89d..a3e569221414b 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -676,7 +676,8 @@ impl FusedIterator for Iter<'_> {} impl Clone for Iter<'_> { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `Iter`. + unsafe { Self::from_iter_unchecked(self.0.clone()) } } } @@ -689,7 +690,8 @@ impl Debug for Iter<'_> { impl Default for Iter<'_> { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `Iter` is empty. + unsafe { Self::from_iter_unchecked(Default::default()) } } } @@ -763,7 +765,8 @@ impl FusedIterator for IntoIter {} impl Clone for IntoIter { #[inline] fn clone(&self) -> Self { - Self(self.0.clone(), PhantomData) + // SAFETY: We are cloning an already valid `IntoIter`. + unsafe { Self::from_into_iter_unchecked(self.0.clone()) } } } @@ -779,7 +782,8 @@ impl Debug for IntoIter { impl Default for IntoIter { #[inline] fn default() -> Self { - Self(Default::default(), PhantomData) + // SAFETY: `IntoIter` is empty. + unsafe { Self::from_into_iter_unchecked(Default::default()) } } } diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index e375b45f56fb4..28349170b37fd 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -174,7 +174,8 @@ impl DerefMut for UniqueEntityEquivalentArr impl Default for UniqueEntityEquivalentArray { #[inline] fn default() -> Self { - Self(Default::default()) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_array_unchecked(Default::default()) } } } @@ -396,42 +397,48 @@ impl IndexMut> impl From<&[T; 1]> for UniqueEntityEquivalentArray { #[inline] fn from(value: &[T; 1]) -> Self { - Self(value.clone()) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_array_unchecked(value.clone()) } } } impl From<&[T; 0]> for UniqueEntityEquivalentArray { #[inline] fn from(value: &[T; 0]) -> Self { - Self(value.clone()) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_array_unchecked(value.clone()) } } } impl From<&mut [T; 1]> for UniqueEntityEquivalentArray { #[inline] fn from(value: &mut [T; 1]) -> Self { - Self(value.clone()) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_array_unchecked(value.clone()) } } } impl From<&mut [T; 0]> for UniqueEntityEquivalentArray { #[inline] fn from(value: &mut [T; 0]) -> Self { - Self(value.clone()) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_array_unchecked(value.clone()) } } } impl From<[T; 1]> for UniqueEntityEquivalentArray { #[inline] fn from(value: [T; 1]) -> Self { - Self(value) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_array_unchecked(value) } } } impl From<[T; 0]> for UniqueEntityEquivalentArray { #[inline] fn from(value: [T; 0]) -> Self { - Self(value) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_array_unchecked(value) } } } diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index a40ec61e4f85c..e6b6c26b56cc9 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -53,7 +53,8 @@ impl UniqueEntityEquivalentVec { /// Equivalent to [`Vec::new`]. #[inline] pub const fn new() -> Self { - Self(Vec::new()) + // SAFETY: Any empty Vec cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::new()) } } /// Constructs a new, empty `UniqueEntityEquivalentVec` with at least the specified capacity. @@ -61,7 +62,8 @@ impl UniqueEntityEquivalentVec { /// Equivalent to [`Vec::with_capacity`] #[inline] pub fn with_capacity(capacity: usize) -> Self { - Self(Vec::with_capacity(capacity)) + // SAFETY: Any empty Vec cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::with_capacity(capacity)) } } /// Creates a `UniqueEntityEquivalentVec` directly from a pointer, a length, and a capacity. @@ -74,8 +76,9 @@ impl UniqueEntityEquivalentVec { /// and the resulting [`Vec`] must only contain unique elements. #[inline] pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self { - // SAFETY: Caller ensures it's safe to call `Vec::from_raw_parts` - Self(unsafe { Vec::from_raw_parts(ptr, length, capacity) }) + // SAFETY: Caller ensures it is safe to call `Vec::from_raw_parts`, and that + // the resulting `Vec` only contains unique elements. + unsafe { Self::from_vec_unchecked(Vec::from_raw_parts(ptr, length, capacity)) } } /// Constructs a `UniqueEntityEquivalentVec` from a [`Vec`] unsafely. @@ -420,7 +423,8 @@ impl UniqueEntityEquivalentVec { /// Equivalent to [`Vec::split_off`]. #[inline] pub fn split_off(&mut self, at: usize) -> Self { - Self(self.0.split_off(at)) + // SAFETY: Any subslice/subsection of a `UniqueEntityVec` is also unique. + unsafe { Self::from_vec_unchecked(self.0.split_off(at)) } } /// Resizes the `Vec` in-place so that `len` is equal to `new_len`. @@ -481,7 +485,8 @@ impl UniqueEntityEquivalentVec { impl Default for UniqueEntityEquivalentVec { #[inline] fn default() -> Self { - Self(Vec::default()) + // SAFETY: An empty `Vec` cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::default()) } } } @@ -798,42 +803,48 @@ where impl From<&[T; 1]> for UniqueEntityEquivalentVec { #[inline] fn from(value: &[T; 1]) -> Self { - Self(Vec::from(value)) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } impl From<&[T; 0]> for UniqueEntityEquivalentVec { #[inline] fn from(value: &[T; 0]) -> Self { - Self(Vec::from(value)) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } impl From<&mut [T; 1]> for UniqueEntityEquivalentVec { #[inline] fn from(value: &mut [T; 1]) -> Self { - Self(Vec::from(value)) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } impl From<&mut [T; 0]> for UniqueEntityEquivalentVec { #[inline] fn from(value: &mut [T; 0]) -> Self { - Self(Vec::from(value)) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } impl From<[T; 1]> for UniqueEntityEquivalentVec { #[inline] fn from(value: [T; 1]) -> Self { - Self(Vec::from(value)) + // SAFETY: An array with 1 element cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } impl From<[T; 0]> for UniqueEntityEquivalentVec { #[inline] fn from(value: [T; 0]) -> Self { - Self(Vec::from(value)) + // SAFETY: An empty array cannot contain duplicates. + unsafe { Self::from_vec_unchecked(Vec::from(value)) } } } @@ -842,7 +853,8 @@ impl From<&UniqueEntityEquivalentAr { #[inline] fn from(value: &UniqueEntityEquivalentArray) -> Self { - Self(Vec::from(value.as_inner().clone())) + // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements. + unsafe { Self::from_vec_unchecked(Vec::from(value.as_inner().clone())) } } } @@ -851,7 +863,8 @@ impl From<&mut UniqueEntityEquivale { #[inline] fn from(value: &mut UniqueEntityEquivalentArray) -> Self { - Self(Vec::from(value.as_inner().clone())) + // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements. + unsafe { Self::from_vec_unchecked(Vec::from(value.as_inner().clone())) } } } @@ -860,7 +873,8 @@ impl From { #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { - Self(Vec::from(value.into_inner())) + // SAFETY: `UniqueEntityEquivalentArray` only contains unique elements. + unsafe { Self::from_vec_unchecked(Vec::from(value.into_inner())) } } } @@ -993,7 +1007,8 @@ impl TryFrom> impl From> for UniqueEntityEquivalentVec { #[inline] fn from(value: BTreeSet) -> Self { - Self(value.into_iter().collect::>()) + // SAFETY: A `BTreeSet` over an `EntityEquivalent` T only contains unique elements. + unsafe { Self::from_vec_unchecked(value.into_iter().collect::>()) } } } From 68546a916b527d8ca5555184e9e948cf7c8b77a6 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Sat, 11 Oct 2025 00:37:55 +0200 Subject: [PATCH 5/8] turn various entity set functions const --- crates/bevy_ecs/src/entity/entity_set.rs | 12 ++++---- crates/bevy_ecs/src/entity/hash_map.rs | 10 +++--- crates/bevy_ecs/src/entity/hash_set.rs | 14 +++++---- crates/bevy_ecs/src/entity/index_map.rs | 24 ++++++++------- crates/bevy_ecs/src/entity/index_set.rs | 14 +++++---- crates/bevy_ecs/src/entity/unique_array.rs | 4 +-- crates/bevy_ecs/src/entity/unique_slice.rs | 12 ++++---- crates/bevy_ecs/src/entity/unique_vec.rs | 36 +++++++++++----------- 8 files changed, 67 insertions(+), 59 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index 68f0e72fc4f55..f784a9aff88f6 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -373,7 +373,7 @@ pub struct UniqueEntityIter> { impl UniqueEntityIter { /// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`]. #[inline] - pub fn from_entity_set_iter(iter: I) -> Self { + pub const fn from_entity_set_iter(iter: I) -> Self { // SAFETY: iter implements `EntitySetIterator`. unsafe { Self::from_iter_unchecked(iter) } } @@ -386,7 +386,7 @@ impl> UniqueEntityIter { /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. #[inline] - pub unsafe fn from_iter_unchecked(iter: I) -> Self { + pub const unsafe fn from_iter_unchecked(iter: I) -> Self { Self { iter } } @@ -396,7 +396,7 @@ impl> UniqueEntityIter { /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. #[inline] - pub unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self { + pub const unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self { // SAFETY: UniqueEntityIter is a transparent wrapper around I. unsafe { &*ptr::from_ref(iter).cast() } } @@ -407,7 +407,7 @@ impl> UniqueEntityIter { /// `iter` must only yield unique elements. /// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`]. #[inline] - pub unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self { + pub const unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self { // SAFETY: UniqueEntityIter is a transparent wrapper around I. unsafe { &mut *ptr::from_mut(iter).cast() } } @@ -420,7 +420,7 @@ impl> UniqueEntityIter { /// Returns a reference to the inner `I`. #[inline] - pub fn as_inner(&self) -> &I { + pub const fn as_inner(&self) -> &I { &self.iter } @@ -431,7 +431,7 @@ impl> UniqueEntityIter { /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. #[inline] - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index 02273e0163bf0..8392f56433e9f 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -44,7 +44,7 @@ impl EntityHashMap { /// Constructs an `EntityHashMap` from an [`HashMap`]. #[inline] - pub fn from_index_map(set: HashMap) -> Self { + pub const fn from_index_map(set: HashMap) -> Self { Self(set) } @@ -193,13 +193,15 @@ impl<'a, V> Keys<'a, V> { /// `keys` must either be empty, or have been obtained from a /// [`hash_map::HashMap`] using the `S` hasher. #[inline] - pub unsafe fn from_keys_unchecked(keys: hash_map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { + pub const unsafe fn from_keys_unchecked( + keys: hash_map::Keys<'a, Entity, V>, + ) -> Keys<'a, V, S> { Keys::<'_, _, S>(keys, PhantomData) } /// Returns the inner [`Keys`](hash_map::Keys). #[inline] - pub fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { + pub const fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { self.0 } } @@ -269,7 +271,7 @@ impl IntoKeys { /// `into_keys` must either be empty, or have been obtained from a /// [`hash_map::HashMap`] using the `S` hasher. #[inline] - pub unsafe fn from_into_keys_unchecked( + pub const unsafe fn from_into_keys_unchecked( into_keys: hash_map::IntoKeys, ) -> IntoKeys { IntoKeys::<_, S>(into_keys, PhantomData) diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index ce6bfdbb10577..287e5c0c1bf74 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -59,7 +59,7 @@ impl EntityHashSet { /// Constructs an `EntityHashSet` from an [`HashSet`]. #[inline] - pub fn from_hash_set(set: HashSet) -> Self { + pub const fn from_hash_set(set: HashSet) -> Self { Self(set) } @@ -255,13 +255,13 @@ impl<'a> Iter<'a> { /// `iter` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. #[inline] - pub unsafe fn from_iter_unchecked(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> { + pub const unsafe fn from_iter_unchecked(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> { Iter::<'_, S>(iter, PhantomData) } /// Returns the inner [`Iter`](hash_set::Iter). #[inline] - pub fn into_inner(self) -> hash_set::Iter<'a, Entity> { + pub const fn into_inner(self) -> hash_set::Iter<'a, Entity> { self.0 } } @@ -328,7 +328,7 @@ impl IntoIter { /// `into_iter` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. #[inline] - pub unsafe fn from_into_iter_unchecked( + pub const unsafe fn from_into_iter_unchecked( into_iter: hash_set::IntoIter, ) -> IntoIter { IntoIter::(into_iter, PhantomData) @@ -398,7 +398,9 @@ impl<'a> Drain<'a> { /// `drain` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. #[inline] - pub unsafe fn from_drain_unchecked(drain: hash_set::Drain<'a, Entity>) -> Drain<'a, S> { + pub const unsafe fn from_drain_unchecked( + drain: hash_set::Drain<'a, Entity>, + ) -> Drain<'a, S> { Drain::<'_, S>(drain, PhantomData) } @@ -461,7 +463,7 @@ impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> { /// `extract_if` must either be empty, or have been obtained from a /// [`hash_set::HashSet`] using the `S` hasher. #[inline] - pub unsafe fn from_extract_if_unchecked( + pub const unsafe fn from_extract_if_unchecked( extract_if: hash_set::ExtractIf<'a, Entity, F>, ) -> ExtractIf<'a, F, S> { ExtractIf::<'_, _, S>(extract_if, PhantomData) diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index fd471583214ed..15e1a347fc577 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -53,7 +53,7 @@ impl EntityIndexMap { /// Constructs an `EntityIndexMap` from an [`IndexMap`]. #[inline] - pub fn from_index_map(set: IndexMap) -> Self { + pub const fn from_index_map(set: IndexMap) -> Self { Self(set) } @@ -482,7 +482,7 @@ impl Slice { reason = "We wish to access the Box API of the inner type, without consuming it." )] #[inline] - pub fn as_boxed_inner(self: &Box) -> &Box> { + pub const fn as_boxed_inner(self: &Box) -> &Box> { // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice. unsafe { &*(ptr::from_ref(self).cast::>>()) } } @@ -943,13 +943,13 @@ impl<'a, V> Iter<'a, V> { /// `iter` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_iter_unchecked(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> { + pub const unsafe fn from_iter_unchecked(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> { Iter::<'_, _, S>(iter, PhantomData) } /// Returns the inner [`Iter`](map::Iter). #[inline] - pub fn into_inner(self) -> map::Iter<'a, Entity, V> { + pub const fn into_inner(self) -> map::Iter<'a, Entity, V> { self.0 } @@ -1028,7 +1028,7 @@ impl<'a, V> IterMut<'a, V> { /// `iter_mut` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_iter_mut_unchecked( + pub const unsafe fn from_iter_mut_unchecked( iter_mut: map::IterMut<'a, Entity, V>, ) -> IterMut<'a, V, S> { IterMut::<'_, _, S>(iter_mut, PhantomData) @@ -1036,7 +1036,7 @@ impl<'a, V> IterMut<'a, V> { /// Returns the inner [`IterMut`](map::IterMut). #[inline] - pub fn into_inner(self) -> map::IterMut<'a, Entity, V> { + pub const fn into_inner(self) -> map::IterMut<'a, Entity, V> { self.0 } @@ -1119,7 +1119,7 @@ impl IntoIter { /// `into_iter` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_into_iter_unchecked( + pub const unsafe fn from_into_iter_unchecked( into_iter: map::IntoIter, ) -> IntoIter { IntoIter::<_, S>(into_iter, PhantomData) @@ -1218,7 +1218,9 @@ impl<'a, V> Drain<'a, V> { /// `drain` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_drain_unchecked(drain: map::Drain<'a, Entity, V>) -> Drain<'a, V, S> { + pub const unsafe fn from_drain_unchecked( + drain: map::Drain<'a, Entity, V>, + ) -> Drain<'a, V, S> { Drain::<'_, _, S>(drain, PhantomData) } @@ -1290,13 +1292,13 @@ impl<'a, V> Keys<'a, V> { /// `keys` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_keys_unchecked(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { + pub const unsafe fn from_keys_unchecked(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> { Keys::<'_, _, S>(keys, PhantomData) } /// Returns the inner [`Keys`](map::Keys). #[inline] - pub fn into_inner(self) -> map::Keys<'a, Entity, V> { + pub const fn into_inner(self) -> map::Keys<'a, Entity, V> { self.0 } } @@ -1378,7 +1380,7 @@ impl IntoKeys { /// `into_keys` must either be empty, or have been obtained from a /// [`IndexMap`] using the `S` hasher. #[inline] - pub unsafe fn from_into_keys_unchecked( + pub const unsafe fn from_into_keys_unchecked( into_keys: map::IntoKeys, ) -> IntoKeys { IntoKeys::<_, S>(into_keys, PhantomData) diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index a3e569221414b..7c1fede9ec3fe 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -50,7 +50,7 @@ impl EntityIndexSet { /// Constructs an `EntityIndexSet` from an [`IndexSet`]. #[inline] - pub fn from_index_set(set: IndexSet) -> Self { + pub const fn from_index_set(set: IndexSet) -> Self { Self(set) } @@ -362,7 +362,7 @@ impl Slice { reason = "We wish to access the Box API of the inner type, without consuming it." )] #[inline] - pub fn as_boxed_inner(self: &Box) -> &Box> { + pub const fn as_boxed_inner(self: &Box) -> &Box> { // SAFETY: Slice is a transparent wrapper around indexmap::set::Slice. unsafe { &*(ptr::from_ref(self).cast::>>()) } } @@ -624,13 +624,13 @@ impl<'a> Iter<'a> { /// `iter` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. #[inline] - pub unsafe fn from_iter_unchecked(iter: set::Iter<'a, Entity>) -> Iter<'a, S> { + pub const unsafe fn from_iter_unchecked(iter: set::Iter<'a, Entity>) -> Iter<'a, S> { Iter::<'_, S>(iter, PhantomData) } /// Returns the inner [`Iter`](set::Iter). #[inline] - pub fn into_inner(self) -> set::Iter<'a, Entity> { + pub const fn into_inner(self) -> set::Iter<'a, Entity> { self.0 } @@ -713,7 +713,9 @@ impl IntoIter { /// `into_iter` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. #[inline] - pub unsafe fn from_into_iter_unchecked(into_iter: set::IntoIter) -> IntoIter { + pub const unsafe fn from_into_iter_unchecked( + into_iter: set::IntoIter, + ) -> IntoIter { IntoIter::(into_iter, PhantomData) } @@ -805,7 +807,7 @@ impl<'a> Drain<'a> { /// `drain` must either be empty, or have been obtained from a /// [`IndexSet`] using the `S` hasher. #[inline] - pub unsafe fn from_drain_unchecked(drain: set::Drain<'a, Entity>) -> Drain<'a, S> { + pub const unsafe fn from_drain_unchecked(drain: set::Drain<'a, Entity>) -> Drain<'a, S> { Drain::<'_, S>(drain, PhantomData) } diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index 28349170b37fd..57bf5ce9249be 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -124,7 +124,7 @@ impl UniqueEntityEquivalentArray { /// Returns a reference to the inner array. #[inline] - pub fn as_inner(&self) -> &[T; N] { + pub const fn as_inner(&self) -> &[T; N] { &self.0 } @@ -138,7 +138,7 @@ impl UniqueEntityEquivalentArray { /// Returns a mutable slice containing the entire array. Equivalent to /// `&mut s[..]`. #[inline] - pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { + pub const fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { // SAFETY: All elements in the original array are unique. unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } } diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index a279bc676359d..deaf028b64ff5 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -1659,7 +1659,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// All elements in each of the slices must be unique. #[inline] - pub unsafe fn from_slice_iter_unchecked(iter: I) -> Self { + pub const unsafe fn from_slice_iter_unchecked(iter: I) -> Self { Self { iter } } @@ -1671,7 +1671,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// Returns a reference to the inner `I`. #[inline] - pub fn as_inner(&self) -> &I { + pub const fn as_inner(&self) -> &I { &self.iter } @@ -1682,7 +1682,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. #[inline] - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } @@ -1853,7 +1853,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// All elements in each of the slices must be unique. #[inline] - pub unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self { + pub const unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self { Self { iter } } @@ -1865,7 +1865,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// Returns a reference to the inner `I`. #[inline] - pub fn as_inner(&self) -> &I { + pub const fn as_inner(&self) -> &I { &self.iter } @@ -1876,7 +1876,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. #[inline] - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index e6b6c26b56cc9..9145948016da5 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -87,7 +87,7 @@ impl UniqueEntityEquivalentVec { /// /// `vec` must contain only unique elements. #[inline] - pub unsafe fn from_vec_unchecked(vec: Vec) -> Self { + pub const unsafe fn from_vec_unchecked(vec: Vec) -> Self { Self(vec) } @@ -97,7 +97,7 @@ impl UniqueEntityEquivalentVec { /// /// `vec` must contain only unique elements. #[inline] - pub unsafe fn from_vec_ref_unchecked(vec: &Vec) -> &Self { + pub const unsafe fn from_vec_ref_unchecked(vec: &Vec) -> &Self { // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. unsafe { &*ptr::from_ref(vec).cast() } } @@ -108,7 +108,7 @@ impl UniqueEntityEquivalentVec { /// /// `vec` must contain only unique elements. #[inline] - pub unsafe fn from_vec_mut_unchecked(vec: &mut Vec) -> &mut Self { + pub const unsafe fn from_vec_mut_unchecked(vec: &mut Vec) -> &mut Self { // SAFETY: UniqueEntityEquivalentVec is a transparent wrapper around Vec. unsafe { &mut *ptr::from_mut(vec).cast() } } @@ -121,7 +121,7 @@ impl UniqueEntityEquivalentVec { /// Returns a reference to the inner [`Vec`]. #[inline] - pub fn as_vec(&self) -> &Vec { + pub const fn as_vec(&self) -> &Vec { &self.0 } @@ -132,7 +132,7 @@ impl UniqueEntityEquivalentVec { /// The elements of this `Vec` must always remain unique, even while /// this mutable reference is live. #[inline] - pub unsafe fn as_mut_vec(&mut self) -> &mut Vec { + pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec { &mut self.0 } @@ -141,7 +141,7 @@ impl UniqueEntityEquivalentVec { /// /// Equivalent to [`Vec::capacity`]. #[inline] - pub fn capacity(&self) -> usize { + pub const fn capacity(&self) -> usize { self.0.capacity() } @@ -208,14 +208,16 @@ impl UniqueEntityEquivalentVec { /// Extracts a slice containing the entire vector. #[inline] - pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { - self + pub const fn as_slice(&self) -> &UniqueEntityEquivalentSlice { + // SAFETY: All elements in the original slice are unique. + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(self.0.as_slice()) } } /// Extracts a mutable slice of the entire vector. #[inline] - pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { - self + pub const fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { + // SAFETY: All elements in the original slice are unique. + unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) } } /// Shortens the vector, keeping the first `len` elements and dropping @@ -232,7 +234,7 @@ impl UniqueEntityEquivalentVec { /// /// Equivalent to [`Vec::as_ptr`]. #[inline] - pub fn as_ptr(&self) -> *const T { + pub const fn as_ptr(&self) -> *const T { self.0.as_ptr() } /// Returns a raw mutable pointer to the vector's buffer, or a dangling @@ -240,7 +242,7 @@ impl UniqueEntityEquivalentVec { /// /// Equivalent to [`Vec::as_mut_ptr`]. #[inline] - pub fn as_mut_ptr(&mut self) -> *mut T { + pub const fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr() } @@ -406,7 +408,7 @@ impl UniqueEntityEquivalentVec { /// /// Equivalent to [`Vec::len`]. #[inline] - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.len() } @@ -414,7 +416,7 @@ impl UniqueEntityEquivalentVec { /// /// Equivalent to [`Vec::is_empty`]. #[inline] - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.0.is_empty() } @@ -495,16 +497,14 @@ impl Deref for UniqueEntityEquivalentVec { #[inline] fn deref(&self) -> &Self::Target { - // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } + self.as_slice() } } impl DerefMut for UniqueEntityEquivalentVec { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - // SAFETY: All elements in the original slice are unique. - unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } + self.as_mut_slice() } } From 1f1d20527b0de00ad3fe123a504e39d86d962b3c Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Sat, 11 Oct 2025 00:38:07 +0200 Subject: [PATCH 6/8] miscellaneous simplifications --- crates/bevy_ecs/src/entity/entity_set.rs | 2 +- crates/bevy_ecs/src/entity/hash_set.rs | 6 ------ crates/bevy_ecs/src/entity/unique_array.rs | 6 ++---- .../src/relationship/relationship_source_collection.rs | 2 +- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index f784a9aff88f6..849def365e6c4 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -373,7 +373,7 @@ pub struct UniqueEntityIter> { impl UniqueEntityIter { /// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`]. #[inline] - pub const fn from_entity_set_iter(iter: I) -> Self { + pub const fn from_entity_set_iter(iter: I) -> Self { // SAFETY: iter implements `EntitySetIterator`. unsafe { Self::from_iter_unchecked(iter) } } diff --git a/crates/bevy_ecs/src/entity/hash_set.rs b/crates/bevy_ecs/src/entity/hash_set.rs index 287e5c0c1bf74..92d3357242c3a 100644 --- a/crates/bevy_ecs/src/entity/hash_set.rs +++ b/crates/bevy_ecs/src/entity/hash_set.rs @@ -45,12 +45,6 @@ impl EntityHashSet { Self(HashSet::with_capacity_and_hasher(n, EntityHash)) } - /// Returns the number of elements in the set. - #[inline] - pub fn len(&self) -> usize { - self.0.len() - } - /// Returns `true` if the set contains no elements. #[inline] pub fn is_empty(&self) -> bool { diff --git a/crates/bevy_ecs/src/entity/unique_array.rs b/crates/bevy_ecs/src/entity/unique_array.rs index 57bf5ce9249be..7e578f6d33590 100644 --- a/crates/bevy_ecs/src/entity/unique_array.rs +++ b/crates/bevy_ecs/src/entity/unique_array.rs @@ -158,16 +158,14 @@ impl Deref for UniqueEntityEquivalentArray< #[inline] fn deref(&self) -> &Self::Target { - // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked(&self.0) } + self.as_slice() } } impl DerefMut for UniqueEntityEquivalentArray { #[inline] fn deref_mut(&mut self) -> &mut Self::Target { - // SAFETY: All elements in the original array are unique. - unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(&mut self.0) } + self.as_mut_slice() } } diff --git a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs index 4f3fdb2c036c2..514a1f3f49f89 100644 --- a/crates/bevy_ecs/src/relationship/relationship_source_collection.rs +++ b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs @@ -262,7 +262,7 @@ impl RelationshipSourceCollection for EntityHashSet { } fn len(&self) -> usize { - self.len() + self.deref().len() } fn clear(&mut self) { From a1530b04464014995aedfd37dfda77a575472196 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Sat, 11 Oct 2025 00:59:11 +0200 Subject: [PATCH 7/8] doc cleanup --- crates/bevy_ecs/src/entity/entity_set.rs | 6 +++--- crates/bevy_ecs/src/entity/hash_map.rs | 2 +- crates/bevy_ecs/src/entity/index_map.rs | 14 +++++++++----- crates/bevy_ecs/src/entity/index_set.rs | 2 +- crates/bevy_ecs/src/entity/unique_slice.rs | 3 ++- crates/bevy_ecs/src/entity/unique_vec.rs | 4 ++-- crates/bevy_ecs/src/system/query.rs | 6 +++--- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/crates/bevy_ecs/src/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index 849def365e6c4..b1f2ea7aefd65 100644 --- a/crates/bevy_ecs/src/entity/entity_set.rs +++ b/crates/bevy_ecs/src/entity/entity_set.rs @@ -58,7 +58,7 @@ pub trait ContainsEntity { /// To obtain hash values forming the same total order as [`Entity`], any [`Hasher`] used must be /// deterministic and concerning [`Entity`], collisionless. /// Standard library hash collections handle collisions with an [`Eq`] fallback, but do not account for -/// determinism when [`BuildHasher`] is unspecified,. +/// determinism when [`BuildHasher`] is unspecified. /// /// [`Hash`]: core::hash::Hash /// [`Hasher`]: core::hash::Hasher @@ -149,12 +149,12 @@ unsafe impl EntityEquivalent for Arc {} /// As a consequence, [`into_iter()`] on `EntitySet` will always produce another `EntitySet`. /// /// Implementing this trait allows for unique query iteration over a list of entities. -/// See [`iter_many_unique`] and [`iter_many_unique_mut`] +/// See [`iter_many_unique`] and [`iter_many_unique_mut`]. /// /// Note that there is no guarantee of the [`IntoIterator`] impl being deterministic, /// it might return different iterators when called multiple times. /// Neither is there a guarantee that the comparison trait impls of `EntitySet` match that -/// of the respective [`EntitySetIterator`] (or of a [`Vec`] collected from its elements) +/// of the respective [`EntitySetIterator`] (or of a [`Vec`] collected from its elements). /// /// [`Self::IntoIter`]: IntoIterator::IntoIter /// [`into_iter()`]: IntoIterator::into_iter diff --git a/crates/bevy_ecs/src/entity/hash_map.rs b/crates/bevy_ecs/src/entity/hash_map.rs index 8392f56433e9f..ef55643d2e36f 100644 --- a/crates/bevy_ecs/src/entity/hash_map.rs +++ b/crates/bevy_ecs/src/entity/hash_map.rs @@ -36,7 +36,7 @@ impl EntityHashMap { /// /// Equivalent to [`HashMap::with_capacity_and_hasher(n, EntityHash)`]. /// - /// [`HashMap:with_capacity_and_hasher(n, EntityHash)`]: HashMap::with_capacity_and_hasher + /// [`HashMap::with_capacity_and_hasher(n, EntityHash)`]: HashMap::with_capacity_and_hasher #[inline] pub fn with_capacity(n: usize) -> Self { Self(HashMap::with_capacity_and_hasher(n, EntityHash)) diff --git a/crates/bevy_ecs/src/entity/index_map.rs b/crates/bevy_ecs/src/entity/index_map.rs index 15e1a347fc577..0ae3c76d431b5 100644 --- a/crates/bevy_ecs/src/entity/index_map.rs +++ b/crates/bevy_ecs/src/entity/index_map.rs @@ -18,7 +18,11 @@ use core::{ #[cfg(feature = "bevy_reflect")] use bevy_reflect::Reflect; pub use indexmap::map::Entry; -use indexmap::map::{self, IndexMap, IntoValues, ValuesMut}; +use indexmap::{ + self, + map::{self, IntoValues, ValuesMut}, + IndexMap, +}; use super::{Entity, EntityEquivalent, EntityHash, EntitySetIterator}; @@ -35,7 +39,7 @@ impl EntityIndexMap { /// /// Equivalent to [`IndexMap::with_hasher(EntityHash)`]. /// - /// [`IndexMap::with_hasher(EntityHash)`]: IndexMap::with_hasher + /// [`IndexMap::with_hasher(EntityHash)`]: indexmap::IndexMap::with_hasher #[inline] pub const fn new() -> Self { Self(IndexMap::with_hasher(EntityHash)) @@ -45,7 +49,7 @@ impl EntityIndexMap { /// /// Equivalent to [`IndexMap::with_capacity_and_hasher(n, EntityHash)`]. /// - /// [`IndexMap:with_capacity_and_hasher(n, EntityHash)`]: IndexMap::with_capacity_and_hasher + /// [`IndexMap::with_capacity_and_hasher(n, EntityHash)`]: indexmap::IndexMap::with_capacity_and_hasher #[inline] pub fn with_capacity(n: usize) -> Self { Self(IndexMap::with_capacity_and_hasher(n, EntityHash)) @@ -1224,7 +1228,7 @@ impl<'a, V> Drain<'a, V> { Drain::<'_, _, S>(drain, PhantomData) } - /// Returns the inner [`Drain`](map::Drain). + /// Returns the inner [`Drain`](indexmap::map::Drain). #[inline] pub fn into_inner(self) -> map::Drain<'a, Entity, V> { self.0 @@ -1232,7 +1236,7 @@ impl<'a, V> Drain<'a, V> { /// Returns a slice of the remaining entries in the iterator. /// - /// Equivalent to [`map::Drain::as_slice`]. + /// Equivalent to [`map::Drain::as_slice`](`indexmap::map::Drain::as_slice`). #[inline] pub fn as_slice(&self) -> &Slice { // SAFETY: The source IndexMap uses EntityHash. diff --git a/crates/bevy_ecs/src/entity/index_set.rs b/crates/bevy_ecs/src/entity/index_set.rs index 7c1fede9ec3fe..586fa73bfe041 100644 --- a/crates/bevy_ecs/src/entity/index_set.rs +++ b/crates/bevy_ecs/src/entity/index_set.rs @@ -16,7 +16,7 @@ use core::{ ptr, }; -use indexmap::set::{self, IndexSet}; +use indexmap::{self, set, IndexSet}; use super::{Entity, EntityHash, EntitySetIterator}; diff --git a/crates/bevy_ecs/src/entity/unique_slice.rs b/crates/bevy_ecs/src/entity/unique_slice.rs index deaf028b64ff5..e38440282d54a 100644 --- a/crates/bevy_ecs/src/entity/unique_slice.rs +++ b/crates/bevy_ecs/src/entity/unique_slice.rs @@ -369,7 +369,8 @@ impl UniqueEntityEquivalentSlice { } } - /// + /// Returns an iterator over `chunk_size` elements of the slice at a time, starting at the + /// beginning of the slice. /// /// Equivalent to [`[T]::chunks_exact`]. /// diff --git a/crates/bevy_ecs/src/entity/unique_vec.rs b/crates/bevy_ecs/src/entity/unique_vec.rs index 9145948016da5..24fc32f4e2dec 100644 --- a/crates/bevy_ecs/src/entity/unique_vec.rs +++ b/crates/bevy_ecs/src/entity/unique_vec.rs @@ -91,7 +91,7 @@ impl UniqueEntityEquivalentVec { Self(vec) } - /// Constructs a `UniqueEntityEquivalentVec` from a [`&Vec`] unsafely. + /// Constructs a `UniqueEntityEquivalentVec` from a [`&Vec`](Vec) unsafely. /// /// # Safety /// @@ -102,7 +102,7 @@ impl UniqueEntityEquivalentVec { unsafe { &*ptr::from_ref(vec).cast() } } - /// Constructs a `UniqueEntityEquivalentVec` from a [`&mut Vec`] unsafely. + /// Constructs a `UniqueEntityEquivalentVec` from a [`&mut Vec`](Vec) unsafely. /// /// # Safety /// diff --git a/crates/bevy_ecs/src/system/query.rs b/crates/bevy_ecs/src/system/query.rs index fb89216075afa..e9c9998ca08b2 100644 --- a/crates/bevy_ecs/src/system/query.rs +++ b/crates/bevy_ecs/src/system/query.rs @@ -939,7 +939,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// fn into_iter(self) -> Self::IntoIter { /// // SAFETY: `Friends` ensures that it unique_list contains only unique entities. - /// unsafe { UniqueEntityIter::from_iterator_unchecked(self.unique_list.iter()) } + /// unsafe { UniqueEntityIter::from_iter_unchecked(self.unique_list.iter()) } /// } /// } /// @@ -994,7 +994,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// fn into_iter(self) -> Self::IntoIter { /// // SAFETY: `Friends` ensures that it unique_list contains only unique entities. - /// unsafe { UniqueEntityIter::from_iterator_unchecked(self.unique_list.iter()) } + /// unsafe { UniqueEntityIter::from_iter_unchecked(self.unique_list.iter()) } /// } /// } /// @@ -1050,7 +1050,7 @@ impl<'w, 's, D: QueryData, F: QueryFilter> Query<'w, 's, D, F> { /// /// fn into_iter(self) -> Self::IntoIter { /// // SAFETY: `Friends` ensures that it unique_list contains only unique entities. - /// unsafe { UniqueEntityIter::from_iterator_unchecked(self.unique_list.iter()) } + /// unsafe { UniqueEntityIter::from_iter_unchecked(self.unique_list.iter()) } /// } /// } /// From 3c0c67a7eccf2f179a9d3c4715f2c13f8aeab4a3 Mon Sep 17 00:00:00 2001 From: Victoron <59878206+Victoronz@users.noreply.github.com> Date: Sat, 11 Oct 2025 00:59:34 +0200 Subject: [PATCH 8/8] update bevy_ecs msrv to 1.89 --- Cargo.toml | 2 +- crates/bevy_ecs/Cargo.toml | 2 +- crates/bevy_ecs/src/component/clone.rs | 20 +++++----- crates/bevy_ecs/src/entity/clone_entities.rs | 10 ++--- crates/bevy_ecs/src/error/bevy_error.rs | 33 ++++++++-------- crates/bevy_ecs/src/observer/mod.rs | 24 ++++++------ crates/bevy_ecs/src/query/fetch.rs | 8 ++-- crates/bevy_ecs/src/relationship/mod.rs | 41 ++++++++++---------- crates/bevy_ecs/src/system/system.rs | 8 ++-- 9 files changed, 73 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ad106d740ec7e..3c62d2a9983ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["game", "engine", "gamedev", "graphics", "bevy"] license = "MIT OR Apache-2.0" repository = "https://github.com/bevyengine/bevy" documentation = "https://docs.rs/bevy" -rust-version = "1.88.0" +rust-version = "1.89.0" [workspace] resolver = "2" diff --git a/crates/bevy_ecs/Cargo.toml b/crates/bevy_ecs/Cargo.toml index eb39ed859e33a..86ab6110926be 100644 --- a/crates/bevy_ecs/Cargo.toml +++ b/crates/bevy_ecs/Cargo.toml @@ -8,7 +8,7 @@ repository = "https://github.com/bevyengine/bevy" license = "MIT OR Apache-2.0" keywords = ["ecs", "game", "bevy"] categories = ["game-engines", "data-structures"] -rust-version = "1.86.0" +rust-version = "1.89.0" [features] default = ["std", "bevy_reflect", "async_executor", "backtrace"] diff --git a/crates/bevy_ecs/src/component/clone.rs b/crates/bevy_ecs/src/component/clone.rs index 3a3784350e36a..a645463923bb6 100644 --- a/crates/bevy_ecs/src/component/clone.rs +++ b/crates/bevy_ecs/src/component/clone.rs @@ -110,20 +110,18 @@ pub fn component_clone_via_reflect(source: &SourceComponent, ctx: &mut Component // Try to clone using ReflectFromReflect if let Some(reflect_from_reflect) = registry.get_type_data::(type_id) - { - if let Some(mut component) = + && let Some(mut component) = reflect_from_reflect.from_reflect(source_component_reflect.as_partial_reflect()) + { + if let Some(reflect_component) = + registry.get_type_data::(type_id) { - if let Some(reflect_component) = - registry.get_type_data::(type_id) - { - reflect_component.map_entities(&mut *component, ctx.entity_mapper()); - } - drop(registry); - - ctx.write_target_component_reflect(component); - return; + reflect_component.map_entities(&mut *component, ctx.entity_mapper()); } + drop(registry); + + ctx.write_target_component_reflect(component); + return; } // Else, try to clone using ReflectDefault if let Some(reflect_default) = diff --git a/crates/bevy_ecs/src/entity/clone_entities.rs b/crates/bevy_ecs/src/entity/clone_entities.rs index 4c110d0057c9a..5cc8f1671a05c 100644 --- a/crates/bevy_ecs/src/entity/clone_entities.rs +++ b/crates/bevy_ecs/src/entity/clone_entities.rs @@ -1139,11 +1139,11 @@ impl OptOut { #[inline] fn filter_deny(&mut self, id: ComponentId, world: &World) { self.deny.insert(id); - if self.attach_required_by_components { - if let Some(required_by) = world.components().get_required_by(id) { - self.deny.extend(required_by.iter()); - }; - } + if self.attach_required_by_components + && let Some(required_by) = world.components().get_required_by(id) + { + self.deny.extend(required_by.iter()); + }; } } diff --git a/crates/bevy_ecs/src/error/bevy_error.rs b/crates/bevy_ecs/src/error/bevy_error.rs index c290e249b2cfd..414d51d7aa3df 100644 --- a/crates/bevy_ecs/src/error/bevy_error.rs +++ b/crates/bevy_ecs/src/error/bevy_error.rs @@ -191,10 +191,10 @@ mod tests { // On mac backtraces can start with Backtrace::create let mut skip = false; - if let Some(line) = lines.peek() { - if &line[6..] == "std::backtrace::Backtrace::create" { - skip = true; - } + if let Some(line) = lines.peek() + && &line[6..] == "std::backtrace::Backtrace::create" + { + skip = true; } if skip { @@ -212,10 +212,10 @@ mod tests { let line = lines.next().unwrap(); assert_eq!(&line[6..], expected); let mut skip = false; - if let Some(line) = lines.peek() { - if line.starts_with(" at") { - skip = true; - } + if let Some(line) = lines.peek() + && line.starts_with(" at") + { + skip = true; } if skip { @@ -225,19 +225,20 @@ mod tests { // on linux there is a second call_once let mut skip = false; - if let Some(line) = lines.peek() { - if &line[6..] == "core::ops::function::FnOnce::call_once" { - skip = true; - } + if let Some(line) = lines.peek() + && &line[6..] == "core::ops::function::FnOnce::call_once" + { + skip = true; } + if skip { lines.next().unwrap(); } let mut skip = false; - if let Some(line) = lines.peek() { - if line.starts_with(" at") { - skip = true; - } + if let Some(line) = lines.peek() + && line.starts_with(" at") + { + skip = true; } if skip { diff --git a/crates/bevy_ecs/src/observer/mod.rs b/crates/bevy_ecs/src/observer/mod.rs index d268085c40e8f..6732b35c043b2 100644 --- a/crates/bevy_ecs/src/observer/mod.rs +++ b/crates/bevy_ecs/src/observer/mod.rs @@ -229,18 +229,18 @@ impl World { && observers.entity_component_observers.is_empty() { cache.component_observers.remove(component); - if let Some(flag) = Observers::is_archetype_cached(event_key) { - if let Some(by_component) = archetypes.by_component.get(component) { - for archetype in by_component.keys() { - let archetype = &mut archetypes.archetypes[archetype.index()]; - if archetype.contains(*component) { - let no_longer_observed = archetype - .iter_components() - .all(|id| !cache.component_observers.contains_key(&id)); - - if no_longer_observed { - archetype.flags.set(flag, false); - } + if let Some(flag) = Observers::is_archetype_cached(event_key) + && let Some(by_component) = archetypes.by_component.get(component) + { + for archetype in by_component.keys() { + let archetype = &mut archetypes.archetypes[archetype.index()]; + if archetype.contains(*component) { + let no_longer_observed = archetype + .iter_components() + .all(|id| !cache.component_observers.contains_key(&id)); + + if no_longer_observed { + archetype.flags.set(flag, false); } } } diff --git a/crates/bevy_ecs/src/query/fetch.rs b/crates/bevy_ecs/src/query/fetch.rs index 7b1ff2387f1d4..e2a68a262b89c 100644 --- a/crates/bevy_ecs/src/query/fetch.rs +++ b/crates/bevy_ecs/src/query/fetch.rs @@ -3208,10 +3208,10 @@ mod tests { fn system(query: Query) { for entity_ref in &query { - if let Some(c) = entity_ref.get_ref::() { - if !c.is_added() { - panic!("Expected C to be added"); - } + if let Some(c) = entity_ref.get_ref::() + && !c.is_added() + { + panic!("Expected C to be added"); } } } diff --git a/crates/bevy_ecs/src/relationship/mod.rs b/crates/bevy_ecs/src/relationship/mod.rs index b234318ea8947..c236da9213cfe 100644 --- a/crates/bevy_ecs/src/relationship/mod.rs +++ b/crates/bevy_ecs/src/relationship/mod.rs @@ -186,28 +186,27 @@ pub trait Relationship: Component + Sized { } } let target_entity = world.entity(entity).get::().unwrap().get(); - if let Ok(mut target_entity_mut) = world.get_entity_mut(target_entity) { - if let Some(mut relationship_target) = + if let Ok(mut target_entity_mut) = world.get_entity_mut(target_entity) + && let Some(mut relationship_target) = target_entity_mut.get_mut::() - { - relationship_target.collection_mut_risky().remove(entity); - if relationship_target.len() == 0 { - let command = |mut entity: EntityWorldMut| { - // this "remove" operation must check emptiness because in the event that an identical - // relationship is inserted on top, this despawn would result in the removal of that identical - // relationship ... not what we want! - if entity - .get::() - .is_some_and(RelationshipTarget::is_empty) - { - entity.remove::(); - } - }; - - world - .commands() - .queue_silenced(command.with_entity(target_entity)); - } + { + relationship_target.collection_mut_risky().remove(entity); + if relationship_target.len() == 0 { + let command = |mut entity: EntityWorldMut| { + // this "remove" operation must check emptiness because in the event that an identical + // relationship is inserted on top, this despawn would result in the removal of that identical + // relationship ... not what we want! + if entity + .get::() + .is_some_and(RelationshipTarget::is_empty) + { + entity.remove::(); + } + }; + + world + .commands() + .queue_silenced(command.with_entity(target_entity)); } } } diff --git a/crates/bevy_ecs/src/system/system.rs b/crates/bevy_ecs/src/system/system.rs index 8986ec812a462..04a7cb8147e8e 100644 --- a/crates/bevy_ecs/src/system/system.rs +++ b/crates/bevy_ecs/src/system/system.rs @@ -442,10 +442,10 @@ where // Note that the `downcast_mut` check is based on the static type, // and can be optimized out after monomorphization. let any: &mut dyn Any = &mut value; - if let Some(err) = any.downcast_mut::() { - if err.skipped { - return Self::Skipped(core::mem::replace(err, SystemParamValidationError::EMPTY)); - } + if let Some(err) = any.downcast_mut::() + && err.skipped + { + return Self::Skipped(core::mem::replace(err, SystemParamValidationError::EMPTY)); } Self::Failed(From::from(value)) }