Skip to content

Commit 3517a72

Browse files
committed
turn various entity set functions const
1 parent dceec17 commit 3517a72

File tree

8 files changed

+68
-60
lines changed

8 files changed

+68
-60
lines changed

crates/bevy_ecs/src/entity/entity_set.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub struct UniqueEntityIter<I: Iterator<Item: EntityEquivalent>> {
373373
impl<I: EntitySetIterator> UniqueEntityIter<I> {
374374
/// Constructs a `UniqueEntityIter` from an [`EntitySetIterator`].
375375
#[inline]
376-
pub fn from_entity_set_iter<S>(iter: I) -> Self {
376+
pub const fn from_entity_set_iter<S>(iter: I) -> Self {
377377
// SAFETY: iter implements `EntitySetIterator`.
378378
unsafe { Self::from_iter_unchecked(iter) }
379379
}
@@ -386,7 +386,7 @@ impl<I: Iterator<Item: EntityEquivalent>> UniqueEntityIter<I> {
386386
/// `iter` must only yield unique elements.
387387
/// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`].
388388
#[inline]
389-
pub unsafe fn from_iter_unchecked(iter: I) -> Self {
389+
pub const unsafe fn from_iter_unchecked(iter: I) -> Self {
390390
Self { iter }
391391
}
392392

@@ -396,7 +396,7 @@ impl<I: Iterator<Item: EntityEquivalent>> UniqueEntityIter<I> {
396396
/// `iter` must only yield unique elements.
397397
/// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`].
398398
#[inline]
399-
pub unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self {
399+
pub const unsafe fn from_iter_ref_unchecked(iter: &I) -> &Self {
400400
// SAFETY: UniqueEntityIter is a transparent wrapper around I.
401401
unsafe { &*ptr::from_ref(iter).cast() }
402402
}
@@ -407,7 +407,7 @@ impl<I: Iterator<Item: EntityEquivalent>> UniqueEntityIter<I> {
407407
/// `iter` must only yield unique elements.
408408
/// As in, the resulting iterator must adhere to the safety contract of [`EntitySetIterator`].
409409
#[inline]
410-
pub unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self {
410+
pub const unsafe fn from_iter_mut_unchecked(iter: &mut I) -> &mut Self {
411411
// SAFETY: UniqueEntityIter is a transparent wrapper around I.
412412
unsafe { &mut *ptr::from_mut(iter).cast() }
413413
}
@@ -420,7 +420,7 @@ impl<I: Iterator<Item: EntityEquivalent>> UniqueEntityIter<I> {
420420

421421
/// Returns a reference to the inner `I`.
422422
#[inline]
423-
pub fn as_inner(&self) -> &I {
423+
pub const fn as_inner(&self) -> &I {
424424
&self.iter
425425
}
426426

@@ -431,7 +431,7 @@ impl<I: Iterator<Item: EntityEquivalent>> UniqueEntityIter<I> {
431431
/// `self` must always contain an iterator that yields unique elements,
432432
/// even while this reference is live.
433433
#[inline]
434-
pub unsafe fn as_mut_inner(&mut self) -> &mut I {
434+
pub const unsafe fn as_mut_inner(&mut self) -> &mut I {
435435
&mut self.iter
436436
}
437437
}

crates/bevy_ecs/src/entity/hash_map.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl<V> EntityHashMap<V> {
4444

4545
/// Constructs an `EntityHashMap` from an [`HashMap`].
4646
#[inline]
47-
pub fn from_index_map(set: HashMap<Entity, V, EntityHash>) -> Self {
47+
pub const fn from_index_map(set: HashMap<Entity, V, EntityHash>) -> Self {
4848
Self(set)
4949
}
5050

@@ -193,13 +193,15 @@ impl<'a, V> Keys<'a, V> {
193193
/// `keys` must either be empty, or have been obtained from a
194194
/// [`hash_map::HashMap`] using the `S` hasher.
195195
#[inline]
196-
pub unsafe fn from_keys_unchecked<S>(keys: hash_map::Keys<'a, Entity, V>) -> Keys<'a, V, S> {
196+
pub const unsafe fn from_keys_unchecked<S>(
197+
keys: hash_map::Keys<'a, Entity, V>,
198+
) -> Keys<'a, V, S> {
197199
Keys::<'_, _, S>(keys, PhantomData)
198200
}
199201

200202
/// Returns the inner [`Keys`](hash_map::Keys).
201203
#[inline]
202-
pub fn into_inner(self) -> hash_map::Keys<'a, Entity, V> {
204+
pub const fn into_inner(self) -> hash_map::Keys<'a, Entity, V> {
203205
self.0
204206
}
205207
}
@@ -269,7 +271,7 @@ impl<V> IntoKeys<V> {
269271
/// `into_keys` must either be empty, or have been obtained from a
270272
/// [`hash_map::HashMap`] using the `S` hasher.
271273
#[inline]
272-
pub unsafe fn from_into_keys_unchecked<S>(
274+
pub const unsafe fn from_into_keys_unchecked<S>(
273275
into_keys: hash_map::IntoKeys<Entity, V>,
274276
) -> IntoKeys<V, S> {
275277
IntoKeys::<_, S>(into_keys, PhantomData)

crates/bevy_ecs/src/entity/hash_set.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl EntityHashSet {
5959

6060
/// Constructs an `EntityHashSet` from an [`HashSet`].
6161
#[inline]
62-
pub fn from_hash_set(set: HashSet<Entity, EntityHash>) -> Self {
62+
pub const fn from_hash_set(set: HashSet<Entity, EntityHash>) -> Self {
6363
Self(set)
6464
}
6565

@@ -255,13 +255,13 @@ impl<'a> Iter<'a> {
255255
/// `iter` must either be empty, or have been obtained from a
256256
/// [`hash_set::HashSet`] using the `S` hasher.
257257
#[inline]
258-
pub unsafe fn from_iter_unchecked<S>(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> {
258+
pub const unsafe fn from_iter_unchecked<S>(iter: hash_set::Iter<'a, Entity>) -> Iter<'a, S> {
259259
Iter::<'_, S>(iter, PhantomData)
260260
}
261261

262262
/// Returns the inner [`Iter`](hash_set::Iter).
263263
#[inline]
264-
pub fn into_inner(self) -> hash_set::Iter<'a, Entity> {
264+
pub const fn into_inner(self) -> hash_set::Iter<'a, Entity> {
265265
self.0
266266
}
267267
}
@@ -328,7 +328,7 @@ impl IntoIter {
328328
/// `into_iter` must either be empty, or have been obtained from a
329329
/// [`hash_set::HashSet`] using the `S` hasher.
330330
#[inline]
331-
pub unsafe fn from_into_iter_unchecked<S>(
331+
pub const unsafe fn from_into_iter_unchecked<S>(
332332
into_iter: hash_set::IntoIter<Entity>,
333333
) -> IntoIter<S> {
334334
IntoIter::<S>(into_iter, PhantomData)
@@ -398,7 +398,9 @@ impl<'a> Drain<'a> {
398398
/// `drain` must either be empty, or have been obtained from a
399399
/// [`hash_set::HashSet`] using the `S` hasher.
400400
#[inline]
401-
pub unsafe fn from_drain_unchecked<S>(drain: hash_set::Drain<'a, Entity>) -> Drain<'a, S> {
401+
pub const unsafe fn from_drain_unchecked<S>(
402+
drain: hash_set::Drain<'a, Entity>,
403+
) -> Drain<'a, S> {
402404
Drain::<'_, S>(drain, PhantomData)
403405
}
404406

@@ -461,7 +463,7 @@ impl<'a, F: FnMut(&Entity) -> bool> ExtractIf<'a, F> {
461463
/// `extract_if` must either be empty, or have been obtained from a
462464
/// [`hash_set::HashSet`] using the `S` hasher.
463465
#[inline]
464-
pub unsafe fn from_extract_if_unchecked<S>(
466+
pub const unsafe fn from_extract_if_unchecked<S>(
465467
extract_if: hash_set::ExtractIf<'a, Entity, F>,
466468
) -> ExtractIf<'a, F, S> {
467469
ExtractIf::<'_, _, S>(extract_if, PhantomData)

crates/bevy_ecs/src/entity/index_map.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<V> EntityIndexMap<V> {
5353

5454
/// Constructs an `EntityIndexMap` from an [`IndexMap`].
5555
#[inline]
56-
pub fn from_index_map(set: IndexMap<Entity, V, EntityHash>) -> Self {
56+
pub const fn from_index_map(set: IndexMap<Entity, V, EntityHash>) -> Self {
5757
Self(set)
5858
}
5959

@@ -482,7 +482,7 @@ impl<V> Slice<V> {
482482
reason = "We wish to access the Box API of the inner type, without consuming it."
483483
)]
484484
#[inline]
485-
pub fn as_boxed_inner(self: &Box<Self>) -> &Box<map::Slice<Entity, V>> {
485+
pub const fn as_boxed_inner(self: &Box<Self>) -> &Box<map::Slice<Entity, V>> {
486486
// SAFETY: Slice is a transparent wrapper around indexmap::map::Slice.
487487
unsafe { &*(ptr::from_ref(self).cast::<Box<map::Slice<Entity, V>>>()) }
488488
}
@@ -943,13 +943,13 @@ impl<'a, V> Iter<'a, V> {
943943
/// `iter` must either be empty, or have been obtained from a
944944
/// [`IndexMap`] using the `S` hasher.
945945
#[inline]
946-
pub unsafe fn from_iter_unchecked<S>(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> {
946+
pub const unsafe fn from_iter_unchecked<S>(iter: map::Iter<'a, Entity, V>) -> Iter<'a, V, S> {
947947
Iter::<'_, _, S>(iter, PhantomData)
948948
}
949949

950950
/// Returns the inner [`Iter`](map::Iter).
951951
#[inline]
952-
pub fn into_inner(self) -> map::Iter<'a, Entity, V> {
952+
pub const fn into_inner(self) -> map::Iter<'a, Entity, V> {
953953
self.0
954954
}
955955

@@ -1028,15 +1028,15 @@ impl<'a, V> IterMut<'a, V> {
10281028
/// `iter_mut` must either be empty, or have been obtained from a
10291029
/// [`IndexMap`] using the `S` hasher.
10301030
#[inline]
1031-
pub unsafe fn from_iter_mut_unchecked<S>(
1031+
pub const unsafe fn from_iter_mut_unchecked<S>(
10321032
iter_mut: map::IterMut<'a, Entity, V>,
10331033
) -> IterMut<'a, V, S> {
10341034
IterMut::<'_, _, S>(iter_mut, PhantomData)
10351035
}
10361036

10371037
/// Returns the inner [`IterMut`](map::IterMut).
10381038
#[inline]
1039-
pub fn into_inner(self) -> map::IterMut<'a, Entity, V> {
1039+
pub const fn into_inner(self) -> map::IterMut<'a, Entity, V> {
10401040
self.0
10411041
}
10421042

@@ -1119,7 +1119,7 @@ impl<V> IntoIter<V> {
11191119
/// `into_iter` must either be empty, or have been obtained from a
11201120
/// [`IndexMap`] using the `S` hasher.
11211121
#[inline]
1122-
pub unsafe fn from_into_iter_unchecked<S>(
1122+
pub const unsafe fn from_into_iter_unchecked<S>(
11231123
into_iter: map::IntoIter<Entity, V>,
11241124
) -> IntoIter<V, S> {
11251125
IntoIter::<_, S>(into_iter, PhantomData)
@@ -1218,7 +1218,9 @@ impl<'a, V> Drain<'a, V> {
12181218
/// `drain` must either be empty, or have been obtained from a
12191219
/// [`IndexMap`] using the `S` hasher.
12201220
#[inline]
1221-
pub unsafe fn from_drain_unchecked<S>(drain: map::Drain<'a, Entity, V>) -> Drain<'a, V, S> {
1221+
pub const unsafe fn from_drain_unchecked<S>(
1222+
drain: map::Drain<'a, Entity, V>,
1223+
) -> Drain<'a, V, S> {
12221224
Drain::<'_, _, S>(drain, PhantomData)
12231225
}
12241226

@@ -1290,13 +1292,13 @@ impl<'a, V> Keys<'a, V> {
12901292
/// `keys` must either be empty, or have been obtained from a
12911293
/// [`IndexMap`] using the `S` hasher.
12921294
#[inline]
1293-
pub unsafe fn from_keys_unchecked<S>(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> {
1295+
pub const unsafe fn from_keys_unchecked<S>(keys: map::Keys<'a, Entity, V>) -> Keys<'a, V, S> {
12941296
Keys::<'_, _, S>(keys, PhantomData)
12951297
}
12961298

12971299
/// Returns the inner [`Keys`](map::Keys).
12981300
#[inline]
1299-
pub fn into_inner(self) -> map::Keys<'a, Entity, V> {
1301+
pub const fn into_inner(self) -> map::Keys<'a, Entity, V> {
13001302
self.0
13011303
}
13021304
}
@@ -1378,7 +1380,7 @@ impl<V> IntoKeys<V> {
13781380
/// `into_keys` must either be empty, or have been obtained from a
13791381
/// [`IndexMap`] using the `S` hasher.
13801382
#[inline]
1381-
pub unsafe fn from_into_keys_unchecked<S>(
1383+
pub const unsafe fn from_into_keys_unchecked<S>(
13821384
into_keys: map::IntoKeys<Entity, V>,
13831385
) -> IntoKeys<V, S> {
13841386
IntoKeys::<_, S>(into_keys, PhantomData)

crates/bevy_ecs/src/entity/index_set.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl EntityIndexSet {
5050

5151
/// Constructs an `EntityIndexSet` from an [`IndexSet`].
5252
#[inline]
53-
pub fn from_index_set(set: IndexSet<Entity, EntityHash>) -> Self {
53+
pub const fn from_index_set(set: IndexSet<Entity, EntityHash>) -> Self {
5454
Self(set)
5555
}
5656

@@ -362,7 +362,7 @@ impl Slice {
362362
reason = "We wish to access the Box API of the inner type, without consuming it."
363363
)]
364364
#[inline]
365-
pub fn as_boxed_inner(self: &Box<Self>) -> &Box<set::Slice<Entity>> {
365+
pub const fn as_boxed_inner(self: &Box<Self>) -> &Box<set::Slice<Entity>> {
366366
// SAFETY: Slice is a transparent wrapper around indexmap::set::Slice.
367367
unsafe { &*(ptr::from_ref(self).cast::<Box<set::Slice<Entity>>>()) }
368368
}
@@ -624,13 +624,13 @@ impl<'a> Iter<'a> {
624624
/// `iter` must either be empty, or have been obtained from a
625625
/// [`IndexSet`] using the `S` hasher.
626626
#[inline]
627-
pub unsafe fn from_iter_unchecked<S>(iter: set::Iter<'a, Entity>) -> Iter<'a, S> {
627+
pub const unsafe fn from_iter_unchecked<S>(iter: set::Iter<'a, Entity>) -> Iter<'a, S> {
628628
Iter::<'_, S>(iter, PhantomData)
629629
}
630630

631631
/// Returns the inner [`Iter`](set::Iter).
632632
#[inline]
633-
pub fn into_inner(self) -> set::Iter<'a, Entity> {
633+
pub const fn into_inner(self) -> set::Iter<'a, Entity> {
634634
self.0
635635
}
636636

@@ -713,7 +713,9 @@ impl IntoIter {
713713
/// `into_iter` must either be empty, or have been obtained from a
714714
/// [`IndexSet`] using the `S` hasher.
715715
#[inline]
716-
pub unsafe fn from_into_iter_unchecked<S>(into_iter: set::IntoIter<Entity>) -> IntoIter<S> {
716+
pub const unsafe fn from_into_iter_unchecked<S>(
717+
into_iter: set::IntoIter<Entity>,
718+
) -> IntoIter<S> {
717719
IntoIter::<S>(into_iter, PhantomData)
718720
}
719721

@@ -805,7 +807,7 @@ impl<'a> Drain<'a> {
805807
/// `drain` must either be empty, or have been obtained from a
806808
/// [`IndexSet`] using the `S` hasher.
807809
#[inline]
808-
pub unsafe fn from_drain_unchecked<S>(drain: set::Drain<'a, Entity>) -> Drain<'a, S> {
810+
pub const unsafe fn from_drain_unchecked<S>(drain: set::Drain<'a, Entity>) -> Drain<'a, S> {
809811
Drain::<'_, S>(drain, PhantomData)
810812
}
811813

crates/bevy_ecs/src/entity/unique_array.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<T: EntityEquivalent, const N: usize> UniqueEntityEquivalentArray<T, N> {
124124

125125
/// Returns a reference to the inner array.
126126
#[inline]
127-
pub fn as_inner(&self) -> &[T; N] {
127+
pub const fn as_inner(&self) -> &[T; N] {
128128
&self.0
129129
}
130130

@@ -138,7 +138,7 @@ impl<T: EntityEquivalent, const N: usize> UniqueEntityEquivalentArray<T, N> {
138138
/// Returns a mutable slice containing the entire array. Equivalent to
139139
/// `&mut s[..]`.
140140
#[inline]
141-
pub fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
141+
pub const fn as_mut_slice(&mut self) -> &mut UniqueEntityEquivalentSlice<T> {
142142
// SAFETY: All elements in the original array are unique.
143143
unsafe { UniqueEntityEquivalentSlice::from_slice_unchecked_mut(self.0.as_mut_slice()) }
144144
}
@@ -148,7 +148,7 @@ impl<T: EntityEquivalent, const N: usize> UniqueEntityEquivalentArray<T, N> {
148148
///
149149
/// Equivalent to [`[T; N]::as_ref`](array::each_ref).
150150
#[inline]
151-
pub fn each_ref(&self) -> UniqueEntityEquivalentArray<&T, N> {
151+
pub const fn each_ref(&self) -> UniqueEntityEquivalentArray<&T, N> {
152152
UniqueEntityEquivalentArray(self.0.each_ref())
153153
}
154154
}

crates/bevy_ecs/src/entity/unique_slice.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]>>
16591659
///
16601660
/// All elements in each of the slices must be unique.
16611661
#[inline]
1662-
pub unsafe fn from_slice_iter_unchecked(iter: I) -> Self {
1662+
pub const unsafe fn from_slice_iter_unchecked(iter: I) -> Self {
16631663
Self { iter }
16641664
}
16651665

@@ -1671,7 +1671,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]>>
16711671

16721672
/// Returns a reference to the inner `I`.
16731673
#[inline]
1674-
pub fn as_inner(&self) -> &I {
1674+
pub const fn as_inner(&self) -> &I {
16751675
&self.iter
16761676
}
16771677

@@ -1682,7 +1682,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a [T]>>
16821682
/// `self` must always contain an iterator that yields unique elements,
16831683
/// even while this reference is live.
16841684
#[inline]
1685-
pub unsafe fn as_mut_inner(&mut self) -> &mut I {
1685+
pub const unsafe fn as_mut_inner(&mut self) -> &mut I {
16861686
&mut self.iter
16871687
}
16881688
}
@@ -1853,7 +1853,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]>>
18531853
///
18541854
/// All elements in each of the slices must be unique.
18551855
#[inline]
1856-
pub unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self {
1856+
pub const unsafe fn from_mut_slice_iter_unchecked(iter: I) -> Self {
18571857
Self { iter }
18581858
}
18591859

@@ -1865,7 +1865,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]>>
18651865

18661866
/// Returns a reference to the inner `I`.
18671867
#[inline]
1868-
pub fn as_inner(&self) -> &I {
1868+
pub const fn as_inner(&self) -> &I {
18691869
&self.iter
18701870
}
18711871

@@ -1876,7 +1876,7 @@ impl<'a, T: EntityEquivalent + 'a, I: Iterator<Item = &'a mut [T]>>
18761876
/// `self` must always contain an iterator that yields unique elements,
18771877
/// even while this reference is live.
18781878
#[inline]
1879-
pub unsafe fn as_mut_inner(&mut self) -> &mut I {
1879+
pub const unsafe fn as_mut_inner(&mut self) -> &mut I {
18801880
&mut self.iter
18811881
}
18821882
}

0 commit comments

Comments
 (0)