Skip to content

Commit 45cd178

Browse files
Reorganize fetch.rs and add more documentation
1 parent a06c865 commit 45cd178

File tree

1 file changed

+107
-104
lines changed

1 file changed

+107
-104
lines changed

crates/bevy_ecs/src/query/fetch.rs

+107-104
Original file line numberDiff line numberDiff line change
@@ -467,23 +467,6 @@ pub trait WorldQueryBatch<const N: usize, const ALIGN: usize>:
467467
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch;
468468
}
469469

470-
impl<const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for Entity
471-
where
472-
Entity: AlignedBatchGat<N, ALIGN>,
473-
{
474-
#[inline]
475-
unsafe fn table_fetch_batched_aligned<'w>(
476-
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
477-
table_row_start: usize,
478-
len: usize,
479-
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
480-
let entities = fetch
481-
.entities
482-
.unwrap_or_else(|| debug_checked_unreachable());
483-
entities.get_batch_aligned::<N, ALIGN>(table_row_start, len)
484-
}
485-
}
486-
487470
/// A helper trait for [`WorldQueryBatch`] that works around Rust's lack of Generic Associated Types.
488471
///
489472
/// **Note**: Consider using the type alias [`QueryBatch`] for conenvience instead of referring to `FullBatch` directly.
@@ -597,6 +580,23 @@ unsafe impl WorldQuery for Entity {
597580
}
598581
}
599582

583+
impl<const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for Entity
584+
where
585+
Entity: AlignedBatchGat<N, ALIGN>,
586+
{
587+
#[inline]
588+
unsafe fn table_fetch_batched_aligned<'w>(
589+
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
590+
table_row_start: usize,
591+
len: usize,
592+
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
593+
let entities = fetch
594+
.entities
595+
.unwrap_or_else(|| debug_checked_unreachable());
596+
entities.get_batch_aligned::<N, ALIGN>(table_row_start, len)
597+
}
598+
}
599+
600600
impl<'w> WorldQueryGats<'w> for Entity {
601601
type Fetch = EntityFetch<'w>;
602602
type Item = Entity;
@@ -622,29 +622,6 @@ pub struct ReadFetch<'w, T> {
622622
sparse_set: Option<&'w ComponentSparseSet>,
623623
}
624624

625-
impl<T: Component, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for &T
626-
where
627-
T: AlignedBatchGat<N, ALIGN>,
628-
{
629-
#[inline]
630-
unsafe fn table_fetch_batched_aligned<'w>(
631-
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
632-
table_row_start: usize,
633-
len: usize,
634-
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch
635-
where
636-
T: AlignedBatchGat<N, ALIGN>,
637-
{
638-
//TODO: when generalized const expresions are stable, want the following:
639-
//gcd::euclid_usize(ptr::MAX_SIMD_ALIGNMENT, N * core::mem::size_of::<T>());
640-
641-
let components = fetch
642-
.table_components
643-
.unwrap_or_else(|| debug_checked_unreachable());
644-
components.get_batch_aligned_deref::<N, ALIGN>(table_row_start, len)
645-
}
646-
}
647-
648625
/// SAFETY: `ROQueryFetch<Self>` is the same as `QueryFetch<Self>`
649626
unsafe impl<T: Component> WorldQuery for &T {
650627
type ReadOnly = Self;
@@ -775,6 +752,29 @@ unsafe impl<T: Component> WorldQuery for &T {
775752
}
776753
}
777754

755+
impl<T: Component, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for &T
756+
where
757+
T: AlignedBatchGat<N, ALIGN>,
758+
{
759+
#[inline]
760+
unsafe fn table_fetch_batched_aligned<'w>(
761+
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
762+
table_row_start: usize,
763+
len: usize,
764+
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch
765+
where
766+
T: AlignedBatchGat<N, ALIGN>,
767+
{
768+
//TODO: when generalized const expresions are stable, want the following:
769+
//gcd::euclid_usize(ptr::MAX_SIMD_ALIGNMENT, N * core::mem::size_of::<T>());
770+
771+
let components = fetch
772+
.table_components
773+
.unwrap_or_else(|| debug_checked_unreachable());
774+
components.get_batch_aligned_deref::<N, ALIGN>(table_row_start, len)
775+
}
776+
}
777+
778778
impl<T> Clone for ReadFetch<'_, T> {
779779
fn clone(&self) -> Self {
780780
Self {
@@ -815,39 +815,6 @@ pub struct WriteFetch<'w, T> {
815815
change_tick: u32,
816816
}
817817

818-
//TODO: inlines
819-
820-
impl<'__w, T: Component, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN>
821-
for &'__w mut T
822-
where
823-
T: AlignedBatchGat<N, ALIGN>,
824-
ComponentTicks: AlignedBatchGat<N, ALIGN>,
825-
{
826-
#[inline]
827-
unsafe fn table_fetch_batched_aligned<'w>(
828-
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
829-
table_row_start: usize,
830-
len: usize,
831-
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
832-
let (table_components, table_ticks) = fetch
833-
.table_components
834-
.zip(fetch.table_ticks)
835-
.unwrap_or_else(|| debug_checked_unreachable());
836-
837-
MutBatch::<T, N, ALIGN> {
838-
value: table_components.get_batch_aligned_deref_mut::<N, ALIGN>(table_row_start, len),
839-
ticks: TicksBatch {
840-
// SAFETY: [table_row_start..+batch.len()] is in range
841-
component_ticks: table_ticks
842-
.get_batch_aligned_deref_mut::<N, ALIGN>(table_row_start, len),
843-
change_tick: fetch.change_tick,
844-
last_change_tick: fetch.last_change_tick,
845-
},
846-
_marker: PhantomData,
847-
}
848-
}
849-
}
850-
851818
/// SAFETY: access of `&T` is a subset of `&mut T`
852819
unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
853820
type ReadOnly = &'__w T;
@@ -1010,6 +977,37 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
1010977
}
1011978
}
1012979

980+
impl<'__w, T: Component, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN>
981+
for &'__w mut T
982+
where
983+
T: AlignedBatchGat<N, ALIGN>,
984+
ComponentTicks: AlignedBatchGat<N, ALIGN>,
985+
{
986+
#[inline]
987+
unsafe fn table_fetch_batched_aligned<'w>(
988+
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
989+
table_row_start: usize,
990+
len: usize,
991+
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
992+
let (table_components, table_ticks) = fetch
993+
.table_components
994+
.zip(fetch.table_ticks)
995+
.unwrap_or_else(|| debug_checked_unreachable());
996+
997+
MutBatch::<T, N, ALIGN> {
998+
value: table_components.get_batch_aligned_deref_mut::<N, ALIGN>(table_row_start, len),
999+
ticks: TicksBatch {
1000+
// SAFETY: [table_row_start..+batch.len()] is in range
1001+
component_ticks: table_ticks
1002+
.get_batch_aligned_deref_mut::<N, ALIGN>(table_row_start, len),
1003+
change_tick: fetch.change_tick,
1004+
last_change_tick: fetch.last_change_tick,
1005+
},
1006+
_marker: PhantomData,
1007+
}
1008+
}
1009+
}
1010+
10131011
impl<T> Clone for WriteFetch<'_, T> {
10141012
fn clone(&self) -> Self {
10151013
Self {
@@ -1055,28 +1053,6 @@ where
10551053
}
10561054
}
10571055

1058-
impl<T: WorldQuery, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for Option<T>
1059-
where
1060-
T: WorldQueryBatch<N, ALIGN>,
1061-
{
1062-
#[inline]
1063-
unsafe fn table_fetch_batched_aligned<'w>(
1064-
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
1065-
table_row_start: usize,
1066-
len: usize,
1067-
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
1068-
if fetch.matches {
1069-
Some(T::table_fetch_batched_aligned(
1070-
&mut fetch.fetch,
1071-
table_row_start,
1072-
len,
1073-
))
1074-
} else {
1075-
None
1076-
}
1077-
}
1078-
}
1079-
10801056
// SAFETY: defers to soundness of `T: WorldQuery` impl
10811057
unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
10821058
type ReadOnly = Option<T::ReadOnly>;
@@ -1179,6 +1155,28 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
11791155
}
11801156
}
11811157

1158+
impl<T: WorldQuery, const N: usize, const ALIGN: usize> WorldQueryBatch<N, ALIGN> for Option<T>
1159+
where
1160+
T: WorldQueryBatch<N, ALIGN>,
1161+
{
1162+
#[inline]
1163+
unsafe fn table_fetch_batched_aligned<'w>(
1164+
fetch: &mut <Self as WorldQueryGats<'w>>::Fetch,
1165+
table_row_start: usize,
1166+
len: usize,
1167+
) -> <Self as WorldQueryBatchGats<'w, N, ALIGN>>::FullBatch {
1168+
if fetch.matches {
1169+
Some(T::table_fetch_batched_aligned(
1170+
&mut fetch.fetch,
1171+
table_row_start,
1172+
len,
1173+
))
1174+
} else {
1175+
None
1176+
}
1177+
}
1178+
}
1179+
11821180
/// SAFETY: [`OptionFetch`] is read only because `T` is read only
11831181
unsafe impl<T: ReadOnlyWorldQuery> ReadOnlyWorldQuery for Option<T> {}
11841182

@@ -1259,6 +1257,8 @@ impl<T: Component> ChangeTrackers<T> {
12591257
}
12601258
}
12611259

1260+
/// A batch of [`ChangeTrackers`]. This is used when performing queries with Change Trackers using the
1261+
/// [`Query::for_each_mut_batched`](crate::system::Query::for_each_mut_batched) and [`Query::for_each_batched`](crate::system::Query::for_each_batched) functions.
12621262
#[derive(Clone)]
12631263
pub struct ChangeTrackersBatch<'a, T, const N: usize, const ALIGN: usize>
12641264
where
@@ -1275,6 +1275,7 @@ where
12751275
ComponentTicks: AlignedBatchGat<N, ALIGN>,
12761276
{
12771277
/// Returns true if this component has been added since the last execution of this system.
1278+
#[inline]
12781279
pub fn is_added(&self) -> bool {
12791280
self.component_ticks
12801281
.as_array()
@@ -1283,6 +1284,7 @@ where
12831284
}
12841285

12851286
/// Returns true if this component has been changed since the last execution of this system.
1287+
#[inline]
12861288
pub fn is_changed(&self) -> bool {
12871289
self.component_ticks
12881290
.as_array()
@@ -1615,15 +1617,6 @@ macro_rules! impl_tuple_fetch {
16151617
}
16161618
}
16171619

1618-
/// SAFETY: each item in the tuple is read only
1619-
unsafe impl<$($name: ReadOnlyWorldQuery),*> ReadOnlyWorldQuery for ($($name,)*) {}
1620-
1621-
#[allow(unused_variables)]
1622-
#[allow(non_snake_case)]
1623-
impl<'w, const N: usize, const ALIGN: usize, $($name: WorldQueryBatchGats<'w, N, ALIGN>),*> WorldQueryBatchGats<'w, N, ALIGN> for ($($name,)*) {
1624-
type FullBatch = ($($name::FullBatch,)*);
1625-
}
1626-
16271620
#[allow(unused_variables)]
16281621
#[allow(non_snake_case)]
16291622
#[allow(clippy::unused_unit)]
@@ -1641,6 +1634,16 @@ macro_rules! impl_tuple_fetch {
16411634
($($name::table_fetch_batched_aligned($name, _table_row_start, _len),)*)
16421635
}
16431636
}
1637+
1638+
/// SAFETY: each item in the tuple is read only
1639+
unsafe impl<$($name: ReadOnlyWorldQuery),*> ReadOnlyWorldQuery for ($($name,)*) {}
1640+
1641+
#[allow(unused_variables)]
1642+
#[allow(non_snake_case)]
1643+
impl<'w, const N: usize, const ALIGN: usize, $($name: WorldQueryBatchGats<'w, N, ALIGN>),*> WorldQueryBatchGats<'w, N, ALIGN> for ($($name,)*) {
1644+
type FullBatch = ($($name::FullBatch,)*);
1645+
}
1646+
16441647
};
16451648
}
16461649

0 commit comments

Comments
 (0)