-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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] - Allow access to non-send resource through World::resource_scope
#6113
Conversation
crates/bevy_ecs/src/lib.rs
Outdated
#[test] | ||
fn non_send_resource_scope() { | ||
let mut world = World::default(); | ||
world.insert_non_send_resource(A(0)); |
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.
I think we should probably use a different resource type that is deliberately not Send.
A small nit about the tests, but the logic itself looks sensible to me. Very nice first contribution! |
crates/bevy_ecs/src/lib.rs
Outdated
} | ||
|
||
#[test] | ||
#[should_panic] |
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.
This can probably use #[should_panic(expected = "fill this in with some error text")]
here.
crates/bevy_ecs/src/lib.rs
Outdated
// Should result in a panic | ||
world.resource_scope(|world: &mut World, mut value: Mut<A>| { | ||
value.0 += 1; | ||
assert!(!world.contains_resource::<A>()); |
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.
Do you need this assert here? The test should panic before hitting this line.
crates/bevy_ecs/src/lib.rs
Outdated
}); | ||
}); | ||
|
||
assert_eq!(world.get_non_send_resource::<A>().unwrap().0, 1); |
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.
Same as above here.
Thanks for the feedback, I think it's fixed now. |
crates/bevy_ecs/src/lib.rs
Outdated
}); | ||
|
||
if let Err(err) = thread.join() { | ||
panic!("{}", err.downcast::<String>().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.
}); | |
if let Err(err) = thread.join() { | |
panic!("{}", err.downcast::<String>().unwrap()); | |
} | |
}).join().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.
I tried to do it that way, but then the panic message doesn't match the expected one ("called Result::unwrap()
on an Err
value: Any { .. }" instead of "attempted to access NonSend resource bevy_ecs::tests::NonSend off of the main thread").
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.
It's sad that that doesn't contain the actual error message. This approach looks fine then. Just made a small suggestion above.
Co-authored-by: Mike <mike.hsu@gmail.com>
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.
Seems reasonable to me.
The pr should have a Changelog section I think, but this isn't a breaking change so shouldn't need a migration guide.
crates/bevy_ecs/src/lib.rs
Outdated
@@ -78,6 +79,9 @@ mod tests { | |||
#[derive(Component, Debug, PartialEq, Eq, Clone, Copy)] | |||
struct C; | |||
|
|||
#[derive(Default)] | |||
struct NonSend(usize, PhantomData<*mut ()>); |
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.
It's not ideal that this shadows the name of the 'real' NonSend
.
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.
Changed it to NonSendA
@@ -1149,14 +1149,27 @@ impl World { | |||
/// }); | |||
/// assert_eq!(world.get_resource::<A>().unwrap().0, 2); | |||
/// ``` | |||
pub fn resource_scope<R: Resource, U>(&mut self, f: impl FnOnce(&mut World, Mut<R>) -> U) -> U { | |||
pub fn resource_scope< | |||
R: 'static, /* The resource doesn't need to be Send nor Sync. */ |
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.
Really we should remove the Send/Sync
supertraits on resources, because the fact that you can add anything as a non send resource is a bit messy.
But this is fine for now.
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.
I think you should open another issue about it, since it's out of the scope of this PR.
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.
bors r+
) # Objective Relaxes the trait bound for `World::resource_scope` to allow non-send resources. Fixes #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>
Pull request successfully merged into main. Build succeeded: |
World::resource_scope
World::resource_scope
…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>
Objective
Relaxes the trait bound for
World::resource_scope
to allow non-send resources. Fixes #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 nowWorld::resource_scope
verifies non-send access if the resource is non-sendWorld::resource_scope
with a non-send resource, and one for invalid use (calling it from a different thread, resulting in panic)