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

[Merged by Bors] - Remove the SystemParamState trait and remove types like ResState #6919

Closed
wants to merge 56 commits into from
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
d38a4db
simplify `Extract` bounds
JoJoJet Dec 7, 2022
5913ea0
flatten `SystemParam`
JoJoJet Dec 7, 2022
d24d2a9
Add a test case for ignored/marker type params
JoJoJet Dec 7, 2022
c9d45d6
impl `SystemParam` for `PhantomData`
JoJoJet Dec 7, 2022
e6a21d0
add a marker param
JoJoJet Dec 7, 2022
1928bf6
remove trivial `State` types
JoJoJet Dec 7, 2022
2e3b75f
don't generate state struct in the `SystemParam` derive
JoJoJet Dec 7, 2022
d6a81dc
remove `ParamSetState`
JoJoJet Dec 7, 2022
2ffed09
reduce macro churn
JoJoJet Dec 10, 2022
d6d619a
undelete a safety comment
JoJoJet Dec 10, 2022
f58cb66
simplify `SystemParam` derive
JoJoJet Dec 10, 2022
5939101
Revert "impl `SystemParam` for `PhantomData`"
JoJoJet Dec 10, 2022
31d07a9
simplify `ExclusiveSystemParam`
JoJoJet Dec 10, 2022
4d31780
yeet `LocalState`
JoJoJet Dec 10, 2022
4492780
revert a `StaticSystemParam` bound
JoJoJet Dec 10, 2022
5d1223d
remove a useless bound from `ParamSet`
JoJoJet Dec 10, 2022
f18bc7e
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Dec 12, 2022
2c75ad4
add docs to `SystemParam::Item`
JoJoJet Dec 12, 2022
c166192
fix safety comment rebase error
JoJoJet Dec 12, 2022
21e62a4
use elided lifetimes
JoJoJet Dec 12, 2022
65a949f
fix a doc link
JoJoJet Dec 12, 2022
0c18690
churn bad
JoJoJet Dec 12, 2022
2789f28
be more specific about a type
JoJoJet Dec 12, 2022
87b696b
simplify `ParamSet::Item`
JoJoJet Dec 12, 2022
bd7a0c2
remove doc references to `SystemParamState`
JoJoJet Dec 12, 2022
b3eb785
fix outdated comments
JoJoJet Dec 12, 2022
7418497
be more specific about a safety invariant
JoJoJet Dec 12, 2022
8325859
add docs to `SystemParam::State`
JoJoJet Dec 12, 2022
db60b72
fix meaningless safety invariants
JoJoJet Dec 12, 2022
17372f6
document `SystemParam::apply`
JoJoJet Dec 12, 2022
fe01fb1
add docs to `new_archetype`
JoJoJet Dec 12, 2022
5d0c3b4
add docs to `init`
JoJoJet Dec 12, 2022
ab256a5
add docs to `SystemParamItem`
JoJoJet Dec 14, 2022
23204dc
remove another trivial state type
JoJoJet Dec 14, 2022
be4b98b
move docs above an attribute
JoJoJet Dec 14, 2022
7edd6dc
yeet `SystemNameState`
JoJoJet Dec 14, 2022
fbd94f1
remove unnecessary fully-qualified type syntax
JoJoJet Dec 16, 2022
fa56c5e
`init` -> `init_state`
JoJoJet Dec 16, 2022
5d871df
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Dec 16, 2022
7682ae2
fix doc links
JoJoJet Dec 16, 2022
5f59bf3
..
JoJoJet Dec 16, 2022
a1ce6cc
Merge branch 'main' into flatten-system-param
JoJoJet Dec 21, 2022
566f7f3
cargo fmt
JoJoJet Dec 21, 2022
58623ee
fix merge errors
JoJoJet Dec 21, 2022
b3149d0
remove more unnecessary fully qualified types
JoJoJet Dec 21, 2022
5df68ae
simplify repetion
JoJoJet Dec 21, 2022
721e5f6
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Dec 25, 2022
6980477
removed unused punctuated generics
JoJoJet Dec 25, 2022
d94919b
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Dec 27, 2022
76dea87
simplify `ParamSet::new_archetype`
JoJoJet Dec 27, 2022
a06129a
simplify more repetition
JoJoJet Dec 27, 2022
e3a2021
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Jan 5, 2023
02a7009
remove an unused variable
JoJoJet Jan 5, 2023
5e1625d
re-add a comment
JoJoJet Jan 5, 2023
9bc0b2f
Merge remote-tracking branch 'upstream/main' into flatten-system-param
JoJoJet Jan 7, 2023
550edd5
reduce extract_param churn
JoJoJet Jan 7, 2023
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
99 changes: 36 additions & 63 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
let mut tokens = TokenStream::new();
let max_params = 8;
let params = get_idents(|i| format!("P{i}"), max_params);
let params_state = get_idents(|i| format!("PF{i}"), max_params);
let metas = get_idents(|i| format!("m{i}"), max_params);
let mut param_fn_muts = Vec::new();
for (i, param) in params.iter().enumerate() {
Expand All @@ -228,44 +227,37 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
// Conflicting params in ParamSet are not accessible at the same time
// ParamSets are guaranteed to not conflict with other SystemParams
unsafe {
<#param::State as SystemParamState>::get_param(&mut self.param_states.#index, &self.system_meta, self.world, self.change_tick)
<#param as SystemParam>::get_param(&mut self.param_states.#index, &self.system_meta, self.world, self.change_tick)
}
}
});
}

for param_count in 1..=max_params {
let param = &params[0..param_count];
let param_state = &params_state[0..param_count];
let meta = &metas[0..param_count];
let param_fn_mut = &param_fn_muts[0..param_count];
tokens.extend(TokenStream::from(quote! {
impl<'w, 's, #(#param: SystemParam,)*> SystemParam for ParamSet<'w, 's, (#(#param,)*)>
{
type State = ParamSetState<(#(#param::State,)*)>;
}

// SAFETY: All parameters are constrained to ReadOnlyState, so World is only read

// SAFETY: All parameters are constrained to ReadOnlySystemParam, so World is only read
unsafe impl<'w, 's, #(#param,)*> ReadOnlySystemParam for ParamSet<'w, 's, (#(#param,)*)>
where #(#param: ReadOnlySystemParam,)*
{ }

// SAFETY: Relevant parameter ComponentId and ArchetypeComponentId access is applied to SystemMeta. If any ParamState conflicts
// with any prior access, a panic will occur.

unsafe impl<#(#param_state: SystemParamState,)*> SystemParamState for ParamSetState<(#(#param_state,)*)>
unsafe impl<'_w, '_s, #(#param: SystemParam,)*> SystemParam for ParamSet<'_w, '_s, (#(#param,)*)>
{
type Item<'w, 's> = ParamSet<'w, 's, (#(<#param_state as SystemParamState>::Item::<'w, 's>,)*)>;
type State = (#(#param::State,)*);
type Item<'w, 's> = ParamSet<'w, 's, (#(#param,)*)>;

fn init(world: &mut World, system_meta: &mut SystemMeta) -> Self {
fn init(world: &mut World, system_meta: &mut SystemMeta) -> Self::State {
#(
// Pretend to add each param to the system alone, see if it conflicts
let mut #meta = system_meta.clone();
#meta.component_access_set.clear();
#meta.archetype_component_access.clear();
#param_state::init(world, &mut #meta);
let #param = #param_state::init(world, &mut system_meta.clone());
#param::init(world, &mut #meta);
let #param = #param::init(world, &mut system_meta.clone());
)*
#(
system_meta
Expand All @@ -275,29 +267,29 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {
.archetype_component_access
.extend(&#meta.archetype_component_access);
)*
ParamSetState((#(#param,)*))
(#(#param,)*)
}

fn new_archetype(&mut self, archetype: &Archetype, system_meta: &mut SystemMeta) {
let (#(#param,)*) = &mut self.0;
fn new_archetype(state: &mut Self::State, archetype: &Archetype, system_meta: &mut SystemMeta) {
let (#(#param,)*) = state;
#(
#param.new_archetype(archetype, system_meta);
<#param as SystemParam>::new_archetype(#param, archetype, system_meta);
)*
}

fn apply(&mut self, system_meta: &SystemMeta, world: &mut World) {
self.0.apply(system_meta, world)
fn apply(state: &mut Self::State, system_meta: &SystemMeta, world: &mut World) {
<(#(#param,)*) as SystemParam>::apply(state, system_meta, world);
}

#[inline]
unsafe fn get_param<'w, 's>(
state: &'s mut Self,
state: &'s mut Self::State,
system_meta: &SystemMeta,
world: &'w World,
change_tick: u32,
) -> Self::Item<'w, 's> {
ParamSet {
param_states: &mut state.0,
param_states: state,
system_meta: system_meta.clone(),
world,
change_tick,
Expand All @@ -307,7 +299,6 @@ pub fn impl_param_set(_input: TokenStream) -> TokenStream {

impl<'w, 's, #(#param: SystemParam,)*> ParamSet<'w, 's, (#(#param,)*)>
{

#(#param_fn_mut)*
}
}));
Expand Down Expand Up @@ -401,6 +392,12 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
.filter(|g| matches!(g, GenericParam::Type(_)))
.collect();

let mut shadowed_lifetimes: Vec<_> = generics.lifetimes().map(|x| x.lifetime.clone()).collect();
for lifetime in &mut shadowed_lifetimes {
let shadowed_ident = format_ident!("_{}", lifetime.ident);
lifetime.ident = shadowed_ident;
}

let mut punctuated_generics = Punctuated::<_, Token![,]>::new();
punctuated_generics.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => GenericParam::Type(TypeParam {
Expand All @@ -427,57 +424,33 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}

let struct_name = &ast.ident;
let state_struct_visibility = &ast.vis;

TokenStream::from(quote! {
// We define the FetchState struct in an anonymous scope to avoid polluting the user namespace.
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
// The struct can still be accessed via SystemParam::State, e.g. EventReaderState can be accessed via
// <EventReader<'static, 'static, T> as SystemParam>::State
const _: () = {
impl<'w, 's, #punctuated_generics> #path::system::SystemParam for #struct_name #ty_generics #where_clause {
type State = State<'w, 's, #punctuated_generic_idents>;
}

#[doc(hidden)]
type State<'w, 's, #punctuated_generic_idents> = FetchState<
(#(<#field_types as #path::system::SystemParam>::State,)*),
#punctuated_generic_idents
>;

#[doc(hidden)]
#state_struct_visibility struct FetchState <TSystemParamState, #punctuated_generic_idents> {
state: TSystemParamState,
marker: std::marker::PhantomData<fn()->(#punctuated_generic_idents)>
}
unsafe impl<'w, 's, #punctuated_generics> #path::system::SystemParam for #struct_name #ty_generics #where_clause {
type State = <(#(#field_types,)*) as #path::system::SystemParam>::State;
type Item<'_w, '_s> = #struct_name <#(#shadowed_lifetimes,)* #punctuated_generic_idents>;

unsafe impl<'__w, '__s, #punctuated_generics> #path::system::SystemParamState for
State<'__w, '__s, #punctuated_generic_idents>
#where_clause {
type Item<'w, 's> = #struct_name #ty_generics;

fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta) -> Self {
Self {
state: #path::system::SystemParamState::init(world, system_meta),
marker: std::marker::PhantomData,
}
fn init(world: &mut #path::world::World, system_meta: &mut #path::system::SystemMeta) -> Self::State {
<(#(#field_types,)*) as #path::system::SystemParam>::init(world, system_meta)
}

fn new_archetype(&mut self, archetype: &#path::archetype::Archetype, system_meta: &mut #path::system::SystemMeta) {
self.state.new_archetype(archetype, system_meta)
fn new_archetype(state: &mut Self::State, archetype: &#path::archetype::Archetype, system_meta: &mut #path::system::SystemMeta) {
<(#(#field_types,)*) as #path::system::SystemParam>::new_archetype(state, archetype, system_meta)
}

fn apply(&mut self, system_meta: &#path::system::SystemMeta, world: &mut #path::world::World) {
self.state.apply(system_meta, world)
fn apply(state: &mut Self::State, system_meta: &#path::system::SystemMeta, world: &mut #path::world::World) {
<(#(#field_types,)*) as #path::system::SystemParam>::apply(state, system_meta, world);
}

unsafe fn get_param<'w, 's>(
state: &'s mut Self,
unsafe fn get_param<'w2, 's2>(
state: &'s2 mut Self::State,
system_meta: &#path::system::SystemMeta,
world: &'w #path::world::World,
world: &'w2 #path::world::World,
change_tick: u32,
) -> Self::Item<'w, 's> {
) -> Self::Item<'w2, 's2> {
#struct_name {
#(#fields: <<#field_types as #path::system::SystemParam>::State as #path::system::SystemParamState>::get_param(&mut state.state.#field_indices, system_meta, world, change_tick),)*
#(#fields: <#field_types as #path::system::SystemParam>::get_param(&mut state.#field_indices, system_meta, world, change_tick),)*
#(#ignored_fields: <#ignored_field_types>::default(),)*
}
}
Expand Down
21 changes: 9 additions & 12 deletions crates/bevy_ecs/src/system/commands/parallel_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use thread_local::ThreadLocal;
use crate::{
entity::Entities,
prelude::World,
system::{SystemMeta, SystemParam, SystemParamState},
system::{SystemMeta, SystemParam},
};

use super::{CommandQueue, Commands};

#[doc(hidden)]
#[derive(Default)]
/// The internal [`SystemParamState`] of the [`ParallelCommands`] type
/// The internal [`SystemParam`] state of the [`ParallelCommands`] type
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
pub struct ParallelCommandsState {
thread_local_storage: ThreadLocal<Cell<CommandQueue>>,
}
Expand Down Expand Up @@ -48,30 +48,27 @@ pub struct ParallelCommands<'w, 's> {
entities: &'w Entities,
}

impl SystemParam for ParallelCommands<'_, '_> {
type State = ParallelCommandsState;
}

// SAFETY: no component or resource access to report
unsafe impl SystemParamState for ParallelCommandsState {
unsafe impl SystemParam for ParallelCommands<'_, '_> {
type State = ParallelCommandsState;
type Item<'w, 's> = ParallelCommands<'w, 's>;

fn init(_: &mut World, _: &mut crate::system::SystemMeta) -> Self {
Self::default()
fn init(_: &mut World, _: &mut crate::system::SystemMeta) -> Self::State {
ParallelCommandsState::default()
}

fn apply(&mut self, _system_meta: &SystemMeta, world: &mut World) {
fn apply(state: &mut Self::State, _system_meta: &SystemMeta, world: &mut World) {
#[cfg(feature = "trace")]
let _system_span =
bevy_utils::tracing::info_span!("system_commands", name = _system_meta.name())
.entered();
for cq in &mut self.thread_local_storage {
for cq in &mut state.thread_local_storage {
cq.get_mut().apply(world);
}
}

unsafe fn get_param<'w, 's>(
state: &'s mut Self,
state: &'s mut Self::State,
_: &crate::system::SystemMeta,
world: &'w World,
_: u32,
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_ecs/src/system/exclusive_function_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
schedule::{SystemLabel, SystemLabelId},
system::{
check_system_change_tick, AsSystemLabel, ExclusiveSystemParam, ExclusiveSystemParamItem,
ExclusiveSystemParamState, IntoSystem, System, SystemMeta, SystemTypeIdLabel,
IntoSystem, System, SystemMeta, SystemTypeIdLabel,
},
world::{World, WorldId},
};
Expand Down Expand Up @@ -94,7 +94,7 @@ where
let saved_last_tick = world.last_change_tick;
world.last_change_tick = self.system_meta.last_change_tick;

let params = <Param as ExclusiveSystemParam>::State::get_param(
let params = Param::get_param(
self.param_state.as_mut().expect(PARAM_MESSAGE),
&self.system_meta,
);
Expand Down Expand Up @@ -122,17 +122,14 @@ where
#[inline]
fn apply_buffers(&mut self, world: &mut World) {
let param_state = self.param_state.as_mut().expect(PARAM_MESSAGE);
param_state.apply(world);
Param::apply(param_state, world);
}

#[inline]
fn initialize(&mut self, world: &mut World) {
self.world_id = Some(world.id());
self.system_meta.last_change_tick = world.change_tick().wrapping_sub(MAX_CHANGE_AGE);
self.param_state = Some(<Param::State as ExclusiveSystemParamState>::init(
world,
&mut self.system_meta,
));
self.param_state = Some(Param::init(world, &mut self.system_meta));
}

fn update_archetype_component_access(&mut self, _world: &World) {}
Expand Down
Loading