moirai-for-games 0.1.0

A small deterministic no_std ECS for constrained and headless games
Documentation
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! World event send, read, and component lifecycle emission.
//!
//! Manual and frame events honor schedule-declared emit/consume access. Component
//! added/removed channels are registered automatically during world construction.

use core::any::{type_name, TypeId};

use crate::event::{ComponentAdded, ComponentRemoved, EventReader, EventReaderStart};
use crate::world::{EventReadError, World, WorldError};

impl World {
    /// Send one instance of registered event `E`.
    pub fn send<E: Clone + 'static>(&mut self, event: E) -> Result<(), WorldError> {
        let event_id = self
            .events
            .registry
            .id_of::<E>(&self.owner)
            .ok_or_else(|| WorldError::UnregisteredEvent {
                name: alloc::string::String::from(type_name::<E>()),
            })?;
        self.ensure_event_emit_allowed(&event_id)?;
        self.events.storage.send(&event_id, event)
    }

    /// Open a reader for registered event `E` starting at `start`.
    pub fn event_reader<E: Clone + 'static>(
        &mut self,
        start: EventReaderStart,
    ) -> Result<EventReader<E>, WorldError> {
        let event_id = self
            .events
            .registry
            .id_of::<E>(&self.owner)
            .ok_or_else(|| WorldError::UnregisteredEvent {
                name: alloc::string::String::from(type_name::<E>()),
            })?;
        self.ensure_event_consume_allowed(&event_id)?;
        self.events
            .storage
            .create_reader(self.owner.clone(), event_id, start)
    }

    /// Read the next retained event through `reader`.
    pub fn read_event<'a, E: Clone + 'static>(
        &mut self,
        reader: &'a mut EventReader<E>,
    ) -> Result<Option<&'a E>, EventReadError> {
        if reader.event_id.validate_owner(&self.owner).is_err() {
            return Err(EventReadError::OwnerMismatch {
                name: alloc::format!("event {}", reader.event_id.index()),
            });
        }
        if !self.run_guard.permits_consume(&reader.event_id) {
            return Err(EventReadError::UnregisteredEvent {
                name: alloc::format!("undeclared event {}", reader.event_id.index()),
            });
        }
        self.events.storage.read_next(&self.owner, reader)
    }

    /// Reader for component-added lifecycle events of `T`.
    pub fn on_add_reader<T: 'static>(
        &mut self,
        start: EventReaderStart,
    ) -> Result<EventReader<ComponentAdded>, WorldError> {
        let component_index = self.component_index::<T>()?;
        let event_id = self
            .events
            .lifecycle
            .added_event_id(&self.owner, component_index)
            .ok_or_else(|| WorldError::UnregisteredComponent {
                name: alloc::string::String::from(type_name::<T>()),
            })?;
        self.ensure_event_consume_allowed(&event_id)?;
        self.events
            .storage
            .create_reader(self.owner.clone(), event_id, start)
    }

    /// Reader for component-removed lifecycle events of `T`.
    pub fn on_remove_reader<T: 'static>(
        &mut self,
        start: EventReaderStart,
    ) -> Result<EventReader<ComponentRemoved>, WorldError> {
        let component_index = self.component_index::<T>()?;
        let event_id = self
            .events
            .lifecycle
            .removed_event_id(&self.owner, component_index)
            .ok_or_else(|| WorldError::UnregisteredComponent {
                name: alloc::string::String::from(type_name::<T>()),
            })?;
        self.ensure_event_consume_allowed(&event_id)?;
        self.events
            .storage
            .create_reader(self.owner.clone(), event_id, start)
    }

    pub(crate) fn fork_event_reader<E: Clone + 'static>(
        &mut self,
        reader: &EventReader<E>,
    ) -> Result<EventReader<E>, WorldError> {
        self.ensure_event_consume_allowed(&reader.event_id)?;
        self.events.storage.fork_reader(&self.owner, reader)
    }

    pub(crate) fn event_id_of_type(&self, type_id: TypeId) -> Option<crate::event::EventId> {
        self.events.registry.id_of_type_id(&self.owner, type_id)
    }

    pub(crate) fn event_options(
        &self,
        event_id: &crate::event::EventId,
    ) -> Option<crate::event::EventOptions> {
        self.events.registry.options(event_id)
    }

    pub(crate) fn lifecycle_event_id(
        &self,
        component_type: TypeId,
        added: bool,
    ) -> Option<crate::event::EventId> {
        let component_index = self.registry_id_of_type(component_type)?.index();
        if added {
            self.events
                .lifecycle
                .added_event_id(&self.owner, component_index)
        } else {
            self.events
                .lifecycle
                .removed_event_id(&self.owner, component_index)
        }
    }

    fn ensure_event_emit_allowed(
        &self,
        event_id: &crate::event::EventId,
    ) -> Result<(), WorldError> {
        if self.run_guard.permits_emit(event_id) {
            Ok(())
        } else {
            Err(WorldError::UnregisteredEvent {
                name: alloc::format!("undeclared event {}", event_id.index()),
            })
        }
    }

    fn ensure_event_consume_allowed(
        &self,
        event_id: &crate::event::EventId,
    ) -> Result<(), WorldError> {
        if self.run_guard.permits_consume(event_id) {
            Ok(())
        } else {
            Err(WorldError::UnregisteredEvent {
                name: alloc::format!("undeclared event {}", event_id.index()),
            })
        }
    }

    #[cfg(test)]
    pub(crate) fn set_event_sequence_for_test(
        &mut self,
        event_index: usize,
        next_sequence: u64,
        closed: bool,
    ) {
        self.events
            .storage
            .set_channel_state_for_test(event_index, next_sequence, closed);
    }

    #[allow(dead_code)]
    pub(crate) fn clear_frame_events(&mut self, operation: crate::operation::StageOperation) {
        self.events.storage.clear_frame(operation);
    }

    pub(crate) fn emit_component_added(
        &mut self,
        entity: crate::entity::EntityId,
        component_index: usize,
        is_new: bool,
    ) -> Result<(), WorldError> {
        if self.lifecycle_events_suppressed {
            return Ok(());
        }
        if !is_new {
            return Ok(());
        }
        self.record_component_query_topology(entity, component_index);
        match self.events.lifecycle.emit_added(
            &mut self.events.storage,
            &self.owner,
            entity,
            component_index,
        ) {
            Ok(()) | Err(WorldError::EventChannelClosed) => Ok(()),
            Err(error) => Err(error),
        }
    }

    pub(crate) fn emit_component_removed_if(
        &mut self,
        should_emit: bool,
        entity: crate::entity::EntityId,
        component_index: usize,
    ) -> Result<(), WorldError> {
        match should_emit {
            true => self.emit_component_removed(entity, component_index),
            false => Ok(()),
        }
    }

    pub(crate) fn emit_component_removed(
        &mut self,
        entity: crate::entity::EntityId,
        component_index: usize,
    ) -> Result<(), WorldError> {
        if self.lifecycle_events_suppressed {
            return Ok(());
        }
        self.record_component_query_topology(entity, component_index);
        match self.events.lifecycle.emit_removed(
            &mut self.events.storage,
            &self.owner,
            entity,
            component_index,
        ) {
            Ok(()) | Err(WorldError::EventChannelClosed) => Ok(()),
            Err(error) => Err(error),
        }
    }
}

#[cfg(test)]
pub(crate) fn set_event_sequence_for_test<E: Clone + 'static>(
    world: &mut World,
    next_sequence: u64,
    closed: bool,
) -> Result<(), WorldError> {
    let event_id = world
        .events
        .registry
        .id_of::<E>(&world.owner)
        .ok_or_else(|| WorldError::UnregisteredEvent {
            name: alloc::string::String::from(type_name::<E>()),
        })?;
    world
        .events
        .storage
        .set_channel_state_for_test(event_id.index(), next_sequence, closed);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::component::ComponentOptions;
    use crate::event::{EventOptions, EventReaderStart};
    use crate::world::WorldBuilder;

    #[derive(Clone, Copy)]
    struct Health(#[allow(dead_code)] i32);

    #[derive(Clone, Debug, PartialEq)]
    struct Damage(u32);

    #[test]
    fn event_sequence_exhaustion_closes_channel_and_reader() {
        let mut builder = WorldBuilder::new();
        builder
            .add_event::<Damage>(EventOptions::manual())
            .expect("register");
        let mut world = builder.build().expect("world");
        let mut reader = world
            .event_reader::<Damage>(EventReaderStart::FromNow)
            .expect("reader");

        set_event_sequence_for_test::<Damage>(&mut world, u64::MAX, false)
            .expect("registered event");
        assert!(matches!(
            world.send(Damage(1)),
            Err(WorldError::EventChannelClosed)
        ));
        assert!(matches!(
            world.read_event(&mut reader),
            Err(EventReadError::ChannelClosed)
        ));
    }

    #[test]
    fn oldest_retained_reader_reads_near_sequence_exhaustion() {
        let mut builder = WorldBuilder::new();
        builder
            .add_event::<Damage>(EventOptions::manual())
            .expect("register");
        let mut world = builder.build().expect("world");

        set_event_sequence_for_test::<Damage>(&mut world, u64::MAX - 2, false)
            .expect("registered event");
        world.send(Damage(7)).expect("send near max");
        let mut reader = world
            .event_reader::<Damage>(EventReaderStart::OldestRetained)
            .expect("reader");

        assert_eq!(
            world.read_event(&mut reader).expect("read").cloned(),
            Some(Damage(7))
        );
    }

    #[test]
    fn event_sequence_test_support_rejects_unregistered_channels() {
        let mut world = WorldBuilder::new().build().expect("world");
        assert!(matches!(
            set_event_sequence_for_test::<Damage>(&mut world, 0, false),
            Err(WorldError::UnregisteredEvent { .. })
        ));
    }

    #[test]
    fn event_reader_rejects_unregistered_event_type() {
        let mut world = WorldBuilder::new().build().expect("world");
        assert!(matches!(
            world.event_reader::<Health>(EventReaderStart::OldestRetained),
            Err(WorldError::UnregisteredEvent { .. })
        ));
    }

    #[test]
    fn lifecycle_readers_reject_missing_lifecycle_channels() {
        let mut builder = WorldBuilder::new();
        builder
            .register_component::<Health>(ComponentOptions::sparse())
            .expect("register");
        let mut world = builder.build().expect("world");
        world.events.lifecycle.clear_added_event_for_test(0);
        assert!(matches!(
            world.on_add_reader::<Health>(EventReaderStart::OldestRetained),
            Err(WorldError::UnregisteredComponent { .. })
        ));
        world.events.lifecycle.clear_removed_event_for_test(0);
        assert!(matches!(
            world.on_remove_reader::<Health>(EventReaderStart::OldestRetained),
            Err(WorldError::UnregisteredComponent { .. })
        ));
    }

    #[test]
    fn emit_component_added_propagates_non_closed_send_errors() {
        let mut builder = WorldBuilder::new();
        builder
            .register_component::<Health>(ComponentOptions::sparse())
            .expect("register");
        let mut world = builder.build().expect("world");
        let entity = world.spawn().expect("spawn");
        world.events.storage.clear_channels_for_test();
        assert!(matches!(
            world.emit_component_added(entity, 0, true),
            Err(WorldError::UnregisteredEvent { .. })
        ));
    }

    #[test]
    fn emit_component_removed_if_skips_emit_when_not_requested() {
        let mut builder = WorldBuilder::new();
        builder
            .register_component::<Health>(ComponentOptions::sparse())
            .expect("register");
        let mut world = builder.build().expect("world");
        let entity = world.spawn().expect("spawn");
        world.insert(entity, Health(1)).expect("insert");
        let mut reader = world
            .on_remove_reader::<Health>(EventReaderStart::OldestRetained)
            .expect("reader");

        world
            .emit_component_removed_if(false, entity, 0)
            .expect("skip emit");
        assert!(world
            .read_event(&mut reader)
            .expect("read after skip")
            .is_none());

        world
            .emit_component_removed_if(true, entity, 0)
            .expect("emit");
        let event = world
            .read_event(&mut reader)
            .expect("read after emit")
            .expect("removed event");
        assert_eq!(event.entity, entity);
    }

    #[test]
    fn emit_component_removed_propagates_non_closed_send_errors() {
        let mut builder = WorldBuilder::new();
        builder
            .register_component::<Health>(ComponentOptions::sparse())
            .expect("register");
        let mut world = builder.build().expect("world");
        let entity = world.spawn().expect("spawn");
        world.insert(entity, Health(1)).expect("insert");
        world.events.storage.clear_channels_for_test();
        assert!(matches!(
            world.emit_component_removed(entity, 0),
            Err(WorldError::UnregisteredEvent { .. })
        ));
    }
}