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
Add resource_scope
variant for nonsend resources
#6037
Comments
In theory, |
Hm, right. In that case it should be enough to just remove the |
let mut world = World::default();
world.insert_non_send_resource(A(0));
std::thread::scope(|_| {
world.resource_scope(|world: &mut World, mut value: Mut<A>| {
value.0 += 1;
assert!(!world.contains_resource::<A>());
});
assert_eq!(world.get_non_send_resource::<A>().unwrap().0, 1);
}); would have to be disallowed, right?
true? |
We store whether a resource is registered as |
Ah yes, forgot about that field. let component_info = self.components().get_info(component_id).unwrap();
if !component_info.is_send_and_sync() {
self.validate_non_send_access::<R>();
} |
I would like to try it, since noone is assigned. |
…vyengine#6113) # Objective Relaxes the trait bound for `World::resource_scope` to allow non-send resources. Fixes bevyengine#6037. ## Solution No big changes in code had to be made. Added a check so that the non-send resources won't be accessed from a different thread. --- ## Changelog - `World::resource_scope` accepts non-send resources now - `World::resource_scope` verifies non-send access if the resource is non-send - Two new tests are added, one for valid use of `World::resource_scope` with a non-send resource, and one for invalid use (calling it from a different thread, resulting in panic) Co-authored-by: Dawid Piotrowski <41804418+Pietrek14@users.noreply.github.com>
…vyengine#6113) # Objective Relaxes the trait bound for `World::resource_scope` to allow non-send resources. Fixes bevyengine#6037. ## Solution No big changes in code had to be made. Added a check so that the non-send resources won't be accessed from a different thread. --- ## Changelog - `World::resource_scope` accepts non-send resources now - `World::resource_scope` verifies non-send access if the resource is non-send - Two new tests are added, one for valid use of `World::resource_scope` with a non-send resource, and one for invalid use (calling it from a different thread, resulting in panic) Co-authored-by: Dawid Piotrowski <41804418+Pietrek14@users.noreply.github.com>
…vyengine#6113) # Objective Relaxes the trait bound for `World::resource_scope` to allow non-send resources. Fixes bevyengine#6037. ## Solution No big changes in code had to be made. Added a check so that the non-send resources won't be accessed from a different thread. --- ## Changelog - `World::resource_scope` accepts non-send resources now - `World::resource_scope` verifies non-send access if the resource is non-send - Two new tests are added, one for valid use of `World::resource_scope` with a non-send resource, and one for invalid use (calling it from a different thread, resulting in panic) Co-authored-by: Dawid Piotrowski <41804418+Pietrek14@users.noreply.github.com>
What problem does this solve or what need does it fill?
Bevy has a
resource_scope
method which lets you have mutable access to a resource and the world at the same time by temporarily removing the resource:Is is roughly equivalent to
Bevy also has the concept of non-send resources; resources which do not implement the
Send
auto trait (andSync
?) so any systems that access them are only scheduled on the main thread. This is useful for example for some scripting language runtimes or OS operations that can only run on one thread.There is, however, no variant of
resource_scope
which works for nonsend resources.What solution would you like?
Add a
non_send_resource_scope
function, which is pretty much a copy ofresource_scope
(bevy/crates/bevy_ecs/src/world/mod.rs
Line 1087 in bc863ce
R: Resource
bound toR: 'static
.The method also needs to validate that it is being run on the main thread, #6037 (comment).
What alternative(s) have you considered?
Write
instead. Less ergonomic and a tiny bit less performant.
The text was updated successfully, but these errors were encountered: