evenio 0.6.0

An event-driven entity component system
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
//! Defines the [`World`] and related APIs.

#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec, vec::Vec};
use core::alloc::Layout;
use core::any::{self, TypeId};
use core::fmt::Write;
use core::marker::PhantomData;
use core::mem;
use core::ptr::NonNull;

use crate::access::ComponentAccess;
use crate::archetype::Archetypes;
use crate::component::{
    AddComponent, Component, ComponentDescriptor, ComponentId, ComponentInfo, Components,
    RemoveComponent,
};
use crate::drop::{drop_fn_of, DropFn};
use crate::entity::{Entities, EntityId, EntityLocation, ReservedEntities};
use crate::event::{
    AddGlobalEvent, AddTargetedEvent, Despawn, Event, EventDescriptor, EventKind, EventMeta,
    EventPtr, EventQueue, GlobalEvent, GlobalEventId, GlobalEventIdx, GlobalEventInfo,
    GlobalEvents, Insert, Remove, RemoveGlobalEvent, RemoveTargetedEvent, Spawn, TargetedEvent,
    TargetedEventId, TargetedEventIdx, TargetedEventInfo, TargetedEvents,
};
use crate::handler::{
    AddHandler, Handler, HandlerConfig, HandlerId, HandlerInfo, HandlerInfoInner, HandlerList,
    Handlers, IntoHandler, MaybeInvalidAccess, ReceivedEventId, RemoveHandler,
};
use crate::mutability::{Mutability, Mutable};

/// A container for all data in the ECS. This includes entities, components,
/// handlers, and events.
#[derive(Debug)]
pub struct World {
    entities: Entities,
    reserved_entities: ReservedEntities,
    components: Components,
    handlers: Handlers,
    archetypes: Archetypes,
    global_events: GlobalEvents,
    targeted_events: TargetedEvents,
    event_queue: EventQueue,
    /// So the world doesn't accidentally implement `Send` or `Sync`.
    _marker: PhantomData<*const ()>,
}

impl World {
    /// Creates a new, empty world.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// let mut world = World::new();
    /// ```
    pub fn new() -> Self {
        Self {
            entities: Entities::new(),
            reserved_entities: ReservedEntities::new(),
            components: Components::new(),
            handlers: Handlers::new(),
            archetypes: Archetypes::new(),
            global_events: GlobalEvents::new(),
            targeted_events: TargetedEvents::new(),
            event_queue: EventQueue::new(),
            _marker: PhantomData,
        }
    }

    /// Broadcast a global event to all handlers in this world. All handlers
    /// which listen for this event
    ///
    /// Any events sent by handlers will also broadcast. This process continues
    /// recursively until all events have finished broadcasting.
    ///
    /// See also [`World::send_to`] to send a [`TargetedEvent`].
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(GlobalEvent)]
    /// struct MyEvent(i32);
    ///
    /// fn my_handler(r: Receiver<MyEvent>) {
    ///     println!("got event: {}", r.event.0);
    /// }
    ///
    /// let mut world = World::new();
    ///
    /// world.add_handler(my_handler);
    /// world.send(MyEvent(123));
    /// ```
    ///
    /// Output:
    ///
    /// ```txt
    /// got event: 123
    /// ```
    pub fn send<E: GlobalEvent>(&mut self, event: E) {
        let idx = self.add_global_event::<E>().index();

        unsafe { self.event_queue.push_front_global(event, idx) };

        self.flush_event_queue();
    }

    /// Broadcast a targeted event to all handlers in this world.
    ///
    /// Any events sent by handlers will also broadcast. This process continues
    /// recursively until all events have finished broadcasting.
    ///
    /// See also [`World::send`] to send a [`GlobalEvent`].
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(TargetedEvent)]
    /// struct MyEvent(i32);
    ///
    /// fn my_handler(r: Receiver<MyEvent, EntityId>) {
    ///     println!("target of received event is {:?}", r.query);
    /// }
    ///
    /// let mut world = World::new();
    ///
    /// world.add_handler(my_handler);
    ///
    /// let target = world.spawn();
    ///
    /// // Send my event to `target` entity.
    /// world.send_to(target, MyEvent(123));
    /// ```
    pub fn send_to<E: TargetedEvent>(&mut self, target: EntityId, event: E) {
        let idx = self.add_targeted_event::<E>().index();

        unsafe { self.event_queue.push_front_targeted(target, event, idx) };

        self.flush_event_queue();
    }

    /// Creates a new entity, returns its [`EntityId`], and sends the [`Spawn`]
    /// event to signal its creation.
    ///
    /// The new entity is spawned without any components attached. The returned
    /// `EntityId` will not have been used by any previous entities in this
    /// world.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// let mut world = World::new();
    /// let id = world.spawn();
    ///
    /// assert!(world.entities().contains(id));
    /// ```
    pub fn spawn(&mut self) -> EntityId {
        let id = self.reserved_entities.reserve(&self.entities);

        self.send(Spawn(id));

        id
    }

    /// Sends the [`Insert`] event.
    ///
    /// This is equivalent to:
    ///
    /// ```
    /// # use evenio::prelude::*;
    /// #
    /// # let mut world = World::new();
    /// #
    /// # let entity = world.spawn();
    /// #
    /// # #[derive(Component)]
    /// # struct C;
    /// #
    /// # let component = C;
    /// #
    /// world.send_to(entity, Insert(component));
    /// ```
    pub fn insert<C: Component>(&mut self, entity: EntityId, component: C) {
        self.send_to(entity, Insert(component))
    }

    /// Sends the [`Remove`] event.
    ///
    /// This is equivalent to:
    ///
    /// ```
    /// # use evenio::prelude::*;
    /// #
    /// # let mut world = World::new();
    /// #
    /// # let entity = world.spawn();
    /// #
    /// # #[derive(Component)]
    /// # struct C;
    /// #
    /// world.send_to(entity, Remove::<C>);
    /// ```
    pub fn remove<C: Component>(&mut self, entity: EntityId) {
        self.send_to(entity, Remove::<C>)
    }

    /// Sends the [`Despawn`] event.
    ///
    /// This is equivalent to:
    ///
    /// ```
    /// # use evenio::prelude::*;
    /// #
    /// # let mut world = World::new();
    /// #
    /// # let entity = world.spawn();
    /// #
    /// world.send_to(entity, Despawn);
    /// ```
    pub fn despawn(&mut self, entity: EntityId) {
        self.send_to(entity, Despawn)
    }

    /// Gets an immutable reference to component `C` on `entity`. Returns `None`
    /// if `entity` doesn't exist or doesn't have the requested component.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(Component, PartialEq, Debug)]
    /// struct MyComponent(i32);
    ///
    /// let mut world = World::new();
    ///
    /// let e = world.spawn();
    /// world.insert(e, MyComponent(123));
    ///
    /// assert_eq!(world.get::<MyComponent>(e), Some(&MyComponent(123)));
    /// ```
    pub fn get<C: Component>(&self, entity: EntityId) -> Option<&C> {
        let loc = self.entities.get(entity)?;

        let component_idx = self
            .components()
            .get_by_type_id(TypeId::of::<C>())?
            .id()
            .index();

        let arch = unsafe { self.archetypes().get(loc.archetype).unwrap_unchecked() };

        let col = arch.column_of(component_idx)?;

        Some(unsafe {
            &*col
                .data()
                .as_ptr()
                .cast_const()
                .cast::<C>()
                .add(loc.row.0 as usize)
        })
    }

    /// Gets a mutable reference to component `C` on `entity`. Returns `None` if
    /// `entity` doesn't exist or doesn't have the requested component.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(Component, PartialEq, Debug)]
    /// struct MyComponent(i32);
    ///
    /// let mut world = World::new();
    ///
    /// let e = world.spawn();
    /// world.insert(e, MyComponent(123));
    ///
    /// assert_eq!(world.get_mut::<MyComponent>(e), Some(&mut MyComponent(123)));
    /// ```
    pub fn get_mut<C: Component<Mutability = Mutable>>(
        &mut self,
        entity: EntityId,
    ) -> Option<&mut C> {
        let loc = self.entities.get(entity)?;

        let component_idx = self
            .components()
            .get_by_type_id(TypeId::of::<C>())?
            .id()
            .index();

        let arch = unsafe { self.archetypes().get(loc.archetype).unwrap_unchecked() };

        let col = arch.column_of(component_idx)?;

        Some(unsafe { &mut *col.data().as_ptr().cast::<C>().add(loc.row.0 as usize) })
    }

    pub(crate) fn try_add_handler<H: IntoHandler<M>, M>(
        &mut self,
        handler: H,
    ) -> Result<HandlerId, String> {
        let mut handler = handler.into_handler();
        let mut config = HandlerConfig::default();

        let type_id = handler.type_id();

        if let Some(type_id) = type_id {
            if let Some(info) = self.handlers.get_by_type_id(type_id) {
                return Ok(info.id());
            }
        }

        let handler_name = handler.name();

        if let Err(e) = handler.init(self, &mut config) {
            return Err(format!("initialization of {handler_name} failed: {e}"));
        }

        let received_event = match config.received_event {
            ReceivedEventId::None => {
                return Err(format!(
                    "handler {handler_name} did not specify an event to receive"
                ));
            }
            ReceivedEventId::Ok(event) => event,
            ReceivedEventId::Invalid => {
                return Err(format!(
                    "handler {handler_name} attempted to listen for more than one event type"
                ))
            }
        };

        let received_event_access = match config.received_event_access {
            MaybeInvalidAccess::Ok(access) => access,
            MaybeInvalidAccess::Invalid => {
                return Err(format!(
                    "handler {handler_name} has conflicting access to the received event"
                ))
            }
        };

        let event_queue_access = match config.event_queue_access {
            MaybeInvalidAccess::Ok(access) => access,
            MaybeInvalidAccess::Invalid => {
                return Err(format!(
                    "handler {handler_name} has conflicting access to the event queue"
                ));
            }
        };

        let component_access_conjunction = config
            .component_accesses
            .iter()
            .fold(ComponentAccess::new_true(), |acc, a| acc.and(a));

        let conflicts = component_access_conjunction.collect_conflicts();

        if !conflicts.is_empty() {
            let mut errmsg = format!(
                "handler {handler_name} contains conflicting component access (aliased \
                 mutability)\nconflicting components are...\n"
            );

            for idx in conflicts {
                errmsg += "- ";
                match self.components.get_by_index(idx) {
                    Some(info) => errmsg += info.name(),
                    None => {
                        write!(&mut errmsg, "{idx:?}").unwrap();
                    }
                };
            }

            return Err(errmsg);
        }

        let component_access_disjunction = config
            .component_accesses
            .iter()
            .fold(ComponentAccess::new_false(), |acc, a| acc.or(a));

        let info = HandlerInfo::new(HandlerInfoInner {
            name: handler_name,
            id: HandlerId::NULL, // Filled in later.
            type_id,
            order: 0, // Filled in later.
            received_event,
            received_event_access,
            targeted_event_component_access: config.targeted_event_component_access,
            sent_untargeted_events: config.sent_global_events,
            sent_targeted_events: config.sent_targeted_events,
            event_queue_access,
            component_access: component_access_conjunction,
            archetype_filter: component_access_disjunction,
            referenced_components: config.referenced_components,
            priority: config.priority,
            handler,
        });

        let id = self.handlers.add(info);
        let info = self.handlers.get_mut(id).unwrap();

        self.archetypes.register_handler(info);

        self.send(AddHandler(id));

        Ok(id)
    }

    /// Adds a new handler to the world, returns its [`HandlerId`], and sends
    /// the [`AddHandler`] event to signal its creation.
    ///
    /// If the handler already exists (as determined by [`Handler::type_id`])
    /// then the `HandlerId` of the existing handler is returned and no
    /// event is sent.
    ///
    /// # Panics
    ///
    /// Panics if the configuration of the handler is invalid. This can occur
    /// when, for instance, the handler does not specify an event to receive.
    ///
    /// ```should_panic
    /// # use evenio::prelude::*;
    /// #
    /// # let mut world = World::new();
    /// #
    /// world.add_handler(|| {}); // Panics
    /// ```
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    /// # #[derive(GlobalEvent)]
    /// # struct MyEvent;
    ///
    /// fn my_handler(_: Receiver<MyEvent>) {};
    ///
    /// let mut world = World::new();
    /// let id = world.add_handler(my_handler);
    ///
    /// assert!(world.handlers().contains(id));
    /// ```
    ///
    /// [`Handler::type_id`]: crate::handler::Handler::type_id
    #[track_caller]
    pub fn add_handler<H: IntoHandler<M>, M>(&mut self, handler: H) -> HandlerId {
        match self.try_add_handler(handler) {
            Ok(id) => id,
            Err(e) => panic!("{e}"),
        }
    }

    /// Removes a handler from the world, returns its [`HandlerInfo`], and sends
    /// the [`RemoveHandler`] event. If the `handler` ID is invalid, then `None`
    /// is returned and no event is sent.
    ///
    /// # Example
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// let mut world = World::new();
    ///
    /// # #[derive(GlobalEvent)]
    /// # struct MyEvent;
    /// let handler_id = world.add_handler(|_: Receiver<MyEvent>| {});
    ///
    /// let info = world.remove_handler(handler_id).unwrap();
    ///
    /// assert_eq!(info.id(), handler_id);
    /// assert!(!world.handlers().contains(handler_id));
    /// ```
    pub fn remove_handler(&mut self, handler: HandlerId) -> Option<HandlerInfo> {
        if !self.handlers.contains(handler) {
            return None;
        }

        self.send(RemoveHandler(handler));

        let info = self.handlers.remove(handler).unwrap();

        self.archetypes.remove_handler(&info);

        Some(info)
    }

    /// Adds the component `C` to the world, returns its [`ComponentId`], and
    /// sends the [`AddComponent`] event to signal its creation.
    ///
    /// If the component already exists, then the [`ComponentId`] of the
    /// existing component is returned and no event is sent.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(Component)]
    /// struct MyComponent;
    ///
    /// let mut world = World::new();
    /// let id = world.add_component::<MyComponent>();
    ///
    /// assert_eq!(id, world.add_component::<MyComponent>());
    /// ```
    pub fn add_component<C: Component>(&mut self) -> ComponentId {
        let desc = ComponentDescriptor {
            name: any::type_name::<C>().into(),
            type_id: Some(TypeId::of::<C>()),
            layout: Layout::new::<C>(),
            drop: drop_fn_of::<C>(),
            mutability: Mutability::of::<C::Mutability>(),
        };

        unsafe { self.add_component_with_descriptor(desc) }
    }

    /// Adds a component described by a given [`ComponentDescriptor`].
    ///
    /// Like [`add_component`], an [`AddComponent`] event is sent if the
    /// component is newly added. If the [`TypeId`] of the component matches an
    /// existing component, then the existing component's [`ComponentId`] is
    /// returned and no event is sent.
    ///
    /// # Safety
    ///
    /// - If the component is given a [`TypeId`], then the `layout` and `drop`
    ///   function must be compatible with the Rust type identified by the type
    ///   ID.
    /// - Drop function must be safe to call with a pointer to the component as
    ///   described by [`DropFn`]'s documentation.
    ///
    /// [`add_component`]: World::add_component
    pub unsafe fn add_component_with_descriptor(
        &mut self,
        desc: ComponentDescriptor,
    ) -> ComponentId {
        let (id, is_new) = self.components.add(desc);

        if is_new {
            self.send(AddComponent(id));
        }

        id
    }

    /// Removes a component from the world and returns its [`ComponentInfo`]. If
    /// the `component` ID is invalid, then `None` is returned and the function
    /// has no effect.
    ///
    /// Removing a component has the following effects in the order listed:
    /// 1. The [`RemoveComponent`] event is sent.
    /// 2. All entities with the component are despawned.
    /// 3. All handlers that reference the component are removed.
    /// 4. The corresponding [`Insert`] events for the component are removed.
    /// 5. The corresponding [`Remove`] events for the component are removed.
    ///
    /// # Examples
    ///
    /// ```
    /// # use evenio::prelude::*;
    /// # let mut world = World::new();
    /// # #[derive(Component)] struct C;
    /// # #[derive(GlobalEvent)] struct E;
    /// #
    /// let component = world.add_component::<C>();
    /// let handler = world.add_handler(|_: Receiver<E>, _: Fetcher<&C>| {});
    ///
    /// assert!(world.components().contains(component));
    /// assert!(world.handlers().contains(handler));
    ///
    /// world.remove_component(component);
    ///
    /// assert!(!world.components().contains(component));
    /// // Handler was also removed because it references `C` in its `Fetcher`.
    /// assert!(!world.handlers().contains(handler));
    /// ```
    pub fn remove_component(&mut self, component: ComponentId) -> Option<ComponentInfo> {
        if !self.components.contains(component) {
            return None;
        }

        // Send event first.
        self.send(RemoveComponent(component));

        let despawn_idx = self.add_targeted_event::<Despawn>().index();

        // Attempt to despawn all entities that still have this component.
        for arch in self.archetypes.iter() {
            if arch.column_of(component.index()).is_some() {
                for &entity_id in arch.entity_ids() {
                    unsafe {
                        self.event_queue
                            .push_front_targeted(entity_id, Despawn, despawn_idx)
                    };
                }
            }
        }

        self.flush_event_queue();

        // Remove all handlers that reference this component.
        let mut handlers_to_remove = vec![];

        for handler in self.handlers.iter() {
            if handler.references_component(component.index()) {
                handlers_to_remove.push(handler.id());
            }
        }

        for handler_id in handlers_to_remove {
            self.remove_handler(handler_id);
        }

        let info = &self.components[component];

        // Remove all the `Insert` and `Remove` events for this component.
        let events_to_remove = info
            .insert_events()
            .iter()
            .copied()
            .chain(info.remove_events().iter().copied())
            .collect::<Vec<_>>();

        for event in events_to_remove {
            self.remove_targeted_event(event);
        }

        let mut info = self
            .components
            .remove(component)
            .expect("component should still exist");

        // Remove all archetypes with this component. If there are still entities with
        // the component by this point, then they will be silently removed.
        self.archetypes
            .remove_component(&mut info, &mut self.components, |id| {
                self.entities.remove(id);
            });

        Some(info)
    }

    /// Adds the global event `E` to the world, returns its [`GlobalEventId`],
    /// and sends the [`AddGlobalEvent`] event to signal its creation.
    ///
    /// If the event already exists, then the [`GlobalEventId`] of the existing
    /// event is returned and no event is sent.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(GlobalEvent)]
    /// struct MyEvent;
    ///
    /// let mut world = World::new();
    /// let id = world.add_global_event::<MyEvent>();
    ///
    /// assert_eq!(id, world.add_global_event::<MyEvent>());
    /// ```
    pub fn add_global_event<E: GlobalEvent>(&mut self) -> GlobalEventId {
        let desc = EventDescriptor::new::<E>(self);
        unsafe { self.add_global_event_with_descriptor(desc) }
    }

    /// Adds the targeted event `E` to the world, returns its
    /// [`TargetedEventId`], and sends the [`AddTargetedEvent`] event to
    /// signal its creation.
    ///
    /// If the event already exists, then the [`TargetedEventId`] of the
    /// existing event is returned and no event is sent.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(TargetedEvent)]
    /// struct MyEvent;
    ///
    /// let mut world = World::new();
    /// let id = world.add_targeted_event::<MyEvent>();
    ///
    /// assert_eq!(id, world.add_targeted_event::<MyEvent>());
    /// ```
    pub fn add_targeted_event<E: TargetedEvent>(&mut self) -> TargetedEventId {
        let desc = EventDescriptor::new::<E>(self);
        unsafe { self.add_targeted_event_with_descriptor(desc) }
    }

    /// Adds a global event described by a given [`EventDescriptor`].
    ///
    /// Like [`add_global_event`], an [`AddGlobalEvent`] event is sent if the
    /// event is newly added. If the [`TypeId`] of the event matches an
    /// existing global event, then the existing event's [`GlobalEventId`] is
    /// returned and no event is sent.
    ///
    /// # Safety
    ///
    /// - If the event is given a [`TypeId`], then the `layout` and `drop`
    ///   function must be compatible with the Rust type identified by the type
    ///   ID.
    /// - Drop function must be safe to call with a pointer to the event as
    ///   described by [`DropFn`]'s documentation.
    /// - The event's kind must be correct for the descriptor. See [`EventKind`]
    ///   for more information.
    ///
    /// [`add_global_event`]: World::add_global_event
    pub unsafe fn add_global_event_with_descriptor(
        &mut self,
        desc: EventDescriptor,
    ) -> GlobalEventId {
        let (id, is_new) = self.global_events.add(desc);

        if is_new {
            self.handlers.register_event(id.index());
            self.send(AddGlobalEvent(id));
        }

        id
    }

    /// Adds a targeted event described by a given [`EventDescriptor`].
    ///
    /// Like [`add_targeted_event`], an [`AddTargetedEvent`] event is sent if
    /// the event is newly added. If the [`TypeId`] of the event matches an
    /// existing targeted event, then the existing event's [`GlobalEventId`] is
    /// returned and no event is sent.
    ///
    /// # Safety
    ///
    /// - If the event is given a [`TypeId`], then the `layout` and `drop`
    ///   function must be compatible with the Rust type identified by the type
    ///   ID.
    /// - Drop function must be safe to call with a pointer to the event as
    ///   described by [`DropFn`]'s documentation.
    /// - The event's kind must be correct for the descriptor. See [`EventKind`]
    ///   for more information.
    ///
    /// [`add_targeted_event`]: World::add_targeted_event
    pub unsafe fn add_targeted_event_with_descriptor(
        &mut self,
        desc: EventDescriptor,
    ) -> TargetedEventId {
        let kind = desc.kind;

        let (id, is_new) = self.targeted_events.add(desc);

        if is_new {
            match kind {
                EventKind::Normal => {}
                EventKind::Insert { component_idx } => {
                    if let Some(info) = self.components.get_by_index_mut(component_idx) {
                        info.insert_events.insert(id);
                    }
                }
                EventKind::Remove { component_idx } => {
                    if let Some(info) = self.components.get_by_index_mut(component_idx) {
                        info.remove_events.insert(id);
                    }
                }
                EventKind::Spawn => {}
                EventKind::Despawn => {}
            }

            self.send(AddTargetedEvent(id))
        }

        id
    }

    /// Removes a global event from the world and returns its
    /// [`GlobalEventInfo`]. If the event ID is invalid, then `None` is
    /// returned and the function has no effect.
    ///
    /// Removing an event has the following additional effects in the order
    /// listed:
    /// 1. The [`RemoveTargetedEvent`] event is sent.
    /// 2. All handlers that send or receive the event are removed.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(GlobalEvent)]
    /// struct MyEvent;
    ///
    /// let mut world = World::new();
    ///
    /// let id = world.add_global_event::<MyEvent>();
    /// world.remove_global_event(id);
    ///
    /// assert!(!world.global_events().contains(id));
    /// ```
    pub fn remove_global_event(&mut self, event: GlobalEventId) -> Option<GlobalEventInfo> {
        assert!(self.event_queue.is_empty());

        if !self.global_events.contains(event) {
            return None;
        }

        // Send event before removing anything.
        self.send(RemoveGlobalEvent(event));

        // Remove all handlers that send or receive this event.
        let mut to_remove = vec![];

        for handler in self.handlers.iter() {
            if handler.received_event() == event.into()
                || handler.sent_global_events_bitset().contains(event.index())
            {
                to_remove.push(handler.id());
            }
        }

        for id in to_remove {
            self.remove_handler(id);
        }

        Some(self.global_events.remove(event).unwrap())
    }

    /// Removes a targeted event from the world and returns its
    /// [`TargetedEventInfo`]. If the event ID is invalid, then `None` is
    /// returned and the function has no effect.
    ///
    /// Removing an event has the following additional effects in the order
    /// listed:
    /// 1. The [`RemoveTargetedEvent`] event is sent.
    /// 2. All handlers that send or receive the event are removed.
    ///
    /// # Examples
    ///
    /// ```
    /// use evenio::prelude::*;
    ///
    /// #[derive(TargetedEvent)]
    /// struct MyEvent;
    ///
    /// let mut world = World::new();
    ///
    /// let id = world.add_targeted_event::<MyEvent>();
    /// world.remove_targeted_event(id);
    ///
    /// assert!(!world.targeted_events().contains(id));
    /// ```
    pub fn remove_targeted_event(&mut self, event: TargetedEventId) -> Option<TargetedEventInfo> {
        assert!(self.event_queue.is_empty());

        if !self.targeted_events.contains(event) {
            return None;
        }

        // Send event before doing anything else.
        self.send(RemoveTargetedEvent(event));

        // Remove all handlers that send or receive this event.
        let mut to_remove = vec![];

        for handler in self.handlers.iter() {
            if handler.received_event() == event.into()
                || handler
                    .sent_targeted_events_bitset()
                    .contains(event.index())
            {
                to_remove.push(handler.id());
            }
        }

        for id in to_remove {
            self.remove_handler(id);
        }

        let info = self.targeted_events.remove(event).unwrap();

        match info.kind() {
            EventKind::Normal => {}
            EventKind::Insert { component_idx } => {
                if let Some(info) = self.components.get_by_index_mut(component_idx) {
                    info.insert_events.remove(&event);
                }
            }
            EventKind::Remove { component_idx } => {
                if let Some(info) = self.components.get_by_index_mut(component_idx) {
                    info.remove_events.remove(&event);
                }
            }
            EventKind::Spawn => {}
            EventKind::Despawn => {}
        }

        Some(info)
    }

    /// Returns the [`Entities`] for this world.
    pub fn entities(&self) -> &Entities {
        &self.entities
    }

    /// Returns the [`Components`] for this world.  
    pub fn components(&self) -> &Components {
        &self.components
    }

    /// Returns the [`Handlers`] for this world.
    pub fn handlers(&self) -> &Handlers {
        &self.handlers
    }

    /// Returns the [`Archetypes`] for this world.
    pub fn archetypes(&self) -> &Archetypes {
        &self.archetypes
    }

    /// Returns the [`GlobalEvents`] for this world.
    pub fn global_events(&self) -> &GlobalEvents {
        &self.global_events
    }

    /// Returns the [`TargetedEvents`] for this world.
    pub fn targeted_events(&self) -> &TargetedEvents {
        &self.targeted_events
    }

    /// Send all queued events to handlers. The event queue will be empty after
    /// this call.
    fn flush_event_queue(&mut self) {
        'next_event: while let Some(item) = self.event_queue.pop_front() {
            struct EventDropper<'a> {
                event: NonNull<u8>,
                drop: DropFn,
                ownership_flag: bool,
                world: &'a mut World,
            }

            impl<'a> EventDropper<'a> {
                fn new(event: NonNull<u8>, drop: DropFn, world: &'a mut World) -> Self {
                    Self {
                        event,
                        drop,
                        ownership_flag: false,
                        world,
                    }
                }

                /// Extracts the event pointer and drop fn without running
                /// the destructor.
                #[inline]
                fn unpack(self) -> (NonNull<u8>, DropFn) {
                    let event = self.event;
                    let drop = self.drop;
                    mem::forget(self);

                    (event, drop)
                }

                /// Drops the held event.
                #[inline]
                unsafe fn drop_event(self) {
                    if let (event, Some(drop)) = self.unpack() {
                        drop(event);
                    }
                }
            }

            impl Drop for EventDropper<'_> {
                #[cold]
                fn drop(&mut self) {
                    // In case `Handler::run` unwinds, we need to drop the event we're holding on
                    // the stack as well as the events still the in the event queue.

                    #[cfg(feature = "std")]
                    debug_assert!(std::thread::panicking());

                    // Drop the held event.
                    if !self.ownership_flag {
                        if let Some(drop) = self.drop {
                            unsafe { drop(self.event) };
                        }
                    }

                    // Drop all events remaining in the event queue.
                    // This must be done here instead of the World's destructor because events
                    // could contain borrowed data.
                    for item in self.world.event_queue.iter() {
                        let drop = match item.meta {
                            EventMeta::Global { idx } => unsafe {
                                self.world
                                    .global_events
                                    .get_by_index(idx)
                                    .unwrap_unchecked()
                                    .drop()
                            },
                            EventMeta::Targeted { idx, .. } => unsafe {
                                self.world
                                    .targeted_events
                                    .get_by_index(idx)
                                    .unwrap_unchecked()
                                    .drop()
                            },
                        };

                        if let Some(drop) = drop {
                            unsafe { drop(item.event) };
                        }
                    }

                    self.world.event_queue.clear();
                }
            }

            let (mut ctx, event_kind, handlers, target_location) = match item.meta {
                EventMeta::Global { idx } => {
                    let info = unsafe { self.global_events.get_by_index(idx).unwrap_unchecked() };
                    let kind = info.kind();
                    let handlers: *const [_] =
                        unsafe { self.handlers.get_global_list(idx).unwrap_unchecked() }.slice();
                    let ctx = EventDropper::new(item.event, info.drop(), self);

                    let location = EntityLocation::NULL;

                    (ctx, kind, handlers, location)
                }
                EventMeta::Targeted { idx, target } => {
                    let info = unsafe { self.targeted_events.get_by_index(idx).unwrap_unchecked() };
                    let kind = info.kind();
                    let ctx = EventDropper::new(item.event, info.drop(), self);

                    let Some(location) = ctx.world.entities.get(target) else {
                        // Entity doesn't exist. Skip the event.
                        unsafe { ctx.drop_event() };
                        continue;
                    };

                    let arch = unsafe {
                        ctx.world
                            .archetypes
                            .get(location.archetype)
                            .unwrap_unchecked()
                    };

                    static EMPTY: HandlerList = HandlerList::new();

                    let handlers: *const [_] = arch.handler_list_for(idx).unwrap_or(&EMPTY).slice();

                    (ctx, kind, handlers, location)
                }
            };

            let events_before = ctx.world.event_queue.len();

            for mut info_ptr in unsafe { (*handlers).iter().copied() } {
                let info = unsafe { info_ptr.as_info_mut() };

                let handler: *mut dyn Handler = info.handler_mut();

                let event_ptr = EventPtr::new(ctx.event, NonNull::from(&mut ctx.ownership_flag));

                let world_cell = ctx.world.unsafe_cell_mut();

                unsafe { (*handler).run(info, event_ptr, target_location, world_cell) };

                // Did the handler take ownership of the event?
                if ctx.ownership_flag {
                    // Don't drop event since we don't own it anymore.
                    ctx.unpack();

                    // Reverse pushed events so they're handled in FIFO order.
                    unsafe { self.event_queue.reverse_from(events_before) };

                    continue 'next_event;
                }
            }

            // Reverse pushed events so they're handled in FIFO order.
            unsafe { ctx.world.event_queue.reverse_from(events_before) };

            match event_kind {
                EventKind::Normal => {
                    // Ordinary event. Run drop fn.
                    unsafe { ctx.drop_event() };
                }
                EventKind::Insert { component_idx } => {
                    debug_assert_ne!(target_location, EntityLocation::NULL);

                    let dst = unsafe {
                        ctx.world.archetypes.traverse_insert(
                            target_location.archetype,
                            component_idx,
                            &mut ctx.world.components,
                            &mut ctx.world.handlers,
                        )
                    };

                    // `Insert<C>` is `repr(transparent)`.
                    let component_ptr = ctx.event.as_ptr().cast_const();

                    unsafe {
                        ctx.world.archetypes.move_entity(
                            target_location,
                            dst,
                            [(component_idx, component_ptr)],
                            &mut ctx.world.entities,
                        )
                    };

                    // Inserted component is owned by the archetype now. We wait to unpack
                    // in case one of the above functions panics.
                    ctx.unpack();
                }
                EventKind::Remove { component_idx } => {
                    // `Remove` doesn't need drop.
                    let _ = ctx.unpack();

                    let dst = unsafe {
                        self.archetypes.traverse_remove(
                            target_location.archetype,
                            component_idx,
                            &mut self.components,
                            &mut self.handlers,
                        )
                    };

                    unsafe {
                        self.archetypes
                            .move_entity(target_location, dst, [], &mut self.entities)
                    };
                }
                EventKind::Spawn => {
                    // `Spawn` doesn't need drop.
                    let _ = ctx.unpack();

                    // Spawn all entities from the reserved entity queue.
                    self.reserved_entities
                        .spawn_all(&mut self.entities, |id| self.archetypes.spawn(id));
                }
                EventKind::Despawn => {
                    // `Despawn` doesn't need drop.
                    let _ = ctx.unpack();

                    unsafe {
                        self.archetypes
                            .remove_entity(target_location, &mut self.entities)
                    };

                    // Reset next key iter.
                    self.reserved_entities.refresh(&self.entities);
                }
            }
        }

        self.event_queue.clear();
    }

    /// Returns a new [`UnsafeWorldCell`] with permission to _read_ all data in
    /// this world.
    pub fn unsafe_cell(&self) -> UnsafeWorldCell {
        UnsafeWorldCell {
            world: NonNull::from(self),
            _marker: PhantomData,
        }
    }

    /// Returns a new [`UnsafeWorldCell`] with permission to _read and write_
    /// all data in this world.
    pub fn unsafe_cell_mut(&mut self) -> UnsafeWorldCell {
        UnsafeWorldCell {
            world: NonNull::from(self),
            _marker: PhantomData,
        }
    }
}

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

/// Reference to a [`World`] where all methods take `&self` and aliasing rules
/// are not checked. It is the caller's responsibility to ensure that
/// Rust's aliasing rules are not violated.
#[derive(Clone, Copy, Debug)]
pub struct UnsafeWorldCell<'a> {
    world: NonNull<World>,
    _marker: PhantomData<&'a World>,
}

impl<'a> UnsafeWorldCell<'a> {
    /// # Safety
    ///
    /// - Must be called from within a handler.
    /// - Must have permission to access the event queue mutably.
    /// - Event index must be correct for the given event.
    #[inline]
    pub unsafe fn send_global<E: Event>(self, event: E, idx: GlobalEventIdx) {
        unsafe {
            (*self.world.as_ptr())
                .event_queue
                .push_front_global(event, idx)
        }
    }

    /// # Safety
    ///
    /// - Must be called from within a handler.
    /// - Must have permission to access the event queue mutably.
    /// - Event index must be correct for the given event.
    #[inline]
    pub unsafe fn send_targeted<E: Event>(self, target: EntityId, event: E, idx: TargetedEventIdx) {
        unsafe {
            (*self.world.as_ptr())
                .event_queue
                .push_front_targeted(target, event, idx)
        }
    }

    /// # Safety
    ///
    /// - Must be called from within a handler.
    /// - Must have permission to access the event queue mutably.
    pub unsafe fn queue_spawn(self) -> EntityId {
        let entity_id = (*self.world.as_ptr())
            .reserved_entities
            .reserve(self.entities());

        entity_id
    }

    /// Returns the [`Entities`] for this world.
    pub fn entities(self) -> &'a Entities {
        unsafe { &(*self.world.as_ptr()).entities }
    }

    /// Returns the [`Components`] for this world.
    pub fn components(self) -> &'a Components {
        unsafe { &(*self.world.as_ptr()).components }
    }

    /// Returns the [`Handlers`] for this world.
    pub fn handlers(self) -> &'a Handlers {
        unsafe { &(*self.world.as_ptr()).handlers }
    }

    /// Returns the [`Archetypes`] for this world.
    pub fn archetypes(self) -> &'a Archetypes {
        unsafe { &(*self.world.as_ptr()).archetypes }
    }

    /// Returns the [`GlobalEvents`] for this world.
    pub fn global_events(self) -> &'a GlobalEvents {
        unsafe { &(*self.world.as_ptr()).global_events }
    }

    /// Returns the [`TargetedEvents`] for this world.
    pub fn targeted_events(self) -> &'a TargetedEvents {
        unsafe { &(*self.world.as_ptr()).targeted_events }
    }

    /// Returns an immutable reference to the underlying world.
    ///
    /// # Safety
    ///
    /// Must have permission to access the entire world immutably.
    pub fn world(self) -> &'a World {
        unsafe { &*self.world.as_ptr() }
    }

    /// Returns a mutable reference to the underlying world.
    ///
    /// # Safety
    ///
    /// Must have permission the access the entire world immutably.
    pub unsafe fn world_mut(self) -> &'a mut World {
        &mut *self.world.as_ptr()
    }
}

#[cfg(test)]
mod tests {
    use alloc::rc::Rc;
    use core::panic::AssertUnwindSafe;
    use std::panic;

    use crate::prelude::*;

    #[test]
    fn world_drops_events() {
        #[derive(GlobalEvent)]
        struct A(Rc<()>);

        #[derive(GlobalEvent)]
        struct B(Rc<()>);

        #[derive(GlobalEvent)]
        struct C(Rc<()>);

        let mut world = World::new();

        world.add_handler(|r: Receiver<A>, mut s: Sender<B>| {
            s.send(B(r.event.0.clone()));
            s.send(B(r.event.0.clone()));
        });

        world.add_handler(|r: Receiver<B>, mut s: Sender<C>| {
            s.send(C(r.event.0.clone()));
            s.send(C(r.event.0.clone()));
        });

        world.add_handler(|r: Receiver<C>| println!("got C {:?}", Rc::as_ptr(&r.event.0)));

        let rc = Rc::new(());

        world.send(A(rc.clone()));

        drop(world);

        assert_eq!(Rc::strong_count(&rc), 1);
    }

    #[test]
    fn world_drops_events_on_panic() {
        #[derive(GlobalEvent)]
        struct A(Rc<()>);

        impl Drop for A {
            fn drop(&mut self) {
                eprintln!("calling A destructor");
            }
        }

        #[derive(GlobalEvent)]
        struct B(Rc<()>);

        #[allow(dead_code)]
        #[derive(GlobalEvent)]
        struct C(Rc<()>);

        let mut world = World::new();

        world.add_handler(|r: Receiver<A>, mut s: Sender<B>| {
            s.send(B(r.event.0.clone()));
            s.send(B(r.event.0.clone()));
        });

        world.add_handler(|r: Receiver<B>, mut sender: Sender<C>| {
            sender.send(C(r.event.0.clone()));
            sender.send(C(r.event.0.clone()));
        });

        world.add_handler(|_: Receiver<C>| panic!("oops!"));

        let arc = Rc::new(());
        let arc_cloned = arc.clone();

        let mut world = AssertUnwindSafe(world);

        let res = panic::catch_unwind(move || world.send(A(arc_cloned)));

        assert_eq!(*res.unwrap_err().downcast::<&str>().unwrap(), "oops!");

        assert_eq!(Rc::strong_count(&arc), 1);
    }
}