Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - fix mutable aliases for a very short time if WorldCell is already borrowed #6639

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 24 additions & 28 deletions crates/bevy_ecs/src/world/world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,22 @@ pub struct WorldBorrow<'w, T> {
}

impl<'w, T> WorldBorrow<'w, T> {
fn new(
value: &'w T,
fn try_new(
value: impl FnOnce() -> Option<&'w T>,
archetype_component_id: ArchetypeComponentId,
access: Rc<RefCell<ArchetypeComponentAccess>>,
) -> Self {
) -> Option<Self> {
assert!(
access.borrow_mut().read(archetype_component_id),
"Attempted to immutably access {}, but it is already mutably borrowed",
std::any::type_name::<T>(),
);
Self {
let value = value()?;
Some(Self {
value,
archetype_component_id,
access,
}
})
}
}

Expand All @@ -129,21 +130,22 @@ pub struct WorldBorrowMut<'w, T> {
}

impl<'w, T> WorldBorrowMut<'w, T> {
fn new(
value: Mut<'w, T>,
fn try_new(
value: impl FnOnce() -> Option<Mut<'w, T>>,
archetype_component_id: ArchetypeComponentId,
access: Rc<RefCell<ArchetypeComponentAccess>>,
) -> Self {
) -> Option<Self> {
assert!(
access.borrow_mut().write(archetype_component_id),
"Attempted to mutably access {}, but it is already mutably borrowed",
std::any::type_name::<T>(),
);
Self {
let value = value()?;
Some(Self {
value,
archetype_component_id,
access,
}
})
}
}

Expand Down Expand Up @@ -189,12 +191,12 @@ impl<'w> WorldCell<'w> {
let archetype_component_id = self
.world
.get_resource_archetype_component_id(component_id)?;
Some(WorldBorrow::new(
WorldBorrow::try_new(
// SAFETY: ComponentId matches TypeId
unsafe { self.world.get_resource_with_id(component_id)? },
|| unsafe { self.world.get_resource_with_id(component_id) },
archetype_component_id,
self.access.clone(),
))
)
}

/// Gets a reference to the resource of the given type
Expand Down Expand Up @@ -222,15 +224,12 @@ impl<'w> WorldCell<'w> {
let archetype_component_id = self
.world
.get_resource_archetype_component_id(component_id)?;
Some(WorldBorrowMut::new(
WorldBorrowMut::try_new(
// SAFETY: ComponentId matches TypeId and access is checked by WorldBorrowMut
unsafe {
self.world
.get_resource_unchecked_mut_with_id(component_id)?
},
|| unsafe { self.world.get_resource_unchecked_mut_with_id(component_id) },
archetype_component_id,
self.access.clone(),
))
)
}

/// Gets a mutable reference to the resource of the given type
Expand Down Expand Up @@ -258,12 +257,12 @@ impl<'w> WorldCell<'w> {
let archetype_component_id = self
.world
.get_resource_archetype_component_id(component_id)?;
Some(WorldBorrow::new(
WorldBorrow::try_new(
// SAFETY: ComponentId matches TypeId
unsafe { self.world.get_non_send_with_id(component_id)? },
|| unsafe { self.world.get_non_send_with_id(component_id) },
archetype_component_id,
self.access.clone(),
))
)
}

/// Gets an immutable reference to the non-send resource of the given type, if it exists.
Expand Down Expand Up @@ -291,15 +290,12 @@ impl<'w> WorldCell<'w> {
let archetype_component_id = self
.world
.get_resource_archetype_component_id(component_id)?;
Some(WorldBorrowMut::new(
WorldBorrowMut::try_new(
// SAFETY: ComponentId matches TypeId and access is checked by WorldBorrowMut
unsafe {
self.world
.get_non_send_unchecked_mut_with_id(component_id)?
},
|| unsafe { self.world.get_non_send_unchecked_mut_with_id(component_id) },
archetype_component_id,
self.access.clone(),
))
)
}

/// Gets a mutable reference to the non-send resource of the given type, if it exists.
Expand Down