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