@@ -1499,23 +1499,42 @@ impl World {
1499
1499
/// use this in cases where the actual types are not known at compile time.**
1500
1500
#[ inline]
1501
1501
pub fn get_resource_mut_by_id ( & mut self , component_id : ComponentId ) -> Option < MutUntyped < ' _ > > {
1502
+ // SAFETY: unique world access
1503
+ unsafe { self . get_resource_unchecked_mut_by_id ( component_id) }
1504
+ }
1505
+
1506
+ /// Gets a resource to the resource with the id [`ComponentId`] if it exists.
1507
+ /// The returned pointer may be used to modify the resource, as long as the mutable borrow
1508
+ /// of the [`World`] is still valid.
1509
+ ///
1510
+ /// **You should prefer to use the typed API [`World::get_resource_mut`] where possible and only
1511
+ /// use this in cases where the actual types are not known at compile time.**
1512
+ ///
1513
+ /// # Safety
1514
+ /// This will allow aliased mutable access to the given resource type. The caller must ensure
1515
+ /// that there is either only one mutable access or multiple immutable accesses at a time.
1516
+ #[ inline]
1517
+ pub unsafe fn get_resource_unchecked_mut_by_id (
1518
+ & self ,
1519
+ component_id : ComponentId ,
1520
+ ) -> Option < MutUntyped < ' _ > > {
1502
1521
let info = self . components . get_info ( component_id) ?;
1503
1522
if !info. is_send_and_sync ( ) {
1504
1523
self . validate_non_send_access_untyped ( info. name ( ) ) ;
1505
1524
}
1506
1525
1507
- let change_tick = self . change_tick ( ) ;
1508
-
1509
1526
let ( ptr, ticks) = self . get_resource_with_ticks ( component_id) ?;
1510
1527
1511
- // SAFETY: This function has exclusive access to the world so nothing aliases `ticks`.
1528
+ // SAFETY:
1512
1529
// - index is in-bounds because the column is initialized and non-empty
1513
- // - no other reference to the ticks of the same row can exist at the same time
1514
- let ticks = unsafe { Ticks :: from_tick_cells ( ticks, self . last_change_tick ( ) , change_tick) } ;
1530
+ // - no other reference to the ticks, because the caller promises it
1531
+ let ticks = unsafe {
1532
+ Ticks :: from_tick_cells ( ticks, self . last_change_tick ( ) , self . read_change_tick ( ) )
1533
+ } ;
1515
1534
1516
1535
Some ( MutUntyped {
1517
1536
// SAFETY: This function has exclusive access to the world so nothing aliases `ptr`.
1518
- value : unsafe { ptr. assert_unique ( ) } ,
1537
+ value : ptr. assert_unique ( ) ,
1519
1538
ticks,
1520
1539
} )
1521
1540
}
@@ -1574,7 +1593,7 @@ impl World {
1574
1593
component_id : ComponentId ,
1575
1594
) -> Option < MutUntyped < ' _ > > {
1576
1595
self . components ( ) . get_info ( component_id) ?;
1577
- // SAFETY: entity_location is valid, component_id is valid as checked by the line above
1596
+ // SAFETY: entity_location is valid, component_id is valid as checked by the line above, world access is unique
1578
1597
unsafe {
1579
1598
get_mut_by_id (
1580
1599
self ,
0 commit comments