Skip to content

Commit 49a9517

Browse files
add get_resource_unchecked_mut_by_id
1 parent 7d9e864 commit 49a9517

File tree

1 file changed

+20
-1
lines changed
  • crates/bevy_ecs/src/world

1 file changed

+20
-1
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,6 +1401,25 @@ impl World {
14011401
/// use this in cases where the actual types are not known at compile time.**
14021402
#[inline]
14031403
pub fn get_resource_mut_by_id(&mut self, component_id: ComponentId) -> Option<MutUntyped<'_>> {
1404+
// SAFETY: unique world access
1405+
unsafe { self.get_resource_unchecked_mut_by_id(component_id) }
1406+
}
1407+
1408+
/// Gets a resource to the resource with the id [`ComponentId`] if it exists.
1409+
/// The returned pointer may be used to modify the resource, as long as the mutable borrow
1410+
/// of the [`World`] is still valid.
1411+
///
1412+
/// **You should prefer to use the typed API [`World::get_resource_mut`] where possible and only
1413+
/// use this in cases where the actual types are not known at compile time.**
1414+
///
1415+
/// # Safety
1416+
/// This will allow aliased mutable access to the given resource type. The caller must ensure
1417+
/// that there is either only one mutable access or multiple immutable accesses at a time.
1418+
#[inline]
1419+
pub unsafe fn get_resource_unchecked_mut_by_id(
1420+
&self,
1421+
component_id: ComponentId,
1422+
) -> Option<MutUntyped<'_>> {
14041423
let info = self.components.get_info(component_id)?;
14051424
if !info.is_send_and_sync() {
14061425
self.validate_non_send_access_untyped(info.name());
@@ -1479,7 +1498,7 @@ impl World {
14791498
component_id: ComponentId,
14801499
) -> Option<MutUntyped<'_>> {
14811500
self.components().get_info(component_id)?;
1482-
// SAFETY: entity_location is valid, component_id is valid as checked by the line above
1501+
// SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is unique
14831502
unsafe {
14841503
get_mut_by_id(
14851504
self,

0 commit comments

Comments
 (0)