Skip to content

Commit 36e9b12

Browse files
committed
Finish API
Cleverly avoid needing the param to be 'static This same trick can't work for `StaticSystemParam` fwiw This is because `StaticSystemParam`'s item needs to exactly reference the original param, whereas this just stores it
1 parent fbb794d commit 36e9b12

File tree

2 files changed

+19
-64
lines changed

2 files changed

+19
-64
lines changed
Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,36 @@
1-
use std::ops::{Deref, DerefMut};
2-
31
use crate::MainWorld;
42
use bevy_ecs::{
53
prelude::*,
64
system::{
7-
ReadOnlySystemParamFetch, SystemParam, SystemParamFetch, SystemParamItem, SystemParamState,
8-
SystemState,
5+
ReadOnlySystemParamFetch, SystemParam, SystemParamFetch, SystemParamItem, SystemState,
96
},
107
};
118

12-
pub struct ExtractFromMainWorld<'s, P: SystemParam + 'static>(SystemParamItem<'s, 's, P>)
13-
where
14-
P::Fetch: ReadOnlySystemParamFetch;
15-
16-
impl<'s, P: SystemParam + 'static> Deref for ExtractFromMainWorld<'s, P>
17-
where
18-
P::Fetch: ReadOnlySystemParamFetch,
19-
{
20-
type Target = SystemParamItem<'s, 's, P>;
21-
22-
fn deref(&self) -> &Self::Target {
23-
&self.0
24-
}
25-
}
26-
27-
impl<'s, P: SystemParam + 'static> DerefMut for ExtractFromMainWorld<'s, P>
28-
where
29-
P::Fetch: ReadOnlySystemParamFetch,
30-
{
31-
fn deref_mut(&mut self) -> &mut Self::Target {
32-
&mut self.0
33-
}
34-
}
9+
pub struct MainWorldState<P: SystemParam>(SystemState<P>);
3510

36-
impl<'s, P: SystemParam + 'static> ExtractFromMainWorld<'s, P>
37-
where
38-
P::Fetch: ReadOnlySystemParamFetch,
39-
{
40-
pub fn into_inner(self) -> SystemParamItem<'s, 's, P> {
41-
self.0
11+
impl<P: SystemParam> FromWorld for MainWorldState<P> {
12+
fn from_world(world: &mut World) -> Self {
13+
Self(SystemState::new(&mut world.resource_mut::<MainWorld>().0))
4214
}
4315
}
4416

45-
impl<'s, P: SystemParam + 'static> SystemParam for ExtractFromMainWorld<'s, P>
17+
#[derive(SystemParam)]
18+
pub struct Extract<'w, 's, P: SystemParam + 'static>
4619
where
4720
P::Fetch: ReadOnlySystemParamFetch,
4821
{
49-
type Fetch = ExtractFromMainWorldState<P>;
22+
state: Local<
23+
's,
24+
MainWorldState<<<P as SystemParam>::Fetch as SystemParamFetch<'static, 'static>>::Item>,
25+
>,
26+
world: Res<'w, World>,
5027
}
5128

52-
unsafe impl<P: SystemParam + 'static> SystemParamState for ExtractFromMainWorldState<P> {
53-
fn init(world: &mut World, system_meta: &mut bevy_ecs::system::SystemMeta) -> Self {
54-
Self {
55-
world: SystemParamState::init(world, system_meta),
56-
state: SystemState::new(&mut (*world.resource_mut::<MainWorld>())),
57-
}
58-
}
59-
}
60-
61-
pub struct ExtractFromMainWorldState<P: SystemParam + 'static> {
62-
world: <Res<'static, MainWorld> as SystemParam>::Fetch,
63-
state: SystemState<P>,
64-
}
65-
66-
impl<'world, 'state, P: SystemParam + 'static> SystemParamFetch<'world, 'state>
67-
for ExtractFromMainWorldState<P>
29+
impl<'w, 's, P: SystemParam + 'static> Extract<'w, 's, P>
6830
where
6931
P::Fetch: ReadOnlySystemParamFetch,
7032
{
71-
type Item = ExtractFromMainWorld<'state, P>;
72-
73-
unsafe fn get_param(
74-
state: &'state mut Self,
75-
system_meta: &bevy_ecs::system::SystemMeta,
76-
world: &'world World,
77-
change_tick: u32,
78-
) -> Self::Item {
79-
let world = SystemParamFetch::get_param(&mut state.world, system_meta, world, change_tick);
80-
ExtractFromMainWorld(state.state.get(&world))
33+
pub fn value(&mut self) -> SystemParamItem<'_, '_, P> {
34+
self.state.0.get(&self.world)
8135
}
8236
}

crates/bevy_render/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ pub mod renderer;
1515
pub mod settings;
1616
pub mod texture;
1717
pub mod view;
18-
pub use extract_param::ExtractFromMainWorld;
18+
19+
pub use extract_param::Extract;
1920

2021
pub mod prelude {
2122
#[doc(hidden)]
@@ -312,7 +313,7 @@ fn extract(app_world: &mut World, render_app: &mut App) {
312313
// temporarily add the app world to the render world as a resource
313314
let scratch_world = app_world.remove_resource::<ScratchMainWorld>().unwrap();
314315
let inserted_world = std::mem::replace(app_world, scratch_world.0);
315-
let running_world = &mut render_app.world;
316+
let mut running_world = &mut render_app.world;
316317
running_world.insert_resource(MainWorld(inserted_world));
317318

318319
extract.run(&mut running_world);
@@ -321,5 +322,5 @@ fn extract(app_world: &mut World, render_app: &mut App) {
321322
// move the app world back, as if nothing happened.
322323
let inserted_world = running_world.remove_resource::<MainWorld>().unwrap();
323324
let scratch_world = std::mem::replace(app_world, inserted_world.0);
324-
inserted_world.insert_resource(ScratchMainWorld(scratch_world));
325+
app_world.insert_resource(ScratchMainWorld(scratch_world));
325326
}

0 commit comments

Comments
 (0)