pub struct Storage<'e, T, D> { /* private fields */ }
Expand description
A wrapper around the masked storage and the generations vector.
Can be used for safe lookup of components, insertions and removes.
This is what World::read/write
fetches for the user.
Implementations
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
sourcepub fn entry<'a>(
&'a mut self,
e: Entity
) -> Result<StorageEntry<'a, 'e, T, D>, WrongGeneration> where
'e: 'a,
pub fn entry<'a>(
&'a mut self,
e: Entity
) -> Result<StorageEntry<'a, 'e, T, D>, WrongGeneration> where
'e: 'a,
Returns an entry to the component associated to the entity.
Behaves somewhat similarly to std::collections::HashMap
’s entry api.
Example
if let Ok(entry) = storage.entry(entity) {
entry.or_insert(Comp { field: 55 });
}
sourcepub fn entries<'a>(&'a mut self) -> Entries<'a, 'e, T, D>
pub fn entries<'a>(&'a mut self) -> Entries<'a, 'e, T, D>
Returns a Join
-able structure that yields all indices, returning
Entry
for all elements
WARNING: Do not have a join of only Entries
s. Otherwise the join will
iterate over every single index of the bitset. If you want a join with
all Entries
s, add an EntitiesRes
to the join as well to bound the
join to all entities that are alive.
Example
for (mut counter, _) in (counters.entries(), &marker).join() {
let counter = counter.or_insert_with(Default::default);
counter.increase();
if counter.reached_limit() {
counter.reset();
// Do something
}
}
sourceimpl<'st, T, D> Storage<'st, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
impl<'st, T, D> Storage<'st, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
sourcepub fn restrict<'rf>(
&'rf self
) -> RestrictedStorage<'rf, 'st, T, &T::Storage, &BitSet, ImmutableParallelRestriction>
pub fn restrict<'rf>(
&'rf self
) -> RestrictedStorage<'rf, 'st, T, &T::Storage, &BitSet, ImmutableParallelRestriction>
Builds an immutable RestrictedStorage
out of a Storage
. Allows
deferred unchecked access to the entity’s component.
This is returned as a ParallelRestriction
version since you can only
get immutable components with this which is safe for parallel by
default.
sourceimpl<'st, T, D> Storage<'st, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
impl<'st, T, D> Storage<'st, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
sourcepub fn restrict_mut<'rf>(
&'rf mut self
) -> RestrictedStorage<'rf, 'st, T, &mut T::Storage, &BitSet, SequentialRestriction>
pub fn restrict_mut<'rf>(
&'rf mut self
) -> RestrictedStorage<'rf, 'st, T, &mut T::Storage, &BitSet, SequentialRestriction>
Builds a mutable RestrictedStorage
out of a Storage
. Allows
restricted access to the inner components without allowing
invalidating the bitset for iteration in Join
.
sourcepub fn par_restrict_mut<'rf>(
&'rf mut self
) -> RestrictedStorage<'rf, 'st, T, &mut T::Storage, &BitSet, MutableParallelRestriction>
pub fn par_restrict_mut<'rf>(
&'rf mut self
) -> RestrictedStorage<'rf, 'st, T, &mut T::Storage, &BitSet, MutableParallelRestriction>
Builds a mutable, parallel RestrictedStorage
,
does not allow mutably getting other components
aside from the current iteration.
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
T::Storage: Tracked,
D: Deref<Target = MaskedStorage<T>>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
T::Storage: Tracked,
D: Deref<Target = MaskedStorage<T>>,
sourcepub fn channel(&self) -> &EventChannel<ComponentEvent>
pub fn channel(&self) -> &EventChannel<ComponentEvent>
Returns the event channel tracking modified components.
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
T::Storage: Tracked,
D: DerefMut<Target = MaskedStorage<T>>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
T::Storage: Tracked,
D: DerefMut<Target = MaskedStorage<T>>,
sourcepub fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>
pub fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>
Returns the event channel for insertions/removals/modifications of this storage’s components.
sourcepub fn register_reader(&mut self) -> ReaderId<ComponentEvent>
pub fn register_reader(&mut self) -> ReaderId<ComponentEvent>
Starts tracking component events. Note that this reader id should be used every frame, otherwise events will pile up and memory use by the event channel will grow waiting for this reader.
sourcepub fn flag(&mut self, event: ComponentEvent)
pub fn flag(&mut self, event: ComponentEvent)
Flags an index with a ComponentEvent
.
sourceimpl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
sourcepub fn new(entities: Fetch<'e, EntitiesRes>, data: D) -> Storage<'e, T, D>
pub fn new(entities: Fetch<'e, EntitiesRes>, data: D) -> Storage<'e, T, D>
Creates a new Storage
from a fetched allocator and a immutable or
mutable MaskedStorage
, named data
.
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
sourcepub fn unprotected_storage(&self) -> &T::Storage
pub fn unprotected_storage(&self) -> &T::Storage
Gets the wrapped storage.
sourcepub fn fetched_entities(&self) -> &EntitiesRes
pub fn fetched_entities(&self) -> &EntitiesRes
Returns the EntitiesRes
resource fetched by this storage.
This does not have anything to do with the components inside.
You only want to use this when implementing additional methods
for Storage
via an extension trait.
sourcepub fn count(&self) -> usize
pub fn count(&self) -> usize
Computes the number of elements this Storage
contains by counting the
bits in the bit set. This operation will never be performed in
constant time.
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks whether this Storage
is empty. This operation is very cheap.
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
T::Storage: SliceAccess<T>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
T::Storage: SliceAccess<T>,
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
T::Storage: SliceAccess<T>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
T::Storage: SliceAccess<T>,
sourcepub fn as_mut_slice(&mut self) -> &mut [<T::Storage as SliceAccess<T>>::Element]
pub fn as_mut_slice(&mut self) -> &mut [<T::Storage as SliceAccess<T>>::Element]
Returns the component data as a slice.
The indices of this slice may not correspond to anything in particular. Check the underlying storage documentation for details.
sourceimpl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
impl<'e, T, D> Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
sourcepub unsafe fn unprotected_storage_mut(&mut self) -> &mut T::Storage
pub unsafe fn unprotected_storage_mut(&mut self) -> &mut T::Storage
Gets mutable access to the wrapped storage.
Safety
This is unsafe because modifying the wrapped storage without also updating the mask bitset accordingly can result in illegal memory access.
sourcepub fn get_mut(&mut self, e: Entity) -> Option<&'_ mut T>
pub fn get_mut(&mut self, e: Entity) -> Option<&'_ mut T>
Tries to mutate the data associated with an Entity
.
sourcepub fn insert(&mut self, e: Entity, v: T) -> InsertResult<T>
pub fn insert(&mut self, e: Entity, v: T) -> InsertResult<T>
Inserts new data for a given Entity
.
Returns the result of the operation as a InsertResult<T>
If a component already existed for the given Entity
, then it will
be overwritten with the new component. If it did overwrite, then the
result will contain Some(T)
where T
is the previous component.
Trait Implementations
sourceimpl<'a, 'e, T, D> Join for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
impl<'a, 'e, T, D> Join for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
sourceunsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Open this join by returning the mask and the storages. Read more
sourceunsafe fn get(v: &mut Self::Value, i: Index) -> &'a T
unsafe fn get(v: &mut Self::Value, i: Index) -> &'a T
Get a joined component value by a given index. Read more
sourcefn join(self) -> JoinIter<Self>ⓘNotable traits for JoinIter<J>impl<J: Join> Iterator for JoinIter<J> type Item = J::Type;
where
Self: Sized,
fn join(self) -> JoinIter<Self>ⓘNotable traits for JoinIter<J>impl<J: Join> Iterator for JoinIter<J> type Item = J::Type;
where
Self: Sized,
Create a joined iterator over the contents.
sourcefn maybe(self) -> MaybeJoin<Self> where
Self: Sized,
fn maybe(self) -> MaybeJoin<Self> where
Self: Sized,
Returns a Join
-able structure that yields all indices, returning
None
for all missing elements and Some(T)
for found elements. Read more
sourcefn is_unconstrained() -> bool
fn is_unconstrained() -> bool
If this Join
typically returns all indices in the mask, then iterating
over only it or combined with other joins that are also dangerous
will cause the JoinIter
/ParJoin
to go through all indices which
is usually not what is wanted and will kill performance. Read more
sourceimpl<'a, 'e, T, D> Join for &'a mut Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
impl<'a, 'e, T, D> Join for &'a mut Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
sourceunsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Open this join by returning the mask and the storages. Read more
sourceunsafe fn get(v: &mut Self::Value, i: Index) -> Self::Type
unsafe fn get(v: &mut Self::Value, i: Index) -> Self::Type
Get a joined component value by a given index. Read more
sourcefn join(self) -> JoinIter<Self>ⓘNotable traits for JoinIter<J>impl<J: Join> Iterator for JoinIter<J> type Item = J::Type;
where
Self: Sized,
fn join(self) -> JoinIter<Self>ⓘNotable traits for JoinIter<J>impl<J: Join> Iterator for JoinIter<J> type Item = J::Type;
where
Self: Sized,
Create a joined iterator over the contents.
sourcefn maybe(self) -> MaybeJoin<Self> where
Self: Sized,
fn maybe(self) -> MaybeJoin<Self> where
Self: Sized,
Returns a Join
-able structure that yields all indices, returning
None
for all missing elements and Some(T)
for found elements. Read more
sourcefn is_unconstrained() -> bool
fn is_unconstrained() -> bool
If this Join
typically returns all indices in the mask, then iterating
over only it or combined with other joins that are also dangerous
will cause the JoinIter
/ParJoin
to go through all indices which
is usually not what is wanted and will kill performance. Read more
sourceimpl<'a, 'e, T, D> Not for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
impl<'a, 'e, T, D> Not for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
sourceimpl<'a, 'e, T, D> ParJoin for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
T::Storage: Sync,
impl<'a, 'e, T, D> ParJoin for &'a Storage<'e, T, D> where
T: Component,
D: Deref<Target = MaskedStorage<T>>,
T::Storage: Sync,
sourcefn par_join(self) -> JoinParIter<Self> where
Self: Sized,
fn par_join(self) -> JoinParIter<Self> where
Self: Sized,
Create a joined parallel iterator over the contents.
sourceimpl<'a, 'e, T, D> ParJoin for &'a mut Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
T::Storage: Sync + DistinctStorage,
impl<'a, 'e, T, D> ParJoin for &'a mut Storage<'e, T, D> where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
T::Storage: Sync + DistinctStorage,
sourcefn par_join(self) -> JoinParIter<Self> where
Self: Sized,
fn par_join(self) -> JoinParIter<Self> where
Self: Sized,
Create a joined parallel iterator over the contents.
impl<'a, T: Component, D> DistinctStorage for Storage<'a, T, D> where
T::Storage: DistinctStorage,
Auto Trait Implementations
impl<'e, T, D> !RefUnwindSafe for Storage<'e, T, D>
impl<'e, T, D> Send for Storage<'e, T, D> where
D: Send,
T: Send,
impl<'e, T, D> Sync for Storage<'e, T, D> where
D: Sync,
T: Sync,
impl<'e, T, D> Unpin for Storage<'e, T, D> where
D: Unpin,
T: Unpin,
impl<'e, T, D> !UnwindSafe for Storage<'e, T, D>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<'a, T> DynamicSystemData<'a> for T where
T: SystemData<'a>,
impl<'a, T> DynamicSystemData<'a> for T where
T: SystemData<'a>,
type Accessor = StaticAccessor<T>
type Accessor = StaticAccessor<T>
The accessor of the SystemData
, which specifies the read and write
dependencies and does the fetching. Read more
sourcefn setup(&StaticAccessor<T>, world: &mut World)
fn setup(&StaticAccessor<T>, world: &mut World)
Sets up World
for fetching this system data.