@@ -637,26 +637,31 @@ impl<'w> BundleInserter<'w> {
637
637
location : EntityLocation ,
638
638
bundle : T ,
639
639
) -> EntityLocation {
640
- /// # Safety
641
- /// Must
642
- unsafe fn trigger_hooks (
640
+ #[ inline]
641
+ fn trigger_hooks (
643
642
entity : Entity ,
644
643
bundle_info : & BundleInfo ,
645
644
add_bundle : & AddBundle ,
646
645
archetype : & Archetype ,
647
646
mut world : DeferredWorld ,
648
647
) {
649
648
if archetype. has_on_add ( ) {
650
- world. trigger_on_add (
651
- entity,
652
- bundle_info
653
- . iter_components ( )
654
- . zip ( add_bundle. bundle_status . iter ( ) )
655
- . filter ( |( _, & status) | status == ComponentStatus :: Added )
656
- . map ( |( id, _) | id) ,
657
- ) ;
649
+ // SAFETY: All components in the bundle are guarenteed to exist in the World
650
+ // as they must be initialized before creating the BundleInfo.
651
+ unsafe {
652
+ world. trigger_on_add (
653
+ entity,
654
+ bundle_info
655
+ . iter_components ( )
656
+ . zip ( add_bundle. bundle_status . iter ( ) )
657
+ . filter ( |( _, & status) | status == ComponentStatus :: Added )
658
+ . map ( |( id, _) | id) ,
659
+ ) ;
660
+ }
658
661
}
659
662
if archetype. has_on_insert ( ) {
663
+ // SAFETY: All components in the bundle are guarenteed to exist in the World
664
+ // as they must be initialized before creating the BundleInfo.
660
665
unsafe { world. trigger_on_insert ( entity, bundle_info. iter_components ( ) ) }
661
666
}
662
667
}
@@ -686,15 +691,8 @@ impl<'w> BundleInserter<'w> {
686
691
) ;
687
692
}
688
693
// SAFETY: We have no outstanding mutable references to world as they were dropped
689
- unsafe {
690
- trigger_hooks (
691
- entity,
692
- bundle_info,
693
- add_bundle,
694
- archetype,
695
- self . world . into_deferred ( ) ,
696
- ) ;
697
- }
694
+ let deferred_world = unsafe { self . world . into_deferred ( ) } ;
695
+ trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
698
696
location
699
697
}
700
698
InsertBundleResult :: NewArchetypeSameTable { new_archetype } => {
@@ -737,15 +735,8 @@ impl<'w> BundleInserter<'w> {
737
735
} ;
738
736
739
737
// SAFETY: We have no outstanding mutable references to world as they were dropped
740
- unsafe {
741
- trigger_hooks (
742
- entity,
743
- bundle_info,
744
- add_bundle,
745
- archetype,
746
- self . world . into_deferred ( ) ,
747
- ) ;
748
- }
738
+ let deferred_world = unsafe { self . world . into_deferred ( ) } ;
739
+ trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
749
740
750
741
new_location
751
742
}
@@ -839,15 +830,8 @@ impl<'w> BundleInserter<'w> {
839
830
} ;
840
831
841
832
// SAFETY: We have no outstanding mutable references to world as they were dropped
842
- unsafe {
843
- trigger_hooks (
844
- entity,
845
- bundle_info,
846
- add_bundle,
847
- archetype,
848
- self . world . into_deferred ( ) ,
849
- ) ;
850
- }
833
+ let deferred_world = unsafe { self . world . into_deferred ( ) } ;
834
+ trigger_hooks ( entity, bundle_info, add_bundle, archetype, deferred_world) ;
851
835
852
836
new_location
853
837
}
0 commit comments