@@ -126,6 +126,31 @@ impl<'w> EntityRef<'w> {
126
126
// SAFETY: entity_location is valid, component_id is valid as checked by the line above
127
127
unsafe { get_component ( self . world , component_id, self . entity , self . location ) }
128
128
}
129
+
130
+ /// Gets the component of the given [`ComponentId`] from the entity.
131
+ ///
132
+ /// **You should prefer to use the typed API where possible and only
133
+ /// use this in cases where the actual component types are not known at
134
+ /// compile time.**
135
+ ///
136
+ /// Unlike [`EntityRef::get`], this returns a raw pointer to the component,
137
+ /// which is only valid while the `'w` borrow of the lifetime is active.
138
+ ///
139
+ /// # Safety
140
+ ///
141
+ /// - The returned reference must never alias a mutable borrow of this component.
142
+ /// - The returned reference must not be used after this component is moved which
143
+ /// may happen from **any** `insert_component`, `remove_component` or `despawn`
144
+ /// operation on this world (non-exhaustive list).
145
+ #[ inline]
146
+ pub unsafe fn get_unchecked_mut_by_id (
147
+ & self ,
148
+ component_id : ComponentId ,
149
+ ) -> Option < MutUntyped < ' w > > {
150
+ self . world . components ( ) . get_info ( component_id) ?;
151
+ // SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is promised by the caller
152
+ unsafe { get_mut_by_id ( self . world , self . entity , self . location , component_id) }
153
+ }
129
154
}
130
155
131
156
impl < ' w > From < EntityMut < ' w > > for EntityRef < ' w > {
@@ -555,7 +580,7 @@ impl<'w> EntityMut<'w> {
555
580
#[ inline]
556
581
pub fn get_mut_by_id ( & mut self , component_id : ComponentId ) -> Option < MutUntyped < ' _ > > {
557
582
self . world . components ( ) . get_info ( component_id) ?;
558
- // SAFETY: entity_location is valid, component_id is valid as checked by the line above
583
+ // SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is unique
559
584
unsafe { get_mut_by_id ( self . world , self . entity , self . location , component_id) }
560
585
}
561
586
}
@@ -889,15 +914,17 @@ pub(crate) unsafe fn get_mut<T: Component>(
889
914
)
890
915
}
891
916
892
- // SAFETY: EntityLocation must be valid, component_id must be valid
917
+ // SAFETY:
918
+ // - EntityLocation must be valid, component_id must be valid
919
+ // - world access to the component must be valid, either because the caller has a `&mut` world or it synchronizes access like systems do
893
920
#[ inline]
894
921
pub ( crate ) unsafe fn get_mut_by_id (
895
- world : & mut World ,
922
+ world : & World ,
896
923
entity : Entity ,
897
924
location : EntityLocation ,
898
925
component_id : ComponentId ,
899
926
) -> Option < MutUntyped > {
900
- // SAFETY: world access is unique , entity location and component_id required to be valid
927
+ // SAFETY: world access promised by the caller , entity location and component_id required to be valid
901
928
get_component_and_ticks ( world, component_id, entity, location) . map ( |( value, ticks) | {
902
929
MutUntyped {
903
930
value : value. assert_unique ( ) ,
0 commit comments