From 178b3078dd22f5447ab6620b3a45ca8ab80eeb0b Mon Sep 17 00:00:00 2001 From: Mike Hsu Date: Sun, 17 Dec 2023 21:30:08 -0800 Subject: [PATCH] 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>) {} +}