Skip to content

Commit

Permalink
Rename UnsafeWorldCell::read_change_tick (#8588)
Browse files Browse the repository at this point in the history
# Objective

The method `UnsafeWorldCell::read_change_tick` is longer than it needs
to be. `World` only has a method called this because it has two methods
for getting a change tick: one that takes `&self` and one that takes
`&mut self`. Since this distinction is not applicable to
`UnsafeWorldCell`, we should just call this method `change_tick`.

## Solution

Deprecate the current method and add a new one called `change_tick`.

---

## Changelog

- Renamed `UnsafeWorldCell::read_change_tick` to `change_tick`.

## Migration Guide

The `UnsafeWorldCell` method `read_change_tick` has been renamed to
`change_tick`.
  • Loading branch information
JoJoJet authored May 10, 2023
1 parent 86aaad7 commit 35240fe
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/bevy_ecs/src/world/unsafe_world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
system::Resource,
};
use bevy_ptr::Ptr;
use std::{any::TypeId, cell::UnsafeCell, marker::PhantomData, sync::atomic::Ordering};
use std::{any::TypeId, cell::UnsafeCell, marker::PhantomData};

/// Variant of the [`World`] where resource and component accesses take `&self`, and the responsibility to avoid
/// aliasing violations are given to the caller instead of being checked at compile-time by rust's unique XOR shared rule.
Expand Down Expand Up @@ -191,13 +191,17 @@ impl<'w> UnsafeWorldCell<'w> {

/// Reads the current change tick of this world.
#[inline]
#[deprecated = "this method has been renamed to `UnsafeWorldCell::change_tick`"]
pub fn read_change_tick(self) -> Tick {
self.change_tick()
}

/// Gets the current change tick of this world.
#[inline]
pub fn change_tick(self) -> Tick {
// SAFETY:
// - we only access world metadata
let tick = unsafe { self.world_metadata() }
.change_tick
.load(Ordering::Acquire);
Tick::new(tick)
unsafe { self.world_metadata() }.read_change_tick()
}

#[inline]
Expand Down Expand Up @@ -382,7 +386,7 @@ impl<'w> UnsafeWorldCell<'w> {
// - index is in-bounds because the column is initialized and non-empty
// - the caller promises that no other reference to the ticks of the same row can exist at the same time
let ticks = unsafe {
TicksMut::from_tick_cells(ticks, self.last_change_tick(), self.read_change_tick())
TicksMut::from_tick_cells(ticks, self.last_change_tick(), self.change_tick())
};

Some(MutUntyped {
Expand Down Expand Up @@ -430,7 +434,7 @@ impl<'w> UnsafeWorldCell<'w> {
self,
component_id: ComponentId,
) -> Option<MutUntyped<'w>> {
let change_tick = self.read_change_tick();
let change_tick = self.change_tick();
// SAFETY: we only access data that the caller has ensured is unaliased and `self`
// has permission to access.
let (ptr, ticks) = unsafe { self.unsafe_world() }
Expand Down Expand Up @@ -650,9 +654,7 @@ impl<'w> UnsafeEntityCell<'w> {
#[inline]
pub unsafe fn get_mut<T: Component>(self) -> Option<Mut<'w, T>> {
// SAFETY: same safety requirements
unsafe {
self.get_mut_using_ticks(self.world.last_change_tick(), self.world.read_change_tick())
}
unsafe { self.get_mut_using_ticks(self.world.last_change_tick(), self.world.change_tick()) }
}

/// # Safety
Expand Down Expand Up @@ -744,7 +746,7 @@ impl<'w> UnsafeEntityCell<'w> {
ticks: TicksMut::from_tick_cells(
cells,
self.world.last_change_tick(),
self.world.read_change_tick(),
self.world.change_tick(),
),
})
}
Expand Down

0 comments on commit 35240fe

Please sign in to comment.