Miscellaneous cleanups to World::resource_scope (#8256)

# Objective

* Make a safety comment less tautological.
* Clearly document that the function can run user code.
* Other small changes.
This commit is contained in:
JoJoJet 2023-03-30 06:17:08 -04:00 committed by GitHub
parent a141aed67d
commit dbb9e428cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1263,7 +1263,8 @@ impl World {
} }
} }
/// Temporarily removes the requested resource from this [`World`], then re-adds it before returning. /// Temporarily removes the requested resource from this [`World`], runs custom user code,
/// then re-adds the resource before returning.
/// ///
/// This enables safe simultaneous mutable access to both a resource and the rest of the [`World`]. /// This enables safe simultaneous mutable access to both a resource and the rest of the [`World`].
/// For more complex access patterns, consider using [`SystemState`](crate::system::SystemState). /// For more complex access patterns, consider using [`SystemState`](crate::system::SystemState).
@ -1293,7 +1294,6 @@ impl World {
.components .components
.get_resource_id(TypeId::of::<R>()) .get_resource_id(TypeId::of::<R>())
.unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::<R>())); .unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::<R>()));
// If the resource isn't send and sync, validate that we are on the main thread, so that we can access it.
let (ptr, mut ticks) = self let (ptr, mut ticks) = self
.storages .storages
.resources .resources
@ -1301,7 +1301,7 @@ impl World {
.and_then(|info| info.remove()) .and_then(|info| info.remove())
.unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::<R>())); .unwrap_or_else(|| panic!("resource does not exist: {}", std::any::type_name::<R>()));
// Read the value onto the stack to avoid potential mut aliasing. // Read the value onto the stack to avoid potential mut aliasing.
// SAFETY: pointer is of type R // SAFETY: `ptr` was obtained from the TypeId of `R`.
let mut value = unsafe { ptr.read::<R>() }; let mut value = unsafe { ptr.read::<R>() };
let value_mut = Mut { let value_mut = Mut {
value: &mut value, value: &mut value,
@ -1315,7 +1315,7 @@ impl World {
let result = f(self, value_mut); let result = f(self, value_mut);
assert!(!self.contains_resource::<R>(), assert!(!self.contains_resource::<R>(),
"Resource `{}` was inserted during a call to World::resource_scope.\n\ "Resource `{}` was inserted during a call to World::resource_scope.\n\
This is not allowed as the original resource is reinserted to the world after the FnOnce param is invoked.", This is not allowed as the original resource is reinserted to the world after the closure is invoked.",
std::any::type_name::<R>()); std::any::type_name::<R>());
OwningPtr::make(value, |ptr| { OwningPtr::make(value, |ptr| {