1
1
use crate :: {
2
2
archetype:: { Archetype , ArchetypeId , Archetypes } ,
3
3
bundle:: { Bundle , BundleInfo } ,
4
- change_detection:: { MutUntyped , TicksMut } ,
5
- component:: {
6
- Component , ComponentId , ComponentStorage , ComponentTicks , Components , StorageType ,
7
- } ,
4
+ change_detection:: MutUntyped ,
5
+ component:: { Component , ComponentId , ComponentTicks , Components , StorageType } ,
8
6
entity:: { Entities , Entity , EntityLocation } ,
9
7
removal_detection:: RemovedComponentEvents ,
10
8
storage:: Storages ,
@@ -79,12 +77,14 @@ impl<'w> EntityRef<'w> {
79
77
80
78
#[ inline]
81
79
pub fn contains_id ( & self , component_id : ComponentId ) -> bool {
82
- contains_component_with_id ( self . world , component_id, self . location )
80
+ self . as_unsafe_world_cell_readonly ( )
81
+ . contains_id ( component_id)
83
82
}
84
83
85
84
#[ inline]
86
85
pub fn contains_type_id ( & self , type_id : TypeId ) -> bool {
87
- contains_component_with_type ( self . world , type_id, self . location )
86
+ self . as_unsafe_world_cell_readonly ( )
87
+ . contains_type_id ( type_id)
88
88
}
89
89
90
90
#[ inline]
@@ -209,12 +209,14 @@ impl<'w> EntityMut<'w> {
209
209
210
210
#[ inline]
211
211
pub fn contains_id ( & self , component_id : ComponentId ) -> bool {
212
- contains_component_with_id ( self . world , component_id, self . location )
212
+ self . as_unsafe_world_cell_readonly ( )
213
+ . contains_id ( component_id)
213
214
}
214
215
215
216
#[ inline]
216
217
pub fn contains_type_id ( & self , type_id : TypeId ) -> bool {
217
- contains_component_with_type ( self . world , type_id, self . location )
218
+ self . as_unsafe_world_cell_readonly ( )
219
+ . contains_type_id ( type_id)
218
220
}
219
221
220
222
#[ inline]
@@ -600,19 +602,10 @@ impl<'w> EntityMut<'w> {
600
602
/// which is only valid while the [`EntityMut`] is alive.
601
603
#[ inline]
602
604
pub fn get_by_id ( & self , component_id : ComponentId ) -> Option < Ptr < ' _ > > {
603
- let info = self . world . components ( ) . get_info ( component_id) ?;
604
605
// SAFETY:
605
- // - entity_location is valid
606
- // - component_id is valid as checked by the line above
607
- // - the storage type is accurate as checked by the fetched ComponentInfo
608
- unsafe {
609
- self . world . get_component (
610
- component_id,
611
- info. storage_type ( ) ,
612
- self . entity ,
613
- self . location ,
614
- )
615
- }
606
+ // - `&self` ensures that no mutable references exist to this entity's components.
607
+ // - `as_unsafe_world_cell_readonly` gives read only permission for all components on this entity
608
+ unsafe { self . as_unsafe_world_cell_readonly ( ) . get_by_id ( component_id) }
616
609
}
617
610
618
611
/// Gets a [`MutUntyped`] of the component of the given [`ComponentId`] from the entity.
@@ -625,32 +618,13 @@ impl<'w> EntityMut<'w> {
625
618
/// which is only valid while the [`EntityMut`] is alive.
626
619
#[ inline]
627
620
pub fn get_mut_by_id ( & mut self , component_id : ComponentId ) -> Option < MutUntyped < ' _ > > {
628
- self . world . components ( ) . get_info ( component_id) ?;
629
- // SAFETY: entity_location is valid, component_id is valid as checked by the line above
630
- unsafe { get_mut_by_id ( self . world , self . entity , self . location , component_id) }
631
- }
632
- }
633
-
634
- pub ( crate ) fn contains_component_with_type (
635
- world : & World ,
636
- type_id : TypeId ,
637
- location : EntityLocation ,
638
- ) -> bool {
639
- if let Some ( component_id) = world. components . get_id ( type_id) {
640
- contains_component_with_id ( world, component_id, location)
641
- } else {
642
- false
621
+ // SAFETY:
622
+ // - `&mut self` ensures that no references exist to this entity's components.
623
+ // - `as_unsafe_world_cell` gives mutable permission for all components on this entity
624
+ unsafe { self . as_unsafe_world_cell ( ) . get_mut_by_id ( component_id) }
643
625
}
644
626
}
645
627
646
- pub ( crate ) fn contains_component_with_id (
647
- world : & World ,
648
- component_id : ComponentId ,
649
- location : EntityLocation ,
650
- ) -> bool {
651
- world. archetypes [ location. archetype_id ] . contains ( component_id)
652
- }
653
-
654
628
/// Removes a bundle from the given archetype and returns the resulting archetype (or None if the
655
629
/// removal was invalid). in the event that adding the given bundle does not result in an Archetype
656
630
/// change. Results are cached in the Archetype Graph to avoid redundant work.
@@ -768,59 +742,6 @@ fn sorted_remove<T: Eq + Ord + Copy>(source: &mut Vec<T>, remove: &[T]) {
768
742
} ) ;
769
743
}
770
744
771
- // SAFETY: EntityLocation must be valid
772
- #[ inline]
773
- pub ( crate ) unsafe fn get_mut < T : Component > (
774
- world : & mut World ,
775
- entity : Entity ,
776
- location : EntityLocation ,
777
- ) -> Option < Mut < ' _ , T > > {
778
- let change_tick = world. change_tick ( ) ;
779
- let last_change_tick = world. last_change_tick ( ) ;
780
- // SAFETY:
781
- // - world access is unique
782
- // - entity location is valid
783
- // - returned component is of type T
784
- world
785
- . get_component_and_ticks_with_type (
786
- TypeId :: of :: < T > ( ) ,
787
- T :: Storage :: STORAGE_TYPE ,
788
- entity,
789
- location,
790
- )
791
- . map ( |( value, ticks) | Mut {
792
- // SAFETY:
793
- // - world access is unique and ties world lifetime to `Mut` lifetime
794
- // - `value` is of type `T`
795
- value : value. assert_unique ( ) . deref_mut :: < T > ( ) ,
796
- ticks : TicksMut :: from_tick_cells ( ticks, last_change_tick, change_tick) ,
797
- } )
798
- }
799
-
800
- // SAFETY: EntityLocation must be valid, component_id must be valid
801
- #[ inline]
802
- pub ( crate ) unsafe fn get_mut_by_id (
803
- world : & mut World ,
804
- entity : Entity ,
805
- location : EntityLocation ,
806
- component_id : ComponentId ,
807
- ) -> Option < MutUntyped < ' _ > > {
808
- let change_tick = world. change_tick ( ) ;
809
- // SAFETY: component_id is valid
810
- let info = world. components . get_info_unchecked ( component_id) ;
811
- // SAFETY:
812
- // - world access is unique
813
- // - entity location is valid
814
- // - returned component is of type T
815
- world
816
- . get_component_and_ticks ( component_id, info. storage_type ( ) , entity, location)
817
- . map ( |( value, ticks) | MutUntyped {
818
- // SAFETY: world access is unique and ties world lifetime to `MutUntyped` lifetime
819
- value : value. assert_unique ( ) ,
820
- ticks : TicksMut :: from_tick_cells ( ticks, world. last_change_tick ( ) , change_tick) ,
821
- } )
822
- }
823
-
824
745
/// Moves component data out of storage.
825
746
///
826
747
/// This function leaves the underlying memory unchanged, but the component behind
0 commit comments