wde_renderer/core/
extract_macros.rs1use bevy::{
5 ecs::{
6 change_detection::Tick,
7 system::{ReadOnlySystemParam, SystemMeta, SystemParam, SystemParamItem, SystemState},
8 world::unsafe_world_cell::UnsafeWorldCell
9 },
10 prelude::*
11};
12use std::ops::{Deref, DerefMut};
13
14use super::MainWorld;
15
16pub struct ExtractWorld<'w, 's, P>
27where
28 P: ReadOnlySystemParam + 'static
29{
30 item: SystemParamItem<'w, 's, P>
31}
32pub struct ExtractState<P: SystemParam + 'static> {
33 state: SystemState<P>,
34 main_world_state: <Res<'static, MainWorld> as SystemParam>::State
35}
36unsafe impl<P> ReadOnlySystemParam for ExtractWorld<'_, '_, P> where P: ReadOnlySystemParam {}
38unsafe impl<P> SystemParam for ExtractWorld<'_, '_, P>
41where
42 P: ReadOnlySystemParam
43{
44 type State = ExtractState<P>;
45 type Item<'w, 's> = ExtractWorld<'w, 's, P>;
46
47 fn init_access(
48 state: &Self::State,
49 system_meta: &mut SystemMeta,
50 component_access_set: &mut bevy::ecs::query::FilteredAccessSet,
51 world: &mut World
52 ) {
53 Res::<MainWorld>::init_access(
55 &state.main_world_state,
56 system_meta,
57 component_access_set,
58 world
59 );
60 }
61
62 fn init_state(world: &mut World) -> Self::State {
63 let mut main_world = world.resource_mut::<MainWorld>();
64 ExtractState {
65 state: SystemState::new(&mut main_world),
66 main_world_state: Res::<MainWorld>::init_state(world)
67 }
68 }
69
70 unsafe fn get_param<'w, 's>(
71 state: &'s mut Self::State,
72 system_meta: &SystemMeta,
73 world: UnsafeWorldCell<'w>,
74 change_tick: Tick
75 ) -> Self::Item<'w, 's> {
76 let main_world = unsafe {
80 Res::<MainWorld>::get_param(
81 &mut state.main_world_state,
82 system_meta,
83 world,
84 change_tick
85 )
86 };
87 let item = state.state.get(main_world.into_inner());
88 ExtractWorld { item }
89 }
90}
91impl<'w, 's, P> Deref for ExtractWorld<'w, 's, P>
92where
93 P: ReadOnlySystemParam
94{
95 type Target = SystemParamItem<'w, 's, P>;
96 #[inline]
97 fn deref(&self) -> &Self::Target {
98 &self.item
99 }
100}
101impl<P> DerefMut for ExtractWorld<'_, '_, P>
102where
103 P: ReadOnlySystemParam
104{
105 #[inline]
106 fn deref_mut(&mut self) -> &mut Self::Target {
107 &mut self.item
108 }
109}
110impl<'a, 'w, 's, P> IntoIterator for &'a ExtractWorld<'w, 's, P>
111where
112 P: ReadOnlySystemParam,
113 &'a SystemParamItem<'w, 's, P>: IntoIterator
114{
115 type Item = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::Item;
116 type IntoIter = <&'a SystemParamItem<'w, 's, P> as IntoIterator>::IntoIter;
117 fn into_iter(self) -> Self::IntoIter {
118 (&self.item).into_iter()
119 }
120}