Path: blob/main/crates/bevy_ecs/src/entity/index_map.rs
6849 views
//! Contains the [`EntityIndexMap`] type, an [`IndexMap`] pre-configured to use [`EntityHash`] hashing.1//!2//! This module is a lightweight wrapper around `indexmap`'s [`IndexMap`] that is more performant for [`Entity`] keys.34use core::{5cmp::Ordering,6fmt::{self, Debug, Formatter},7hash::{BuildHasher, Hash, Hasher},8iter::FusedIterator,9marker::PhantomData,10ops::{11Bound, Deref, DerefMut, Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull,12RangeInclusive, RangeTo, RangeToInclusive,13},14ptr,15};1617#[cfg(feature = "bevy_reflect")]18use bevy_reflect::Reflect;19use indexmap::map::{self, IndexMap, IntoValues, ValuesMut};2021use super::{Entity, EntityEquivalent, EntityHash, EntitySetIterator};2223use bevy_platform::prelude::Box;2425/// A [`IndexMap`] pre-configured to use [`EntityHash`] hashing.26#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]27#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]28#[derive(Debug, Clone)]29pub struct EntityIndexMap<V>(pub(crate) IndexMap<Entity, V, EntityHash>);3031impl<V> EntityIndexMap<V> {32/// Creates an empty `EntityIndexMap`.33///34/// Equivalent to [`IndexMap::with_hasher(EntityHash)`].35///36/// [`IndexMap::with_hasher(EntityHash)`]: IndexMap::with_hasher37pub const fn new() -> Self {38Self(IndexMap::with_hasher(EntityHash))39}4041/// Creates an empty `EntityIndexMap` with the specified capacity.42///43/// Equivalent to [`IndexMap::with_capacity_and_hasher(n, EntityHash)`].44///45/// [`IndexMap:with_capacity_and_hasher(n, EntityHash)`]: IndexMap::with_capacity_and_hasher46pub fn with_capacity(n: usize) -> Self {47Self(IndexMap::with_capacity_and_hasher(n, EntityHash))48}4950/// Returns the inner [`IndexMap`].51pub fn into_inner(self) -> IndexMap<Entity, V, EntityHash> {52self.053}5455/// Returns a slice of all the key-value pairs in the map.56///57/// Equivalent to [`IndexMap::as_slice`].58pub fn as_slice(&self) -> &Slice<V> {59// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.60unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }61}6263/// Returns a mutable slice of all the key-value pairs in the map.64///65/// Equivalent to [`IndexMap::as_mut_slice`].66pub fn as_mut_slice(&mut self) -> &mut Slice<V> {67// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.68unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) }69}7071/// Converts into a boxed slice of all the key-value pairs in the map.72///73/// Equivalent to [`IndexMap::into_boxed_slice`].74pub fn into_boxed_slice(self) -> Box<Slice<V>> {75// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.76unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) }77}7879/// Returns a slice of key-value pairs in the given range of indices.80///81/// Equivalent to [`IndexMap::get_range`].82pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<V>> {83self.0.get_range(range).map(|slice|84// SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice.85unsafe { Slice::from_slice_unchecked(slice) })86}8788/// Returns a mutable slice of key-value pairs in the given range of indices.89///90/// Equivalent to [`IndexMap::get_range_mut`].91pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<V>> {92self.0.get_range_mut(range).map(|slice|93// SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice.94unsafe { Slice::from_slice_unchecked_mut(slice) })95}9697/// Return an iterator over the key-value pairs of the map, in their order.98///99/// Equivalent to [`IndexMap::iter`].100pub fn iter(&self) -> Iter<'_, V> {101Iter(self.0.iter(), PhantomData)102}103104/// Return a mutable iterator over the key-value pairs of the map, in their order.105///106/// Equivalent to [`IndexMap::iter_mut`].107pub fn iter_mut(&mut self) -> IterMut<'_, V> {108IterMut(self.0.iter_mut(), PhantomData)109}110111/// Clears the `IndexMap` in the given index range, returning those112/// key-value pairs as a drain iterator.113///114/// Equivalent to [`IndexMap::drain`].115pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_, V> {116Drain(self.0.drain(range), PhantomData)117}118119/// Return an iterator over the keys of the map, in their order.120///121/// Equivalent to [`IndexMap::keys`].122pub fn keys(&self) -> Keys<'_, V> {123Keys(self.0.keys(), PhantomData)124}125126/// Return an owning iterator over the keys of the map, in their order.127///128/// Equivalent to [`IndexMap::into_keys`].129pub fn into_keys(self) -> IntoKeys<V> {130IntoKeys(self.0.into_keys(), PhantomData)131}132}133134impl<V> Default for EntityIndexMap<V> {135fn default() -> Self {136Self(Default::default())137}138}139140impl<V> Deref for EntityIndexMap<V> {141type Target = IndexMap<Entity, V, EntityHash>;142143fn deref(&self) -> &Self::Target {144&self.0145}146}147148impl<V> DerefMut for EntityIndexMap<V> {149fn deref_mut(&mut self) -> &mut Self::Target {150&mut self.0151}152}153154impl<'a, V: Copy> Extend<(&'a Entity, &'a V)> for EntityIndexMap<V> {155fn extend<T: IntoIterator<Item = (&'a Entity, &'a V)>>(&mut self, iter: T) {156self.0.extend(iter);157}158}159160impl<V> Extend<(Entity, V)> for EntityIndexMap<V> {161fn extend<T: IntoIterator<Item = (Entity, V)>>(&mut self, iter: T) {162self.0.extend(iter);163}164}165166impl<V, const N: usize> From<[(Entity, V); N]> for EntityIndexMap<V> {167fn from(value: [(Entity, V); N]) -> Self {168Self(IndexMap::from_iter(value))169}170}171172impl<V> FromIterator<(Entity, V)> for EntityIndexMap<V> {173fn from_iter<I: IntoIterator<Item = (Entity, V)>>(iterable: I) -> Self {174Self(IndexMap::from_iter(iterable))175}176}177178impl<V, Q: EntityEquivalent + ?Sized> Index<&Q> for EntityIndexMap<V> {179type Output = V;180fn index(&self, key: &Q) -> &V {181self.0.index(&key.entity())182}183}184185impl<V> Index<(Bound<usize>, Bound<usize>)> for EntityIndexMap<V> {186type Output = Slice<V>;187fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {188// SAFETY: The source IndexMap uses EntityHash.189unsafe { Slice::from_slice_unchecked(self.0.index(key)) }190}191}192193impl<V> Index<Range<usize>> for EntityIndexMap<V> {194type Output = Slice<V>;195fn index(&self, key: Range<usize>) -> &Self::Output {196// SAFETY: The source IndexMap uses EntityHash.197unsafe { Slice::from_slice_unchecked(self.0.index(key)) }198}199}200201impl<V> Index<RangeFrom<usize>> for EntityIndexMap<V> {202type Output = Slice<V>;203fn index(&self, key: RangeFrom<usize>) -> &Self::Output {204// SAFETY: The source IndexMap uses EntityHash.205unsafe { Slice::from_slice_unchecked(self.0.index(key)) }206}207}208209impl<V> Index<RangeFull> for EntityIndexMap<V> {210type Output = Slice<V>;211fn index(&self, key: RangeFull) -> &Self::Output {212// SAFETY: The source IndexMap uses EntityHash.213unsafe { Slice::from_slice_unchecked(self.0.index(key)) }214}215}216217impl<V> Index<RangeInclusive<usize>> for EntityIndexMap<V> {218type Output = Slice<V>;219fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {220// SAFETY: The source IndexMap uses EntityHash.221unsafe { Slice::from_slice_unchecked(self.0.index(key)) }222}223}224225impl<V> Index<RangeTo<usize>> for EntityIndexMap<V> {226type Output = Slice<V>;227fn index(&self, key: RangeTo<usize>) -> &Self::Output {228// SAFETY: The source IndexMap uses EntityHash.229unsafe { Slice::from_slice_unchecked(self.0.index(key)) }230}231}232233impl<V> Index<RangeToInclusive<usize>> for EntityIndexMap<V> {234type Output = Slice<V>;235fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {236// SAFETY: The source IndexMap uses EntityHash.237unsafe { Slice::from_slice_unchecked(self.0.index(key)) }238}239}240241impl<V> Index<usize> for EntityIndexMap<V> {242type Output = V;243fn index(&self, key: usize) -> &V {244self.0.index(key)245}246}247248impl<V, Q: EntityEquivalent + ?Sized> IndexMut<&Q> for EntityIndexMap<V> {249fn index_mut(&mut self, key: &Q) -> &mut V {250self.0.index_mut(&key.entity())251}252}253254impl<V> IndexMut<(Bound<usize>, Bound<usize>)> for EntityIndexMap<V> {255fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {256// SAFETY: The source IndexMap uses EntityHash.257unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }258}259}260261impl<V> IndexMut<Range<usize>> for EntityIndexMap<V> {262fn index_mut(&mut self, key: Range<usize>) -> &mut Self::Output {263// SAFETY: The source IndexMap uses EntityHash.264unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }265}266}267268impl<V> IndexMut<RangeFrom<usize>> for EntityIndexMap<V> {269fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self::Output {270// SAFETY: The source IndexMap uses EntityHash.271unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }272}273}274275impl<V> IndexMut<RangeFull> for EntityIndexMap<V> {276fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output {277// SAFETY: The source IndexMap uses EntityHash.278unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }279}280}281282impl<V> IndexMut<RangeInclusive<usize>> for EntityIndexMap<V> {283fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self::Output {284// SAFETY: The source IndexMap uses EntityHash.285unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }286}287}288289impl<V> IndexMut<RangeTo<usize>> for EntityIndexMap<V> {290fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self::Output {291// SAFETY: The source IndexMap uses EntityHash.292unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }293}294}295296impl<V> IndexMut<RangeToInclusive<usize>> for EntityIndexMap<V> {297fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self::Output {298// SAFETY: The source IndexMap uses EntityHash.299unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }300}301}302303impl<V> IndexMut<usize> for EntityIndexMap<V> {304fn index_mut(&mut self, key: usize) -> &mut V {305self.0.index_mut(key)306}307}308309impl<'a, V> IntoIterator for &'a EntityIndexMap<V> {310type Item = (&'a Entity, &'a V);311type IntoIter = Iter<'a, V>;312313fn into_iter(self) -> Self::IntoIter {314Iter(self.0.iter(), PhantomData)315}316}317318impl<'a, V> IntoIterator for &'a mut EntityIndexMap<V> {319type Item = (&'a Entity, &'a mut V);320type IntoIter = IterMut<'a, V>;321322fn into_iter(self) -> Self::IntoIter {323IterMut(self.0.iter_mut(), PhantomData)324}325}326327impl<V> IntoIterator for EntityIndexMap<V> {328type Item = (Entity, V);329type IntoIter = IntoIter<V>;330331fn into_iter(self) -> Self::IntoIter {332IntoIter(self.0.into_iter(), PhantomData)333}334}335336impl<V1, V2, S2> PartialEq<IndexMap<Entity, V2, S2>> for EntityIndexMap<V1>337where338V1: PartialEq<V2>,339S2: BuildHasher,340{341fn eq(&self, other: &IndexMap<Entity, V2, S2>) -> bool {342self.0.eq(other)343}344}345346impl<V1, V2> PartialEq<EntityIndexMap<V2>> for EntityIndexMap<V1>347where348V1: PartialEq<V2>,349{350fn eq(&self, other: &EntityIndexMap<V2>) -> bool {351self.0.eq(other)352}353}354355impl<V: Eq> Eq for EntityIndexMap<V> {}356357/// A dynamically-sized slice of key-value pairs in an [`EntityIndexMap`].358///359/// Equivalent to an [`indexmap::map::Slice<V>`] whose source [`IndexMap`]360/// uses [`EntityHash`].361#[repr(transparent)]362pub struct Slice<V, S = EntityHash>(PhantomData<S>, map::Slice<Entity, V>);363364impl<V> Slice<V> {365/// Returns an empty slice.366///367/// Equivalent to [`map::Slice::new`].368pub const fn new<'a>() -> &'a Self {369// SAFETY: The source slice is empty.370unsafe { Self::from_slice_unchecked(map::Slice::new()) }371}372373/// Returns an empty mutable slice.374///375/// Equivalent to [`map::Slice::new_mut`].376pub fn new_mut<'a>() -> &'a mut Self {377// SAFETY: The source slice is empty.378unsafe { Self::from_slice_unchecked_mut(map::Slice::new_mut()) }379}380381/// Constructs a [`entity::index_map::Slice`] from a [`indexmap::map::Slice`] unsafely.382///383/// # Safety384///385/// `slice` must stem from an [`IndexMap`] using [`EntityHash`].386///387/// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`388pub const unsafe fn from_slice_unchecked(slice: &map::Slice<Entity, V>) -> &Self {389// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.390unsafe { &*(ptr::from_ref(slice) as *const Self) }391}392393/// Constructs a [`entity::index_map::Slice`] from a [`indexmap::map::Slice`] unsafely.394///395/// # Safety396///397/// `slice` must stem from an [`IndexMap`] using [`EntityHash`].398///399/// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`400pub const unsafe fn from_slice_unchecked_mut(slice: &mut map::Slice<Entity, V>) -> &mut Self {401// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.402unsafe { &mut *(ptr::from_mut(slice) as *mut Self) }403}404405/// Casts `self` to the inner slice.406pub const fn as_inner(&self) -> &map::Slice<Entity, V> {407&self.1408}409410/// Constructs a boxed [`entity::index_map::Slice`] from a boxed [`indexmap::map::Slice`] unsafely.411///412/// # Safety413///414/// `slice` must stem from an [`IndexMap`] using [`EntityHash`].415///416/// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`417pub unsafe fn from_boxed_slice_unchecked(slice: Box<map::Slice<Entity, V>>) -> Box<Self> {418// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.419unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) }420}421422/// Casts a reference to `self` to the inner slice.423#[expect(424clippy::borrowed_box,425reason = "We wish to access the Box API of the inner type, without consuming it."426)]427pub fn as_boxed_inner(self: &Box<Self>) -> &Box<map::Slice<Entity, V>> {428// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.429unsafe { &*(ptr::from_ref(self).cast::<Box<map::Slice<Entity, V>>>()) }430}431432/// Casts `self` to the inner slice.433pub fn into_boxed_inner(self: Box<Self>) -> Box<map::Slice<Entity, V>> {434// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.435unsafe { Box::from_raw(Box::into_raw(self) as *mut map::Slice<Entity, V>) }436}437438/// Get a key-value pair by index, with mutable access to the value.439///440/// Equivalent to [`map::Slice::get_index_mut`].441pub fn get_index_mut(&mut self, index: usize) -> Option<(&Entity, &mut V)> {442self.1.get_index_mut(index)443}444445/// Returns a slice of key-value pairs in the given range of indices.446///447/// Equivalent to [`map::Slice::get_range`].448pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {449self.1.get_range(range).map(|slice|450// SAFETY: This a subslice of a valid slice.451unsafe { Self::from_slice_unchecked(slice) })452}453454/// Returns a mutable slice of key-value pairs in the given range of indices.455///456/// Equivalent to [`map::Slice::get_range_mut`].457pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Self> {458self.1.get_range_mut(range).map(|slice|459// SAFETY: This a subslice of a valid slice.460unsafe { Self::from_slice_unchecked_mut(slice) })461}462463/// Get the first key-value pair, with mutable access to the value.464///465/// Equivalent to [`map::Slice::first_mut`].466pub fn first_mut(&mut self) -> Option<(&Entity, &mut V)> {467self.1.first_mut()468}469470/// Get the last key-value pair, with mutable access to the value.471///472/// Equivalent to [`map::Slice::last_mut`].473pub fn last_mut(&mut self) -> Option<(&Entity, &mut V)> {474self.1.last_mut()475}476477/// Divides one slice into two at an index.478///479/// Equivalent to [`map::Slice::split_at`].480pub fn split_at(&self, index: usize) -> (&Self, &Self) {481let (slice_1, slice_2) = self.1.split_at(index);482// SAFETY: These are subslices of a valid slice.483unsafe {484(485Self::from_slice_unchecked(slice_1),486Self::from_slice_unchecked(slice_2),487)488}489}490491/// Divides one mutable slice into two at an index.492///493/// Equivalent to [`map::Slice::split_at_mut`].494pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {495let (slice_1, slice_2) = self.1.split_at_mut(index);496// SAFETY: These are subslices of a valid slice.497unsafe {498(499Self::from_slice_unchecked_mut(slice_1),500Self::from_slice_unchecked_mut(slice_2),501)502}503}504505/// Returns the first key-value pair and the rest of the slice,506/// or `None` if it is empty.507///508/// Equivalent to [`map::Slice::split_first`].509pub fn split_first(&self) -> Option<((&Entity, &V), &Self)> {510self.1.split_first().map(|(first, rest)| {511(512first,513// SAFETY: This a subslice of a valid slice.514unsafe { Self::from_slice_unchecked(rest) },515)516})517}518519/// Returns the first key-value pair and the rest of the slice,520/// with mutable access to the value, or `None` if it is empty.521///522/// Equivalent to [`map::Slice::split_first_mut`].523pub fn split_first_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> {524self.1.split_first_mut().map(|(first, rest)| {525(526first,527// SAFETY: This a subslice of a valid slice.528unsafe { Self::from_slice_unchecked_mut(rest) },529)530})531}532533/// Returns the last key-value pair and the rest of the slice,534/// or `None` if it is empty.535///536/// Equivalent to [`map::Slice::split_last`].537pub fn split_last(&self) -> Option<((&Entity, &V), &Self)> {538self.1.split_last().map(|(last, rest)| {539(540last,541// SAFETY: This a subslice of a valid slice.542unsafe { Self::from_slice_unchecked(rest) },543)544})545}546547/// Returns the last key-value pair and the rest of the slice,548/// with mutable access to the value, or `None` if it is empty.549///550/// Equivalent to [`map::Slice::split_last_mut`].551pub fn split_last_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> {552self.1.split_last_mut().map(|(last, rest)| {553(554last,555// SAFETY: This a subslice of a valid slice.556unsafe { Self::from_slice_unchecked_mut(rest) },557)558})559}560561/// Return an iterator over the key-value pairs of the map slice.562///563/// Equivalent to [`map::Slice::iter`].564pub fn iter(&self) -> Iter<'_, V> {565Iter(self.1.iter(), PhantomData)566}567568/// Return an iterator over the key-value pairs of the map slice.569///570/// Equivalent to [`map::Slice::iter_mut`].571pub fn iter_mut(&mut self) -> IterMut<'_, V> {572IterMut(self.1.iter_mut(), PhantomData)573}574575/// Return an iterator over the keys of the map slice.576///577/// Equivalent to [`map::Slice::keys`].578pub fn keys(&self) -> Keys<'_, V> {579Keys(self.1.keys(), PhantomData)580}581582/// Return an owning iterator over the keys of the map slice.583///584/// Equivalent to [`map::Slice::into_keys`].585pub fn into_keys(self: Box<Self>) -> IntoKeys<V> {586IntoKeys(self.into_boxed_inner().into_keys(), PhantomData)587}588589/// Return an iterator over mutable references to the the values of the map slice.590///591/// Equivalent to [`map::Slice::values_mut`].592pub fn values_mut(&mut self) -> ValuesMut<'_, Entity, V> {593self.1.values_mut()594}595596/// Return an owning iterator over the values of the map slice.597///598/// Equivalent to [`map::Slice::into_values`].599pub fn into_values(self: Box<Self>) -> IntoValues<Entity, V> {600self.into_boxed_inner().into_values()601}602}603604impl<V> Deref for Slice<V> {605type Target = map::Slice<Entity, V>;606607fn deref(&self) -> &Self::Target {608&self.1609}610}611612impl<V: Debug> Debug for Slice<V> {613fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {614f.debug_tuple("Slice")615.field(&self.0)616.field(&&self.1)617.finish()618}619}620621impl<V: Clone> Clone for Box<Slice<V>> {622fn clone(&self) -> Self {623// SAFETY: This a clone of a valid slice.624unsafe { Slice::from_boxed_slice_unchecked(self.as_boxed_inner().clone()) }625}626}627628impl<V> Default for &Slice<V> {629fn default() -> Self {630// SAFETY: The source slice is empty.631unsafe { Slice::from_slice_unchecked(<&map::Slice<Entity, V>>::default()) }632}633}634635impl<V> Default for &mut Slice<V> {636fn default() -> Self {637// SAFETY: The source slice is empty.638unsafe { Slice::from_slice_unchecked_mut(<&mut map::Slice<Entity, V>>::default()) }639}640}641642impl<V> Default for Box<Slice<V>> {643fn default() -> Self {644// SAFETY: The source slice is empty.645unsafe { Slice::from_boxed_slice_unchecked(<Box<map::Slice<Entity, V>>>::default()) }646}647}648649impl<V: Copy> From<&Slice<V>> for Box<Slice<V>> {650fn from(value: &Slice<V>) -> Self {651// SAFETY: This slice is a copy of a valid slice.652unsafe { Slice::from_boxed_slice_unchecked(value.1.into()) }653}654}655656impl<V: Hash> Hash for Slice<V> {657fn hash<H: Hasher>(&self, state: &mut H) {658self.1.hash(state);659}660}661662impl<'a, V> IntoIterator for &'a Slice<V> {663type Item = (&'a Entity, &'a V);664type IntoIter = Iter<'a, V>;665666fn into_iter(self) -> Self::IntoIter {667Iter(self.1.iter(), PhantomData)668}669}670671impl<'a, V> IntoIterator for &'a mut Slice<V> {672type Item = (&'a Entity, &'a mut V);673type IntoIter = IterMut<'a, V>;674675fn into_iter(self) -> Self::IntoIter {676IterMut(self.1.iter_mut(), PhantomData)677}678}679680impl<V> IntoIterator for Box<Slice<V>> {681type Item = (Entity, V);682type IntoIter = IntoIter<V>;683684fn into_iter(self) -> Self::IntoIter {685IntoIter(self.into_boxed_inner().into_iter(), PhantomData)686}687}688689impl<V: PartialOrd> PartialOrd for Slice<V> {690fn partial_cmp(&self, other: &Self) -> Option<Ordering> {691self.1.partial_cmp(&other.1)692}693}694695impl<V: Ord> Ord for Slice<V> {696fn cmp(&self, other: &Self) -> Ordering {697self.1.cmp(other)698}699}700701impl<V: PartialEq> PartialEq for Slice<V> {702fn eq(&self, other: &Self) -> bool {703self.1 == other.1704}705}706707impl<V: Eq> Eq for Slice<V> {}708709impl<V> Index<(Bound<usize>, Bound<usize>)> for Slice<V> {710type Output = Self;711fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self {712// SAFETY: This a subslice of a valid slice.713unsafe { Self::from_slice_unchecked(self.1.index(key)) }714}715}716717impl<V> Index<Range<usize>> for Slice<V> {718type Output = Self;719fn index(&self, key: Range<usize>) -> &Self {720// SAFETY: This a subslice of a valid slice.721unsafe { Self::from_slice_unchecked(self.1.index(key)) }722}723}724725impl<V> Index<RangeFrom<usize>> for Slice<V> {726type Output = Self;727fn index(&self, key: RangeFrom<usize>) -> &Self {728// SAFETY: This a subslice of a valid slice.729unsafe { Self::from_slice_unchecked(self.1.index(key)) }730}731}732733impl<V> Index<RangeFull> for Slice<V> {734type Output = Self;735fn index(&self, key: RangeFull) -> &Self {736// SAFETY: This a subslice of a valid slice.737unsafe { Self::from_slice_unchecked(self.1.index(key)) }738}739}740741impl<V> Index<RangeInclusive<usize>> for Slice<V> {742type Output = Self;743fn index(&self, key: RangeInclusive<usize>) -> &Self {744// SAFETY: This a subslice of a valid slice.745unsafe { Self::from_slice_unchecked(self.1.index(key)) }746}747}748749impl<V> Index<RangeTo<usize>> for Slice<V> {750type Output = Self;751fn index(&self, key: RangeTo<usize>) -> &Self {752// SAFETY: This a subslice of a valid slice.753unsafe { Self::from_slice_unchecked(self.1.index(key)) }754}755}756757impl<V> Index<RangeToInclusive<usize>> for Slice<V> {758type Output = Self;759fn index(&self, key: RangeToInclusive<usize>) -> &Self {760// SAFETY: This a subslice of a valid slice.761unsafe { Self::from_slice_unchecked(self.1.index(key)) }762}763}764765impl<V> Index<usize> for Slice<V> {766type Output = V;767fn index(&self, key: usize) -> &V {768self.1.index(key)769}770}771772impl<V> IndexMut<(Bound<usize>, Bound<usize>)> for Slice<V> {773fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self {774// SAFETY: This a subslice of a valid slice.775unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }776}777}778779impl<V> IndexMut<Range<usize>> for Slice<V> {780fn index_mut(&mut self, key: Range<usize>) -> &mut Self {781// SAFETY: This a subslice of a valid slice.782unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }783}784}785786impl<V> IndexMut<RangeFrom<usize>> for Slice<V> {787fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self {788// SAFETY: This a subslice of a valid slice.789unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }790}791}792793impl<V> IndexMut<RangeFull> for Slice<V> {794fn index_mut(&mut self, key: RangeFull) -> &mut Self {795// SAFETY: This a subslice of a valid slice.796unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }797}798}799800impl<V> IndexMut<RangeInclusive<usize>> for Slice<V> {801fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self {802// SAFETY: This a subslice of a valid slice.803unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }804}805}806807impl<V> IndexMut<RangeTo<usize>> for Slice<V> {808fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self {809// SAFETY: This a subslice of a valid slice.810unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }811}812}813814impl<V> IndexMut<RangeToInclusive<usize>> for Slice<V> {815fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self {816// SAFETY: This a subslice of a valid slice.817unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }818}819}820821impl<V> IndexMut<usize> for Slice<V> {822fn index_mut(&mut self, key: usize) -> &mut V {823self.1.index_mut(key)824}825}826827/// An iterator over the entries of an [`EntityIndexMap`].828///829/// This `struct` is created by the [`EntityIndexMap::iter`] method.830/// See its documentation for more.831pub struct Iter<'a, V, S = EntityHash>(map::Iter<'a, Entity, V>, PhantomData<S>);832833impl<'a, V> Iter<'a, V> {834/// Returns the inner [`Iter`](map::Iter).835pub fn into_inner(self) -> map::Iter<'a, Entity, V> {836self.0837}838839/// Returns a slice of the remaining entries in the iterator.840///841/// Equivalent to [`map::Iter::as_slice`].842pub fn as_slice(&self) -> &Slice<V> {843// SAFETY: The source IndexMap uses EntityHash.844unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }845}846}847848impl<'a, V> Deref for Iter<'a, V> {849type Target = map::Iter<'a, Entity, V>;850851fn deref(&self) -> &Self::Target {852&self.0853}854}855856impl<'a, V> Iterator for Iter<'a, V> {857type Item = (&'a Entity, &'a V);858859fn next(&mut self) -> Option<Self::Item> {860self.0.next()861}862}863864impl<V> DoubleEndedIterator for Iter<'_, V> {865fn next_back(&mut self) -> Option<Self::Item> {866self.0.next_back()867}868}869870impl<V> ExactSizeIterator for Iter<'_, V> {}871872impl<V> FusedIterator for Iter<'_, V> {}873874impl<V> Clone for Iter<'_, V> {875fn clone(&self) -> Self {876Self(self.0.clone(), PhantomData)877}878}879880impl<V: Debug> Debug for Iter<'_, V> {881fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {882f.debug_tuple("Iter").field(&self.0).field(&self.1).finish()883}884}885886impl<V> Default for Iter<'_, V> {887fn default() -> Self {888Self(Default::default(), PhantomData)889}890}891892/// A mutable iterator over the entries of an [`EntityIndexMap`].893///894/// This `struct` is created by the [`EntityIndexMap::iter_mut`] method.895/// See its documentation for more.896pub struct IterMut<'a, V, S = EntityHash>(map::IterMut<'a, Entity, V>, PhantomData<S>);897898impl<'a, V> IterMut<'a, V> {899/// Returns the inner [`IterMut`](map::IterMut).900pub fn into_inner(self) -> map::IterMut<'a, Entity, V> {901self.0902}903904/// Returns a slice of the remaining entries in the iterator.905///906/// Equivalent to [`map::IterMut::as_slice`].907pub fn as_slice(&self) -> &Slice<V> {908// SAFETY: The source IndexMap uses EntityHash.909unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }910}911912/// Returns a mutable slice of the remaining entries in the iterator.913///914/// Equivalent to [`map::IterMut::into_slice`].915pub fn into_slice(self) -> &'a mut Slice<V> {916// SAFETY: The source IndexMap uses EntityHash.917unsafe { Slice::from_slice_unchecked_mut(self.0.into_slice()) }918}919}920921impl<'a, V> Deref for IterMut<'a, V> {922type Target = map::IterMut<'a, Entity, V>;923924fn deref(&self) -> &Self::Target {925&self.0926}927}928929impl<'a, V> Iterator for IterMut<'a, V> {930type Item = (&'a Entity, &'a mut V);931932fn next(&mut self) -> Option<Self::Item> {933self.0.next()934}935}936937impl<V> DoubleEndedIterator for IterMut<'_, V> {938fn next_back(&mut self) -> Option<Self::Item> {939self.0.next_back()940}941}942943impl<V> ExactSizeIterator for IterMut<'_, V> {}944945impl<V> FusedIterator for IterMut<'_, V> {}946947impl<V: Debug> Debug for IterMut<'_, V> {948fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {949f.debug_tuple("IterMut")950.field(&self.0)951.field(&self.1)952.finish()953}954}955956impl<V> Default for IterMut<'_, V> {957fn default() -> Self {958Self(Default::default(), PhantomData)959}960}961962/// An owning iterator over the entries of an [`IndexMap`].963///964/// This `struct` is created by the [`IndexMap::into_iter`] method965/// (provided by the [`IntoIterator`] trait). See its documentation for more.966pub struct IntoIter<V, S = EntityHash>(map::IntoIter<Entity, V>, PhantomData<S>);967968impl<V> IntoIter<V> {969/// Returns the inner [`IntoIter`](map::IntoIter).970pub fn into_inner(self) -> map::IntoIter<Entity, V> {971self.0972}973974/// Returns a slice of the remaining entries in the iterator.975///976/// Equivalent to [`map::IntoIter::as_slice`].977pub fn as_slice(&self) -> &Slice<V> {978// SAFETY: The source IndexMap uses EntityHash.979unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }980}981982/// Returns a mutable slice of the remaining entries in the iterator.983///984/// Equivalent to [`map::IntoIter::as_mut_slice`].985pub fn as_mut_slice(&mut self) -> &mut Slice<V> {986// SAFETY: The source IndexMap uses EntityHash.987unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) }988}989}990991impl<V> Deref for IntoIter<V> {992type Target = map::IntoIter<Entity, V>;993994fn deref(&self) -> &Self::Target {995&self.0996}997}998999impl<V> Iterator for IntoIter<V> {1000type Item = (Entity, V);10011002fn next(&mut self) -> Option<Self::Item> {1003self.0.next()1004}1005}10061007impl<V> DoubleEndedIterator for IntoIter<V> {1008fn next_back(&mut self) -> Option<Self::Item> {1009self.0.next_back()1010}1011}10121013impl<V> ExactSizeIterator for IntoIter<V> {}10141015impl<V> FusedIterator for IntoIter<V> {}10161017impl<V: Clone> Clone for IntoIter<V> {1018fn clone(&self) -> Self {1019Self(self.0.clone(), PhantomData)1020}1021}10221023impl<V: Debug> Debug for IntoIter<V> {1024fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {1025f.debug_tuple("IntoIter")1026.field(&self.0)1027.field(&self.1)1028.finish()1029}1030}10311032impl<V> Default for IntoIter<V> {1033fn default() -> Self {1034Self(Default::default(), PhantomData)1035}1036}10371038/// A draining iterator over the entries of an [`EntityIndexMap`].1039///1040/// This `struct` is created by the [`EntityIndexMap::drain`] method.1041/// See its documentation for more.1042pub struct Drain<'a, V, S = EntityHash>(map::Drain<'a, Entity, V>, PhantomData<S>);10431044impl<'a, V> Drain<'a, V> {1045/// Returns the inner [`Drain`](map::Drain).1046pub fn into_inner(self) -> map::Drain<'a, Entity, V> {1047self.01048}10491050/// Returns a slice of the remaining entries in the iterator.1051///1052/// Equivalent to [`map::Drain::as_slice`].1053pub fn as_slice(&self) -> &Slice<V> {1054// SAFETY: The source IndexMap uses EntityHash.1055unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }1056}1057}10581059impl<'a, V> Deref for Drain<'a, V> {1060type Target = map::Drain<'a, Entity, V>;10611062fn deref(&self) -> &Self::Target {1063&self.01064}1065}10661067impl<V> Iterator for Drain<'_, V> {1068type Item = (Entity, V);10691070fn next(&mut self) -> Option<Self::Item> {1071self.0.next()1072}1073}10741075impl<V> DoubleEndedIterator for Drain<'_, V> {1076fn next_back(&mut self) -> Option<Self::Item> {1077self.0.next_back()1078}1079}10801081impl<V> ExactSizeIterator for Drain<'_, V> {}10821083impl<V> FusedIterator for Drain<'_, V> {}10841085impl<V: Debug> Debug for Drain<'_, V> {1086fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {1087f.debug_tuple("Drain")1088.field(&self.0)1089.field(&self.1)1090.finish()1091}1092}10931094/// An iterator over the keys of an [`EntityIndexMap`].1095///1096/// This `struct` is created by the [`EntityIndexMap::keys`] method.1097/// See its documentation for more.1098pub struct Keys<'a, V, S = EntityHash>(map::Keys<'a, Entity, V>, PhantomData<S>);10991100impl<'a, V> Keys<'a, V> {1101/// Returns the inner [`Keys`](map::Keys).1102pub fn into_inner(self) -> map::Keys<'a, Entity, V> {1103self.01104}1105}11061107impl<'a, V, S> Deref for Keys<'a, V, S> {1108type Target = map::Keys<'a, Entity, V>;11091110fn deref(&self) -> &Self::Target {1111&self.01112}1113}11141115impl<'a, V> Iterator for Keys<'a, V> {1116type Item = &'a Entity;11171118fn next(&mut self) -> Option<Self::Item> {1119self.0.next()1120}1121}11221123impl<V> DoubleEndedIterator for Keys<'_, V> {1124fn next_back(&mut self) -> Option<Self::Item> {1125self.0.next_back()1126}1127}11281129impl<V> ExactSizeIterator for Keys<'_, V> {}11301131impl<V> FusedIterator for Keys<'_, V> {}11321133impl<V> Index<usize> for Keys<'_, V> {1134type Output = Entity;11351136fn index(&self, index: usize) -> &Entity {1137self.0.index(index)1138}1139}11401141impl<V> Clone for Keys<'_, V> {1142fn clone(&self) -> Self {1143Self(self.0.clone(), PhantomData)1144}1145}11461147impl<V: Debug> Debug for Keys<'_, V> {1148fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {1149f.debug_tuple("Keys").field(&self.0).field(&self.1).finish()1150}1151}11521153impl<V> Default for Keys<'_, V> {1154fn default() -> Self {1155Self(Default::default(), PhantomData)1156}1157}11581159// SAFETY: Keys stems from a correctly behaving `IndexMap<Entity, V, EntityHash>`.1160unsafe impl<V> EntitySetIterator for Keys<'_, V> {}11611162/// An owning iterator over the keys of an [`EntityIndexMap`].1163///1164/// This `struct` is created by the [`EntityIndexMap::into_keys`] method.1165/// See its documentation for more.1166pub struct IntoKeys<V, S = EntityHash>(map::IntoKeys<Entity, V>, PhantomData<S>);11671168impl<V> IntoKeys<V> {1169/// Returns the inner [`IntoKeys`](map::IntoKeys).1170pub fn into_inner(self) -> map::IntoKeys<Entity, V> {1171self.01172}1173}11741175impl<V> Deref for IntoKeys<V> {1176type Target = map::IntoKeys<Entity, V>;11771178fn deref(&self) -> &Self::Target {1179&self.01180}1181}11821183impl<V> Iterator for IntoKeys<V> {1184type Item = Entity;11851186fn next(&mut self) -> Option<Self::Item> {1187self.0.next()1188}1189}11901191impl<V> DoubleEndedIterator for IntoKeys<V> {1192fn next_back(&mut self) -> Option<Self::Item> {1193self.0.next_back()1194}1195}11961197impl<V> ExactSizeIterator for IntoKeys<V> {}11981199impl<V> FusedIterator for IntoKeys<V> {}12001201impl<V: Debug> Debug for IntoKeys<V> {1202fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {1203f.debug_tuple("IntoKeys")1204.field(&self.0)1205.field(&self.1)1206.finish()1207}1208}12091210impl<V> Default for IntoKeys<V> {1211fn default() -> Self {1212Self(Default::default(), PhantomData)1213}1214}12151216// SAFETY: IntoKeys stems from a correctly behaving `IndexMap<Entity, V, EntityHash>`.1217unsafe impl<V> EntitySetIterator for IntoKeys<V> {}121812191220