Skip to content

Commit

Permalink
use UnsafeWorldCell for QueryState/Query
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed Jan 27, 2023
1 parent d50be4e commit 8abb3cf
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 96 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_ecs/macros/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub fn derive_world_query_impl(ast: DeriveInput) -> TokenStream {
}

unsafe fn init_fetch<'__w>(
_world: &'__w #path::world::World,
_world: #path::world::unsafe_world_cell::UnsafeWorldCell<'__w>,
state: &Self::State,
_last_change_tick: u32,
_change_tick: u32
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_ecs/src/query/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
entity::Entity,
query::{Access, DebugCheckedUnwrap, FilteredAccess},
storage::{ComponentSparseSet, Table, TableRow},
world::{Mut, Ref, World},
world::{unsafe_world_cell::UnsafeWorldCell, Mut, Ref, World},
};
use bevy_ecs_macros::all_tuples;
pub use bevy_ecs_macros::WorldQuery;
Expand Down Expand Up @@ -331,7 +331,7 @@ pub unsafe trait WorldQuery {
/// `state` must have been initialized (via [`WorldQuery::init_state`]) using the same `world` passed
/// in to this function.
unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
state: &Self::State,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -462,7 +462,7 @@ unsafe impl WorldQuery for Entity {
const IS_ARCHETYPAL: bool = true;

unsafe fn init_fetch<'w>(
_world: &'w World,
_world: UnsafeWorldCell<'w>,
_state: &Self::State,
_last_change_tick: u32,
_change_tick: u32,
Expand Down Expand Up @@ -544,7 +544,7 @@ unsafe impl<T: Component> WorldQuery for &T {
const IS_ARCHETYPAL: bool = true;

unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
&component_id: &ComponentId,
_last_change_tick: u32,
_change_tick: u32,
Expand Down Expand Up @@ -689,7 +689,7 @@ unsafe impl<'__w, T: Component> WorldQuery for Ref<'__w, T> {
const IS_ARCHETYPAL: bool = true;

unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
&component_id: &ComponentId,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -850,7 +850,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
const IS_ARCHETYPAL: bool = true;

unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
&component_id: &ComponentId,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -998,7 +998,7 @@ unsafe impl<T: WorldQuery> WorldQuery for Option<T> {
const IS_ARCHETYPAL: bool = T::IS_ARCHETYPAL;

unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
state: &T::State,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -1192,7 +1192,7 @@ unsafe impl<T: Component> WorldQuery for ChangeTrackers<T> {
const IS_ARCHETYPAL: bool = true;

unsafe fn init_fetch<'w>(
world: &'w World,
world: UnsafeWorldCell<'w>,
&component_id: &ComponentId,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -1339,7 +1339,7 @@ macro_rules! impl_tuple_fetch {
}

#[allow(clippy::unused_unit)]
unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> {
unsafe fn init_fetch<'w>(_world: UnsafeWorldCell<'w>, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> {
let ($($name,)*) = state;
($($name::init_fetch(_world, $name, _last_change_tick, _change_tick),)*)
}
Expand Down Expand Up @@ -1448,7 +1448,7 @@ macro_rules! impl_anytuple_fetch {
}

#[allow(clippy::unused_unit)]
unsafe fn init_fetch<'w>(_world: &'w World, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> {
unsafe fn init_fetch<'w>(_world: UnsafeWorldCell<'w>, state: &Self::State, _last_change_tick: u32, _change_tick: u32) -> Self::Fetch<'w> {
let ($($name,)*) = state;
($(($name::init_fetch(_world, $name, _last_change_tick, _change_tick), false),)*)
}
Expand Down Expand Up @@ -1587,7 +1587,7 @@ unsafe impl<Q: WorldQuery> WorldQuery for NopWorldQuery<Q> {

#[inline(always)]
unsafe fn init_fetch(
_world: &World,
_world: UnsafeWorldCell<'_>,
_state: &Q::State,
_last_change_tick: u32,
_change_tick: u32,
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
entity::Entity,
query::{Access, DebugCheckedUnwrap, FilteredAccess, WorldQuery},
storage::{Column, ComponentSparseSet, Table, TableRow},
world::World,
world::{unsafe_world_cell::UnsafeWorldCell, World},
};
use bevy_ecs_macros::all_tuples;
use bevy_ptr::{ThinSlicePtr, UnsafeCellDeref};
Expand Down Expand Up @@ -51,7 +51,7 @@ unsafe impl<T: Component> WorldQuery for With<T> {
fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {}

unsafe fn init_fetch(
_world: &World,
_world: UnsafeWorldCell<'_>,
_state: &ComponentId,
_last_change_tick: u32,
_change_tick: u32,
Expand Down Expand Up @@ -153,7 +153,7 @@ unsafe impl<T: Component> WorldQuery for Without<T> {
fn shrink<'wlong: 'wshort, 'wshort>(_: Self::Item<'wlong>) -> Self::Item<'wshort> {}

unsafe fn init_fetch(
_world: &World,
_world: UnsafeWorldCell<'_>,
_state: &ComponentId,
_last_change_tick: u32,
_change_tick: u32,
Expand Down Expand Up @@ -277,7 +277,7 @@ macro_rules! impl_query_filter_tuple {

const IS_ARCHETYPAL: bool = true $(&& $filter::IS_ARCHETYPAL)*;

unsafe fn init_fetch<'w>(world: &'w World, state: &Self::State, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> {
unsafe fn init_fetch<'w>(world: UnsafeWorldCell<'w>, state: &Self::State, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> {
let ($($filter,)*) = state;
($(OrFetch {
fetch: $filter::init_fetch(world, $filter, last_change_tick, change_tick),
Expand Down Expand Up @@ -432,7 +432,7 @@ macro_rules! impl_tick_filter {
item
}

unsafe fn init_fetch<'w>(world: &'w World, &id: &ComponentId, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> {
unsafe fn init_fetch<'w>(world: UnsafeWorldCell<'w>, &id: &ComponentId, last_change_tick: u32, change_tick: u32) -> Self::Fetch<'w> {
Self::Fetch::<'w> {
table_ticks: None,
sparse_set: (T::Storage::STORAGE_TYPE == StorageType::SparseSet)
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_ecs/src/query/iter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::{
archetype::{ArchetypeEntity, ArchetypeId, Archetypes},
entity::{Entities, Entity},
prelude::World,
query::{ArchetypeFilter, DebugCheckedUnwrap, QueryState, WorldQuery},
storage::{TableId, TableRow, Tables},
world::unsafe_world_cell::UnsafeWorldCell,
};
use std::{borrow::Borrow, iter::FusedIterator, marker::PhantomData, mem::MaybeUninit};

Expand All @@ -27,15 +27,15 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIter<'w, 's, Q, F> {
/// This does not validate that `world.id()` matches `query_state.world_id`. Calling this on a `world`
/// with a mismatched [`WorldId`](crate::world::WorldId) is unsound.
pub(crate) unsafe fn new(
world: &'w World,
world: UnsafeWorldCell<'w>,
query_state: &'s QueryState<Q, F>,
last_change_tick: u32,
change_tick: u32,
) -> Self {
QueryIter {
query_state,
tables: &world.storages().tables,
archetypes: &world.archetypes,
archetypes: world.archetypes(),
cursor: QueryIterationCursor::init(world, query_state, last_change_tick, change_tick),
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ where
/// This does not validate that `world.id()` matches `query_state.world_id`. Calling this on a `world`
/// with a mismatched [`WorldId`](crate::world::WorldId) is unsound.
pub(crate) unsafe fn new<EntityList: IntoIterator<IntoIter = I>>(
world: &'w World,
world: UnsafeWorldCell<'w>,
query_state: &'s QueryState<Q, F>,
entity_list: EntityList,
last_change_tick: u32,
Expand All @@ -115,9 +115,9 @@ where
);
QueryManyIter {
query_state,
entities: &world.entities,
archetypes: &world.archetypes,
tables: &world.storages.tables,
entities: world.entities(),
archetypes: world.archetypes(),
tables: &world.storages().tables,
fetch,
filter,
entity_iter: entity_list.into_iter(),
Expand Down Expand Up @@ -296,7 +296,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize>
/// This does not validate that `world.id()` matches `query_state.world_id`. Calling this on a
/// `world` with a mismatched [`WorldId`](crate::world::WorldId) is unsound.
pub(crate) unsafe fn new(
world: &'w World,
world: UnsafeWorldCell<'w>,
query_state: &'s QueryState<Q, F>,
last_change_tick: u32,
change_tick: u32,
Expand Down Expand Up @@ -328,7 +328,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery, const K: usize>
QueryCombinationIter {
query_state,
tables: &world.storages().tables,
archetypes: &world.archetypes,
archetypes: world.archetypes(),
cursors: array.assume_init(),
}
}
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, 's,
const IS_DENSE: bool = Q::IS_DENSE && F::IS_DENSE;

unsafe fn init_empty(
world: &'w World,
world: UnsafeWorldCell<'w>,
query_state: &'s QueryState<Q, F>,
last_change_tick: u32,
change_tick: u32,
Expand All @@ -507,7 +507,7 @@ impl<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> QueryIterationCursor<'w, 's,
}

unsafe fn init(
world: &'w World,
world: UnsafeWorldCell<'w>,
query_state: &'s QueryState<Q, F>,
last_change_tick: u32,
change_tick: u32,
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/query/par_iter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::world::World;
use crate::world::unsafe_world_cell::UnsafeWorldCell;
use bevy_tasks::ComputeTaskPool;
use std::ops::Range;

Expand Down Expand Up @@ -79,7 +79,7 @@ impl BatchingStrategy {
/// This struct is created by the [`Query::par_iter`](crate::system::Query::iter) and
/// [`Query::par_iter_mut`](crate::system::Query::iter_mut) methods.
pub struct QueryParIter<'w, 's, Q: WorldQuery, F: ReadOnlyWorldQuery> {
pub(crate) world: &'w World,
pub(crate) world: UnsafeWorldCell<'w>,
pub(crate) state: &'s QueryState<Q, F>,
pub(crate) batching_strategy: BatchingStrategy,
}
Expand Down
Loading

0 comments on commit 8abb3cf

Please sign in to comment.