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/entity/entity_set.rs b/crates/bevy_ecs/src/entity/entity_set.rs index e4860685fe07f..b1f2ea7aefd65 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}; @@ -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 @@ -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() } @@ -143,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 @@ -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()); @@ -358,14 +365,17 @@ 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 { - Self { iter } + #[inline] + pub const fn from_entity_set_iter(iter: I) -> Self { + // SAFETY: iter implements `EntitySetIterator`. + unsafe { Self::from_iter_unchecked(iter) } } } @@ -375,17 +385,42 @@ 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 { + #[inline] + pub const 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`]. + #[inline] + 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() } + } + + /// 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`]. + #[inline] + 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() } + } + /// Returns the inner `I`. + #[inline] pub fn into_inner(self) -> I { self.iter } /// Returns a reference to the inner `I`. - pub fn as_inner(&self) -> &I { + #[inline] + pub const fn as_inner(&self) -> &I { &self.iter } @@ -395,7 +430,8 @@ impl> UniqueEntityIter { /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + #[inline] + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } @@ -403,10 +439,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() } @@ -415,6 +453,7 @@ impl> Iterator for UniqueEntityIter { impl> ExactSizeIterator for UniqueEntityIter {} impl> DoubleEndedIterator for UniqueEntityIter { + #[inline] fn next_back(&mut self) -> Option { self.iter.next_back() } @@ -426,6 +465,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() } @@ -434,6 +474,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()) } @@ -443,6 +484,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()) } @@ -451,6 +493,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(), @@ -460,6 +503,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(), @@ -506,7 +550,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.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 d83ea7bae1dcc..ef55643d2e36f 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`. @@ -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)) } @@ -35,12 +36,20 @@ 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)) } + /// Constructs an `EntityHashMap` from an [`HashMap`]. + #[inline] + pub const fn from_index_map(set: HashMap) -> Self { + Self(set) + } + /// Returns the inner [`HashMap`]. + #[inline] pub fn into_inner(self) -> HashMap { self.0 } @@ -49,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) } @@ -58,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()) } @@ -72,49 +84,64 @@ 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) + } +} + impl Index<&Q> for EntityHashMap { type Output = V; + #[inline] fn index(&self, key: &Q) -> &V { self.0.index(&key.entity()) } @@ -124,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() } @@ -133,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() } @@ -142,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() } @@ -156,8 +186,22 @@ 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. + #[inline] + 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). - pub fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { + #[inline] + pub const fn into_inner(self) -> hash_map::Keys<'a, Entity, V> { self.0 } } @@ -165,6 +209,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 } @@ -173,6 +218,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() } @@ -183,8 +229,10 @@ 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) + // SAFETY: We are cloning an already valid `Keys`. + unsafe { Self::from_keys_unchecked(self.0.clone()) } } } @@ -195,8 +243,10 @@ 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()) } } } @@ -214,7 +264,21 @@ 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. + #[inline] + pub const unsafe fn from_into_keys_unchecked( + into_keys: hash_map::IntoKeys, + ) -> IntoKeys { + IntoKeys::<_, S>(into_keys, PhantomData) + } + /// Returns the inner [`IntoKeys`](hash_map::IntoKeys). + #[inline] pub fn into_inner(self) -> hash_map::IntoKeys { self.0 } @@ -223,6 +287,7 @@ impl IntoKeys { impl Deref for IntoKeys { type Target = hash_map::IntoKeys; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -231,6 +296,7 @@ impl Deref for IntoKeys { impl Iterator for IntoKeys { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -250,8 +316,10 @@ 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 7fd1ae9011273..92d3357242c3a 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`. @@ -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,21 +40,25 @@ 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. - 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 const fn from_hash_set(set: HashSet) -> Self { + Self(set) + } + /// Returns the inner [`HashSet`]. + #[inline] pub fn into_inner(self) -> HashSet { self.0 } @@ -61,6 +66,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) } @@ -69,6 +75,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) } @@ -77,6 +84,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) } @@ -85,12 +93,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 } @@ -101,6 +111,7 @@ impl<'a> IntoIterator for &'a EntityHashSet { type IntoIter = Iter<'a>; + #[inline] fn into_iter(self) -> Self::IntoIter { Iter((&self.0).into_iter(), PhantomData) } @@ -111,6 +122,7 @@ impl IntoIterator for EntityHashSet { type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter(), PhantomData) } @@ -173,30 +185,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); @@ -210,6 +227,13 @@ impl FromEntitySetIterator for EntityHashSet { } } +impl From> for EntityHashSet { + #[inline] + 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. @@ -218,8 +242,20 @@ impl FromEntitySetIterator 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. + #[inline] + 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). - pub fn into_inner(self) -> hash_set::Iter<'a, Entity> { + #[inline] + pub const fn into_inner(self) -> hash_set::Iter<'a, Entity> { self.0 } } @@ -227,6 +263,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 } @@ -235,6 +272,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() } @@ -245,8 +283,10 @@ impl ExactSizeIterator for Iter<'_> {} 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()) } } } @@ -257,8 +297,10 @@ 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()) } } } @@ -273,7 +315,21 @@ 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. + #[inline] + pub const unsafe fn from_into_iter_unchecked( + into_iter: hash_set::IntoIter, + ) -> IntoIter { + IntoIter::(into_iter, PhantomData) + } + /// Returns the inner [`IntoIter`](hash_set::IntoIter). + #[inline] pub fn into_inner(self) -> hash_set::IntoIter { self.0 } @@ -282,6 +338,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = hash_set::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -290,6 +347,7 @@ impl Deref for IntoIter { impl Iterator for IntoIter { type Item = Entity; + #[inline] fn next(&mut self) -> Option { self.0.next() } @@ -309,8 +367,10 @@ 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()) } } } @@ -325,7 +385,21 @@ 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. + #[inline] + pub const 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 } @@ -334,6 +408,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 } @@ -342,6 +417,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() } @@ -374,7 +450,21 @@ 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. + #[inline] + 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) + } + /// Returns the inner [`ExtractIf`](hash_set::ExtractIf). + #[inline] pub fn into_inner(self) -> hash_set::ExtractIf<'a, Entity, F> { self.0 } @@ -383,6 +473,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 } @@ -391,6 +482,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 9067b12ca28b2..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}; @@ -28,14 +32,15 @@ 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`. /// /// 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)) } @@ -44,12 +49,20 @@ 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)) } + /// Constructs an `EntityIndexMap` from an [`IndexMap`]. + #[inline] + pub const fn from_index_map(set: IndexMap) -> Self { + Self(set) + } + /// Returns the inner [`IndexMap`]. + #[inline] pub fn into_inner(self) -> IndexMap { self.0 } @@ -57,6 +70,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()) } @@ -65,6 +79,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()) } @@ -73,6 +88,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()) } @@ -81,6 +97,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. @@ -90,6 +107,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. @@ -99,6 +117,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) } @@ -106,6 +125,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) } @@ -114,6 +134,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) } @@ -121,6 +142,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) } @@ -128,12 +150,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()) } @@ -142,36 +166,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)) } @@ -179,6 +209,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()) } @@ -186,6 +217,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)) } @@ -194,6 +226,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)) } @@ -202,6 +235,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)) } @@ -210,6 +244,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)) } @@ -218,6 +253,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)) } @@ -226,6 +262,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)) } @@ -234,6 +271,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)) } @@ -242,18 +280,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)) } @@ -261,6 +302,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)) } @@ -268,6 +310,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)) } @@ -275,6 +318,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)) } @@ -282,6 +326,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)) } @@ -289,6 +334,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)) } @@ -296,6 +342,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)) } @@ -303,6 +350,7 @@ impl IndexMut> for EntityIndexMap { } impl IndexMut for EntityIndexMap { + #[inline] fn index_mut(&mut self, key: usize) -> &mut V { self.0.index_mut(key) } @@ -312,6 +360,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) } @@ -321,6 +370,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) } @@ -330,6 +380,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) } @@ -340,6 +391,7 @@ where V1: PartialEq, S2: BuildHasher, { + #[inline] fn eq(&self, other: &IndexMap) -> bool { self.0.eq(other) } @@ -349,6 +401,7 @@ impl PartialEq> for EntityIndexMap where V1: PartialEq, { + #[inline] fn eq(&self, other: &EntityIndexMap) -> bool { self.0.eq(other) } @@ -367,6 +420,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()) } @@ -375,6 +429,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()) } @@ -387,6 +442,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) } @@ -399,12 +455,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 } @@ -416,6 +474,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) } @@ -426,12 +485,14 @@ impl Slice { clippy::borrowed_box, reason = "We wish to access the Box API of the inner type, without consuming it." )] - pub fn as_boxed_inner(self: &Box) -> &Box> { + #[inline] + 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::>>()) } } /// 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) } @@ -440,6 +501,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) } @@ -447,6 +509,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. @@ -456,6 +519,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. @@ -465,6 +529,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() } @@ -472,6 +537,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() } @@ -479,6 +545,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. @@ -493,6 +560,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. @@ -508,6 +576,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)| { ( @@ -522,6 +591,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)| { ( @@ -536,6 +606,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)| { ( @@ -550,6 +621,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)| { ( @@ -563,6 +635,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) } @@ -570,6 +643,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) } @@ -577,6 +651,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) } @@ -584,6 +659,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) } @@ -591,6 +667,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() } @@ -598,6 +675,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() } @@ -606,6 +684,7 @@ impl Slice { impl Deref for Slice { type Target = map::Slice; + #[inline] fn deref(&self) -> &Self::Target { &self.1 } @@ -621,6 +700,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()) } @@ -628,6 +708,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()) } @@ -635,6 +716,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()) } @@ -642,6 +724,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()) } @@ -649,6 +732,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()) } @@ -656,6 +740,7 @@ impl From<&Slice> for Box> { } impl Hash for Slice { + #[inline] fn hash(&self, state: &mut H) { self.1.hash(state); } @@ -665,6 +750,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) } @@ -674,6 +760,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) } @@ -683,24 +770,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 } @@ -710,6 +801,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)) } @@ -718,6 +810,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)) } @@ -726,6 +819,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)) } @@ -734,6 +828,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)) } @@ -742,6 +837,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)) } @@ -750,6 +846,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)) } @@ -758,6 +855,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)) } @@ -766,12 +864,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)) } @@ -779,6 +879,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)) } @@ -786,6 +887,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)) } @@ -793,6 +895,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)) } @@ -800,6 +903,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)) } @@ -807,6 +911,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)) } @@ -814,6 +919,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)) } @@ -821,6 +927,7 @@ impl IndexMut> for Slice { } impl IndexMut for Slice { + #[inline] fn index_mut(&mut self, key: usize) -> &mut V { self.1.index_mut(key) } @@ -833,14 +940,27 @@ 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. + #[inline] + 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). - pub fn into_inner(self) -> map::Iter<'a, Entity, V> { + #[inline] + pub const fn into_inner(self) -> map::Iter<'a, Entity, V> { self.0 } /// 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()) } @@ -850,6 +970,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 } @@ -858,12 +979,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() } @@ -874,8 +997,10 @@ 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) + // SAFETY: We are cloning an already valid `Iter`. + unsafe { Self::from_iter_unchecked(self.0.clone()) } } } @@ -886,8 +1011,10 @@ 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()) } } } @@ -898,14 +1025,29 @@ 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. + #[inline] + pub const 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> { + #[inline] + pub const fn into_inner(self) -> map::IterMut<'a, Entity, V> { self.0 } /// 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()) } @@ -914,6 +1056,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()) } @@ -923,6 +1066,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 } @@ -931,12 +1075,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() } @@ -956,8 +1102,10 @@ 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()) } } } @@ -968,7 +1116,21 @@ 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. + #[inline] + pub const unsafe fn from_into_iter_unchecked( + into_iter: map::IntoIter, + ) -> IntoIter { + IntoIter::<_, S>(into_iter, PhantomData) + } + /// Returns the inner [`IntoIter`](map::IntoIter). + #[inline] pub fn into_inner(self) -> map::IntoIter { self.0 } @@ -976,6 +1138,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()) } @@ -984,6 +1147,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()) } @@ -993,6 +1157,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = map::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1001,12 +1166,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() } @@ -1017,8 +1184,10 @@ impl ExactSizeIterator for IntoIter {} 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()) } } } @@ -1032,8 +1201,10 @@ 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()) } } } @@ -1044,14 +1215,29 @@ impl Default for IntoIter { pub struct Drain<'a, V, S = EntityHash>(map::Drain<'a, Entity, V>, PhantomData); impl<'a, V> Drain<'a, V> { - /// Returns the inner [`Drain`](map::Drain). + /// 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. + #[inline] + pub const unsafe fn from_drain_unchecked( + drain: map::Drain<'a, Entity, V>, + ) -> Drain<'a, V, S> { + Drain::<'_, _, S>(drain, PhantomData) + } + + /// Returns the inner [`Drain`](indexmap::map::Drain). + #[inline] pub fn into_inner(self) -> map::Drain<'a, Entity, V> { self.0 } /// 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. unsafe { Slice::from_slice_unchecked(self.0.as_slice()) } @@ -1061,6 +1247,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 } @@ -1069,12 +1256,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() } @@ -1100,8 +1289,20 @@ 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. + #[inline] + 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). - pub fn into_inner(self) -> map::Keys<'a, Entity, V> { + #[inline] + pub const fn into_inner(self) -> map::Keys<'a, Entity, V> { self.0 } } @@ -1109,6 +1310,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 } @@ -1117,12 +1319,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() } @@ -1135,14 +1339,17 @@ 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) + // SAFETY: We are cloning an already valid `Keys`. + unsafe { Self::from_keys_unchecked(self.0.clone()) } } } @@ -1153,8 +1360,10 @@ 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()) } } } @@ -1168,7 +1377,21 @@ 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. + #[inline] + pub const unsafe fn from_into_keys_unchecked( + into_keys: map::IntoKeys, + ) -> IntoKeys { + IntoKeys::<_, S>(into_keys, PhantomData) + } + /// Returns the inner [`IntoKeys`](map::IntoKeys). + #[inline] pub fn into_inner(self) -> map::IntoKeys { self.0 } @@ -1177,6 +1400,7 @@ impl IntoKeys { impl Deref for IntoKeys { type Target = map::IntoKeys; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -1185,12 +1409,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() } @@ -1210,8 +1436,10 @@ 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 42f420a211a23..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}; @@ -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`. @@ -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,11 +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 const fn from_index_set(set: IndexSet) -> Self { + Self(set) + } + /// Returns the inner [`IndexSet`]. + #[inline] pub fn into_inner(self) -> IndexSet { self.0 } @@ -54,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()) } @@ -63,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) } @@ -70,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. @@ -79,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) } @@ -86,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()) } @@ -95,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 } @@ -111,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) } @@ -121,6 +138,7 @@ impl IntoIterator for EntityIndexSet { type IntoIter = IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { IntoIter(self.0.into_iter(), PhantomData) } @@ -159,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)) } @@ -186,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) } @@ -201,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)) } @@ -209,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)) } @@ -217,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)) } @@ -225,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)) } @@ -233,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)) } @@ -241,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)) } @@ -249,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)) } @@ -257,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) } @@ -273,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()) } @@ -285,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) } @@ -297,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 } @@ -314,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) } @@ -324,12 +361,14 @@ impl Slice { clippy::borrowed_box, reason = "We wish to access the Box API of the inner type, without consuming it." )] - pub fn as_boxed_inner(self: &Box) -> &Box> { + #[inline] + 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::>>()) } } /// 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) } @@ -338,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. @@ -347,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. @@ -362,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)| { ( @@ -376,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)| { ( @@ -389,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) } @@ -397,6 +441,7 @@ impl Slice { impl Deref for Slice { type Target = set::Slice; + #[inline] fn deref(&self) -> &Self::Target { &self.1 } @@ -406,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() } @@ -415,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()) } @@ -428,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()) } @@ -435,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()) } @@ -451,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()) } @@ -458,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 } @@ -485,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)) } @@ -493,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)) } @@ -501,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)) } @@ -509,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)) } @@ -517,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)) } @@ -525,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)) } @@ -533,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)) } @@ -541,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) } @@ -554,14 +617,27 @@ 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. + #[inline] + 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). - pub fn into_inner(self) -> set::Iter<'a, Entity> { + #[inline] + pub const fn into_inner(self) -> set::Iter<'a, Entity> { self.0 } /// 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()) } @@ -571,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 } @@ -579,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() } @@ -595,8 +674,10 @@ impl ExactSizeIterator for Iter<'_> {} 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()) } } } @@ -607,8 +688,10 @@ 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()) } } } @@ -623,7 +706,21 @@ 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. + #[inline] + pub const 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 } @@ -631,6 +728,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()) } @@ -640,6 +738,7 @@ impl IntoIter { impl Deref for IntoIter { type Target = set::IntoIter; + #[inline] fn deref(&self) -> &Self::Target { &self.0 } @@ -648,12 +747,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() } @@ -664,8 +765,10 @@ impl ExactSizeIterator for IntoIter {} 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()) } } } @@ -679,8 +782,10 @@ 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()) } } } @@ -695,7 +800,19 @@ 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. + #[inline] + pub const 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 } @@ -703,6 +820,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()) } @@ -712,6 +830,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 } @@ -720,12 +839,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 71df33ec5f42c..7e578f6d33590 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]); @@ -45,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) } @@ -54,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()) } @@ -64,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()) } @@ -80,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()) } @@ -96,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. - pub fn as_inner(&self) -> &[T; N] { + #[inline] + pub const 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()) } @@ -125,7 +137,8 @@ impl UniqueEntityEquivalentArray { /// Returns a mutable slice containing the entire array. Equivalent to /// `&mut s[..]`. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { + #[inline] + 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()) } } @@ -134,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()) } @@ -142,22 +156,24 @@ 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) } + 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() } } 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()) } } } @@ -168,9 +184,10 @@ 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_iterator_unchecked(self.0.iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } } } @@ -179,15 +196,17 @@ 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_iterator_unchecked(self.0.into_iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.into_iter()) } } } impl AsRef> for UniqueEntityEquivalentArray { + #[inline] fn as_ref(&self) -> &UniqueEntityEquivalentSlice { self } @@ -196,6 +215,7 @@ impl AsRef> impl AsMut> for UniqueEntityEquivalentArray { + #[inline] fn as_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -204,6 +224,7 @@ impl AsMut> impl Borrow> for UniqueEntityEquivalentArray { + #[inline] fn borrow(&self) -> &UniqueEntityEquivalentSlice { self } @@ -212,6 +233,7 @@ impl Borrow> impl BorrowMut> for UniqueEntityEquivalentArray { + #[inline] fn borrow_mut(&mut self) -> &mut UniqueEntityEquivalentSlice { self } @@ -221,6 +243,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)) } @@ -231,6 +254,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)) } @@ -241,6 +265,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)) } @@ -249,6 +274,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)) } @@ -259,6 +285,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)) } @@ -269,6 +296,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)) } @@ -279,6 +307,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)) } @@ -287,6 +316,7 @@ impl Index> impl Index for UniqueEntityEquivalentArray { type Output = T; + #[inline] fn index(&self, key: usize) -> &T { self.0.index(key) } @@ -295,6 +325,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)) } @@ -304,6 +335,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)) } @@ -313,6 +345,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)) } @@ -322,6 +355,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)) } @@ -331,6 +365,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)) } @@ -340,6 +375,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)) } @@ -349,6 +385,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)) } @@ -356,90 +393,111 @@ 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) } } } 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()) } @@ -448,6 +506,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()) } @@ -456,6 +515,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()) } @@ -464,6 +524,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()) } @@ -472,6 +533,7 @@ impl From> impl From> for BTreeSet { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { BTreeSet::from(value.0) } @@ -480,6 +542,7 @@ impl From From> for BinaryHeap { + #[inline] fn from(value: UniqueEntityEquivalentArray) -> Self { BinaryHeap::from(value.0) } @@ -488,18 +551,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) } @@ -508,6 +574,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()) } @@ -516,6 +583,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()) } @@ -524,6 +592,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) } @@ -532,6 +601,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) } @@ -540,6 +610,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) } @@ -548,6 +619,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) } @@ -556,6 +628,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) } @@ -571,6 +644,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()) } @@ -579,6 +653,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 26ebe8674f498..e38440282d54a 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,29 +297,34 @@ 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_iterator_unchecked(self.0.iter()) } + unsafe { UniqueEntityIter::from_iter_unchecked(self.0.iter()) } } /// Returns an iterator over all contiguous windows of length @@ -309,11 +333,10 @@ 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_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 @@ -322,12 +345,11 @@ 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 { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( - self.0.chunks(chunk_size), - ) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.chunks(chunk_size)) } } @@ -337,24 +359,27 @@ 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 { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.chunks_mut(chunk_size), ) } } - /// + /// 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`]. /// /// [`[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 { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked( self.0.chunks_exact(chunk_size), ) } @@ -366,10 +391,11 @@ 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 { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.chunks_exact_mut(chunk_size), ) } @@ -381,12 +407,11 @@ 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 { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( - self.0.rchunks(chunk_size), - ) + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked(self.0.rchunks(chunk_size)) } } @@ -396,10 +421,11 @@ 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 { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rchunks_mut(chunk_size), ) } @@ -411,10 +437,11 @@ 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 { - UniqueEntityEquivalentSliceIter::from_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIter::from_slice_iter_unchecked( self.0.rchunks_exact(chunk_size), ) } @@ -426,10 +453,11 @@ 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 { - UniqueEntityEquivalentSliceIterMut::from_mut_slice_iterator_unchecked( + UniqueEntityEquivalentSliceIterMut::from_mut_slice_iter_unchecked( self.0.rchunks_exact_mut(chunk_size), ) } @@ -441,14 +469,13 @@ 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, { // 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 @@ -457,13 +484,14 @@ 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, { // 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), ) } @@ -472,6 +500,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. @@ -486,6 +515,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. @@ -506,6 +536,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) }; @@ -527,6 +558,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) }; @@ -543,6 +575,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; @@ -560,6 +593,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; @@ -579,14 +613,13 @@ 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, { // 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 @@ -595,13 +628,14 @@ 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, { // 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), ) } @@ -613,15 +647,14 @@ 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, { // 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)) } } @@ -631,13 +664,14 @@ 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, { // 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), ) } @@ -649,14 +683,13 @@ 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, { // 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 @@ -666,13 +699,14 @@ 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, { // 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), ) } @@ -684,13 +718,14 @@ 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, { // 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)) } } @@ -700,13 +735,14 @@ 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, { // 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), ) } @@ -718,13 +754,14 @@ 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, { // 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)) } } @@ -734,13 +771,14 @@ 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, { // 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), ) } @@ -749,6 +787,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, @@ -760,6 +799,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, @@ -771,6 +811,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, @@ -784,6 +825,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); } @@ -793,6 +835,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); } @@ -800,6 +843,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, @@ -810,6 +854,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, @@ -820,6 +865,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, @@ -831,6 +877,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, @@ -840,6 +887,7 @@ impl UniqueEntityEquivalentSlice { } /// Copies self into a new `UniqueEntityEquivalentVec`. + #[inline] pub fn to_vec(&self) -> UniqueEntityEquivalentVec where T: Clone, @@ -851,6 +899,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`. @@ -864,12 +913,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)) } @@ -883,6 +934,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, @@ -899,6 +951,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, @@ -914,6 +967,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] { @@ -926,6 +980,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] { @@ -938,6 +993,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] { @@ -950,6 +1006,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a UniqueEntityEquivalentSlice; + #[inline] fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -960,6 +1017,7 @@ impl<'a, T: EntityEquivalent> IntoIterator for &'a Box; + #[inline] fn into_iter(self) -> Self::IntoIter { self.iter() } @@ -970,6 +1028,7 @@ impl IntoIterator for Box> { type IntoIter = unique_vec::IntoIter; + #[inline] fn into_iter(self) -> Self::IntoIter { self.into_vec().into_iter() } @@ -978,42 +1037,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()) } @@ -1021,6 +1087,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()) } @@ -1028,6 +1095,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()) } @@ -1037,6 +1105,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()) } @@ -1046,6 +1115,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()) } @@ -1055,6 +1125,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()) } @@ -1064,6 +1135,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) } @@ -1072,6 +1144,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 { @@ -1083,6 +1156,7 @@ impl From From>> for Box> { + #[inline] fn from(value: Cow<'a, UniqueEntityEquivalentSlice>) -> Self { match value { Cow::Borrowed(slice) => Box::from(slice), @@ -1094,12 +1168,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::>() @@ -1108,6 +1184,7 @@ impl FromIterator for Box } impl FromEntitySetIterator for Box> { + #[inline] fn from_entity_set_iter>(iter: I) -> Self { iter.into_iter() .collect_set::>() @@ -1118,6 +1195,7 @@ impl FromEntitySetIterator for Box, U: EntityEquivalent> PartialEq> for &UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1126,6 +1204,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent> PartialEq> for &mut UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1134,6 +1213,7 @@ impl, U: EntityEquivalent> impl, U: EntityEquivalent> PartialEq> for UniqueEntityEquivalentSlice { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.0.eq(other.as_vec()) } @@ -1142,6 +1222,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) } @@ -1150,6 +1231,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) } @@ -1158,12 +1240,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) } @@ -1172,6 +1256,7 @@ impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlic impl, U: EntityEquivalent> PartialEq<&UniqueEntityEquivalentSlice> for VecDeque { + #[inline] fn eq(&self, other: &&UniqueEntityEquivalentSlice) -> bool { self.eq(&&other.0) } @@ -1180,6 +1265,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) } @@ -1188,6 +1274,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) } @@ -1196,6 +1283,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) } @@ -1204,6 +1292,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()) } @@ -1212,6 +1301,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) } @@ -1220,6 +1310,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) } @@ -1228,6 +1319,7 @@ impl, U: EntityEquivalent> PartialEq<&mut UniqueEntityEquivalent impl, U: EntityEquivalent> PartialEq> for [T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1236,6 +1328,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) } @@ -1244,6 +1337,7 @@ impl, U: EntityEquivalent, const N: usize> PartialEq, U: EntityEquivalent> PartialEq> for Vec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.eq(&other.0) } @@ -1252,6 +1346,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) } @@ -1260,6 +1355,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) } @@ -1268,6 +1364,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) } @@ -1276,6 +1373,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) } @@ -1284,6 +1382,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) } @@ -1292,12 +1391,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) } @@ -1306,12 +1407,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) } @@ -1320,6 +1423,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()) } @@ -1331,6 +1435,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. @@ -1343,6 +1448,7 @@ impl TryFrom<&UniqueEntityEquivalent { type Error = TryFromSliceError; + #[inline] fn try_from(value: &UniqueEntityEquivalentSlice) -> Result { <&Self>::try_from(value).copied() } @@ -1353,6 +1459,7 @@ impl TryFrom<&mut UniqueEntityEquiva { type Error = TryFromSliceError; + #[inline] fn try_from(value: &mut UniqueEntityEquivalentSlice) -> Result { ::try_from(&*value) } @@ -1360,6 +1467,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)) } @@ -1368,6 +1476,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)) } @@ -1376,6 +1485,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)) } @@ -1384,6 +1494,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)) } @@ -1392,6 +1503,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)) } @@ -1400,6 +1512,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)) } @@ -1408,6 +1521,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)) } @@ -1417,6 +1531,7 @@ impl Index> for UniqueEntityEquival impl Index for UniqueEntityEquivalentSlice { type Output = T; + #[inline] fn index(&self, index: usize) -> &T { &self.0[index] } @@ -1425,6 +1540,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)) } @@ -1432,6 +1548,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)) } @@ -1439,6 +1556,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)) } @@ -1446,6 +1564,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)) } @@ -1453,6 +1572,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)) } @@ -1460,6 +1580,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)) } @@ -1467,6 +1588,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)) } @@ -1485,6 +1607,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()) } @@ -1498,6 +1621,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 { @@ -1508,6 +1632,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()) } @@ -1516,13 +1641,14 @@ 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, T: EntityEquivalent + 'a, I: Iterator, > { - pub(crate) iter: I, + iter: I, } impl<'a, T: EntityEquivalent + 'a, I: Iterator> @@ -1533,17 +1659,20 @@ 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 { + #[inline] + pub const 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`. - pub fn as_inner(&self) -> &I { + #[inline] + pub const fn as_inner(&self) -> &I { &self.iter } @@ -1553,7 +1682,8 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + #[inline] + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } @@ -1563,12 +1693,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() } @@ -1582,6 +1714,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. @@ -1597,6 +1730,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()) } @@ -1626,6 +1760,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()) } @@ -1650,6 +1785,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()) } @@ -1699,13 +1835,14 @@ 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, T: EntityEquivalent + 'a, I: Iterator, > { - pub(crate) iter: I, + iter: I, } impl<'a, T: EntityEquivalent + 'a, I: Iterator> @@ -1716,17 +1853,20 @@ 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 { + #[inline] + pub const 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`. - pub fn as_inner(&self) -> &I { + #[inline] + pub const fn as_inner(&self) -> &I { &self.iter } @@ -1736,7 +1876,8 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator> /// /// `self` must always contain an iterator that yields unique elements, /// even while this reference is live. - pub unsafe fn as_mut_inner(&mut self) -> &mut I { + #[inline] + pub const unsafe fn as_mut_inner(&mut self) -> &mut I { &mut self.iter } } @@ -1746,12 +1887,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() } @@ -1765,6 +1908,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. @@ -1780,6 +1924,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()) } @@ -1790,6 +1935,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()) } @@ -1817,6 +1963,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()) } @@ -1844,6 +1991,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 30f9984e70be7..24fc32f4e2dec 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); @@ -49,15 +51,19 @@ impl UniqueEntityEquivalentVec { /// Constructs a new, empty `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. /// /// 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. @@ -68,9 +74,11 @@ 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) }) + // 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. @@ -78,17 +86,42 @@ impl UniqueEntityEquivalentVec { /// # Safety /// /// `vec` must contain only unique elements. - pub unsafe fn from_vec_unchecked(vec: Vec) -> Self { + #[inline] + pub const unsafe fn from_vec_unchecked(vec: Vec) -> Self { Self(vec) } + /// Constructs a `UniqueEntityEquivalentVec` from a [`&Vec`](Vec) unsafely. + /// + /// # Safety + /// + /// `vec` must contain only unique elements. + #[inline] + 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() } + } + + /// Constructs a `UniqueEntityEquivalentVec` from a [`&mut Vec`](Vec) unsafely. + /// + /// # Safety + /// + /// `vec` must contain only unique elements. + #[inline] + 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() } + } + /// Returns the inner [`Vec`]. + #[inline] pub fn into_inner(self) -> Vec { self.0 } /// Returns a reference to the inner [`Vec`]. - pub fn as_vec(&self) -> &Vec { + #[inline] + pub const fn as_vec(&self) -> &Vec { &self.0 } @@ -98,7 +131,8 @@ impl UniqueEntityEquivalentVec { /// /// The elements of this `Vec` must always remain unique, even while /// this mutable reference is live. - pub unsafe fn as_mut_vec(&mut self) -> &mut Vec { + #[inline] + pub const unsafe fn as_mut_vec(&mut self) -> &mut Vec { &mut self.0 } @@ -106,7 +140,8 @@ impl UniqueEntityEquivalentVec { /// reallocating. /// /// Equivalent to [`Vec::capacity`]. - pub fn capacity(&self) -> usize { + #[inline] + pub const fn capacity(&self) -> usize { self.0.capacity() } @@ -114,6 +149,7 @@ impl UniqueEntityEquivalentVec { /// in the given `Vec`. /// /// Equivalent to [`Vec::reserve`]. + #[inline] pub fn reserve(&mut self, additional: usize) { self.0.reserve(additional); } @@ -122,6 +158,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); } @@ -130,6 +167,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) } @@ -138,6 +176,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) } @@ -145,6 +184,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(); } @@ -152,11 +192,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 { @@ -165,19 +207,24 @@ impl UniqueEntityEquivalentVec { } /// Extracts a slice containing the entire vector. - pub fn as_slice(&self) -> &UniqueEntityEquivalentSlice { - self + #[inline] + 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. - pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice { - self + #[inline] + 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 /// the rest. /// /// Equivalent to [`Vec::truncate`]. + #[inline] pub fn truncate(&mut self, len: usize) { self.0.truncate(len); } @@ -186,14 +233,16 @@ impl UniqueEntityEquivalentVec { /// valid for zero sized reads if the vector didn't allocate. /// /// Equivalent to [`Vec::as_ptr`]. - pub fn as_ptr(&self) -> *const T { + #[inline] + 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 /// raw pointer valid for zero sized reads if the vector didn't allocate. /// /// Equivalent to [`Vec::as_mut_ptr`]. - pub fn as_mut_ptr(&mut self) -> *mut T { + #[inline] + pub const fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr() } @@ -205,6 +254,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) }; @@ -213,6 +263,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) } @@ -225,6 +276,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); } @@ -233,6 +285,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) } @@ -240,6 +293,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, @@ -254,6 +308,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, @@ -269,6 +324,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, @@ -285,6 +341,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, @@ -299,6 +356,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); } @@ -310,6 +368,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); } @@ -318,6 +377,7 @@ impl UniqueEntityEquivalentVec { /// is empty. /// /// Equivalent to [`Vec::pop`]. + #[inline] pub fn pop(&mut self) -> Option { self.0.pop() } @@ -326,17 +386,19 @@ impl UniqueEntityEquivalentVec { /// removed elements as an iterator. /// /// Equivalent to [`Vec::drain`]. + #[inline] pub fn drain(&mut self, range: R) -> Drain<'_, T> where 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. /// /// Equivalent to [`Vec::clear`]. + #[inline] pub fn clear(&mut self) { self.0.clear(); } @@ -345,22 +407,26 @@ impl UniqueEntityEquivalentVec { /// as its 'length'. /// /// Equivalent to [`Vec::len`]. - pub fn len(&self) -> usize { + #[inline] + pub const fn len(&self) -> usize { self.0.len() } /// Returns `true` if the vector contains no elements. /// /// Equivalent to [`Vec::is_empty`]. - pub fn is_empty(&self) -> bool { + #[inline] + pub const fn is_empty(&self) -> bool { self.0.is_empty() } /// 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)) + // 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`. @@ -370,6 +436,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, @@ -378,6 +445,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()) } @@ -387,6 +455,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() } @@ -400,6 +469,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, @@ -410,29 +480,31 @@ 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)) } } } 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()) } } } 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) } + 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() } } @@ -444,9 +516,10 @@ where type IntoIter = unique_slice::Iter<'a, T>; + #[inline] 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()) } } } @@ -455,61 +528,71 @@ impl IntoIterator for UniqueEntityEquivalentVec { type IntoIter = IntoIter; + #[inline] 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()) } } } 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 } @@ -518,18 +601,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) } @@ -538,12 +624,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) } @@ -552,6 +640,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) } @@ -560,6 +649,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) } @@ -568,6 +658,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()) } @@ -576,6 +667,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) } @@ -584,12 +676,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) } @@ -598,6 +692,7 @@ impl, U> PartialEq<[U]> for UniqueEntityEquiv impl, U: EntityEquivalent> PartialEq> for UniqueEntityEquivalentVec { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentSlice) -> bool { self.0.eq(&**other) } @@ -606,6 +701,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) } @@ -614,24 +710,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) } @@ -640,6 +740,7 @@ impl, U: EntityEquivalent> PartialEq, U: EntityEquivalent> PartialEq> for [T] { + #[inline] fn eq(&self, other: &UniqueEntityEquivalentVec) -> bool { self.eq(&other.0) } @@ -648,12 +749,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) } @@ -662,6 +765,7 @@ impl, U: EntityEquivalent> PartialEq From<&UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &UniqueEntityEquivalentSlice) -> Self { value.to_vec() } @@ -670,6 +774,7 @@ impl From<&UniqueEntityEquivalentSlice> impl From<&mut UniqueEntityEquivalentSlice> for UniqueEntityEquivalentVec { + #[inline] fn from(value: &mut UniqueEntityEquivalentSlice) -> Self { value.to_vec() } @@ -678,6 +783,7 @@ impl From<&mut UniqueEntityEquivalentSlice> impl From>> for UniqueEntityEquivalentVec { + #[inline] fn from(value: Box>) -> Self { value.into_vec() } @@ -688,78 +794,99 @@ 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)) + // 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)) } } } impl From<&UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[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())) } } } impl From<&mut UniqueEntityEquivalentArray> for UniqueEntityEquivalentVec { + #[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())) } } } impl From> for UniqueEntityEquivalentVec { + #[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())) } } } 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) } @@ -768,12 +895,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) } @@ -782,6 +911,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)) } @@ -789,18 +919,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) } @@ -809,6 +942,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)) } @@ -816,6 +950,7 @@ impl From> } impl From> for VecDeque { + #[inline] fn from(value: UniqueEntityEquivalentVec) -> Self { VecDeque::from(value.0) } @@ -824,6 +959,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) } @@ -834,6 +970,7 @@ impl TryFrom> { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { Box::try_from(value.0) .map(|v| @@ -846,6 +983,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) } @@ -856,6 +994,7 @@ impl TryFrom> { type Error = UniqueEntityEquivalentVec; + #[inline] fn try_from(value: UniqueEntityEquivalentVec) -> Result { <[T; N] as TryFrom>>::try_from(value.0) .map(|v| @@ -866,8 +1005,10 @@ 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::>()) } } } @@ -875,6 +1016,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(); @@ -891,6 +1033,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)) } @@ -901,6 +1044,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. @@ -928,6 +1072,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. @@ -953,6 +1098,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)) } @@ -961,6 +1107,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)) } @@ -969,6 +1116,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)) } @@ -977,6 +1125,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)) } @@ -985,6 +1134,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)) } @@ -993,6 +1143,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)) } @@ -1001,6 +1152,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)) } @@ -1009,12 +1161,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)) } @@ -1022,6 +1176,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)) } @@ -1029,6 +1184,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)) } @@ -1036,6 +1192,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)) } @@ -1043,6 +1200,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)) } @@ -1050,6 +1208,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)) } @@ -1057,6 +1216,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)) } @@ -1073,6 +1233,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()) } @@ -1081,6 +1242,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 { @@ -1101,6 +1263,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()) } 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/relationship/relationship_source_collection.rs b/crates/bevy_ecs/src/relationship/relationship_source_collection.rs index 5664fef34068c..514a1f3f49f89 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<'_> { @@ -264,15 +262,15 @@ impl RelationshipSourceCollection for EntityHashSet { } fn len(&self) -> usize { - self.len() + self.deref().len() } 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) { 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()) } /// } /// } /// 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)) } 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;