Skip to content
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
16 changes: 8 additions & 8 deletions benches/benches/bevy_ecs/world/world_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub fn world_query_get(criterion: &mut Criterion) {

for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();

bencher.iter(|| {
Expand All @@ -112,7 +112,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
});
});
group.bench_function(format!("{}_entities_table_wide", entity_count), |bencher| {
let mut world = setup_wide::<(
let world = setup_wide::<(
WideTable<0>,
WideTable<1>,
WideTable<2>,
Expand All @@ -137,7 +137,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
});
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();

bencher.iter(|| {
Expand All @@ -150,7 +150,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
group.bench_function(
format!("{}_entities_sparse_wide", entity_count),
|bencher| {
let mut world = setup_wide::<(
let world = setup_wide::<(
WideSparse<0>,
WideSparse<1>,
WideSparse<2>,
Expand Down Expand Up @@ -187,7 +187,7 @@ pub fn world_query_iter(criterion: &mut Criterion) {

for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();

bencher.iter(|| {
Expand All @@ -201,7 +201,7 @@ pub fn world_query_iter(criterion: &mut Criterion) {
});
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();

bencher.iter(|| {
Expand All @@ -226,7 +226,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {

for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();

bencher.iter(|| {
Expand All @@ -240,7 +240,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {
});
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();

bencher.iter(|| {
Expand Down
23 changes: 7 additions & 16 deletions crates/bevy_asset/src/asset_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//! and triggers whenever the handle or the underlying asset changes.

use crate::{AsAssetId, Asset, AssetId};
use bevy_ecs::component::Components;
use bevy_ecs::{
archetype::Archetype,
component::{ComponentId, Tick},
Expand Down Expand Up @@ -174,8 +173,8 @@ unsafe impl<A: AsAssetId> WorldQuery for AssetChanged<A> {
.map(|ptr| ptr.deref::<AssetChanges<A::Asset>>())
}) else {
error!(
"AssetChanges<{ty}> resource was removed, please do not remove \
AssetChanges<{ty}> when using the AssetChanged<{ty}> world query",
"AssetChanges<{ty}> is not present, please ensure \
AssetChanges<{ty}> is added and not removed when using the AssetChanged<{ty}> world query",
ty = ShortName::of::<A>()
);

Expand Down Expand Up @@ -231,26 +230,18 @@ unsafe impl<A: AsAssetId> WorldQuery for AssetChanged<A> {
access.add_resource_read(state.resource_id);
}

fn init_state(world: &mut World) -> AssetChangedState<A> {
let resource_id = world.init_resource::<AssetChanges<A::Asset>>();
let asset_id = world.register_component::<A>();
fn init_state(world: &World) -> AssetChangedState<A> {
let resource_id = world
.components_queue()
.queue_register_resource::<AssetChanges<A::Asset>>();
let asset_id = world.components_queue().queue_register_component::<A>();
AssetChangedState {
asset_id,
resource_id,
_asset: PhantomData,
}
}

fn get_state(components: &Components) -> Option<Self::State> {
let resource_id = components.resource_id::<AssetChanges<A::Asset>>()?;
let asset_id = components.component_id::<A>()?;
Some(AssetChangedState {
asset_id,
resource_id,
_asset: PhantomData,
})
}

fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
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 @@ -141,8 +141,8 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
}

fn get_component_ids(
components: &#ecs_path::component::Components,
ids: &mut impl FnMut(Option<#ecs_path::component::ComponentId>)
components: #ecs_path::component::ComponentsQueuedRegistrator,
ids: &mut impl FnMut(#ecs_path::component::ComponentId)
){
#(#field_get_component_ids)*
}
Expand Down
8 changes: 1 addition & 7 deletions crates/bevy_ecs/macros/src/world_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,12 @@ pub(crate) fn world_query_impl(
#( <#field_types>::update_component_access(&state.#named_field_idents, _access); )*
}

fn init_state(world: &mut #path::world::World) -> #state_struct_name #user_ty_generics {
fn init_state(world: &#path::world::World) -> #state_struct_name #user_ty_generics {
#state_struct_name {
#(#named_field_idents: <#field_types>::init_state(world),)*
}
}

fn get_state(components: &#path::component::Components) -> Option<#state_struct_name #user_ty_generics> {
Some(#state_struct_name {
#(#named_field_idents: <#field_types>::get_state(components)?,)*
})
}

fn matches_component_set(state: &Self::State, _set_contains_id: &impl Fn(#path::component::ComponentId) -> bool) -> bool {
true #(&& <#field_types>::matches_component_set(&state.#named_field_idents, _set_contains_id))*
}
Expand Down
23 changes: 16 additions & 7 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::{
},
change_detection::MaybeLocation,
component::{
Component, ComponentId, Components, ComponentsRegistrator, RequiredComponentConstructor,
RequiredComponents, StorageType, Tick,
Component, ComponentId, Components, ComponentsQueuedRegistrator, ComponentsRegistrator,
RequiredComponentConstructor, RequiredComponents, StorageType, Tick,
},
entity::{Entities, Entity, EntityLocation},
observer::Observers,
Expand Down Expand Up @@ -153,8 +153,11 @@ pub unsafe trait Bundle: DynamicBundle + Send + Sync + 'static {
#[doc(hidden)]
fn component_ids(components: &mut ComponentsRegistrator, ids: &mut impl FnMut(ComponentId));

/// Gets this [`Bundle`]'s component ids. This will be [`None`] if the component has not been registered.
fn get_component_ids(components: &Components, ids: &mut impl FnMut(Option<ComponentId>));
/// Gets this [`Bundle`]'s component ids.
fn get_component_ids(
components: ComponentsQueuedRegistrator,
ids: &mut impl FnMut(ComponentId),
);

/// Registers components that are required by the components in this [`Bundle`].
fn register_required_components(
Expand Down Expand Up @@ -241,8 +244,11 @@ unsafe impl<C: Component> Bundle for C {
);
}

fn get_component_ids(components: &Components, ids: &mut impl FnMut(Option<ComponentId>)) {
ids(components.get_id(TypeId::of::<C>()));
fn get_component_ids(
components: ComponentsQueuedRegistrator,
ids: &mut impl FnMut(ComponentId),
) {
ids(components.queue_register_component::<C>());
}
}

Expand Down Expand Up @@ -292,7 +298,10 @@ macro_rules! tuple_impl {
$(<$name as Bundle>::component_ids(components, ids);)*
}

fn get_component_ids(components: &Components, ids: &mut impl FnMut(Option<ComponentId>)){
fn get_component_ids(
components: ComponentsQueuedRegistrator,
ids: &mut impl FnMut(ComponentId),
){
$(<$name as Bundle>::get_component_ids(components, ids);)*
}

Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,7 @@ impl ComponentIds {
///
/// As a rule of thumb, if you have mutable access to [`ComponentsRegistrator`], prefer to use that instead.
/// Use this only if you need to know the id of a component but do not need to modify the contents of the world based on that id.
#[derive(Clone, Copy)]
pub struct ComponentsQueuedRegistrator<'w> {
components: &'w Components,
ids: &'w ComponentIds,
Expand Down Expand Up @@ -1388,6 +1389,8 @@ impl<'w> ComponentsQueuedRegistrator<'w> {
/// This will reserve an id and queue the registration.
/// These registrations will be carried out at the next opportunity.
///
/// If this is already registered or queued to be registered, this will return its existing id.
///
/// # Note
///
/// Technically speaking, the returned [`ComponentId`] is not valid, but it will become valid later.
Expand Down Expand Up @@ -1433,6 +1436,8 @@ impl<'w> ComponentsQueuedRegistrator<'w> {
/// This will reserve an id and queue the registration.
/// These registrations will be carried out at the next opportunity.
///
/// If this is already registered or queued to be registered, this will return its existing id.
///
/// # Note
///
/// Technically speaking, the returned [`ComponentId`] is not valid, but it will become valid later.
Expand Down Expand Up @@ -1461,6 +1466,8 @@ impl<'w> ComponentsQueuedRegistrator<'w> {
/// This will reserve an id and queue the registration.
/// These registrations will be carried out at the next opportunity.
///
/// If this is already registered or queued to be registered, this will return its existing id.
///
/// # Note
///
/// Technically speaking, the returned [`ComponentId`] is not valid, but it will become valid later.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/entity/entity_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ mod tests {
fn preserving_uniqueness() {
let mut world = World::new();

let mut query = QueryState::<&mut Thing>::new(&mut world);
let mut query = QueryState::<&mut Thing>::new(&world);

let spawn_batch: Vec<Entity> = world.spawn_batch(vec![Thing; 1000]).collect();

Expand Down
24 changes: 12 additions & 12 deletions crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,62 +1451,62 @@ mod tests {
#[test]
#[should_panic]
fn ref_and_mut_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(&A, &mut A)>();
}

#[test]
#[should_panic]
fn entity_ref_and_mut_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(EntityRef, &mut A)>();
}

#[test]
#[should_panic]
fn mut_and_ref_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(&mut A, &A)>();
}

#[test]
#[should_panic]
fn mut_and_entity_ref_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(&mut A, EntityRef)>();
}

#[test]
#[should_panic]
fn entity_ref_and_entity_mut_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(EntityRef, EntityMut)>();
}

#[test]
#[should_panic]
fn entity_mut_and_entity_mut_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(EntityMut, EntityMut)>();
}

#[test]
fn entity_ref_and_entity_ref_query_no_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(EntityRef, EntityRef)>();
}

#[test]
#[should_panic]
fn mut_and_mut_query_panic() {
let mut world = World::new();
let world = World::new();
world.query::<(&mut A, &mut A)>();
}

#[test]
#[should_panic]
fn multiple_worlds_same_query_iter() {
let mut world_a = World::new();
let world_a = World::new();
let world_b = World::new();
let mut query = world_a.query::<&A>();
query.iter(&world_a);
Expand All @@ -1515,7 +1515,7 @@ mod tests {

#[test]
fn query_filters_dont_collide_with_fetches() {
let mut world = World::new();
let world = World::new();
world.query_filtered::<&mut A, Changed<A>>();
}

Expand All @@ -1540,7 +1540,7 @@ mod tests {
#[test]
#[should_panic]
fn multiple_worlds_same_query_get() {
let mut world_a = World::new();
let world_a = World::new();
let world_b = World::new();
let mut query = world_a.query::<&A>();
let _ = query.get(&world_a, Entity::from_raw(0));
Expand All @@ -1550,7 +1550,7 @@ mod tests {
#[test]
#[should_panic]
fn multiple_worlds_same_query_for_each() {
let mut world_a = World::new();
let world_a = World::new();
let world_b = World::new();
let mut query = world_a.query::<&A>();
query.iter(&world_a).for_each(|_| {});
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/query/access.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::component::ComponentId;
use crate::component::{ComponentId, ComponentInfo};
use crate::storage::SparseSetIndex;
use crate::world::World;
use alloc::{format, string::String, vec, vec::Vec};
Expand Down Expand Up @@ -971,8 +971,8 @@ impl AccessConflicts {
world
.components
.get_info(ComponentId::get_sparse_set_index(index))
.unwrap()
.name()
.map(ComponentInfo::name)
.unwrap_or("unknown component")
)
)
})
Expand Down
Loading
Loading