-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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 an Entry
api to EntityWorldMut
.
#10650
Add an Entry
api to EntityWorldMut
.
#10650
Conversation
…ifetime transmutation with questionable safety, question for the future
…insert (more questionable transmutation)
…ed (otherwise we should be using `or_insert`)
Meets my needs. Minor nit on doc comment. |
Could we also get those for commands? |
Since commands are asynchronous, this would be more difficult: you can't insert a component and get back the same component instance in the same tick (I think). |
Should be possible when we introduce a EntityEntryCommands thing like EntityCommands which you can call |
Co-authored-by: Pascal Hertleif <[email protected]>
Agreed, but let's leave that for another PR :) |
/// assert_eq!(world.query::<&Comp>().single(&world).0, 0); | ||
/// ``` | ||
#[inline] | ||
pub fn or_default(self) -> Mut<'a, T> { |
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.
Might be nice to make both a FromReflect
and FromWorld
equivalent, but those can wait.
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.
Very clean construction, and a useful bit of API.
IMO we should replace the unwrap_unchecked
with simple unwrap
. Unless there's compelling benchmarks, I would much rather have surprising panics than risk UB in a refactor or edge case.
…plaining how it must be occupied before being created (it has private fields so this shouldn't be a problem for user code).
…comments explaining why it should never panic.
Replacing |
# Objective Adds `.entry` to `EntityWorldMut` with `Entry`, `OccupiedEntry` and `VacantEntry` for easier in-situ modification, based on `HashMap.entry`. Fixes bevyengine#10635 ## Solution This adds the `entry` method to `EntityWorldMut` which returns an `Entry`. This is an enum of `OccupiedEntry` and `VacantEntry` and has the methods `and_modify`, `insert_entry`, `or_insert`, `or_insert_with` and `or_default`. The only difference between `OccupiedEntry` and `VacantEntry` is the type, they are both a mutable reference to the `EntityWorldMut` and a marker for the component type, `HashMap` also stores things to make it quicker to access the data in `OccupiedEntry` but I wasn't sure if we had anything it would be logical to store to make accessing/modifying the component faster? As such, the differences are that `OccupiedEntry` assumes the entity has the component (because nothing else can have an `EntityWorldMut` so it can't be changed outside the entry api) and has different methods. All the methods are based very closely off `hashbrown::HashMap` (because its easier to read the source of) with a couple of quirks like `OccupiedEntry.insert` doesn't return the old value because we don't appear to have an api for mem::replacing components. --- ## Changelog - Added a new function `EntityWorldMut.entry` which returns an `Entry`, allowing easier in-situ modification of a component. --------- Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Pascal Hertleif <[email protected]>
Objective
Adds
.entry
toEntityWorldMut
withEntry
,OccupiedEntry
andVacantEntry
for easier in-situ modification, based onHashMap.entry
.Fixes #10635
Solution
This adds the
entry
method toEntityWorldMut
which returns anEntry
. This is an enum ofOccupiedEntry
andVacantEntry
and has the methodsand_modify
,insert_entry
,or_insert
,or_insert_with
andor_default
. The only difference betweenOccupiedEntry
andVacantEntry
is the type, they are both a mutable reference to theEntityWorldMut
and a marker for the component type,HashMap
also stores things to make it quicker to access the data inOccupiedEntry
but I wasn't sure if we had anything it would be logical to store to make accessing/modifying the component faster? As such, the differences are thatOccupiedEntry
assumes the entity has the component (because nothing else can have anEntityWorldMut
so it can't be changed outside the entry api) and has different methods.All the methods are based very closely off
hashbrown::HashMap
(because its easier to read the source of) with a couple of quirks likeOccupiedEntry.insert
doesn't return the old value because we don't appear to have an api for mem::replacing components.Changelog
EntityWorldMut.entry
which returns anEntry
, allowing easier in-situ modification of a component.