initial implementation of shared state for system params#22902
Draft
Person-93 wants to merge 19 commits intobevyengine:mainfrom
Draft
initial implementation of shared state for system params#22902Person-93 wants to merge 19 commits intobevyengine:mainfrom
Person-93 wants to merge 19 commits intobevyengine:mainfrom
Conversation
dfaca53 to
b244844
Compare
Contributor
|
It looks like your PR is a breaking change, but you didn't provide a migration guide. Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes. |
Temporary measure until it's fully implemented.
Hopefully this fixes that tests pass locally but fail in CI.
Rust does not monorphize static variables in generic functions. This means that a `OnceLock` initialized by one tuple would be re-used for all tuple types.
Thanks to Mira for pointing this out in discord
08b3293 to
7d54953
Compare
Thanks to Giooschi in discord for pinpointing the bug
Contributor
Author
|
Putting an update here because this is still tagged as waiting on author. I've stopped working on this because it needs a project goal and it doesn't seem likely to get one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Objective
Fixes #22885
Solution
I added associated function to
SystemParamwhich returns all of the types that the param uses as shared state. It returns a static slice ofSharedStateVTablewhich contains type-erased functions for working with the single instances of each type of shared state.I've also added a new struct
SharedStateswhich is used to manage the type-erased shared states. TheSystemParam::init_statefunction accepts a reference to the shared states and implementers can use it to get typed pointers which it can store as part of the state.SystemParam::init_stateis nowunsafeto call. It is required to make sure the newly constructed state does not outlive theSharedStates.Each type that is to be used as shared state must implement
SystemParamSharedStatewhich is used internally to make the vtables.Open questions
How should
DynSystemParamwork?Currently
SystemParam::sharedis an associated function soDynSystemParamcan't forward to the concrete type. For now, I've added "Don't use shared state" to the safety requirements ofDynSystemParam::new. Another possibility is to make it not share with anything else, but still share internally and log a warning.Todo
SystemParamforCommandsto share theirInternaCommandQueuesSystemParamSharedStatein theSystemParamderive macroSystemParamTesting
I added a test case that confirms the shared state is actually shared.
Showcase
See the test case for basic usage. It shares an
Arc<AtomicBool>. https://github.com/Person-93/bevy/blob/4cb020a0c9065f5bf363316677d49d60b1de4d51/crates/bevy_ecs/src/system/system_param.rs#L3529-L3602