wecs_core/resource/
param.rs1use std::{
2 marker::PhantomData,
3 ops::{Deref, DerefMut},
4};
5
6use crate::system::SystemParam;
7
8use super::Resource;
9
10pub struct Res<'w, R>
11where
12 R: Resource + 'static,
13{
14 resource: &'w R,
15 }
17
18impl<'w, R> Deref for Res<'w, R>
19where
20 R: Resource + 'static,
21{
22 type Target = R;
23
24 fn deref(&self) -> &Self::Target {
25 &self.resource
26 }
27}
28
29impl<'w, R> SystemParam for Res<'w, R>
30where
31 R: Resource + 'static,
32{
33 type Item<'world> = Res<'world, R>;
34
35 fn get_param<'world>(world: crate::world::UnsafeWorldCell<'world>) -> Self::Item<'world> {
36 Res {
37 resource: world.world().get_resource::<R>().unwrap(),
38 }
40 }
41}
42
43pub struct ResMut<'w, R>
44where
45 R: Resource + 'static,
46{
47 resource: &'w mut R,
48 }
50
51impl<'w, R> Deref for ResMut<'w, R>
52where
53 R: Resource + 'static,
54{
55 type Target = R;
56
57 fn deref(&self) -> &Self::Target {
58 &self.resource
59 }
60}
61
62impl<'w, R> DerefMut for ResMut<'w, R>
63where
64 R: Resource + 'static,
65{
66 fn deref_mut(&mut self) -> &mut Self::Target {
67 &mut self.resource
68 }
69}
70
71impl<'w, R> SystemParam for ResMut<'w, R>
72where
73 R: Resource + 'static,
74{
75 type Item<'world> = ResMut<'world, R>;
76
77 fn get_param<'world>(world: crate::world::UnsafeWorldCell<'world>) -> Self::Item<'world> {
78 ResMut {
79 resource: world.world_mut().get_resource_mut::<R>().unwrap(),
80 }
82 }
83}