limnus_system_params/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/swamp/limnus
 * Licensed under the MIT License. See LICENSE in the project root for license information.
 */
pub mod prelude;

use limnus_local_resource::{LocalResource, LocalResourceStorage};
use limnus_message::{Message, Messages};
use limnus_resource::{Resource, ResourceStorage};
use limnus_system::SystemParam;
use limnus_system_state::State;
use std::mem::transmute;
use std::ops::{Deref, DerefMut};

// Mutable resource access
pub struct ReM<'a, T: 'static> {
    value: &'a mut T,
}

impl<'a, T> ReM<'a, T> {
    pub fn new(value: &'a mut T) -> Self {
        Self { value }
    }
}

impl<'a, T> Deref for ReM<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, T> DerefMut for ReM<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

// Mutable resource access
pub struct Re<'a, T: 'static> {
    value: &'a T,
}

impl<'a, T> Re<'a, T> {
    pub fn new(value: &'a T) -> Self {
        Self { value }
    }
}

impl<'a, T> Deref for Re<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<T: Resource + 'static> SystemParam for ReM<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut T = world.resource_mut::<T>();
        let static_ref: &'static mut T = unsafe { transmute(actual_ref) };
        ReM::new(static_ref)
    }
}

impl<T: Resource + 'static> SystemParam for Re<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut T = world.resource_mut::<T>();
        let static_ref: &'static mut T = unsafe { transmute(actual_ref) };
        Re::new(static_ref)
    }
}

impl<T: 'static + Message> SystemParam for Msg<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref = world.message::<T>();
        let static_ref: &'static Messages<T> = unsafe { transmute(actual_ref) };
        Msg::new(static_ref)
    }
}

impl<T: 'static + Message> SystemParam for MsgM<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref = world.message_mut::<T>();
        let static_ref: &'static mut Messages<T> = unsafe { transmute(actual_ref) };
        MsgM::new(static_ref)
    }
}

pub struct ReAll<'a> {
    value: &'a mut ResourceStorage,
}
impl<'a> ReAll<'a> {
    pub fn new(value: &'a mut ResourceStorage) -> Self {
        Self { value }
    }
}

impl<'a> Deref for ReAll<'a> {
    type Target = ResourceStorage;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a> DerefMut for ReAll<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

impl SystemParam for ReAll<'static> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut ResourceStorage = world.resources_mut();
        let static_ref: &'static mut ResourceStorage = unsafe { transmute(actual_ref) };
        ReAll::new(static_ref)
    }
}

// ====================

pub struct Msg<'a, T: 'static + Message> {
    value: &'a Messages<T>,
}

impl<'a, T: Message> Msg<'a, T> {
    #[must_use]
    pub const fn new(value: &'a Messages<T>) -> Self {
        Self { value }
    }
}

impl<'a, T: Message> Deref for Msg<'a, T> {
    type Target = Messages<T>;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

// Mutable message access
pub struct MsgM<'a, T: 'static + Message> {
    value: &'a mut Messages<T>,
}

impl<'a, T: Message> MsgM<'a, T> {
    pub fn new(value: &'a mut Messages<T>) -> Self {
        Self { value }
    }
}

impl<'a, T: Message> Deref for MsgM<'a, T> {
    type Target = Messages<T>;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, T: Message> DerefMut for MsgM<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

// ==========  Local resources

pub struct LocReAll<'a> {
    value: &'a mut LocalResourceStorage,
}
impl<'a> LocReAll<'a> {
    pub fn new(value: &'a mut LocalResourceStorage) -> Self {
        Self { value }
    }
}

impl<'a> Deref for LocReAll<'a> {
    type Target = LocalResourceStorage;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a> DerefMut for LocReAll<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

impl SystemParam for LocReAll<'static> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut LocalResourceStorage = world.local_resources_mut();
        let static_ref: &'static mut LocalResourceStorage = unsafe { transmute(actual_ref) };
        LocReAll::new(static_ref)
    }
}

// === Local Resources

// Mutable local resource access
pub struct LoReM<'a, T: 'static> {
    value: &'a mut T,
}

impl<'a, T> crate::LoReM<'a, T> {
    pub fn new(value: &'a mut T) -> Self {
        Self { value }
    }
}

impl<'a, T> Deref for crate::LoReM<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<'a, T> DerefMut for crate::LoReM<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value
    }
}

impl<T: LocalResource + 'static> SystemParam for crate::LoReM<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut T = world.local_resource_mut::<T>();
        let static_ref: &'static mut T = unsafe { transmute(actual_ref) };
        LoReM::new(static_ref)
    }
}

// Mutable local resource access
pub struct LoRe<'a, T: 'static> {
    value: &'a T,
}

impl<'a, T> crate::LoRe<'a, T> {
    pub fn new(value: &'a T) -> Self {
        Self { value }
    }
}

impl<'a, T> Deref for crate::LoRe<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.value
    }
}

impl<T: LocalResource + 'static> SystemParam for crate::LoRe<'static, T> {
    type Item = Self;

    fn fetch(world: &mut State) -> Self::Item {
        let actual_ref: &mut T = world.local_resource_mut::<T>();
        let static_ref: &'static mut T = unsafe { transmute(actual_ref) };
        LoRe::new(static_ref)
    }
}