@@ -128,6 +128,31 @@ impl<'w> EntityRef<'w> {
128
128
// SAFETY: entity_location is valid, component_id is valid as checked by the line above
129
129
unsafe { get_component ( self . world , component_id, self . entity , self . location ) }
130
130
}
131
+
132
+ /// Gets the component of the given [`ComponentId`] from the entity.
133
+ ///
134
+ /// **You should prefer to use the typed API where possible and only
135
+ /// use this in cases where the actual component types are not known at
136
+ /// compile time.**
137
+ ///
138
+ /// Unlike [`EntityRef::get`], this returns a raw pointer to the component,
139
+ /// which is only valid while the `'w` borrow of the lifetime is active.
140
+ ///
141
+ /// # Safety
142
+ ///
143
+ /// - The returned reference must never alias a mutable borrow of this component.
144
+ /// - The returned reference must not be used after this component is moved which
145
+ /// may happen from **any** `insert_component`, `remove_component` or `despawn`
146
+ /// operation on this world (non-exhaustive list).
147
+ #[ inline]
148
+ pub unsafe fn get_unchecked_mut_by_id (
149
+ & self ,
150
+ component_id : ComponentId ,
151
+ ) -> Option < MutUntyped < ' w > > {
152
+ self . world . components ( ) . get_info ( component_id) ?;
153
+ // SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is promised by the caller
154
+ get_mut_by_id ( self . world , self . entity , self . location , component_id)
155
+ }
131
156
}
132
157
133
158
impl < ' w > From < EntityMut < ' w > > for EntityRef < ' w > {
@@ -586,7 +611,7 @@ impl<'w> EntityMut<'w> {
586
611
#[ inline]
587
612
pub fn get_mut_by_id ( & mut self , component_id : ComponentId ) -> Option < MutUntyped < ' _ > > {
588
613
self . world . components ( ) . get_info ( component_id) ?;
589
- // SAFETY: entity_location is valid, component_id is valid as checked by the line above
614
+ // SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is unique
590
615
unsafe { get_mut_by_id ( self . world , self . entity , self . location , component_id) }
591
616
}
592
617
}
@@ -920,15 +945,17 @@ pub(crate) unsafe fn get_mut<T: Component>(
920
945
)
921
946
}
922
947
923
- // SAFETY: EntityLocation must be valid, component_id must be valid
948
+ // SAFETY:
949
+ // - EntityLocation must be valid, component_id must be valid
950
+ // - world access to the component must be valid, either because the caller has a `&mut` world or it synchronizes access like systems do
924
951
#[ inline]
925
952
pub ( crate ) unsafe fn get_mut_by_id (
926
- world : & mut World ,
953
+ world : & World ,
927
954
entity : Entity ,
928
955
location : EntityLocation ,
929
956
component_id : ComponentId ,
930
957
) -> Option < MutUntyped > {
931
- // SAFETY: world access is unique , entity location and component_id required to be valid
958
+ // SAFETY: world access promised by the caller , entity location and component_id required to be valid
932
959
get_component_and_ticks ( world, component_id, entity, location) . map ( |( value, ticks) | {
933
960
MutUntyped {
934
961
value : value. assert_unique ( ) ,
0 commit comments