-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Fixes #13466, implementing resource_scope_by_id #18527
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
base: main
Are you sure you want to change the base?
Conversation
Welcome, new contributor! Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨ |
crates/bevy_ecs/src/world/mod.rs
Outdated
/// | ||
/// 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). | ||
/// /// # Example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// /// # Example | |
/// | |
/// # Example |
Two points: you had an extra ///
, and you were missing a newline before the heading. I suspect these are related!
/// #[derive(Resource)] | ||
/// struct A(u32); | ||
/// #[derive(Component)] | ||
/// struct B(u32); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// struct B(u32); | |
/// struct B(u32); | |
/// |
/// ``` | ||
/// use bevy_ecs::prelude::*; | ||
/// use std::any::TypeId; | ||
/// #[derive(Resource)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// #[derive(Resource)] | |
/// | |
/// #[derive(Resource)] |
/// .get_resource_id(TypeId::of::<A>()) | ||
/// .expect("Failed to get component ID for MyResource"); | ||
/// world.resource_scope_by_id(component_id, |world: &mut World, mut a: Mut<A>| { | ||
/// let b = world.get_mut::<B>(entity).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should assert that you can't access A inside of this scope :)
crates/bevy_ecs/src/world/mod.rs
Outdated
/// assert_eq!(world.get_resource::<A>().unwrap().0, 2); | ||
/// ``` | ||
/// | ||
/// See also [`try_resource_scope_by_id`](Self::try_resource_scope_by_id). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// See also [`try_resource_scope_by_id`](Self::try_resource_scope_by_id). | |
/// See also [`try_resource_scope_by_id`](Self::try_resource_scope_by_id) for a non-panicking variant. |
crates/bevy_ecs/src/world/mod.rs
Outdated
/// assert_eq!(world.get_resource::<A>().unwrap().0, 2); | ||
/// ``` | ||
/// | ||
/// See also [`try_resource_scope_by_id`](Self::try_resource_scope_by_id). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These docs would benefit from a # Panics
section :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, we're trying to move away from implicit panics, and towards explicit error handling. I think that we should collapse this down into one method, which returns an Option here. That added safety + hassle + lower API surface is appropriate for a niche tool like this.
pub fn resource_scope_by_id<R: Resource, U>( | ||
&mut self, | ||
id: ComponentId, | ||
f: impl FnOnce(&mut World, Mut<R>) -> U, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The callback should accept MutUntyped instead. If you already know the concrete type why would you still use dynamic API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @notmd, if we use MutUntyped. How could we read the ptr's value?
let mut value = unsafe { ptr.read::<R>() };
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi sorry I totally forgot this. MutUntyped can construct directly with PtrMut, you don't need to read to concrete type. See https://dev-docs.bevyengine.org/bevy/ecs/change_detection/struct.MutUntyped.html
…some changes as suggested.
Objective
World::resource_scope_by_id
#13466, implementing resource_scope_by_idTesting
Showcase
Allows mutable retrieval of resources using component id in a way they are then inserted back into the world. Similar to resource_scope.