proof-engine 0.1.1

A mathematical rendering engine for Rust. Every visual is the output of a mathematical function.
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
//! The [`World`] is the central container of the ECS.
//!
//! It owns:
//! - An [`EntityAllocator`] for entity identity management.
//! - A `HashMap<TypeId, Box<dyn AnyComponentStorage>>` for all component data.
//! - A `HashMap<TypeId, Box<dyn Any + Send + Sync>>` for singleton resources.
//!
//! # Usage
//!
//! ```rust,ignore
//! let mut world = World::new();
//! let entity = world.spawn()
//!     .insert(Position { x: 0.0, y: 0.0 })
//!     .insert(Velocity { dx: 1.0, dy: 0.0 })
//!     .id();
//!
//! world.insert_resource(Gravity(9.81));
//! let gravity = world.resource::<Gravity>();
//! ```

use std::any::{Any, TypeId};
use std::collections::HashMap;
use std::marker::PhantomData;

use super::entity::{Entity, EntityAllocator};
use super::storage::{AnyComponentStorage, ComponentStorage, StorageSet, TypedStorage};

// ---------------------------------------------------------------------------
// Component / Resource marker traits
// ---------------------------------------------------------------------------

/// Marker trait for component types. Blanket-implemented for all eligible types.
pub trait Component: 'static + Send + Sync {}
impl<T: 'static + Send + Sync> Component for T {}

/// Marker trait for resource types. Blanket-implemented for all eligible types.
pub trait Resource: 'static + Send + Sync {}
impl<T: 'static + Send + Sync> Resource for T {}

// ---------------------------------------------------------------------------
// World
// ---------------------------------------------------------------------------

/// The central ECS world.
///
/// All entity/component/resource data lives here. Systems receive `&mut World`
/// (or access it through a [`crate::ecs::schedule::Schedule`]).
pub struct World {
    /// Entity identity management.
    pub(crate) entities: EntityAllocator,
    /// Component storages, keyed by component TypeId.
    pub(crate) components: HashMap<TypeId, Box<dyn AnyComponentStorage>>,
    /// Singleton resources, keyed by resource TypeId.
    pub(crate) resources: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
    /// Monotonically increasing world tick, advanced once per schedule run.
    pub(crate) tick: u64,
    /// Running entity count (mirrors `entities.len()`).
    entity_count: usize,
}

impl World {
    // -----------------------------------------------------------------------
    // Construction
    // -----------------------------------------------------------------------

    /// Create an empty world.
    pub fn new() -> Self {
        Self {
            entities: EntityAllocator::new(),
            components: HashMap::new(),
            resources: HashMap::new(),
            tick: 0,
            entity_count: 0,
        }
    }

    /// Create a world with pre-allocated capacity for `entity_cap` entities.
    pub fn with_capacity(entity_cap: usize) -> Self {
        Self {
            entities: EntityAllocator::with_capacity(entity_cap),
            components: HashMap::new(),
            resources: HashMap::new(),
            tick: 0,
            entity_count: 0,
        }
    }

    // -----------------------------------------------------------------------
    // Entity lifecycle
    // -----------------------------------------------------------------------

    /// Begin building a new entity. Returns an [`EntityBuilder`] for fluent insertion.
    ///
    /// # Example
    /// ```rust,ignore
    /// let e = world.spawn().insert(Health(100)).insert(Name("Bob")).id();
    /// ```
    pub fn spawn(&mut self) -> EntityBuilder<'_> {
        let entity = self.entities.allocate();
        self.entity_count += 1;
        EntityBuilder { world: self, entity }
    }

    /// Spawn an entity with no components. Returns the [`Entity`] handle directly.
    pub fn spawn_empty(&mut self) -> Entity {
        let entity = self.entities.allocate();
        self.entity_count += 1;
        entity
    }

    /// Despawn `entity`, removing all its components. Returns `true` if it was alive.
    pub fn despawn(&mut self, entity: Entity) -> bool {
        if !self.entities.is_alive(entity) {
            return false;
        }
        // Remove the entity from every component storage.
        for storage in self.components.values_mut() {
            storage.remove_erased(entity);
        }
        let freed = self.entities.free(entity);
        if freed {
            self.entity_count = self.entity_count.saturating_sub(1);
        }
        freed
    }

    /// Returns `true` if `entity` is currently alive.
    #[inline]
    pub fn is_alive(&self, entity: Entity) -> bool {
        self.entities.is_alive(entity)
    }

    /// Number of currently live entities.
    #[inline]
    pub fn entity_count(&self) -> usize {
        self.entity_count
    }

    /// Current world tick.
    #[inline]
    pub fn tick(&self) -> u64 {
        self.tick
    }

    /// Advance the world tick.
    pub fn advance_tick(&mut self) {
        self.tick += 1;
    }

    // -----------------------------------------------------------------------
    // Component access
    // -----------------------------------------------------------------------

    /// Insert (or replace) a component on `entity`.
    ///
    /// Returns the old component value if one existed.
    ///
    /// # Panics
    /// Panics in debug mode if `entity` is not alive.
    pub fn insert<T: Component>(&mut self, entity: Entity, component: T) -> Option<T> {
        debug_assert!(self.entities.is_alive(entity), "insert: entity {:?} is not alive", entity);
        // ComponentStorage::insert handles the replace case and returns the old value.
        self.get_or_create_storage_mut::<T>()
            .borrow_mut()
            .insert(entity, component)
    }

    /// Insert a component, returning `&mut Self` for chaining.
    pub fn insert_component<T: Component>(&mut self, entity: Entity, component: T) -> &mut Self {
        self.insert(entity, component);
        self
    }

    /// Remove a component from `entity`. Returns the component if present.
    pub fn remove<T: Component>(&mut self, entity: Entity) -> Option<T> {
        let storage = self.components.get_mut(&TypeId::of::<T>())?;
        let typed = storage
            .as_any_mut()
            .downcast_mut::<TypedStorage<T>>()
            .expect("storage downcast failed");
        typed.borrow_mut().remove_and_return(entity)
    }

    /// Get an immutable reference to the component `T` on `entity`.
    ///
    /// Returns `None` if the entity doesn't have this component.
    pub fn get<T: Component>(&self, entity: Entity) -> Option<&T> {
        let storage = self.components.get(&TypeId::of::<T>())?;
        let typed = storage
            .as_any()
            .downcast_ref::<TypedStorage<T>>()
            .expect("storage downcast failed");
        // SAFETY: We borrow the RefCell immutably. The returned reference is
        // tied to the lifetime of `self`, not the `Ref` guard, which is why
        // we use an unsafe pointer cast here. The World owns the storage and
        // ensures no aliased mutable borrows while immutable borrows exist
        // (caller must uphold this through the normal Rust borrow rules on `&self`).
        let guard = typed.borrow();
        let ptr = guard.get(entity)? as *const T;
        // SAFETY: The storage outlives `self`, and no `&mut World` can coexist
        // with this `&World` borrow.
        Some(unsafe { &*ptr })
    }

    /// Get a mutable reference to the component `T` on `entity`.
    pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Option<&mut T> {
        let storage = self.components.get_mut(&TypeId::of::<T>())?;
        let typed = storage
            .as_any_mut()
            .downcast_mut::<TypedStorage<T>>()
            .expect("storage downcast failed");
        let mut guard = typed.borrow_mut();
        let ptr = guard.get_mut(entity)? as *mut T;
        // SAFETY: exclusive access via &mut self.
        Some(unsafe { &mut *ptr })
    }

    /// Returns `true` if `entity` has component `T`.
    pub fn has<T: Component>(&self, entity: Entity) -> bool {
        self.components
            .get(&TypeId::of::<T>())
            .map(|s| s.contains_erased(entity))
            .unwrap_or(false)
    }

    /// Count how many entities have component `T`.
    pub fn component_count<T: Component>(&self) -> usize {
        self.components
            .get(&TypeId::of::<T>())
            .map(|s| s.len_erased())
            .unwrap_or(0)
    }

    /// Iterate over all entities that have component `T`, yielding `(Entity, &T)`.
    pub fn iter_component<T: Component>(&self) -> impl Iterator<Item = (Entity, &T)> {
        match self.components.get(&TypeId::of::<T>()) {
            None => ComponentIter::empty(),
            Some(storage) => {
                let typed = storage
                    .as_any()
                    .downcast_ref::<TypedStorage<T>>()
                    .expect("storage downcast failed");
                ComponentIter::new(typed)
            }
        }
    }

    // -----------------------------------------------------------------------
    // Resource access
    // -----------------------------------------------------------------------

    /// Insert a singleton resource into the world.
    /// Replaces any existing resource of the same type.
    pub fn insert_resource<T: Resource>(&mut self, resource: T) {
        self.resources.insert(TypeId::of::<T>(), Box::new(resource));
    }

    /// Remove a resource, returning it.
    pub fn remove_resource<T: Resource>(&mut self) -> Option<T> {
        let boxed = self.resources.remove(&TypeId::of::<T>())?;
        Some(*boxed.downcast::<T>().expect("resource downcast failed"))
    }

    /// Get an immutable reference to a resource.
    ///
    /// # Panics
    /// Panics if the resource does not exist. Use [`World::try_resource`] for a fallible version.
    pub fn resource<T: Resource>(&self) -> &T {
        self.try_resource::<T>()
            .unwrap_or_else(|| panic!("resource {} not found", std::any::type_name::<T>()))
    }

    /// Get a mutable reference to a resource.
    ///
    /// # Panics
    /// Panics if the resource does not exist.
    pub fn resource_mut<T: Resource>(&mut self) -> &mut T {
        self.try_resource_mut::<T>()
            .unwrap_or_else(|| panic!("resource {} not found", std::any::type_name::<T>()))
    }

    /// Try to get a resource; returns `None` if absent.
    pub fn try_resource<T: Resource>(&self) -> Option<&T> {
        self.resources
            .get(&TypeId::of::<T>())?
            .downcast_ref::<T>()
    }

    /// Try to get a mutable resource; returns `None` if absent.
    pub fn try_resource_mut<T: Resource>(&mut self) -> Option<&mut T> {
        self.resources
            .get_mut(&TypeId::of::<T>())?
            .downcast_mut::<T>()
    }

    /// Returns `true` if the resource `T` exists.
    pub fn has_resource<T: Resource>(&self) -> bool {
        self.resources.contains_key(&TypeId::of::<T>())
    }

    /// Get or insert a resource with a default value.
    pub fn get_resource_or_insert<T: Resource + Default>(&mut self) -> &mut T {
        self.resources
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::new(T::default()))
            .downcast_mut::<T>()
            .expect("resource downcast failed")
    }

    // -----------------------------------------------------------------------
    // Querying
    // -----------------------------------------------------------------------

    /// Returns an iterator over all entities and their `T` components.
    /// Equivalent to a single-component query.
    pub fn query_component<T: Component>(&self) -> Vec<(Entity, &T)> {
        self.iter_component::<T>().collect()
    }

    /// Returns entities matching a predicate on component `T`.
    pub fn query_with<T: Component, F: Fn(&T) -> bool>(&self, pred: F) -> Vec<Entity> {
        self.iter_component::<T>()
            .filter(|(_, c)| pred(c))
            .map(|(e, _)| e)
            .collect()
    }

    /// Returns all entities that have all of the given component TypeIds.
    /// Used internally by the query system.
    pub fn entities_with_all(&self, type_ids: &[TypeId]) -> Vec<Entity> {
        if type_ids.is_empty() {
            return self.entities.alive_entities();
        }

        // If any required component type has no storage, no entity can match.
        let all_types_registered = type_ids
            .iter()
            .all(|tid| self.components.contains_key(tid));
        if !all_types_registered {
            return Vec::new();
        }

        // Iterate all alive entities and filter by required types.
        // For large worlds this could be accelerated with archetype caching.
        self.entities
            .iter_alive()
            .filter(|e| {
                type_ids
                    .iter()
                    .all(|tid| self.components.get(tid).map_or(false, |s| s.contains_erased(*e)))
            })
            .collect()
    }

    /// Returns all entities that have component `T`.
    pub fn entities_with<T: Component>(&self) -> Vec<Entity> {
        self.iter_component::<T>().map(|(e, _)| e).collect()
    }

    // -----------------------------------------------------------------------
    // World maintenance
    // -----------------------------------------------------------------------

    /// Despawn all entities. Resources are preserved.
    pub fn clear(&mut self) {
        for storage in self.components.values_mut() {
            storage.clear_erased();
        }
        self.entities.clear();
        self.entity_count = 0;
    }

    /// Despawn all entities and remove all resources.
    pub fn clear_all(&mut self) {
        self.clear();
        self.resources.clear();
    }

    /// Number of component types currently registered.
    pub fn component_type_count(&self) -> usize {
        self.components.len()
    }

    /// Number of resources currently stored.
    pub fn resource_count(&self) -> usize {
        self.resources.len()
    }

    // -----------------------------------------------------------------------
    // Internal helpers
    // -----------------------------------------------------------------------

    /// Get or create the `TypedStorage<T>` for component type `T`.
    pub(crate) fn get_or_create_storage_mut<T: Component>(&mut self) -> &mut TypedStorage<T> {
        let entry = self
            .components
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::new(TypedStorage::<T>::new()));
        entry
            .as_any_mut()
            .downcast_mut::<TypedStorage<T>>()
            .expect("storage downcast failed")
    }

    /// Get the `TypedStorage<T>` immutably.
    pub(crate) fn get_storage<T: Component>(&self) -> Option<&TypedStorage<T>> {
        self.components
            .get(&TypeId::of::<T>())?
            .as_any()
            .downcast_ref::<TypedStorage<T>>()
    }

    /// Get the `TypedStorage<T>` mutably.
    pub(crate) fn get_storage_mut<T: Component>(&mut self) -> Option<&mut TypedStorage<T>> {
        self.components
            .get_mut(&TypeId::of::<T>())?
            .as_any_mut()
            .downcast_mut::<TypedStorage<T>>()
    }

    /// Register a component type without inserting any data.
    pub fn register<T: Component>(&mut self) {
        self.components
            .entry(TypeId::of::<T>())
            .or_insert_with(|| Box::new(TypedStorage::<T>::new()));
    }

    /// Perform a bulk operation on all components `T`.
    pub fn for_each<T: Component>(&self, mut f: impl FnMut(Entity, &T)) {
        if let Some(storage) = self.get_storage::<T>() {
            let borrowed = storage.borrow();
            for (&e, c) in borrowed.iter() {
                f(e, c);
            }
        }
    }

    /// Perform a bulk mutable operation on all components `T`.
    pub fn for_each_mut<T: Component>(&mut self, mut f: impl FnMut(Entity, &mut T)) {
        if let Some(storage) = self.get_storage_mut::<T>() {
            let mut borrowed = storage.borrow_mut();
            for (&e, c) in borrowed.iter_mut() {
                f(e, c);
            }
        }
    }
}

impl Default for World {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for World {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("World")
            .field("entity_count", &self.entity_count)
            .field("component_types", &self.components.len())
            .field("resources", &self.resources.len())
            .field("tick", &self.tick)
            .finish()
    }
}

// ---------------------------------------------------------------------------
// ComponentIter — helper for iter_component
// ---------------------------------------------------------------------------

/// Iterator over `(Entity, &T)` pairs from a component storage.
///
/// Eagerly collects entity + pointer pairs so iteration is O(n) dense.
/// Pointers into `Vec<T>` remain valid for `'w` because `&World` prevents
/// any mutation while the iterator is live.
pub struct ComponentIter<'w, T: Component> {
    items: Vec<(Entity, *const T)>,
    index: usize,
    _marker: PhantomData<&'w T>,
}

impl<'w, T: Component> ComponentIter<'w, T> {
    fn empty() -> Self {
        Self { items: Vec::new(), index: 0, _marker: PhantomData }
    }

    fn new(typed: &'w TypedStorage<T>) -> Self {
        let guard = typed.borrow();
        let items: Vec<(Entity, *const T)> = guard
            .iter()
            .map(|(&e, v)| (e, v as *const T))
            .collect();
        drop(guard); // RefCell unlocked; pointers remain valid via &World borrow
        Self { items, index: 0, _marker: PhantomData }
    }
}

impl<'w, T: Component> Iterator for ComponentIter<'w, T> {
    type Item = (Entity, &'w T);

    fn next(&mut self) -> Option<Self::Item> {
        let (entity, ptr) = *self.items.get(self.index)?;
        self.index += 1;
        // SAFETY: ptr was obtained from a &T inside a Vec<T> that lives for
        // 'w (the World borrow). No mutation can occur while &World is held.
        Some((entity, unsafe { &*ptr }))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.items.len() - self.index;
        (remaining, Some(remaining))
    }
}

// SAFETY: ComponentIter only holds raw pointers into data owned by World.
// The pointers are valid for 'w. T: Send + Sync per Component bound.
unsafe impl<'w, T: Component> Send for ComponentIter<'w, T> {}
unsafe impl<'w, T: Component> Sync for ComponentIter<'w, T> {}

// ---------------------------------------------------------------------------
// EntityBuilder
// ---------------------------------------------------------------------------

/// Fluent builder for constructing an entity with initial components.
///
/// Created by [`World::spawn`]. Commit by calling [`EntityBuilder::id`].
///
/// ```rust,ignore
/// let entity = world.spawn()
///     .insert(Position::default())
///     .insert(Velocity::default())
///     .id();
/// ```
pub struct EntityBuilder<'w> {
    world: &'w mut World,
    entity: Entity,
}

impl<'w> EntityBuilder<'w> {
    /// Add a component to the entity being built.
    pub fn insert<T: Component>(self, component: T) -> Self {
        // We need to split the borrow: `self.world` and `self.entity`.
        let entity = self.entity;
        self.world.insert(entity, component);
        Self { world: self.world, entity }
    }

    /// Add a component only if a condition is true.
    pub fn insert_if<T: Component>(self, condition: bool, component: impl FnOnce() -> T) -> Self {
        if condition {
            self.insert(component())
        } else {
            self
        }
    }

    /// Finalize the builder and return the entity handle.
    #[inline]
    pub fn id(self) -> Entity {
        self.entity
    }

    /// Get the entity handle without consuming the builder.
    #[inline]
    pub fn entity(&self) -> Entity {
        self.entity
    }

    /// Despawn this entity before finishing (for conditional logic).
    pub fn despawn(self) {
        let entity = self.entity;
        self.world.despawn(entity);
    }
}

// ---------------------------------------------------------------------------
// WorldCell — shared-reference access pattern (for parallel system stubs)
// ---------------------------------------------------------------------------

/// A wrapper around `*mut World` for use in system parameters.
/// Not actually thread-safe — this is a single-threaded proof-of-concept.
pub struct WorldCell {
    world: *mut World,
}

impl WorldCell {
    /// SAFETY: caller must ensure exclusive access for the duration.
    pub unsafe fn new(world: &mut World) -> Self {
        Self { world: world as *mut World }
    }

    pub fn get(&self) -> &World {
        unsafe { &*self.world }
    }

    pub fn get_mut(&self) -> &mut World {
        unsafe { &mut *self.world }
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    // Component types for testing
    #[derive(Debug, Clone, PartialEq)]
    struct Position { x: f32, y: f32 }

    #[derive(Debug, Clone, PartialEq)]
    struct Velocity { dx: f32, dy: f32 }

    #[derive(Debug, Clone, PartialEq)]
    struct Health(i32);

    #[derive(Debug, Clone, PartialEq)]
    struct Name(String);

    #[derive(Debug, Clone, PartialEq)]
    struct Gravity(f32);

    #[test]
    fn test_spawn_and_get() {
        let mut world = World::new();
        let e = world.spawn()
            .insert(Position { x: 1.0, y: 2.0 })
            .insert(Health(100))
            .id();

        assert!(world.is_alive(e));
        assert_eq!(world.get::<Position>(e), Some(&Position { x: 1.0, y: 2.0 }));
        assert_eq!(world.get::<Health>(e), Some(&Health(100)));
        assert_eq!(world.get::<Velocity>(e), None);
        assert_eq!(world.entity_count(), 1);
    }

    #[test]
    fn test_despawn() {
        let mut world = World::new();
        let e = world.spawn().insert(Position { x: 0.0, y: 0.0 }).id();
        assert!(world.is_alive(e));
        assert!(world.despawn(e));
        assert!(!world.is_alive(e));
        assert_eq!(world.entity_count(), 0);
        // Double-despawn is safe.
        assert!(!world.despawn(e));
    }

    #[test]
    fn test_insert_and_remove_component() {
        let mut world = World::new();
        let e = world.spawn_empty();
        world.insert(e, Health(50));
        assert_eq!(world.get::<Health>(e), Some(&Health(50)));

        let old = world.remove::<Health>(e);
        assert_eq!(old, Some(Health(50)));
        assert!(!world.has::<Health>(e));
    }

    #[test]
    fn test_get_mut() {
        let mut world = World::new();
        let e = world.spawn().insert(Health(10)).id();
        if let Some(h) = world.get_mut::<Health>(e) {
            h.0 += 5;
        }
        assert_eq!(world.get::<Health>(e), Some(&Health(15)));
    }

    #[test]
    fn test_resources() {
        let mut world = World::new();
        world.insert_resource(Gravity(9.81));
        assert!(world.has_resource::<Gravity>());
        assert_eq!(world.resource::<Gravity>(), &Gravity(9.81));
        world.resource_mut::<Gravity>().0 = 1.62;
        assert_eq!(world.resource::<Gravity>().0, 1.62);
        let removed = world.remove_resource::<Gravity>();
        assert_eq!(removed, Some(Gravity(1.62)));
        assert!(!world.has_resource::<Gravity>());
    }

    #[test]
    fn test_for_each() {
        let mut world = World::new();
        for i in 0..5i32 {
            world.spawn().insert(Health(i * 10)).insert(Position { x: i as f32, y: 0.0 });
        }
        let mut sum = 0;
        world.for_each::<Health>(|_e, h| { sum += h.0; });
        assert_eq!(sum, 0 + 10 + 20 + 30 + 40);
    }

    #[test]
    fn test_for_each_mut() {
        let mut world = World::new();
        for i in 0..3i32 {
            world.spawn().insert(Health(i));
        }
        world.for_each_mut::<Health>(|_e, h| { h.0 *= 2; });
        let values: Vec<i32> = world
            .entities_with::<Health>()
            .iter()
            .map(|&e| world.get::<Health>(e).unwrap().0)
            .collect();
        let mut sorted = values.clone();
        sorted.sort();
        assert_eq!(sorted, vec![0, 2, 4]);
    }

    #[test]
    fn test_iter_component() {
        let mut world = World::new();
        let e1 = world.spawn().insert(Name("Alice".to_string())).id();
        let e2 = world.spawn().insert(Name("Bob".to_string())).id();

        let names: Vec<_> = world.iter_component::<Name>()
            .map(|(e, n)| (e, n.0.clone()))
            .collect();
        assert_eq!(names.len(), 2);
    }

    #[test]
    fn test_entities_with_all() {
        let mut world = World::new();
        let e1 = world.spawn().insert(Position { x: 0.0, y: 0.0 }).insert(Velocity { dx: 1.0, dy: 0.0 }).id();
        let e2 = world.spawn().insert(Position { x: 1.0, y: 1.0 }).id(); // no Velocity
        let e3 = world.spawn().insert(Velocity { dx: 2.0, dy: 0.0 }).id(); // no Position

        let matches = world.entities_with_all(&[TypeId::of::<Position>(), TypeId::of::<Velocity>()]);
        assert_eq!(matches.len(), 1);
        assert!(matches.contains(&e1));
        assert!(!matches.contains(&e2));
        assert!(!matches.contains(&e3));
    }

    #[test]
    fn test_clear() {
        let mut world = World::new();
        for _ in 0..10 {
            world.spawn().insert(Health(1));
        }
        assert_eq!(world.entity_count(), 10);
        world.clear();
        assert_eq!(world.entity_count(), 0);
        assert_eq!(world.component_count::<Health>(), 0);
    }

    #[test]
    fn test_component_replace() {
        let mut world = World::new();
        let e = world.spawn().insert(Health(10)).id();
        world.insert(e, Health(99));
        assert_eq!(world.get::<Health>(e), Some(&Health(99)));
        assert_eq!(world.component_count::<Health>(), 1);
    }

    #[test]
    fn test_get_resource_or_insert_default() {
        #[derive(Default, PartialEq, Debug)]
        struct Counter(u32);

        let mut world = World::new();
        {
            let c = world.get_resource_or_insert::<Counter>();
            assert_eq!(c, &Counter(0));
            c.0 = 5;
        }
        assert_eq!(world.resource::<Counter>(), &Counter(5));
    }

    #[test]
    fn test_spawn_many() {
        let mut world = World::new();
        let mut entities = Vec::new();
        for i in 0..100i32 {
            let e = world.spawn().insert(Health(i)).id();
            entities.push(e);
        }
        assert_eq!(world.entity_count(), 100);
        // Despawn half.
        for &e in entities.iter().step_by(2) {
            world.despawn(e);
        }
        assert_eq!(world.entity_count(), 50);
    }

    #[test]
    fn test_entity_builder_insert_if() {
        let mut world = World::new();
        let e = world.spawn()
            .insert(Health(10))
            .insert_if(false, || Velocity { dx: 1.0, dy: 0.0 })
            .insert_if(true, || Position { x: 5.0, y: 5.0 })
            .id();

        assert!(world.has::<Health>(e));
        assert!(!world.has::<Velocity>(e));
        assert!(world.has::<Position>(e));
    }
}