[Merged by Bors] - drop old value in insert_resource_by_id if exists#5587
[Merged by Bors] - drop old value in insert_resource_by_id if exists#5587jakobhellermann wants to merge 4 commits intobevyengine:mainfrom
insert_resource_by_id if exists#5587Conversation
| } | ||
|
|
||
| #[test] | ||
| fn insert_resource_by_id_drop_old() { |
There was a problem hiding this comment.
You should use DropTestHelper and a regular rust type for the resource instead of doing this all dynamically. You can get the ComponentId representing the rust type via world.components.get_resource_id(TypeId::of::<Whatever>()).unwrap() (after you've inserted the resource the first time otherwise it wont have been registered lol)
There was a problem hiding this comment.
I changed the test to use DropTestHelper: https://github.com/bevyengine/bevy/pull/5587/files#diff-8799631eaab5e61e5dbc20251e08d83474df566ba159061cf2a78ce8f1fd59d5R1810-R1854
Unfortunately I had to use AssertUnwindSafe to be able to create the world outside of the catch_unwind and reference it afterwards, otherwise I was getting
| `&mut world::World` may not be safely transferred across an unwind boundary
I don't know much about unwind safety, do you know if this assertion is justified?
Alternatively I can create the world inside the catch_unwind and not remove the !contains_resource assert.
There was a problem hiding this comment.
Alternatively I can create the world inside the catch_unwind and not remove the !contains_resource assert.
I prefer this approach as it's a bit less spooky.
There was a problem hiding this comment.
You can set make_component(false, 1) and get rid of the catch unwind/assertunwindsafe. I don't think panic in drop was relevent to this issue was it?
There was a problem hiding this comment.
You can set
make_component(false, 1)and get rid of the catch unwind/assertunwindsafe. I don't think panic in drop was relevent to this issue was it?
That is currently not possible with the DropTestHelper because it needs at least one dropped make_component(true) to "defuse" itself: #5615
There was a problem hiding this comment.
I removed the AssertUnwindSafe and the !contains_resource assert.
There was a problem hiding this comment.
Oh thats unfortunate oh well ...
100a69b to
ee98a94
Compare
|
bors r+ |
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
|
Build failed (retrying...): |
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
|
bors r- |
|
Canceled. |
|
@jakobhellermann you'll need to update this PR to reflect the new explicit |
84694e0 to
d3b7c4d
Compare
done |
|
bors r+ |
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
|
Pull request successfully merged into main. Build succeeded: |
insert_resource_by_id if existsinsert_resource_by_id if exists
# Objective remove `insert_resource_with_id` because `insert_resource_by_id` exists and does almost exactly the same thing blocked on #5587 because otherwise we will leak a resource when it's inserted ## Solution remove the function and also add a safety invariant of to `insert_resource_by_id` that the id be valid for the world. I didn't see any discussion in #4447 about this safety invariant being left off in favor of a panic so I'm curious if there was one or if it just seemed nicer to have less safety invariants for callers to uphold 😅 --- ## Changelog - safety invariant added to `insert_resource_by_id` requiring the id to be valid for world ## Migration Guide - audit any calls to `insert_resource_by_id` making sure that the id is valid for the world Co-authored-by: Carter Anderson <mcanders1@gmail.com>
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
# Objective remove `insert_resource_with_id` because `insert_resource_by_id` exists and does almost exactly the same thing blocked on bevyengine#5587 because otherwise we will leak a resource when it's inserted ## Solution remove the function and also add a safety invariant of to `insert_resource_by_id` that the id be valid for the world. I didn't see any discussion in bevyengine#4447 about this safety invariant being left off in favor of a panic so I'm curious if there was one or if it just seemed nicer to have less safety invariants for callers to uphold 😅 --- ## Changelog - safety invariant added to `insert_resource_by_id` requiring the id to be valid for world ## Migration Guide - audit any calls to `insert_resource_by_id` making sure that the id is valid for the world Co-authored-by: Carter Anderson <mcanders1@gmail.com>
# Objective While trying out the lint `unsafe_op_in_unsafe_fn` I noticed that `insert_resource_by_id` didn't drop the old value if it already existed, and reimplemented `Column::replace` manually for no apparent reason. ## Solution - use `Column::replace` and add a test expecting the correct drop count --- ## Changelog - `World::insert_resource_by_id` will now correctly drop the old resource value, if one already existed
# Objective remove `insert_resource_with_id` because `insert_resource_by_id` exists and does almost exactly the same thing blocked on bevyengine#5587 because otherwise we will leak a resource when it's inserted ## Solution remove the function and also add a safety invariant of to `insert_resource_by_id` that the id be valid for the world. I didn't see any discussion in bevyengine#4447 about this safety invariant being left off in favor of a panic so I'm curious if there was one or if it just seemed nicer to have less safety invariants for callers to uphold 😅 --- ## Changelog - safety invariant added to `insert_resource_by_id` requiring the id to be valid for world ## Migration Guide - audit any calls to `insert_resource_by_id` making sure that the id is valid for the world Co-authored-by: Carter Anderson <mcanders1@gmail.com>
Objective
While trying out the lint
unsafe_op_in_unsafe_fnI noticed thatinsert_resource_by_iddidn't drop the old value if it already existed, and reimplementedColumn::replacemanually for no apparent reason.Solution
Column::replaceand add a test expecting the correct drop countChangelog
World::insert_resource_by_idwill now correctly drop the old resource value, if one already existed