Skip to main content

bevy_ecs/entity/
index_map.rs

1//! Contains the [`EntityIndexMap`] type, an [`IndexMap`] pre-configured to use [`EntityHash`] hashing.
2//!
3//! This module is a lightweight wrapper around `indexmap`'s [`IndexMap`] that is more performant for [`Entity`] keys.
4
5use core::{
6    cmp::Ordering,
7    fmt::{self, Debug, Formatter},
8    hash::{BuildHasher, Hash, Hasher},
9    iter::FusedIterator,
10    marker::PhantomData,
11    ops::{
12        Bound, Deref, DerefMut, Index, IndexMut, Range, RangeBounds, RangeFrom, RangeFull,
13        RangeInclusive, RangeTo, RangeToInclusive,
14    },
15    ptr,
16};
17
18#[cfg(feature = "bevy_reflect")]
19use bevy_reflect::Reflect;
20pub use indexmap::map::Entry;
21use indexmap::{
22    self,
23    map::{self, IntoValues, ValuesMut},
24    IndexMap,
25};
26
27use super::{Entity, EntityEquivalent, EntityHash, EntitySetIterator};
28
29use bevy_platform::prelude::Box;
30
31/// A [`IndexMap`] pre-configured to use [`EntityHash`] hashing.
32#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
33#[cfg_attr(feature = "serialize", derive(serde::Deserialize, serde::Serialize))]
34#[derive(Debug, Clone)]
35pub struct EntityIndexMap<V>(IndexMap<Entity, V, EntityHash>);
36
37impl<V> EntityIndexMap<V> {
38    /// Creates an empty `EntityIndexMap`.
39    ///
40    /// Equivalent to [`IndexMap::with_hasher(EntityHash)`].
41    ///
42    /// [`IndexMap::with_hasher(EntityHash)`]: indexmap::IndexMap::with_hasher
43    pub const fn new() -> Self {
44        Self(IndexMap::with_hasher(EntityHash))
45    }
46
47    /// Creates an empty `EntityIndexMap` with the specified capacity.
48    ///
49    /// Equivalent to [`IndexMap::with_capacity_and_hasher(n, EntityHash)`].
50    ///
51    /// [`IndexMap::with_capacity_and_hasher(n, EntityHash)`]: indexmap::IndexMap::with_capacity_and_hasher
52    pub fn with_capacity(n: usize) -> Self {
53        Self(IndexMap::with_capacity_and_hasher(n, EntityHash))
54    }
55
56    /// Constructs an `EntityIndexMap` from an [`IndexMap`].
57    pub const fn from_index_map(set: IndexMap<Entity, V, EntityHash>) -> Self {
58        Self(set)
59    }
60
61    /// Returns the inner [`IndexMap`].
62    pub fn into_inner(self) -> IndexMap<Entity, V, EntityHash> {
63        self.0
64    }
65
66    /// Returns a slice of all the key-value pairs in the map.
67    ///
68    /// Equivalent to [`IndexMap::as_slice`].
69    pub fn as_slice(&self) -> &Slice<V> {
70        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
71        unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
72    }
73
74    /// Returns a mutable slice of all the key-value pairs in the map.
75    ///
76    /// Equivalent to [`IndexMap::as_mut_slice`].
77    pub fn as_mut_slice(&mut self) -> &mut Slice<V> {
78        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
79        unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
80    }
81
82    /// Converts into a boxed slice of all the key-value pairs in the map.
83    ///
84    /// Equivalent to [`IndexMap::into_boxed_slice`].
85    pub fn into_boxed_slice(self) -> Box<Slice<V>> {
86        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
87        unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) }
88    }
89
90    /// Returns a slice of key-value pairs in the given range of indices.
91    ///
92    /// Equivalent to [`IndexMap::get_range`].
93    pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<V>> {
94        self.0.get_range(range).map(|slice|
95            // SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice.
96            unsafe { Slice::from_slice_unchecked(slice) })
97    }
98
99    /// Returns a mutable slice of key-value pairs in the given range of indices.
100    ///
101    /// Equivalent to [`IndexMap::get_range_mut`].
102    pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Slice<V>> {
103        self.0.get_range_mut(range).map(|slice|
104            // SAFETY: EntityIndexSetSlice is a transparent wrapper around indexmap::set::Slice.
105            unsafe { Slice::from_slice_unchecked_mut(slice) })
106    }
107
108    /// Return an iterator over the key-value pairs of the map, in their order.
109    ///
110    /// Equivalent to [`IndexMap::iter`].
111    pub fn iter(&self) -> Iter<'_, V> {
112        Iter(self.0.iter(), PhantomData)
113    }
114
115    /// Return a mutable iterator over the key-value pairs of the map, in their order.
116    ///
117    /// Equivalent to [`IndexMap::iter_mut`].
118    pub fn iter_mut(&mut self) -> IterMut<'_, V> {
119        IterMut(self.0.iter_mut(), PhantomData)
120    }
121
122    /// Clears the `IndexMap` in the given index range, returning those
123    /// key-value pairs as a drain iterator.
124    ///
125    /// Equivalent to [`IndexMap::drain`].
126    pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_, V> {
127        Drain(self.0.drain(range), PhantomData)
128    }
129
130    /// Return an iterator over the keys of the map, in their order.
131    ///
132    /// Equivalent to [`IndexMap::keys`].
133    pub fn keys(&self) -> Keys<'_, V> {
134        Keys(self.0.keys(), PhantomData)
135    }
136
137    /// Return an owning iterator over the keys of the map, in their order.
138    ///
139    /// Equivalent to [`IndexMap::into_keys`].
140    pub fn into_keys(self) -> IntoKeys<V> {
141        IntoKeys(self.0.into_keys(), PhantomData)
142    }
143}
144
145impl<V> Default for EntityIndexMap<V> {
146    fn default() -> Self {
147        Self(Default::default())
148    }
149}
150
151impl<V> Deref for EntityIndexMap<V> {
152    type Target = IndexMap<Entity, V, EntityHash>;
153
154    fn deref(&self) -> &Self::Target {
155        &self.0
156    }
157}
158
159impl<V> DerefMut for EntityIndexMap<V> {
160    fn deref_mut(&mut self) -> &mut Self::Target {
161        &mut self.0
162    }
163}
164
165impl<'a, V: Copy> Extend<(&'a Entity, &'a V)> for EntityIndexMap<V> {
166    fn extend<T: IntoIterator<Item = (&'a Entity, &'a V)>>(&mut self, iter: T) {
167        self.0.extend(iter);
168    }
169}
170
171impl<V> Extend<(Entity, V)> for EntityIndexMap<V> {
172    fn extend<T: IntoIterator<Item = (Entity, V)>>(&mut self, iter: T) {
173        self.0.extend(iter);
174    }
175}
176
177impl<V, const N: usize> From<[(Entity, V); N]> for EntityIndexMap<V> {
178    fn from(value: [(Entity, V); N]) -> Self {
179        Self(IndexMap::from_iter(value))
180    }
181}
182
183impl<V> FromIterator<(Entity, V)> for EntityIndexMap<V> {
184    fn from_iter<I: IntoIterator<Item = (Entity, V)>>(iterable: I) -> Self {
185        Self(IndexMap::from_iter(iterable))
186    }
187}
188
189impl<V, Q: EntityEquivalent + ?Sized> Index<&Q> for EntityIndexMap<V> {
190    type Output = V;
191
192    fn index(&self, key: &Q) -> &V {
193        self.0.index(&key.entity())
194    }
195}
196
197impl<V> Index<(Bound<usize>, Bound<usize>)> for EntityIndexMap<V> {
198    type Output = Slice<V>;
199
200    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self::Output {
201        // SAFETY: The source IndexMap uses EntityHash.
202        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
203    }
204}
205
206impl<V> Index<Range<usize>> for EntityIndexMap<V> {
207    type Output = Slice<V>;
208
209    fn index(&self, key: Range<usize>) -> &Self::Output {
210        // SAFETY: The source IndexMap uses EntityHash.
211        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
212    }
213}
214
215impl<V> Index<RangeFrom<usize>> for EntityIndexMap<V> {
216    type Output = Slice<V>;
217
218    fn index(&self, key: RangeFrom<usize>) -> &Self::Output {
219        // SAFETY: The source IndexMap uses EntityHash.
220        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
221    }
222}
223
224impl<V> Index<RangeFull> for EntityIndexMap<V> {
225    type Output = Slice<V>;
226
227    fn index(&self, key: RangeFull) -> &Self::Output {
228        // SAFETY: The source IndexMap uses EntityHash.
229        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
230    }
231}
232
233impl<V> Index<RangeInclusive<usize>> for EntityIndexMap<V> {
234    type Output = Slice<V>;
235
236    fn index(&self, key: RangeInclusive<usize>) -> &Self::Output {
237        // SAFETY: The source IndexMap uses EntityHash.
238        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
239    }
240}
241
242impl<V> Index<RangeTo<usize>> for EntityIndexMap<V> {
243    type Output = Slice<V>;
244
245    fn index(&self, key: RangeTo<usize>) -> &Self::Output {
246        // SAFETY: The source IndexMap uses EntityHash.
247        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
248    }
249}
250
251impl<V> Index<RangeToInclusive<usize>> for EntityIndexMap<V> {
252    type Output = Slice<V>;
253
254    fn index(&self, key: RangeToInclusive<usize>) -> &Self::Output {
255        // SAFETY: The source IndexMap uses EntityHash.
256        unsafe { Slice::from_slice_unchecked(self.0.index(key)) }
257    }
258}
259
260impl<V> Index<usize> for EntityIndexMap<V> {
261    type Output = V;
262
263    fn index(&self, key: usize) -> &V {
264        self.0.index(key)
265    }
266}
267
268impl<V, Q: EntityEquivalent + ?Sized> IndexMut<&Q> for EntityIndexMap<V> {
269    fn index_mut(&mut self, key: &Q) -> &mut V {
270        self.0.index_mut(&key.entity())
271    }
272}
273
274impl<V> IndexMut<(Bound<usize>, Bound<usize>)> for EntityIndexMap<V> {
275    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self::Output {
276        // SAFETY: The source IndexMap uses EntityHash.
277        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
278    }
279}
280
281impl<V> IndexMut<Range<usize>> for EntityIndexMap<V> {
282    fn index_mut(&mut self, key: Range<usize>) -> &mut Self::Output {
283        // SAFETY: The source IndexMap uses EntityHash.
284        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
285    }
286}
287
288impl<V> IndexMut<RangeFrom<usize>> for EntityIndexMap<V> {
289    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self::Output {
290        // SAFETY: The source IndexMap uses EntityHash.
291        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
292    }
293}
294
295impl<V> IndexMut<RangeFull> for EntityIndexMap<V> {
296    fn index_mut(&mut self, key: RangeFull) -> &mut Self::Output {
297        // SAFETY: The source IndexMap uses EntityHash.
298        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
299    }
300}
301
302impl<V> IndexMut<RangeInclusive<usize>> for EntityIndexMap<V> {
303    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self::Output {
304        // SAFETY: The source IndexMap uses EntityHash.
305        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
306    }
307}
308
309impl<V> IndexMut<RangeTo<usize>> for EntityIndexMap<V> {
310    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self::Output {
311        // SAFETY: The source IndexMap uses EntityHash.
312        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
313    }
314}
315
316impl<V> IndexMut<RangeToInclusive<usize>> for EntityIndexMap<V> {
317    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self::Output {
318        // SAFETY: The source IndexMap uses EntityHash.
319        unsafe { Slice::from_slice_unchecked_mut(self.0.index_mut(key)) }
320    }
321}
322
323impl<V> IndexMut<usize> for EntityIndexMap<V> {
324    fn index_mut(&mut self, key: usize) -> &mut V {
325        self.0.index_mut(key)
326    }
327}
328
329impl<'a, V> IntoIterator for &'a EntityIndexMap<V> {
330    type Item = (&'a Entity, &'a V);
331    type IntoIter = Iter<'a, V>;
332
333    fn into_iter(self) -> Self::IntoIter {
334        Iter(self.0.iter(), PhantomData)
335    }
336}
337
338impl<'a, V> IntoIterator for &'a mut EntityIndexMap<V> {
339    type Item = (&'a Entity, &'a mut V);
340    type IntoIter = IterMut<'a, V>;
341
342    fn into_iter(self) -> Self::IntoIter {
343        IterMut(self.0.iter_mut(), PhantomData)
344    }
345}
346
347impl<V> IntoIterator for EntityIndexMap<V> {
348    type Item = (Entity, V);
349    type IntoIter = IntoIter<V>;
350
351    fn into_iter(self) -> Self::IntoIter {
352        IntoIter(self.0.into_iter(), PhantomData)
353    }
354}
355
356impl<V1, V2, S2> PartialEq<IndexMap<Entity, V2, S2>> for EntityIndexMap<V1>
357where
358    V1: PartialEq<V2>,
359    S2: BuildHasher,
360{
361    fn eq(&self, other: &IndexMap<Entity, V2, S2>) -> bool {
362        self.0.eq(other)
363    }
364}
365
366impl<V1, V2> PartialEq<EntityIndexMap<V2>> for EntityIndexMap<V1>
367where
368    V1: PartialEq<V2>,
369{
370    fn eq(&self, other: &EntityIndexMap<V2>) -> bool {
371        self.0.eq(other)
372    }
373}
374
375impl<V: Eq> Eq for EntityIndexMap<V> {}
376
377/// A dynamically-sized slice of key-value pairs in an [`EntityIndexMap`].
378///
379/// Equivalent to an [`indexmap::map::Slice<V>`] whose source [`IndexMap`]
380/// uses [`EntityHash`].
381#[repr(transparent)]
382pub struct Slice<V, S = EntityHash>(PhantomData<S>, map::Slice<Entity, V>);
383
384impl<V> Slice<V> {
385    /// Returns an empty slice.    
386    ///
387    /// Equivalent to [`map::Slice::new`].
388    pub const fn new<'a>() -> &'a Self {
389        // SAFETY: The source slice is empty.
390        unsafe { Self::from_slice_unchecked(map::Slice::new()) }
391    }
392
393    /// Returns an empty mutable slice.
394    ///
395    /// Equivalent to [`map::Slice::new_mut`].
396    pub fn new_mut<'a>() -> &'a mut Self {
397        // SAFETY: The source slice is empty.
398        unsafe { Self::from_slice_unchecked_mut(map::Slice::new_mut()) }
399    }
400
401    /// Constructs a [`entity::index_map::Slice`] from a [`indexmap::map::Slice`] unsafely.
402    ///
403    /// # Safety
404    ///
405    /// `slice` must stem from an [`IndexMap`] using [`EntityHash`].
406    ///
407    /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`
408    pub const unsafe fn from_slice_unchecked(slice: &map::Slice<Entity, V>) -> &Self {
409        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
410        unsafe { &*(ptr::from_ref(slice) as *const Self) }
411    }
412
413    /// Constructs a [`entity::index_map::Slice`] from a [`indexmap::map::Slice`] unsafely.
414    ///
415    /// # Safety
416    ///
417    /// `slice` must stem from an [`IndexMap`] using [`EntityHash`].
418    ///
419    /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`
420    pub const unsafe fn from_slice_unchecked_mut(slice: &mut map::Slice<Entity, V>) -> &mut Self {
421        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
422        unsafe { &mut *(ptr::from_mut(slice) as *mut Self) }
423    }
424
425    /// Casts `self` to the inner slice.
426    pub const fn as_inner(&self) -> &map::Slice<Entity, V> {
427        &self.1
428    }
429
430    /// Constructs a boxed [`entity::index_map::Slice`] from a boxed [`indexmap::map::Slice`] unsafely.
431    ///
432    /// # Safety
433    ///
434    /// `slice` must stem from an [`IndexMap`] using [`EntityHash`].
435    ///
436    /// [`entity::index_map::Slice`]: `crate::entity::index_map::Slice`
437    pub unsafe fn from_boxed_slice_unchecked(slice: Box<map::Slice<Entity, V>>) -> Box<Self> {
438        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
439        unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) }
440    }
441
442    /// Casts a reference to `self` to the inner slice.
443    #[expect(
444        clippy::borrowed_box,
445        reason = "We wish to access the Box API of the inner type, without consuming it."
446    )]
447    pub const fn as_boxed_inner(self: &Box<Self>) -> &Box<map::Slice<Entity, V>> {
448        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
449        unsafe { &*(ptr::from_ref(self).cast::<Box<map::Slice<Entity, V>>>()) }
450    }
451
452    /// Casts `self` to the inner slice.
453    pub fn into_boxed_inner(self: Box<Self>) -> Box<map::Slice<Entity, V>> {
454        // SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
455        unsafe { Box::from_raw(Box::into_raw(self) as *mut map::Slice<Entity, V>) }
456    }
457
458    /// Get a key-value pair by index, with mutable access to the value.
459    ///
460    /// Equivalent to [`map::Slice::get_index_mut`].
461    pub fn get_index_mut(&mut self, index: usize) -> Option<(&Entity, &mut V)> {
462        self.1.get_index_mut(index)
463    }
464
465    /// Returns a slice of key-value pairs in the given range of indices.
466    ///
467    /// Equivalent to [`map::Slice::get_range`].
468    pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
469        self.1.get_range(range).map(|slice|
470            // SAFETY: This a subslice of a valid slice.
471            unsafe { Self::from_slice_unchecked(slice) })
472    }
473
474    /// Returns a mutable slice of key-value pairs in the given range of indices.
475    ///
476    /// Equivalent to [`map::Slice::get_range_mut`].
477    pub fn get_range_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Option<&mut Self> {
478        self.1.get_range_mut(range).map(|slice|
479            // SAFETY: This a subslice of a valid slice.
480            unsafe { Self::from_slice_unchecked_mut(slice) })
481    }
482
483    /// Get the first key-value pair, with mutable access to the value.
484    ///
485    /// Equivalent to [`map::Slice::first_mut`].
486    pub fn first_mut(&mut self) -> Option<(&Entity, &mut V)> {
487        self.1.first_mut()
488    }
489
490    /// Get the last key-value pair, with mutable access to the value.
491    ///
492    /// Equivalent to [`map::Slice::last_mut`].
493    pub fn last_mut(&mut self) -> Option<(&Entity, &mut V)> {
494        self.1.last_mut()
495    }
496
497    /// Divides one slice into two at an index.
498    ///
499    /// Equivalent to [`map::Slice::split_at`].
500    pub fn split_at(&self, index: usize) -> (&Self, &Self) {
501        let (slice_1, slice_2) = self.1.split_at(index);
502        // SAFETY: These are subslices of a valid slice.
503        unsafe {
504            (
505                Self::from_slice_unchecked(slice_1),
506                Self::from_slice_unchecked(slice_2),
507            )
508        }
509    }
510
511    /// Divides one mutable slice into two at an index.
512    ///
513    /// Equivalent to [`map::Slice::split_at_mut`].
514    pub fn split_at_mut(&mut self, index: usize) -> (&mut Self, &mut Self) {
515        let (slice_1, slice_2) = self.1.split_at_mut(index);
516        // SAFETY: These are subslices of a valid slice.
517        unsafe {
518            (
519                Self::from_slice_unchecked_mut(slice_1),
520                Self::from_slice_unchecked_mut(slice_2),
521            )
522        }
523    }
524
525    /// Returns the first key-value pair and the rest of the slice,
526    /// or `None` if it is empty.
527    ///
528    /// Equivalent to [`map::Slice::split_first`].
529    pub fn split_first(&self) -> Option<((&Entity, &V), &Self)> {
530        self.1.split_first().map(|(first, rest)| {
531            (
532                first,
533                // SAFETY: This a subslice of a valid slice.
534                unsafe { Self::from_slice_unchecked(rest) },
535            )
536        })
537    }
538
539    /// Returns the first key-value pair and the rest of the slice,
540    /// with mutable access to the value, or `None` if it is empty.
541    ///
542    /// Equivalent to [`map::Slice::split_first_mut`].
543    pub fn split_first_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> {
544        self.1.split_first_mut().map(|(first, rest)| {
545            (
546                first,
547                // SAFETY: This a subslice of a valid slice.
548                unsafe { Self::from_slice_unchecked_mut(rest) },
549            )
550        })
551    }
552
553    /// Returns the last key-value pair and the rest of the slice,
554    /// or `None` if it is empty.
555    ///
556    /// Equivalent to [`map::Slice::split_last`].
557    pub fn split_last(&self) -> Option<((&Entity, &V), &Self)> {
558        self.1.split_last().map(|(last, rest)| {
559            (
560                last,
561                // SAFETY: This a subslice of a valid slice.
562                unsafe { Self::from_slice_unchecked(rest) },
563            )
564        })
565    }
566
567    /// Returns the last key-value pair and the rest of the slice,
568    /// with mutable access to the value, or `None` if it is empty.
569    ///
570    /// Equivalent to [`map::Slice::split_last_mut`].
571    pub fn split_last_mut(&mut self) -> Option<((&Entity, &mut V), &mut Self)> {
572        self.1.split_last_mut().map(|(last, rest)| {
573            (
574                last,
575                // SAFETY: This a subslice of a valid slice.
576                unsafe { Self::from_slice_unchecked_mut(rest) },
577            )
578        })
579    }
580
581    /// Return an iterator over the key-value pairs of the map slice.
582    ///
583    /// Equivalent to [`map::Slice::iter`].
584    pub fn iter(&self) -> Iter<'_, V> {
585        Iter(self.1.iter(), PhantomData)
586    }
587
588    /// Return an iterator over the key-value pairs of the map slice.
589    ///
590    /// Equivalent to [`map::Slice::iter_mut`].
591    pub fn iter_mut(&mut self) -> IterMut<'_, V> {
592        IterMut(self.1.iter_mut(), PhantomData)
593    }
594
595    /// Return an iterator over the keys of the map slice.
596    ///
597    /// Equivalent to [`map::Slice::keys`].
598    pub fn keys(&self) -> Keys<'_, V> {
599        Keys(self.1.keys(), PhantomData)
600    }
601
602    /// Return an owning iterator over the keys of the map slice.
603    ///
604    /// Equivalent to [`map::Slice::into_keys`].
605    pub fn into_keys(self: Box<Self>) -> IntoKeys<V> {
606        IntoKeys(self.into_boxed_inner().into_keys(), PhantomData)
607    }
608
609    /// Return an iterator over mutable references to the values of the map slice.
610    ///
611    /// Equivalent to [`map::Slice::values_mut`].
612    pub fn values_mut(&mut self) -> ValuesMut<'_, Entity, V> {
613        self.1.values_mut()
614    }
615
616    /// Return an owning iterator over the values of the map slice.
617    ///
618    /// Equivalent to [`map::Slice::into_values`].
619    pub fn into_values(self: Box<Self>) -> IntoValues<Entity, V> {
620        self.into_boxed_inner().into_values()
621    }
622}
623
624impl<V> Deref for Slice<V> {
625    type Target = map::Slice<Entity, V>;
626
627    fn deref(&self) -> &Self::Target {
628        &self.1
629    }
630}
631
632impl<V: Debug> Debug for Slice<V> {
633    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
634        f.debug_tuple("Slice")
635            .field(&self.0)
636            .field(&&self.1)
637            .finish()
638    }
639}
640
641impl<V: Clone> Clone for Box<Slice<V>> {
642    fn clone(&self) -> Self {
643        // SAFETY: This a clone of a valid slice.
644        unsafe { Slice::from_boxed_slice_unchecked(self.as_boxed_inner().clone()) }
645    }
646}
647
648impl<V> Default for &Slice<V> {
649    fn default() -> Self {
650        // SAFETY: The source slice is empty.
651        unsafe { Slice::from_slice_unchecked(<&map::Slice<Entity, V>>::default()) }
652    }
653}
654
655impl<V> Default for &mut Slice<V> {
656    fn default() -> Self {
657        // SAFETY: The source slice is empty.
658        unsafe { Slice::from_slice_unchecked_mut(<&mut map::Slice<Entity, V>>::default()) }
659    }
660}
661
662impl<V> Default for Box<Slice<V>> {
663    fn default() -> Self {
664        // SAFETY: The source slice is empty.
665        unsafe { Slice::from_boxed_slice_unchecked(<Box<map::Slice<Entity, V>>>::default()) }
666    }
667}
668
669impl<V: Copy> From<&Slice<V>> for Box<Slice<V>> {
670    fn from(value: &Slice<V>) -> Self {
671        // SAFETY: This slice is a copy of a valid slice.
672        unsafe { Slice::from_boxed_slice_unchecked(value.1.into()) }
673    }
674}
675
676impl<V: Hash> Hash for Slice<V> {
677    fn hash<H: Hasher>(&self, state: &mut H) {
678        self.1.hash(state);
679    }
680}
681
682impl<'a, V> IntoIterator for &'a Slice<V> {
683    type Item = (&'a Entity, &'a V);
684    type IntoIter = Iter<'a, V>;
685
686    fn into_iter(self) -> Self::IntoIter {
687        Iter(self.1.iter(), PhantomData)
688    }
689}
690
691impl<'a, V> IntoIterator for &'a mut Slice<V> {
692    type Item = (&'a Entity, &'a mut V);
693    type IntoIter = IterMut<'a, V>;
694
695    fn into_iter(self) -> Self::IntoIter {
696        IterMut(self.1.iter_mut(), PhantomData)
697    }
698}
699
700impl<V> IntoIterator for Box<Slice<V>> {
701    type Item = (Entity, V);
702    type IntoIter = IntoIter<V>;
703
704    fn into_iter(self) -> Self::IntoIter {
705        IntoIter(self.into_boxed_inner().into_iter(), PhantomData)
706    }
707}
708
709impl<V: PartialOrd> PartialOrd for Slice<V> {
710    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
711        self.1.partial_cmp(&other.1)
712    }
713}
714
715impl<V: Ord> Ord for Slice<V> {
716    fn cmp(&self, other: &Self) -> Ordering {
717        self.1.cmp(other)
718    }
719}
720
721impl<V: PartialEq> PartialEq for Slice<V> {
722    fn eq(&self, other: &Self) -> bool {
723        self.1 == other.1
724    }
725}
726
727impl<V: Eq> Eq for Slice<V> {}
728
729impl<V> Index<(Bound<usize>, Bound<usize>)> for Slice<V> {
730    type Output = Self;
731
732    fn index(&self, key: (Bound<usize>, Bound<usize>)) -> &Self {
733        // SAFETY: This a subslice of a valid slice.
734        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
735    }
736}
737
738impl<V> Index<Range<usize>> for Slice<V> {
739    type Output = Self;
740
741    fn index(&self, key: Range<usize>) -> &Self {
742        // SAFETY: This a subslice of a valid slice.
743        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
744    }
745}
746
747impl<V> Index<RangeFrom<usize>> for Slice<V> {
748    type Output = Self;
749
750    fn index(&self, key: RangeFrom<usize>) -> &Self {
751        // SAFETY: This a subslice of a valid slice.
752        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
753    }
754}
755
756impl<V> Index<RangeFull> for Slice<V> {
757    type Output = Self;
758
759    fn index(&self, key: RangeFull) -> &Self {
760        // SAFETY: This a subslice of a valid slice.
761        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
762    }
763}
764
765impl<V> Index<RangeInclusive<usize>> for Slice<V> {
766    type Output = Self;
767
768    fn index(&self, key: RangeInclusive<usize>) -> &Self {
769        // SAFETY: This a subslice of a valid slice.
770        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
771    }
772}
773
774impl<V> Index<RangeTo<usize>> for Slice<V> {
775    type Output = Self;
776
777    fn index(&self, key: RangeTo<usize>) -> &Self {
778        // SAFETY: This a subslice of a valid slice.
779        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
780    }
781}
782
783impl<V> Index<RangeToInclusive<usize>> for Slice<V> {
784    type Output = Self;
785
786    fn index(&self, key: RangeToInclusive<usize>) -> &Self {
787        // SAFETY: This a subslice of a valid slice.
788        unsafe { Self::from_slice_unchecked(self.1.index(key)) }
789    }
790}
791
792impl<V> Index<usize> for Slice<V> {
793    type Output = V;
794
795    fn index(&self, key: usize) -> &V {
796        self.1.index(key)
797    }
798}
799
800impl<V> IndexMut<(Bound<usize>, Bound<usize>)> for Slice<V> {
801    fn index_mut(&mut self, key: (Bound<usize>, Bound<usize>)) -> &mut Self {
802        // SAFETY: This a subslice of a valid slice.
803        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
804    }
805}
806
807impl<V> IndexMut<Range<usize>> for Slice<V> {
808    fn index_mut(&mut self, key: Range<usize>) -> &mut Self {
809        // SAFETY: This a subslice of a valid slice.
810        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
811    }
812}
813
814impl<V> IndexMut<RangeFrom<usize>> for Slice<V> {
815    fn index_mut(&mut self, key: RangeFrom<usize>) -> &mut Self {
816        // SAFETY: This a subslice of a valid slice.
817        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
818    }
819}
820
821impl<V> IndexMut<RangeFull> for Slice<V> {
822    fn index_mut(&mut self, key: RangeFull) -> &mut Self {
823        // SAFETY: This a subslice of a valid slice.
824        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
825    }
826}
827
828impl<V> IndexMut<RangeInclusive<usize>> for Slice<V> {
829    fn index_mut(&mut self, key: RangeInclusive<usize>) -> &mut Self {
830        // SAFETY: This a subslice of a valid slice.
831        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
832    }
833}
834
835impl<V> IndexMut<RangeTo<usize>> for Slice<V> {
836    fn index_mut(&mut self, key: RangeTo<usize>) -> &mut Self {
837        // SAFETY: This a subslice of a valid slice.
838        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
839    }
840}
841
842impl<V> IndexMut<RangeToInclusive<usize>> for Slice<V> {
843    fn index_mut(&mut self, key: RangeToInclusive<usize>) -> &mut Self {
844        // SAFETY: This a subslice of a valid slice.
845        unsafe { Self::from_slice_unchecked_mut(self.1.index_mut(key)) }
846    }
847}
848
849impl<V> IndexMut<usize> for Slice<V> {
850    fn index_mut(&mut self, key: usize) -> &mut V {
851        self.1.index_mut(key)
852    }
853}
854
855/// An iterator over the entries of an [`EntityIndexMap`].
856///
857/// This `struct` is created by the [`EntityIndexMap::iter`] method.
858/// See its documentation for more.
859pub struct Iter<'a, V, S = EntityHash>(map::Iter<'a, Entity, V>, PhantomData<S>);
860
861impl<'a, V> Iter<'a, V> {
862    /// Constructs a [`Iter<'a, V, S>`] from a [`map::Iter<'a, V>`] unsafely.
863    ///
864    /// # Safety
865    ///
866    /// `iter` must either be empty, or have been obtained from a
867    /// [`IndexMap`] using the `S` hasher.
868    pub const unsafe fn from_iter_unchecked<S>(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> {
869        Iter(iter, PhantomData)
870    }
871
872    /// Returns the inner [`Iter`](map::Iter).
873    pub const fn into_inner(self) -> map::Iter<'a, Entity, V> {
874        self.0
875    }
876
877    /// Returns a slice of the remaining entries in the iterator.
878    ///
879    /// Equivalent to [`map::Iter::as_slice`].
880    pub fn as_slice(&self) -> &Slice<V> {
881        // SAFETY: The source IndexMap uses EntityHash.
882        unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
883    }
884}
885
886impl<'a, V> Deref for Iter<'a, V> {
887    type Target = map::Iter<'a, Entity, V>;
888
889    fn deref(&self) -> &Self::Target {
890        &self.0
891    }
892}
893
894impl<'a, V> Iterator for Iter<'a, V> {
895    type Item = (&'a Entity, &'a V);
896
897    fn next(&mut self) -> Option<Self::Item> {
898        self.0.next()
899    }
900
901    fn size_hint(&self) -> (usize, Option<usize>) {
902        self.0.size_hint()
903    }
904}
905
906impl<V> DoubleEndedIterator for Iter<'_, V> {
907    fn next_back(&mut self) -> Option<Self::Item> {
908        self.0.next_back()
909    }
910}
911
912impl<V> ExactSizeIterator for Iter<'_, V> {}
913
914impl<V> FusedIterator for Iter<'_, V> {}
915
916impl<V> Clone for Iter<'_, V> {
917    fn clone(&self) -> Self {
918        // SAFETY: We are cloning an already valid `Iter`.
919        unsafe { Self::from_iter_unchecked(self.0.clone()) }
920    }
921}
922
923impl<V: Debug> Debug for Iter<'_, V> {
924    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
925        f.debug_tuple("Iter").field(&self.0).field(&self.1).finish()
926    }
927}
928
929impl<V> Default for Iter<'_, V> {
930    fn default() -> Self {
931        // SAFETY: `Iter` is empty.
932        unsafe { Self::from_iter_unchecked(Default::default()) }
933    }
934}
935
936/// A mutable iterator over the entries of an [`EntityIndexMap`].
937///
938/// This `struct` is created by the [`EntityIndexMap::iter_mut`] method.
939/// See its documentation for more.
940pub struct IterMut<'a, V, S = EntityHash>(map::IterMut<'a, Entity, V>, PhantomData<S>);
941
942impl<'a, V> IterMut<'a, V> {
943    /// Constructs a [`IterMut<'a, V, S>`] from a [`map::IterMut<'a, V>`] unsafely.
944    ///
945    /// # Safety
946    ///
947    /// `iter_mut` must either be empty, or have been obtained from a
948    /// [`IndexMap`] using the `S` hasher.
949    pub const unsafe fn from_iter_mut_unchecked<S>(
950        iter_mut: map::IterMut<'a, Entity, V>,
951    ) -> IterMut<'a, V, S> {
952        IterMut(iter_mut, PhantomData)
953    }
954
955    /// Returns the inner [`IterMut`](map::IterMut).
956    pub const fn into_inner(self) -> map::IterMut<'a, Entity, V> {
957        self.0
958    }
959
960    /// Returns a slice of the remaining entries in the iterator.
961    ///
962    /// Equivalent to [`map::IterMut::as_slice`].
963    pub fn as_slice(&self) -> &Slice<V> {
964        // SAFETY: The source IndexMap uses EntityHash.
965        unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
966    }
967
968    /// Returns a mutable slice of the remaining entries in the iterator.
969    ///
970    /// Equivalent to [`map::IterMut::into_slice`].
971    pub fn into_slice(self) -> &'a mut Slice<V> {
972        // SAFETY: The source IndexMap uses EntityHash.
973        unsafe { Slice::from_slice_unchecked_mut(self.0.into_slice()) }
974    }
975}
976
977impl<'a, V> Deref for IterMut<'a, V> {
978    type Target = map::IterMut<'a, Entity, V>;
979
980    fn deref(&self) -> &Self::Target {
981        &self.0
982    }
983}
984
985impl<'a, V> Iterator for IterMut<'a, V> {
986    type Item = (&'a Entity, &'a mut V);
987
988    fn next(&mut self) -> Option<Self::Item> {
989        self.0.next()
990    }
991
992    fn size_hint(&self) -> (usize, Option<usize>) {
993        self.0.size_hint()
994    }
995}
996
997impl<V> DoubleEndedIterator for IterMut<'_, V> {
998    fn next_back(&mut self) -> Option<Self::Item> {
999        self.0.next_back()
1000    }
1001}
1002
1003impl<V> ExactSizeIterator for IterMut<'_, V> {}
1004
1005impl<V> FusedIterator for IterMut<'_, V> {}
1006
1007impl<V: Debug> Debug for IterMut<'_, V> {
1008    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1009        f.debug_tuple("IterMut")
1010            .field(&self.0)
1011            .field(&self.1)
1012            .finish()
1013    }
1014}
1015
1016impl<V> Default for IterMut<'_, V> {
1017    fn default() -> Self {
1018        // SAFETY: `IterMut` is empty.
1019        unsafe { Self::from_iter_mut_unchecked(Default::default()) }
1020    }
1021}
1022
1023/// An owning iterator over the entries of an [`IndexMap`].
1024///
1025/// This `struct` is created by the [`IndexMap::into_iter`] method
1026/// (provided by the [`IntoIterator`] trait). See its documentation for more.
1027pub struct IntoIter<V, S = EntityHash>(map::IntoIter<Entity, V>, PhantomData<S>);
1028
1029impl<V> IntoIter<V> {
1030    /// Constructs a [`IntoIter<V, S>`] from a [`map::IntoIter<V>`] unsafely.
1031    ///
1032    /// # Safety
1033    ///
1034    /// `into_iter` must either be empty, or have been obtained from a
1035    /// [`IndexMap`] using the `S` hasher.
1036    pub const unsafe fn from_into_iter_unchecked<S>(
1037        into_iter: map::IntoIter<Entity, V>,
1038    ) -> IntoIter<V, S> {
1039        IntoIter(into_iter, PhantomData)
1040    }
1041
1042    /// Returns the inner [`IntoIter`](map::IntoIter).
1043    pub fn into_inner(self) -> map::IntoIter<Entity, V> {
1044        self.0
1045    }
1046
1047    /// Returns a slice of the remaining entries in the iterator.
1048    ///
1049    /// Equivalent to [`map::IntoIter::as_slice`].
1050    pub fn as_slice(&self) -> &Slice<V> {
1051        // SAFETY: The source IndexMap uses EntityHash.
1052        unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
1053    }
1054
1055    /// Returns a mutable slice of the remaining entries in the iterator.
1056    ///
1057    /// Equivalent to [`map::IntoIter::as_mut_slice`].
1058    pub fn as_mut_slice(&mut self) -> &mut Slice<V> {
1059        // SAFETY: The source IndexMap uses EntityHash.
1060        unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
1061    }
1062}
1063
1064impl<V> Deref for IntoIter<V> {
1065    type Target = map::IntoIter<Entity, V>;
1066
1067    fn deref(&self) -> &Self::Target {
1068        &self.0
1069    }
1070}
1071
1072impl<V> Iterator for IntoIter<V> {
1073    type Item = (Entity, V);
1074
1075    fn next(&mut self) -> Option<Self::Item> {
1076        self.0.next()
1077    }
1078
1079    fn size_hint(&self) -> (usize, Option<usize>) {
1080        self.0.size_hint()
1081    }
1082}
1083
1084impl<V> DoubleEndedIterator for IntoIter<V> {
1085    fn next_back(&mut self) -> Option<Self::Item> {
1086        self.0.next_back()
1087    }
1088}
1089
1090impl<V> ExactSizeIterator for IntoIter<V> {}
1091
1092impl<V> FusedIterator for IntoIter<V> {}
1093
1094impl<V: Clone> Clone for IntoIter<V> {
1095    fn clone(&self) -> Self {
1096        // SAFETY: We are cloning an already valid `IntoIter`.
1097        unsafe { Self::from_into_iter_unchecked(self.0.clone()) }
1098    }
1099}
1100
1101impl<V: Debug> Debug for IntoIter<V> {
1102    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1103        f.debug_tuple("IntoIter")
1104            .field(&self.0)
1105            .field(&self.1)
1106            .finish()
1107    }
1108}
1109
1110impl<V> Default for IntoIter<V> {
1111    fn default() -> Self {
1112        // SAFETY: `IntoIter` is empty.
1113        unsafe { Self::from_into_iter_unchecked(Default::default()) }
1114    }
1115}
1116
1117/// A draining iterator over the entries of an [`EntityIndexMap`].
1118///
1119/// This `struct` is created by the [`EntityIndexMap::drain`] method.
1120/// See its documentation for more.
1121pub struct Drain<'a, V, S = EntityHash>(map::Drain<'a, Entity, V>, PhantomData<S>);
1122
1123impl<'a, V> Drain<'a, V> {
1124    /// Constructs a [`Drain<'a, V, S>`] from a [`map::Drain<'a, V>`] unsafely.
1125    ///
1126    /// # Safety
1127    ///
1128    /// `drain` must either be empty, or have been obtained from a
1129    /// [`IndexMap`] using the `S` hasher.
1130    pub const unsafe fn from_drain_unchecked<S>(
1131        drain: map::Drain<'a, Entity, V>,
1132    ) -> Drain<'a, V, S> {
1133        Drain(drain, PhantomData)
1134    }
1135
1136    /// Returns the inner [`Drain`](indexmap::map::Drain).
1137    pub fn into_inner(self) -> map::Drain<'a, Entity, V> {
1138        self.0
1139    }
1140
1141    /// Returns a slice of the remaining entries in the iterator.
1142    ///
1143    /// Equivalent to [`map::Drain::as_slice`](`indexmap::map::Drain::as_slice`).
1144    pub fn as_slice(&self) -> &Slice<V> {
1145        // SAFETY: The source IndexMap uses EntityHash.
1146        unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
1147    }
1148}
1149
1150impl<'a, V> Deref for Drain<'a, V> {
1151    type Target = map::Drain<'a, Entity, V>;
1152
1153    fn deref(&self) -> &Self::Target {
1154        &self.0
1155    }
1156}
1157
1158impl<V> Iterator for Drain<'_, V> {
1159    type Item = (Entity, V);
1160
1161    fn next(&mut self) -> Option<Self::Item> {
1162        self.0.next()
1163    }
1164
1165    fn size_hint(&self) -> (usize, Option<usize>) {
1166        self.0.size_hint()
1167    }
1168}
1169
1170impl<V> DoubleEndedIterator for Drain<'_, V> {
1171    fn next_back(&mut self) -> Option<Self::Item> {
1172        self.0.next_back()
1173    }
1174}
1175
1176impl<V> ExactSizeIterator for Drain<'_, V> {}
1177
1178impl<V> FusedIterator for Drain<'_, V> {}
1179
1180impl<V: Debug> Debug for Drain<'_, V> {
1181    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1182        f.debug_tuple("Drain")
1183            .field(&self.0)
1184            .field(&self.1)
1185            .finish()
1186    }
1187}
1188
1189/// An iterator over the keys of an [`EntityIndexMap`].
1190///
1191/// This `struct` is created by the [`EntityIndexMap::keys`] method.
1192/// See its documentation for more.
1193pub struct Keys<'a, V, S = EntityHash>(map::Keys<'a, Entity, V>, PhantomData<S>);
1194
1195impl<'a, V> Keys<'a, V> {
1196    /// Constructs a [`Keys<'a, V, S>`] from a [`map::Keys<'a, V>`] unsafely.
1197    ///
1198    /// # Safety
1199    ///
1200    /// `keys` must either be empty, or have been obtained from a
1201    /// [`IndexMap`] using the `S` hasher.
1202    pub const unsafe fn from_keys_unchecked<S>(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> {
1203        Keys(keys, PhantomData)
1204    }
1205
1206    /// Returns the inner [`Keys`](map::Keys).
1207    pub const fn into_inner(self) -> map::Keys<'a, Entity, V> {
1208        self.0
1209    }
1210}
1211
1212impl<'a, V, S> Deref for Keys<'a, V, S> {
1213    type Target = map::Keys<'a, Entity, V>;
1214
1215    fn deref(&self) -> &Self::Target {
1216        &self.0
1217    }
1218}
1219
1220impl<'a, V> Iterator for Keys<'a, V> {
1221    type Item = &'a Entity;
1222
1223    fn next(&mut self) -> Option<Self::Item> {
1224        self.0.next()
1225    }
1226
1227    fn size_hint(&self) -> (usize, Option<usize>) {
1228        self.0.size_hint()
1229    }
1230}
1231
1232impl<V> DoubleEndedIterator for Keys<'_, V> {
1233    fn next_back(&mut self) -> Option<Self::Item> {
1234        self.0.next_back()
1235    }
1236}
1237
1238impl<V> ExactSizeIterator for Keys<'_, V> {}
1239
1240impl<V> FusedIterator for Keys<'_, V> {}
1241
1242impl<V> Index<usize> for Keys<'_, V> {
1243    type Output = Entity;
1244
1245    fn index(&self, index: usize) -> &Entity {
1246        self.0.index(index)
1247    }
1248}
1249
1250impl<V> Clone for Keys<'_, V> {
1251    fn clone(&self) -> Self {
1252        // SAFETY: We are cloning an already valid `Keys`.
1253        unsafe { Self::from_keys_unchecked(self.0.clone()) }
1254    }
1255}
1256
1257impl<V: Debug> Debug for Keys<'_, V> {
1258    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1259        f.debug_tuple("Keys").field(&self.0).field(&self.1).finish()
1260    }
1261}
1262
1263impl<V> Default for Keys<'_, V> {
1264    fn default() -> Self {
1265        // SAFETY: `Keys` is empty.
1266        unsafe { Self::from_keys_unchecked(Default::default()) }
1267    }
1268}
1269
1270// SAFETY: Keys stems from a correctly behaving `IndexMap<Entity, V, EntityHash>`.
1271unsafe impl<V> EntitySetIterator for Keys<'_, V> {}
1272
1273/// An owning iterator over the keys of an [`EntityIndexMap`].
1274///
1275/// This `struct` is created by the [`EntityIndexMap::into_keys`] method.
1276/// See its documentation for more.
1277pub struct IntoKeys<V, S = EntityHash>(map::IntoKeys<Entity, V>, PhantomData<S>);
1278
1279impl<V> IntoKeys<V> {
1280    /// Constructs a [`IntoKeys<V, S>`] from a [`map::IntoKeys<V>`] unsafely.
1281    ///
1282    /// # Safety
1283    ///
1284    /// `into_keys` must either be empty, or have been obtained from a
1285    /// [`IndexMap`] using the `S` hasher.
1286    pub const unsafe fn from_into_keys_unchecked<S>(
1287        into_keys: map::IntoKeys<Entity, V>,
1288    ) -> IntoKeys<V, S> {
1289        IntoKeys(into_keys, PhantomData)
1290    }
1291
1292    /// Returns the inner [`IntoKeys`](map::IntoKeys).
1293    pub fn into_inner(self) -> map::IntoKeys<Entity, V> {
1294        self.0
1295    }
1296}
1297
1298impl<V> Deref for IntoKeys<V> {
1299    type Target = map::IntoKeys<Entity, V>;
1300
1301    fn deref(&self) -> &Self::Target {
1302        &self.0
1303    }
1304}
1305
1306impl<V> Iterator for IntoKeys<V> {
1307    type Item = Entity;
1308
1309    fn next(&mut self) -> Option<Self::Item> {
1310        self.0.next()
1311    }
1312
1313    fn size_hint(&self) -> (usize, Option<usize>) {
1314        self.0.size_hint()
1315    }
1316}
1317
1318impl<V> DoubleEndedIterator for IntoKeys<V> {
1319    fn next_back(&mut self) -> Option<Self::Item> {
1320        self.0.next_back()
1321    }
1322}
1323
1324impl<V> ExactSizeIterator for IntoKeys<V> {}
1325
1326impl<V> FusedIterator for IntoKeys<V> {}
1327
1328impl<V: Debug> Debug for IntoKeys<V> {
1329    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1330        f.debug_tuple("IntoKeys")
1331            .field(&self.0)
1332            .field(&self.1)
1333            .finish()
1334    }
1335}
1336
1337impl<V> Default for IntoKeys<V> {
1338    fn default() -> Self {
1339        // SAFETY: `IntoKeys` is empty.
1340        unsafe { Self::from_into_keys_unchecked(Default::default()) }
1341    }
1342}
1343
1344// SAFETY: IntoKeys stems from a correctly behaving `IndexMap<Entity, V, EntityHash>`.
1345unsafe impl<V> EntitySetIterator for IntoKeys<V> {}