From 178b3078dd22f5447ab6620b3a45ca8ab80eeb0b Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Sun, 17 Dec 2023 21:30:08 -0800 Subject: [PATCH 1/4] add a generic over SystemParam --- examples/ecs/generic_system.rs | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/examples/ecs/generic_system.rs b/examples/ecs/generic_system.rs index dbd5b63432..7f69ac75f0 100644 --- a/examples/ecs/generic_system.rs +++ b/examples/ecs/generic_system.rs @@ -10,6 +10,7 @@ //! or use bevy::prelude::*; +use system_with_generic_system_param::*; #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, States)] enum AppState { @@ -40,6 +41,8 @@ fn main() { ( print_text_system, transition_to_in_game_system.run_if(in_state(AppState::MainMenu)), + system_with_generic::>, + system_with_generic::>, ), ) // Cleanup systems. @@ -87,3 +90,63 @@ fn cleanup_system(mut commands: Commands, query: Query { + fn calculate_something(&mut self) { + // dbg!(self.0); + self.0 = 5; + } + } + + #[derive(Resource)] + pub struct ResourceB(pub usize); + impl MyTrait for ResMut<'_, ResourceB> { + fn calculate_something(&mut self) { + // dbg!(self.0); + self.0 = 10; + } + } + + pub fn system_with_generic(mut param: SystemParamItem) + where + for<'w, 's> S::Item<'w, 's>: MyTrait, + { + param.calculate_something(); + } +} + +mod system_param_in_associated_type { + use super::*; + use bevy::ecs::system::{lifetimeless::SRes, StaticSystemParam, SystemParam, SystemParamItem}; + + #[derive(Resource)] + struct ResourceA; + + pub trait MyTrait { + type Param: SystemParam + 'static; + + fn do_something(&self, param: &mut SystemParamItem) -> u32; + } + + struct ItemA; + impl MyTrait for ItemA { + type Param = SRes; + + fn do_something(&self, param: &mut SystemParamItem) -> u32 { + todo!() + } + } + + fn system(param: StaticSystemParam<::Param>) {} +} From c21e4063167d45250e5cdabad6da18abd49b9fd9 Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Sun, 11 Feb 2024 11:29:51 -0800 Subject: [PATCH 2/4] clean up associated type --- examples/ecs/generic_system.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/ecs/generic_system.rs b/examples/ecs/generic_system.rs index 7f69ac75f0..f55bc440ce 100644 --- a/examples/ecs/generic_system.rs +++ b/examples/ecs/generic_system.rs @@ -10,6 +10,7 @@ //! or use bevy::prelude::*; +use system_param_in_associated_type::*; use system_with_generic_system_param::*; #[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, States)] @@ -43,6 +44,7 @@ fn main() { transition_to_in_game_system.run_if(in_state(AppState::MainMenu)), system_with_generic::>, system_with_generic::>, + system::, ), ) // Cleanup systems. @@ -64,6 +66,10 @@ fn setup_system(mut commands: Commands) { TextToPrint("I will always print".to_string()), LevelUnload, )); + + commands.insert_resource(ResourceA(1)); + commands.insert_resource(ResourceB(2)); + commands.insert_resource(ResourceC { data: 3 }); } fn print_text_system(time: Res