sim-logic 0.1.0

A code-first application and logic layer for Sim;Engine
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
411
412
//! Count-bounded typed events visible only within one System-stage invocation.

use std::{
    any::{TypeId, type_name},
    error::Error,
    fmt,
    sync::{
        Mutex,
        atomic::{AtomicBool, Ordering},
    },
};

use bevy_ecs::{
    prelude::{Res, ResMut, Resource},
    system::SystemParam,
    world::World,
};

/// A small value that can be broadcast between Systems in one World stage.
///
/// This first-slice contract requires `Copy` so an expired event cannot retain
/// owned heap memory or defer user `Drop` code beyond its stage. Prefer compact
/// values and opaque handles; the configured limit bounds record count, not
/// `size_of::<E>()` bytes.
///
/// ```compile_fail
/// use sim_logic::events::WorldEvent;
///
/// fn accepts_world_event<E: WorldEvent>() {}
/// accepts_world_event::<String>();
/// ```
pub trait WorldEvent: Copy + Send + Sync + 'static {}

impl<T: Copy + Send + Sync + 'static> WorldEvent for T {}

/// A typed event could not be retained by the current stage invocation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventSendError {
    /// This event type already retained its configured maximum record count.
    LimitExceeded {
        /// Rust type name of the channel that first failed.
        event: &'static str,
        /// Maximum records for each event type in one stage invocation.
        limit: usize,
    },
    /// The channel could not reserve storage for one more record.
    AllocationFailed {
        /// Rust type name of the channel that first failed.
        event: &'static str,
    },
    /// An earlier event send already failed in this stage invocation.
    StageAlreadyFailed,
    /// The runtime did not expose an active event-delivery stage.
    StageUnavailable {
        /// Rust type name of the channel used outside a running stage.
        event: &'static str,
    },
}

impl fmt::Display for EventSendError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LimitExceeded { event, limit } => {
                write!(formatter, "event `{event}` exceeded its limit of {limit}")
            }
            Self::AllocationFailed { event } => {
                write!(formatter, "event `{event}` could not reserve storage")
            }
            Self::StageAlreadyFailed => {
                formatter.write_str("an earlier event send already failed in this stage")
            }
            Self::StageUnavailable { event } => {
                write!(
                    formatter,
                    "event `{event}` cannot be sent outside a running stage"
                )
            }
        }
    }
}

impl Error for EventSendError {}

#[derive(Resource)]
struct EventChannel<E: WorldEvent> {
    events: Vec<E>,
    written_epoch: Option<u64>,
    limit: usize,
}

impl<E: WorldEvent> EventChannel<E> {
    const fn new(limit: usize) -> Self {
        Self {
            events: Vec::new(),
            written_epoch: None,
            limit,
        }
    }

    fn begin_writing(&mut self, epoch: u64) {
        if self.written_epoch != Some(epoch) {
            self.events.clear();
            self.written_epoch = Some(epoch);
        }
    }

    fn current(&self, epoch: Option<u64>) -> &[E] {
        if self.written_epoch == epoch && epoch.is_some() {
            &self.events
        } else {
            &[]
        }
    }
}

#[derive(Debug, Default, Resource)]
pub(crate) struct EventStageState {
    last_epoch: u64,
    active_epoch: Option<u64>,
}

impl EventStageState {
    fn begin(&mut self) -> bool {
        if self.active_epoch.is_some() {
            return false;
        }
        let Some(epoch) = self.last_epoch.checked_add(1) else {
            self.active_epoch = None;
            return false;
        };
        self.last_epoch = epoch;
        self.active_epoch = Some(epoch);
        true
    }

    fn end(&mut self) -> bool {
        self.active_epoch.take().is_some()
    }

    const fn active_epoch(&self) -> Option<u64> {
        self.active_epoch
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct EventStageFailure {
    error: EventSendError,
}

impl fmt::Display for EventStageFailure {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.error.fmt(formatter)
    }
}

#[derive(Debug, Default, Resource)]
struct EventFailureState {
    failed: AtomicBool,
    first: Mutex<Option<EventStageFailure>>,
}

impl EventFailureState {
    fn reset(&mut self) -> bool {
        let Ok(first) = self.first.get_mut() else {
            return false;
        };
        *first = None;
        self.failed.store(false, Ordering::Release);
        true
    }

    fn is_failed(&self) -> bool {
        self.failed.load(Ordering::Acquire)
    }

    fn record(&self, error: EventSendError) {
        let Ok(mut first) = self.first.lock() else {
            self.failed.store(true, Ordering::Release);
            return;
        };
        if first.is_none() {
            *first = Some(EventStageFailure { error });
        }
        self.failed.store(true, Ordering::Release);
    }

    fn failure(&self) -> Result<Option<EventStageFailure>, ()> {
        if !self.is_failed() {
            return Ok(None);
        }
        self.first.lock().map(|first| *first).map_err(|_| ())
    }
}

/// Writes ordered typed events for later Systems in the current stage.
///
/// A send failure poisons the complete stage even when its returned error is
/// ignored. The scheduler stops before the next System and discards that
/// stage's pending structural and transition Commands. Successful sends become
/// immediately visible to later [`EventReader`] parameters of the same type.
/// To inspect earlier events and then write the same type in one System, use
/// this writer's [`Self::iter`] method. Requesting both `EventReader<E>` and
/// `EventWriter<E>` in one System conflicts over the same ECS channel.
#[derive(SystemParam)]
pub struct EventWriter<'w, E: WorldEvent> {
    channel: ResMut<'w, EventChannel<E>>,
    stage: Res<'w, EventStageState>,
    failure: Res<'w, EventFailureState>,
}

impl<E: WorldEvent> EventWriter<'_, E> {
    /// Retains one event in call order for later Systems in this stage.
    pub fn send(&mut self, event: E) -> Result<(), EventSendError> {
        if self.failure.is_failed() {
            return Err(EventSendError::StageAlreadyFailed);
        }
        let Some(epoch) = self.stage.active_epoch() else {
            let error = EventSendError::StageUnavailable {
                event: type_name::<E>(),
            };
            self.failure.record(error);
            return Err(error);
        };

        self.channel.begin_writing(epoch);
        if self.channel.events.len() >= self.channel.limit {
            let error = EventSendError::LimitExceeded {
                event: type_name::<E>(),
                limit: self.channel.limit,
            };
            self.failure.record(error);
            return Err(error);
        }
        if self.channel.events.try_reserve(1).is_err() {
            let error = EventSendError::AllocationFailed {
                event: type_name::<E>(),
            };
            self.failure.record(error);
            return Err(error);
        }
        self.channel.events.push(event);
        Ok(())
    }

    /// Iterates over events sent earlier in this stage, including by this writer.
    pub fn iter(&self) -> std::slice::Iter<'_, E> {
        self.channel.current(self.stage.active_epoch()).iter()
    }

    /// Returns the number of events currently visible to this writer.
    pub fn len(&self) -> usize {
        self.channel.current(self.stage.active_epoch()).len()
    }

    /// Returns `true` when this stage has not retained an event of this type.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a, E: WorldEvent> IntoIterator for &'a EventWriter<'_, E> {
    type Item = &'a E;
    type IntoIter = std::slice::Iter<'a, E>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// A repeatable read-only view of events sent earlier in the current stage.
///
/// Reading does not consume records. Every later reader sees the same ordered
/// slice, while a reader that ran before a writer is not invoked again. Do not
/// request a reader and writer of the same event type in one System; use the
/// writer's read-only methods when both operations are needed.
#[derive(SystemParam)]
pub struct EventReader<'w, E: WorldEvent> {
    channel: Res<'w, EventChannel<E>>,
    stage: Res<'w, EventStageState>,
}

impl<E: WorldEvent> EventReader<'_, E> {
    /// Iterates over the complete event slice currently visible to this stage.
    pub fn iter(&self) -> std::slice::Iter<'_, E> {
        self.channel.current(self.stage.active_epoch()).iter()
    }

    /// Returns the number of events visible to this reader.
    pub fn len(&self) -> usize {
        self.channel.current(self.stage.active_epoch()).len()
    }

    /// Returns `true` when no earlier System sent this event type in the stage.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a, E: WorldEvent> IntoIterator for &'a EventReader<'_, E> {
    type Item = &'a E;
    type IntoIter = std::slice::Iter<'a, E>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

struct RegisteredEvent {
    type_id: TypeId,
    install: fn(&mut World, usize),
}

#[derive(Default)]
pub(crate) struct EventRegistry {
    events: Vec<RegisteredEvent>,
}

impl EventRegistry {
    pub(crate) fn register<E: WorldEvent>(&mut self) {
        if self
            .events
            .iter()
            .any(|event| event.type_id == TypeId::of::<E>())
        {
            return;
        }
        self.events.push(RegisteredEvent {
            type_id: TypeId::of::<E>(),
            install: install_channel::<E>,
        });
    }

    pub(crate) fn install(&self, world: &mut World, event_limit: usize) {
        world.insert_resource(EventStageState::default());
        world.insert_resource(EventFailureState::default());
        for event in &self.events {
            (event.install)(world, event_limit);
        }
    }
}

fn install_channel<E: WorldEvent>(world: &mut World, limit: usize) {
    world.insert_resource(EventChannel::<E>::new(limit));
}

pub(crate) fn begin_event_stage(world: &mut World) -> bool {
    let reset = world
        .get_resource_mut::<EventFailureState>()
        .is_some_and(|mut failure| failure.reset());
    reset
        && world
            .get_resource_mut::<EventStageState>()
            .is_some_and(|mut stage| stage.begin())
}

pub(crate) fn end_event_stage(world: &mut World) -> bool {
    let Some(mut stage) = world.get_resource_mut::<EventStageState>() else {
        return false;
    };
    stage.end()
}

pub(crate) fn event_stage_failure(world: &World) -> Result<Option<EventStageFailure>, ()> {
    world
        .get_resource::<EventFailureState>()
        .ok_or(())?
        .failure()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn epochs_hide_old_records_and_reuse_capacity() {
        let mut channel = EventChannel::<u8>::new(2);
        channel.begin_writing(1);
        channel.events.extend([3, 4]);
        let capacity = channel.events.capacity();

        assert_eq!(channel.current(Some(1)), [3, 4]);
        assert!(channel.current(None).is_empty());
        assert!(channel.current(Some(2)).is_empty());

        channel.begin_writing(2);
        channel.events.push(9);
        assert_eq!(channel.current(Some(2)), [9]);
        assert_eq!(channel.events.capacity(), capacity);
    }

    #[test]
    fn stage_epoch_exhaustion_closes_delivery() {
        let mut stage = EventStageState {
            last_epoch: u64::MAX,
            active_epoch: None,
        };

        assert!(!stage.begin());
        assert_eq!(stage.active_epoch(), None);
    }

    #[test]
    fn stage_lifecycle_rejects_nested_begin_and_unmatched_end() {
        let mut stage = EventStageState::default();

        assert!(!stage.end());
        assert!(stage.begin());
        assert!(!stage.begin());
        assert!(stage.end());
        assert!(!stage.end());
    }
}