1use 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#[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 pub const fn new() -> Self {
44 Self(IndexMap::with_hasher(EntityHash))
45 }
46
47 pub fn with_capacity(n: usize) -> Self {
53 Self(IndexMap::with_capacity_and_hasher(n, EntityHash))
54 }
55
56 pub const fn from_index_map(set: IndexMap<Entity, V, EntityHash>) -> Self {
58 Self(set)
59 }
60
61 pub fn into_inner(self) -> IndexMap<Entity, V, EntityHash> {
63 self.0
64 }
65
66 pub fn as_slice(&self) -> &Slice<V> {
70 unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
72 }
73
74 pub fn as_mut_slice(&mut self) -> &mut Slice<V> {
78 unsafe { Slice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
80 }
81
82 pub fn into_boxed_slice(self) -> Box<Slice<V>> {
86 unsafe { Slice::from_boxed_slice_unchecked(self.0.into_boxed_slice()) }
88 }
89
90 pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Slice<V>> {
94 self.0.get_range(range).map(|slice|
95 unsafe { Slice::from_slice_unchecked(slice) })
97 }
98
99 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 unsafe { Slice::from_slice_unchecked_mut(slice) })
106 }
107
108 pub fn iter(&self) -> Iter<'_, V> {
112 Iter(self.0.iter(), PhantomData)
113 }
114
115 pub fn iter_mut(&mut self) -> IterMut<'_, V> {
119 IterMut(self.0.iter_mut(), PhantomData)
120 }
121
122 pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> Drain<'_, V> {
127 Drain(self.0.drain(range), PhantomData)
128 }
129
130 pub fn keys(&self) -> Keys<'_, V> {
134 Keys(self.0.keys(), PhantomData)
135 }
136
137 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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#[repr(transparent)]
382pub struct Slice<V, S = EntityHash>(PhantomData<S>, map::Slice<Entity, V>);
383
384impl<V> Slice<V> {
385 pub const fn new<'a>() -> &'a Self {
389 unsafe { Self::from_slice_unchecked(map::Slice::new()) }
391 }
392
393 pub fn new_mut<'a>() -> &'a mut Self {
397 unsafe { Self::from_slice_unchecked_mut(map::Slice::new_mut()) }
399 }
400
401 pub const unsafe fn from_slice_unchecked(slice: &map::Slice<Entity, V>) -> &Self {
409 unsafe { &*(ptr::from_ref(slice) as *const Self) }
411 }
412
413 pub const unsafe fn from_slice_unchecked_mut(slice: &mut map::Slice<Entity, V>) -> &mut Self {
421 unsafe { &mut *(ptr::from_mut(slice) as *mut Self) }
423 }
424
425 pub const fn as_inner(&self) -> &map::Slice<Entity, V> {
427 &self.1
428 }
429
430 pub unsafe fn from_boxed_slice_unchecked(slice: Box<map::Slice<Entity, V>>) -> Box<Self> {
438 unsafe { Box::from_raw(Box::into_raw(slice) as *mut Self) }
440 }
441
442 #[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 unsafe { &*(ptr::from_ref(self).cast::<Box<map::Slice<Entity, V>>>()) }
450 }
451
452 pub fn into_boxed_inner(self: Box<Self>) -> Box<map::Slice<Entity, V>> {
454 unsafe { Box::from_raw(Box::into_raw(self) as *mut map::Slice<Entity, V>) }
456 }
457
458 pub fn get_index_mut(&mut self, index: usize) -> Option<(&Entity, &mut V)> {
462 self.1.get_index_mut(index)
463 }
464
465 pub fn get_range<R: RangeBounds<usize>>(&self, range: R) -> Option<&Self> {
469 self.1.get_range(range).map(|slice|
470 unsafe { Self::from_slice_unchecked(slice) })
472 }
473
474 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 unsafe { Self::from_slice_unchecked_mut(slice) })
481 }
482
483 pub fn first_mut(&mut self) -> Option<(&Entity, &mut V)> {
487 self.1.first_mut()
488 }
489
490 pub fn last_mut(&mut self) -> Option<(&Entity, &mut V)> {
494 self.1.last_mut()
495 }
496
497 pub fn split_at(&self, index: usize) -> (&Self, &Self) {
501 let (slice_1, slice_2) = self.1.split_at(index);
502 unsafe {
504 (
505 Self::from_slice_unchecked(slice_1),
506 Self::from_slice_unchecked(slice_2),
507 )
508 }
509 }
510
511 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 unsafe {
518 (
519 Self::from_slice_unchecked_mut(slice_1),
520 Self::from_slice_unchecked_mut(slice_2),
521 )
522 }
523 }
524
525 pub fn split_first(&self) -> Option<((&Entity, &V), &Self)> {
530 self.1.split_first().map(|(first, rest)| {
531 (
532 first,
533 unsafe { Self::from_slice_unchecked(rest) },
535 )
536 })
537 }
538
539 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 unsafe { Self::from_slice_unchecked_mut(rest) },
549 )
550 })
551 }
552
553 pub fn split_last(&self) -> Option<((&Entity, &V), &Self)> {
558 self.1.split_last().map(|(last, rest)| {
559 (
560 last,
561 unsafe { Self::from_slice_unchecked(rest) },
563 )
564 })
565 }
566
567 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 unsafe { Self::from_slice_unchecked_mut(rest) },
577 )
578 })
579 }
580
581 pub fn iter(&self) -> Iter<'_, V> {
585 Iter(self.1.iter(), PhantomData)
586 }
587
588 pub fn iter_mut(&mut self) -> IterMut<'_, V> {
592 IterMut(self.1.iter_mut(), PhantomData)
593 }
594
595 pub fn keys(&self) -> Keys<'_, V> {
599 Keys(self.1.keys(), PhantomData)
600 }
601
602 pub fn into_keys(self: Box<Self>) -> IntoKeys<V> {
606 IntoKeys(self.into_boxed_inner().into_keys(), PhantomData)
607 }
608
609 pub fn values_mut(&mut self) -> ValuesMut<'_, Entity, V> {
613 self.1.values_mut()
614 }
615
616 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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
855pub struct Iter<'a, V, S = EntityHash>(map::Iter<'a, Entity, V>, PhantomData<S>);
860
861impl<'a, V> Iter<'a, V> {
862 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 pub const fn into_inner(self) -> map::Iter<'a, Entity, V> {
874 self.0
875 }
876
877 pub fn as_slice(&self) -> &Slice<V> {
881 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 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 unsafe { Self::from_iter_unchecked(Default::default()) }
933 }
934}
935
936pub struct IterMut<'a, V, S = EntityHash>(map::IterMut<'a, Entity, V>, PhantomData<S>);
941
942impl<'a, V> IterMut<'a, V> {
943 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 pub const fn into_inner(self) -> map::IterMut<'a, Entity, V> {
957 self.0
958 }
959
960 pub fn as_slice(&self) -> &Slice<V> {
964 unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
966 }
967
968 pub fn into_slice(self) -> &'a mut Slice<V> {
972 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 unsafe { Self::from_iter_mut_unchecked(Default::default()) }
1020 }
1021}
1022
1023pub struct IntoIter<V, S = EntityHash>(map::IntoIter<Entity, V>, PhantomData<S>);
1028
1029impl<V> IntoIter<V> {
1030 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 pub fn into_inner(self) -> map::IntoIter<Entity, V> {
1044 self.0
1045 }
1046
1047 pub fn as_slice(&self) -> &Slice<V> {
1051 unsafe { Slice::from_slice_unchecked(self.0.as_slice()) }
1053 }
1054
1055 pub fn as_mut_slice(&mut self) -> &mut Slice<V> {
1059 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 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 unsafe { Self::from_into_iter_unchecked(Default::default()) }
1114 }
1115}
1116
1117pub struct Drain<'a, V, S = EntityHash>(map::Drain<'a, Entity, V>, PhantomData<S>);
1122
1123impl<'a, V> Drain<'a, V> {
1124 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 pub fn into_inner(self) -> map::Drain<'a, Entity, V> {
1138 self.0
1139 }
1140
1141 pub fn as_slice(&self) -> &Slice<V> {
1145 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
1189pub struct Keys<'a, V, S = EntityHash>(map::Keys<'a, Entity, V>, PhantomData<S>);
1194
1195impl<'a, V> Keys<'a, V> {
1196 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 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 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 unsafe { Self::from_keys_unchecked(Default::default()) }
1267 }
1268}
1269
1270unsafe impl<V> EntitySetIterator for Keys<'_, V> {}
1272
1273pub struct IntoKeys<V, S = EntityHash>(map::IntoKeys<Entity, V>, PhantomData<S>);
1278
1279impl<V> IntoKeys<V> {
1280 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 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 unsafe { Self::from_into_keys_unchecked(Default::default()) }
1341 }
1342}
1343
1344unsafe impl<V> EntitySetIterator for IntoKeys<V> {}