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§
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
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 LendJoin
-able structure that yields all indices, returning
StorageEntry
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
let mut join = (counters.entries(), &marker).lend_join();
while let Some((mut counter, _)) = join.next() {
let counter = counter.or_insert_with(Default::default);
counter.increase();
if counter.reached_limit() {
counter.reset();
// Do something
}
}
Sourcepub fn entry_inner<'a>(&'a mut self, id: Index) -> StorageEntry<'a, 'e, T, D>
pub fn entry_inner<'a>(&'a mut self, id: Index) -> StorageEntry<'a, 'e, T, D>
Returns an entry to the component associated with the provided index.
Does not check whether an entity is alive!
Source§impl<T, D> Storage<'_, T, D>
impl<T, D> Storage<'_, T, D>
Sourcepub fn restrict<'rf>(&'rf self) -> RestrictedStorage<'rf, T, &T::Storage>
pub fn restrict<'rf>(&'rf self) -> RestrictedStorage<'rf, T, &T::Storage>
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.
Source§impl<T, D> Storage<'_, T, D>
impl<T, D> Storage<'_, T, D>
Sourcepub fn restrict_mut<'rf>(
&'rf mut self,
) -> RestrictedStorage<'rf, T, &mut T::Storage>
pub fn restrict_mut<'rf>( &'rf mut self, ) -> RestrictedStorage<'rf, T, &mut T::Storage>
Builds a mutable RestrictedStorage
out of a Storage
. Allows
restricted access to the inner components without allowing
invalidating the bitset for iteration in Join
.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
Sourcepub fn channel(&self) -> &EventChannel<ComponentEvent>
pub fn channel(&self) -> &EventChannel<ComponentEvent>
Returns the event channel tracking modified components.
Sourcepub fn event_emission(&self) -> bool
pub fn event_emission(&self) -> bool
Returns the actual state of the event emission.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
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
.
Sourcepub fn set_event_emission(&mut self, emit: bool)
pub fn set_event_emission(&mut self, emit: bool)
Controls the events signal emission. When this is set to false the events modified/inserted/removed are not emitted.
Source§impl<'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
.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
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.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
Sourcepub fn as_slice(&self) -> &[<T::Storage as SliceAccess<T>>::Element]
pub fn as_slice(&self) -> &[<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.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
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.
Source§impl<'e, T, D> Storage<'e, T, D>
impl<'e, T, D> Storage<'e, T, D>
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<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>
pub fn get_mut( &mut self, e: Entity, ) -> Option<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>
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§
Source§impl<'a, 'e, T, D> Join for &'a Storage<'e, T, D>
impl<'a, 'e, T, D> Join for &'a Storage<'e, T, D>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get(v: &mut Self::Value, i: Index) -> &'a T
unsafe fn get(v: &mut Self::Value, i: Index) -> &'a T
Source§fn join(self) -> JoinIter<Self> ⓘwhere
Self: Sized,
fn join(self) -> JoinIter<Self> ⓘwhere
Self: Sized,
Source§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
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
to go through all indices which is usually not what
is wanted and will kill performance.Source§impl<'a, 'e, T, D> Join for &'a mut Storage<'e, T, D>
impl<'a, 'e, T, D> Join for &'a mut Storage<'e, T, D>
Source§type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>
type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type
unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type
Source§fn join(self) -> JoinIter<Self> ⓘwhere
Self: Sized,
fn join(self) -> JoinIter<Self> ⓘwhere
Self: Sized,
Source§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
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
to go through all indices which is usually not what
is wanted and will kill performance.Source§impl<'a, 'e, T, D> LendJoin for &'a Storage<'e, T, D>
impl<'a, 'e, T, D> LendJoin for &'a Storage<'e, T, D>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get<'next>(v: &'next mut Self::Value, i: Index) -> &'a T
unsafe fn get<'next>(v: &'next mut Self::Value, i: Index) -> &'a T
Source§fn lend_join(self) -> JoinLendIter<Self>where
Self: Sized,
fn lend_join(self) -> JoinLendIter<Self>where
Self: Sized,
Source§fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
Join
/LendJoin
/MaybeJoin
if the
contained T
does and that yields all indices, returning None
for all
missing elements and Some(T)
for found elements. Read moreSource§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
LendJoin
typically returns all indices in the mask, then
iterating over only it or combined with other joins that are also
dangerous will cause the JoinLendIter
to go through all indices which
is usually not what is wanted and will kill performance.Source§impl<'a, 'e, T, D> LendJoin for &'a mut Storage<'e, T, D>
impl<'a, 'e, T, D> LendJoin for &'a mut Storage<'e, T, D>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get<'next>(
value: &'next mut Self::Value,
id: Index,
) -> <Self as LendJoinඞType<'next>>::T
unsafe fn get<'next>( value: &'next mut Self::Value, id: Index, ) -> <Self as LendJoinඞType<'next>>::T
Source§fn lend_join(self) -> JoinLendIter<Self>where
Self: Sized,
fn lend_join(self) -> JoinLendIter<Self>where
Self: Sized,
Source§fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
Join
/LendJoin
/MaybeJoin
if the
contained T
does and that yields all indices, returning None
for all
missing elements and Some(T)
for found elements. Read moreSource§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
LendJoin
typically returns all indices in the mask, then
iterating over only it or combined with other joins that are also
dangerous will cause the JoinLendIter
to go through all indices which
is usually not what is wanted and will kill performance.Source§impl<'a, 'e, T, D> ParJoin for &'a Storage<'e, T, D>
impl<'a, 'e, T, D> ParJoin for &'a Storage<'e, T, D>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get(v: &Self::Value, i: Index) -> &'a T
unsafe fn get(v: &Self::Value, i: Index) -> &'a T
Source§fn par_join(self) -> JoinParIter<Self>where
Self: Sized,
fn par_join(self) -> JoinParIter<Self>where
Self: Sized,
Source§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
LendJoin
typically returns all indices in the mask, then
iterating over only it or combined with other joins that are also
dangerous will cause the JoinLendIter
to go through all indices which
is usually not what is wanted and will kill performance.Source§impl<'a, 'e, T, D> ParJoin for &'a mut Storage<'e, T, D>where
T: Component,
D: DerefMut<Target = MaskedStorage<T>>,
T::Storage: Sync + SharedGetMutStorage<T> + 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 + SharedGetMutStorage<T> + DistinctStorage,
Source§type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>
type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
unsafe fn open(self) -> (Self::Mask, Self::Value)
Source§unsafe fn get(value: &Self::Value, id: Index) -> Self::Type
unsafe fn get(value: &Self::Value, id: Index) -> Self::Type
Source§fn par_join(self) -> JoinParIter<Self>where
Self: Sized,
fn par_join(self) -> JoinParIter<Self>where
Self: Sized,
Source§fn is_unconstrained() -> bool
fn is_unconstrained() -> bool
LendJoin
typically returns all indices in the mask, then
iterating over only it or combined with other joins that are also
dangerous will cause the JoinLendIter
to go through all indices which
is usually not what is wanted and will kill performance.impl<'a, 'e, T, D> RepeatableLendGet for &'a Storage<'e, T, D>
impl<'a, 'e, T, D> RepeatableLendGet for &'a mut Storage<'e, T, D>
Auto Trait Implementations§
impl<'e, T, D> Freeze for Storage<'e, T, D>where
D: Freeze,
impl<'e, T, D> !RefUnwindSafe for Storage<'e, T, D>
impl<'e, T, D> Send for Storage<'e, T, D>
impl<'e, T, D> Sync for Storage<'e, T, D>
impl<'e, T, D> Unpin for Storage<'e, T, D>
impl<'e, T, D> !UnwindSafe for Storage<'e, T, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<'a, T> DynamicSystemData<'a> for Twhere
T: SystemData<'a>,
impl<'a, T> DynamicSystemData<'a> for Twhere
T: SystemData<'a>,
Source§type Accessor = StaticAccessor<T>
type Accessor = StaticAccessor<T>
SystemData
, which specifies the read and write
dependencies and does the fetching.Source§fn setup(_: &StaticAccessor<T>, world: &mut World)
fn setup(_: &StaticAccessor<T>, world: &mut World)
World
for fetching this system data.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more