@@ -637,26 +637,31 @@ impl<'w> BundleInserter<'w> {
637637 location : EntityLocation ,
638638 bundle : T ,
639639 ) -> EntityLocation {
640- /// # Safety
641- /// Must
642- unsafe fn trigger_hooks (
640+ #[ inline]
641+ fn trigger_hooks (
643642 entity : Entity ,
644643 bundle_info : & BundleInfo ,
645644 add_bundle : & AddBundle ,
646645 archetype : & Archetype ,
647646 mut world : DeferredWorld ,
648647 ) {
649648 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+ }
658661 }
659662 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.
660665 unsafe { world. trigger_on_insert ( entity, bundle_info. iter_components ( ) ) }
661666 }
662667 }
@@ -686,15 +691,8 @@ impl<'w> BundleInserter<'w> {
686691 ) ;
687692 }
688693 // 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) ;
698696 location
699697 }
700698 InsertBundleResult :: NewArchetypeSameTable { new_archetype } => {
@@ -737,15 +735,8 @@ impl<'w> BundleInserter<'w> {
737735 } ;
738736
739737 // 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) ;
749740
750741 new_location
751742 }
@@ -839,15 +830,8 @@ impl<'w> BundleInserter<'w> {
839830 } ;
840831
841832 // 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) ;
851835
852836 new_location
853837 }
0 commit comments