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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! Generation-checked component queries for managed Systems.

use std::{
    error::Error,
    fmt,
    ops::{Deref, DerefMut},
};

use bevy_ecs::{
    entity_disabling::Disabled,
    query::{
        Allow, IterQueryData, QueryData, QueryFilter, QueryIter as BevyQueryIter,
        QuerySingleError as BevyQuerySingleError, ROQueryItem, With, Without,
    },
    system::{Query as BevyQuery, Single as BevySingle, SystemParam},
};

use crate::identity::{LogicEntity, ManagedEntity, WorldGeneration, WorldIdentityState};

type ManagedFilter<F> = (F, Allow<Disabled>, Without<Disabled>, With<ManagedEntity>);

/// A managed entity cannot be resolved by a component query.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QueryEntityError {
    /// The handle belongs to another application or World generation.
    ForeignWorld {
        /// Rejected managed handle.
        entity: LogicEntity,
        /// Generation against which the query was running.
        active: WorldGeneration,
    },
    /// The entity is no longer alive or does not match the lookup rules.
    DoesNotMatch {
        /// Managed handle that could not produce an item.
        entity: LogicEntity,
    },
}

impl fmt::Display for QueryEntityError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ForeignWorld { entity, active } => write!(
                formatter,
                "entity {entity:?} does not belong to active generation {active:?}"
            ),
            Self::DoesNotMatch { entity } => {
                write!(formatter, "entity {entity:?} is absent or does not match")
            }
        }
    }
}

impl Error for QueryEntityError {}

/// A managed query expected exactly one matching enabled entity.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QuerySingleError {
    /// No enabled managed entity matched the query.
    NoEntities,
    /// More than one enabled managed entity matched the query.
    MultipleEntities,
}

impl fmt::Display for QuerySingleError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoEntities => formatter.write_str("no enabled managed entity matches the query"),
            Self::MultipleEntities => {
                formatter.write_str("more than one enabled managed entity matches the query")
            }
        }
    }
}

impl Error for QuerySingleError {}

/// Typed component access for one active Sim;Logic World generation.
///
/// Iteration follows Bevy ECS borrowing and filtering rules, but never yields
/// a raw Bevy entity identifier. Use [`crate::identity::LogicEntityRef`] in the
/// query data when a stable managed handle is needed. Point lookup accepts only
/// [`LogicEntity`] and rejects handles from a retired or foreign World before
/// touching component storage. Within the active World, it also requires the
/// exact live managed identity at that ECS row before fetching user data.
///
/// Disabled entities are always excluded in this first slice, independently
/// of Bevy's mutable global default-query-filter Resource.
#[derive(SystemParam)]
pub struct Query<'w, 's, D: QueryData + 'static, F: QueryFilter + 'static = ()> {
    inner: BevyQuery<'w, 's, D, ManagedFilter<F>>,
    identities: BevyQuery<'w, 's, &'static ManagedEntity, ManagedFilter<()>>,
    identity: bevy_ecs::prelude::Res<'w, WorldIdentityState>,
}

/// Iterator over enabled, runtime-managed entities selected by [`Query`].
///
/// This wrapper keeps Sim;Logic's private managed-entity filter out of the
/// public type signature while preserving allocation-free ECS iteration.
pub struct QueryIterator<'w, 's, D: QueryData, F: QueryFilter> {
    inner: BevyQueryIter<'w, 's, D, ManagedFilter<F>>,
}

/// Exactly one enabled, runtime-managed entity selected for a System.
///
/// Cardinality is a System precondition: if zero or multiple entities match,
/// parameter validation fails before the System body runs and Sim;Logic stops
/// the current stage with a [`crate::system::SystemRunFailure`]. Use
/// [`Query::single`] when absence or multiplicity is expected control flow
/// that the System should handle itself.
///
/// Like [`Query`], this parameter always requires Sim;Logic's private managed
/// identity and excludes [`Disabled`] entities independently of Bevy's mutable
/// global default-query-filter Resource. [`crate::identity::LogicEntityRef`]
/// is the supported way to include the opaque entity handle in `D`.
#[derive(SystemParam)]
pub struct Single<'w, 's, D: IterQueryData + 'static, F: QueryFilter + 'static = ()> {
    inner: BevySingle<'w, 's, D, ManagedFilter<F>>,
}

impl<'w, 's, D: IterQueryData + 'static, F: QueryFilter + 'static> Deref for Single<'w, 's, D, F> {
    type Target = D::Item<'w, 's>;

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

impl<'w, 's, D: IterQueryData + 'static, F: QueryFilter + 'static> DerefMut
    for Single<'w, 's, D, F>
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.inner.deref_mut()
    }
}

impl<'w, 's, D: IterQueryData + 'static, F: QueryFilter + 'static> Single<'w, 's, D, F> {
    /// Returns the fetched query item without performing another lookup.
    ///
    /// This is useful for tuple data containing mutable component borrows.
    pub fn into_inner(self) -> D::Item<'w, 's> {
        self.inner.into_inner()
    }
}

impl<'w, 's, D: IterQueryData, F: QueryFilter> Iterator for QueryIterator<'w, 's, D, F> {
    type Item = D::Item<'w, 's>;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next()
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.inner.size_hint()
    }
}

impl<'s, D: QueryData + 'static, F: QueryFilter + 'static> Query<'_, 's, D, F> {
    /// Iterates over read-only forms of all matching enabled entities.
    ///
    /// Iteration order is not stable API.
    pub fn iter(&self) -> QueryIterator<'_, 's, D::ReadOnly, F> {
        QueryIterator {
            inner: self.inner.iter(),
        }
    }

    /// Iterates over all matching enabled entities with the access declared by
    /// `D`. Iteration order is not stable API.
    pub fn iter_mut(&mut self) -> QueryIterator<'_, 's, D, F>
    where
        D: IterQueryData,
    {
        QueryIterator {
            inner: self.inner.iter_mut(),
        }
    }

    /// Reads the only matching enabled managed entity.
    ///
    /// Returns [`QuerySingleError::NoEntities`] or
    /// [`QuerySingleError::MultipleEntities`] when cardinality is not exactly
    /// one.
    pub fn single(&self) -> Result<ROQueryItem<'_, 's, D>, QuerySingleError> {
        self.inner.single().map_err(map_single_error)
    }

    /// Accesses the only matching enabled managed entity mutably.
    ///
    /// Returns [`QuerySingleError::NoEntities`] or
    /// [`QuerySingleError::MultipleEntities`] when cardinality is not exactly
    /// one.
    pub fn single_mut(&mut self) -> Result<D::Item<'_, 's>, QuerySingleError>
    where
        D: IterQueryData,
    {
        self.inner.single_mut().map_err(map_single_error)
    }

    /// Reads one matching enabled entity after checking application and World
    /// provenance.
    pub fn get(&self, entity: LogicEntity) -> Result<ROQueryItem<'_, 's, D>, QueryEntityError> {
        self.validate(entity)?;
        self.inner
            .get(entity.entity())
            .map_err(|_| QueryEntityError::DoesNotMatch { entity })
    }

    /// Accesses one matching enabled entity mutably after checking application
    /// and World provenance.
    pub fn get_mut(&mut self, entity: LogicEntity) -> Result<D::Item<'_, 's>, QueryEntityError> {
        self.validate(entity)?;
        self.inner
            .get_mut(entity.entity())
            .map_err(|_| QueryEntityError::DoesNotMatch { entity })
    }

    fn validate(&self, entity: LogicEntity) -> Result<(), QueryEntityError> {
        if !self.identity.owns(entity) {
            return Err(QueryEntityError::ForeignWorld {
                entity,
                active: self.identity.generation(),
            });
        }
        if !self
            .identities
            .get(entity.entity())
            .is_ok_and(|managed| managed.handle(entity.entity()) == entity)
        {
            return Err(QueryEntityError::DoesNotMatch { entity });
        }
        Ok(())
    }
}

fn map_single_error(error: BevyQuerySingleError) -> QuerySingleError {
    match error {
        BevyQuerySingleError::NoEntities(_) => QuerySingleError::NoEntities,
        BevyQuerySingleError::MultipleEntities(_) => QuerySingleError::MultipleEntities,
    }
}

impl<'w, 's, D, F> IntoIterator for &'w Query<'_, 's, D, F>
where
    D: QueryData + 'static,
    F: QueryFilter + 'static,
{
    type Item = ROQueryItem<'w, 's, D>;
    type IntoIter = QueryIterator<'w, 's, D::ReadOnly, F>;

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

impl<'w, 's, D, F> IntoIterator for &'w mut Query<'_, 's, D, F>
where
    D: IterQueryData + 'static,
    F: QueryFilter + 'static,
{
    type Item = D::Item<'w, 's>;
    type IntoIter = QueryIterator<'w, 's, D, F>;

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

#[cfg(test)]
mod tests {
    use bevy_ecs::{
        entity_disabling::Disabled,
        prelude::{Component, Res, ResMut, Resource},
        query::Allow,
        world::World,
    };

    use crate::{
        identity::{
            ApplicationId, LogicEntity, LogicEntityRef, ManagedEntity, WorldGeneration,
            WorldIdentityState,
        },
        system::{Stage, StageFactories, SystemRunFailure},
    };

    use super::*;

    #[derive(Component)]
    struct Marker(u8);

    #[derive(Resource, Default)]
    struct Observation(Option<Result<u8, QuerySingleError>>);

    #[derive(Resource, Default)]
    struct RequiredSingleObservation {
        runs: usize,
        value: Option<u8>,
        entity: Option<LogicEntity>,
    }

    #[derive(Resource, Clone, Copy)]
    struct PointLookupTarget(LogicEntity);

    #[derive(Resource, Default)]
    struct PointLookupObservation {
        read: Option<Result<LogicEntity, QueryEntityError>>,
        write: Option<Result<(), QueryEntityError>>,
    }

    fn observe_single(markers: Query<&Marker>, mut observation: ResMut<Observation>) {
        observation.0 = Some(markers.single().map(|marker| marker.0));
    }

    fn increment_single(mut markers: Query<&mut Marker>) {
        if let Ok(mut marker) = markers.single_mut() {
            marker.0 += 1;
        }
    }

    fn observe_required_single(
        marker: Single<&Marker>,
        mut observation: ResMut<RequiredSingleObservation>,
    ) {
        observation.runs += 1;
        observation.value = Some(marker.0);
    }

    fn observe_required_single_tuple(
        marker: Single<(LogicEntityRef, &Marker)>,
        mut observation: ResMut<RequiredSingleObservation>,
    ) {
        let (entity, marker) = marker.into_inner();
        observation.runs += 1;
        observation.value = Some(marker.0);
        observation.entity = Some(entity.handle());
    }

    fn increment_required_single(mut marker: Single<&mut Marker>) {
        marker.0 += 1;
    }

    fn observe_point_lookup(
        target: Res<PointLookupTarget>,
        markers: Query<(LogicEntityRef, &Marker)>,
        mut observation: ResMut<PointLookupObservation>,
    ) {
        observation.read = Some(
            markers
                .get(target.0)
                .map(|(identity, _marker)| identity.handle()),
        );
    }

    fn mutate_point_lookup(
        target: Res<PointLookupTarget>,
        mut markers: Query<&mut Marker>,
        mut observation: ResMut<PointLookupObservation>,
    ) {
        observation.write = Some(markers.get_mut(target.0).map(|mut marker| marker.0 += 1));
    }

    fn managed_world(values: &[(u8, bool)]) -> World {
        let application = ApplicationId::from_raw(1);
        let generation = WorldGeneration::new(application, 1);
        let mut world = World::new();
        world.insert_resource(WorldIdentityState::new(application, generation));
        world.insert_resource(Observation::default());
        world.insert_resource(RequiredSingleObservation::default());
        for (value, disabled) in values {
            let raw = if *disabled {
                world.spawn((Marker(*value), Disabled)).id()
            } else {
                world.spawn(Marker(*value)).id()
            };
            let entity = LogicEntity::new(application, generation, raw);
            world.entity_mut(raw).insert(ManagedEntity::new(entity));
        }
        world
    }

    fn run_system_result<S, MarkerType>(
        world: &mut World,
        system: S,
    ) -> Result<(), SystemRunFailure>
    where
        S: bevy_ecs::system::SystemParamFunction<MarkerType, In = (), Out = ()>
            + Clone
            + Send
            + Sync
            + 'static,
        S::Param: crate::system::SupportedSystemParamTuple,
        MarkerType: 'static,
    {
        let mut factories = StageFactories::default();
        factories.add(system);
        let mut stage = factories
            .instantiate(world, Stage::FixedUpdate)
            .expect("query test System should initialize");
        stage.run(world)
    }

    fn run_system<S, MarkerType>(world: &mut World, system: S)
    where
        S: bevy_ecs::system::SystemParamFunction<MarkerType, In = (), Out = ()>
            + Clone
            + Send
            + Sync
            + 'static,
        S::Param: crate::system::SupportedSystemParamTuple,
        MarkerType: 'static,
    {
        run_system_result(world, system).expect("query test System should run");
    }

    #[test]
    fn single_reports_zero_one_and_multiple_enabled_entities() {
        for (values, expected) in [
            (&[][..], Err(QuerySingleError::NoEntities)),
            (&[(7, false)][..], Ok(7)),
            (
                &[(7, false), (9, false)][..],
                Err(QuerySingleError::MultipleEntities),
            ),
            (&[(7, false), (9, true)][..], Ok(7)),
            (&[(9, true)][..], Err(QuerySingleError::NoEntities)),
        ] {
            let mut world = managed_world(values);
            run_system(&mut world, observe_single);
            assert_eq!(world.resource::<Observation>().0, Some(expected));
        }
    }

    #[test]
    fn single_mut_changes_the_only_enabled_managed_entity() {
        let mut world = managed_world(&[(4, false), (9, true)]);
        run_system(&mut world, increment_single);

        let values: Vec<_> = world
            .query_filtered::<&Marker, Allow<Disabled>>()
            .iter(&world)
            .map(|value| value.0)
            .collect();
        assert!(values.contains(&5));
        assert!(values.contains(&9));
    }

    #[test]
    fn required_single_dereferences_the_only_enabled_managed_entity() {
        let mut world = managed_world(&[(7, false), (9, true)]);
        run_system(&mut world, observe_required_single);

        let observation = world.resource::<RequiredSingleObservation>();
        assert_eq!(observation.runs, 1);
        assert_eq!(observation.value, Some(7));
    }

    #[test]
    fn required_single_cardinality_failure_stops_before_the_system_body() {
        for (values, expected_reason) in [
            (&[][..], "No matching entities"),
            (&[(7, false), (9, false)][..], "Multiple matching entities"),
            (&[(7, true)][..], "No matching entities"),
        ] {
            let mut world = managed_world(values);
            let error = run_system_result(&mut world, observe_required_single)
                .expect_err("invalid cardinality should stop the managed System");

            assert!(
                error.reason().contains(expected_reason),
                "unexpected parameter validation reason: {}",
                error.reason()
            );
            assert!(
                !error.reason().contains("ManagedEntity"),
                "private managed filter leaked into diagnostics: {}",
                error.reason()
            );
            assert_eq!(world.resource::<RequiredSingleObservation>().runs, 0);
        }
    }

    #[test]
    fn required_single_tuple_returns_the_managed_identity_without_an_extra_lookup() {
        let mut world = managed_world(&[(11, false)]);
        let (raw, managed, _) = world
            .query::<(bevy_ecs::entity::Entity, &ManagedEntity, &Marker)>()
            .single(&world)
            .expect("test World should have one managed marker");
        let expected = managed.handle(raw);

        run_system(&mut world, observe_required_single_tuple);

        let observation = world.resource::<RequiredSingleObservation>();
        assert_eq!(observation.runs, 1);
        assert_eq!(observation.value, Some(11));
        assert_eq!(observation.entity, Some(expected));
    }

    #[test]
    fn required_single_mutates_only_the_enabled_managed_entity() {
        let mut world = managed_world(&[(4, false), (9, true)]);
        run_system(&mut world, increment_required_single);

        let values: Vec<_> = world
            .query_filtered::<&Marker, Allow<Disabled>>()
            .iter(&world)
            .map(|value| value.0)
            .collect();
        assert!(values.contains(&5));
        assert!(values.contains(&9));
    }

    #[test]
    fn point_lookups_reject_a_row_with_mismatched_private_provenance() {
        let mut world = managed_world(&[(7, false)]);
        let (raw, managed) = world
            .query::<(bevy_ecs::entity::Entity, &ManagedEntity)>()
            .single(&world)
            .expect("test World should have one managed marker");
        let target = managed.handle(raw);
        world.insert_resource(PointLookupTarget(target));
        world.insert_resource(PointLookupObservation::default());
        let corrupted = WorldGeneration::new(ApplicationId::from_raw(1), 99);
        world
            .entity_mut(raw)
            .insert(ManagedEntity::for_generation(corrupted));

        run_system(&mut world, observe_point_lookup);
        run_system(&mut world, mutate_point_lookup);

        let observation = world.resource::<PointLookupObservation>();
        assert!(matches!(
            observation.read,
            Some(Err(QueryEntityError::DoesNotMatch { entity })) if entity == target
        ));
        assert!(matches!(
            observation.write,
            Some(Err(QueryEntityError::DoesNotMatch { entity })) if entity == target
        ));
        assert_eq!(world.get::<Marker>(raw).map(|marker| marker.0), Some(7));
    }
}