-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Add an API for accessing world storages from UnsafeWorldCell
#8280
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
Closed
Closed
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
46f6924
add a type for interior mutable world storages
joseph-gio 561416a
add types for unsafely accessing individual data stores
joseph-gio 577d0d2
add a method for safely accessing table metadata
joseph-gio a940222
add functions for accessing individual sparse set component ticks
joseph-gio 4c2afb4
add docs to structs
joseph-gio 4e98fb9
make safety comments consisent with `UnsafeWorldCell`
joseph-gio cb79f93
tweak phrasing of `entities`
joseph-gio 310e5df
use consistent docs for `has_column`
joseph-gio e84704a
add a word
joseph-gio 52683ed
add docs to high-level `get` functions
joseph-gio 0c7df0d
resolve links
joseph-gio bf61413
fix resources docs
joseph-gio 5076831
update documentation for `Option`-returning fns
joseph-gio 47801ef
simplify `UnsafeStorages`
joseph-gio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ pub use resource::*; | |
pub use sparse_set::*; | ||
pub use table::*; | ||
|
||
use crate::component::ComponentId; | ||
|
||
/// The raw data stores of a [World](crate::world::World) | ||
#[derive(Default)] | ||
pub struct Storages { | ||
|
@@ -41,3 +43,71 @@ pub struct Storages { | |
/// Backing storage for `!Send` resources. | ||
pub non_send_resources: Resources<false>, | ||
} | ||
|
||
/// Provides interior-mutable access to a world's internal data storages. | ||
/// | ||
/// Any instance of this type is associated with a set of world data that | ||
/// it is allowed to access. This should be described in the documentation | ||
/// of wherever you obtained the `UnsafeStorages`. | ||
/// | ||
/// For instance, if you originally obtained it from a system running on | ||
/// a multi-threaded executor, then you are only allowed to access data | ||
/// that has been registered in the system's `archetype_component_access`. | ||
/// If you originally obtained an `UnsafeStorages` from an `&World`, | ||
/// then you have read-only access to the entire world. | ||
/// | ||
/// Accessing world data that do not have access to, or mutably accessing | ||
/// data that you only have read-access to, is considered undefined behavior. | ||
pub struct UnsafeStorages<'a> { | ||
pub sparse_sets: UnsafeSparseSets<'a>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing docs on these fields. |
||
pub tables: UnsafeTables<'a>, | ||
pub resources: UnsafeResources<'a, true>, | ||
pub non_send_resources: UnsafeResources<'a, false>, | ||
} | ||
|
||
impl<'a> UnsafeStorages<'a> { | ||
pub(crate) fn new(storages: &'a Storages) -> Self { | ||
Self { | ||
sparse_sets: UnsafeSparseSets { | ||
sparse_sets: &storages.sparse_sets, | ||
}, | ||
tables: UnsafeTables { | ||
tables: &storages.tables, | ||
}, | ||
resources: UnsafeResources { | ||
resources: &storages.resources, | ||
}, | ||
non_send_resources: UnsafeResources { | ||
resources: &storages.non_send_resources, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
/// A view into the [`ComponentSparseSet`] collection of [`UnsafeStorages`]. | ||
#[derive(Clone, Copy)] | ||
pub struct UnsafeSparseSets<'a> { | ||
sparse_sets: &'a SparseSets, | ||
} | ||
|
||
impl<'a> UnsafeSparseSets<'a> { | ||
/// Gets a view into the [`ComponentSparseSet`] associated with `component_id`. | ||
joseph-gio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn get(self, component_id: ComponentId) -> Option<UnsafeComponentSparseSet<'a>> { | ||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.sparse_sets | ||
.get(component_id) | ||
.map(|sparse_set| UnsafeComponentSparseSet { sparse_set }) | ||
} | ||
} | ||
|
||
/// A view into the [`Table`] collection of [`UnsafeStorages`]. | ||
#[derive(Clone, Copy)] | ||
pub struct UnsafeTables<'a> { | ||
tables: &'a Tables, | ||
} | ||
|
||
impl<'a> UnsafeTables<'a> { | ||
/// Gets a view into the [`Table`] associated with `id`. | ||
joseph-gio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn get(self, id: TableId) -> Option<UnsafeTable<'a>> { | ||
self.tables.get(id).map(|table| UnsafeTable { table }) | ||
} | ||
} |
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
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
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
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
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.
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.
We should link to the critical concepts here. "interior mutability" and "undefined behavior" would be very nice to link out to for newcomers to Rust who run into this in the docs.
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 agree. I imagine the rustonomicon will be a good starting point, but let me know if there are other resources you think would be worth including here.