Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use UnsafeWorldCell internally #6972

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
unsafe fn get_param<'w, 's>(
state: &'s mut Self::State,
system_meta: &SystemMeta,
world: &'w World,
world: UnsafeWorldCell<'w>,
change_tick: u32,
) -> Self::Item<'w, 's> {
ParamSet {
Expand Down Expand Up @@ -498,7 +498,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
unsafe fn get_param<'w2, 's2>(
state: &'s2 mut Self::State,
system_meta: &#path::system::SystemMeta,
world: &'w2 #path::world::World,
world: #path::world::unsafe_world_cell::UnsafeWorldCell<'w2>,
change_tick: u32,
) -> Self::Item<'w2, 's2> {
let (#(#tuple_patterns,)*) = <
Expand Down
54 changes: 32 additions & 22 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 @@ -328,10 +328,11 @@ pub unsafe trait WorldQuery {
///
/// # Safety
///
/// `state` must have been initialized (via [`WorldQuery::init_state`]) using the same `world` passed
/// - `state` must have been initialized (via [`WorldQuery::init_state`]) using the same `world` passed
/// in to this function.
/// - `world` must be an `UnsafeWorldCell` with access to everything listed in `update_archetype_component_access`
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 +463,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,19 +545,23 @@ 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,
) -> ReadFetch<'w, T> {
ReadFetch {
table_components: None,
sparse_set: (T::Storage::STORAGE_TYPE == StorageType::SparseSet).then(|| {
world
.storages()
.sparse_sets
.get(component_id)
.debug_checked_unwrap()
// SAFETY: TODO
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be a non-TODO comment.

unsafe {
world
.world()
.storages()
.sparse_sets
.get(component_id)
.debug_checked_unwrap()
}
}),
}
}
Expand Down Expand Up @@ -689,19 +694,23 @@ 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,
) -> RefFetch<'w, T> {
RefFetch {
table_data: None,
sparse_set: (T::Storage::STORAGE_TYPE == StorageType::SparseSet).then(|| {
world
.storages()
.sparse_sets
.get(component_id)
.debug_checked_unwrap()
// SAFETY: world has access to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This placeholder needs to be fully written.

unsafe {
world
.world()
.storages()
.sparse_sets
.get(component_id)
.debug_checked_unwrap()
}
}),
last_change_tick,
change_tick,
Expand Down Expand Up @@ -850,7 +859,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 All @@ -859,6 +868,7 @@ unsafe impl<'__w, T: Component> WorldQuery for &'__w mut T {
table_data: None,
sparse_set: (T::Storage::STORAGE_TYPE == StorageType::SparseSet).then(|| {
world
.world()
.storages()
.sparse_sets
.get(component_id)
Expand Down Expand Up @@ -998,7 +1008,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 +1202,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 +1349,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 +1458,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 +1597,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