Struct Storage

Source
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<'st, T, D> Storage<'st, T, D>
where T: Component, D: Deref<Target = MaskedStorage<T>>,

Source

pub fn restrict<'rf>( &'rf self, ) -> RestrictedStorage<'rf, 'st, T, &'rf <T as Component>::Storage, &'rf 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.

Source§

impl<'st, T, D> Storage<'st, T, D>
where T: Component, D: DerefMut<Target = MaskedStorage<T>>,

Source

pub fn restrict_mut<'rf>( &'rf mut self, ) -> RestrictedStorage<'rf, 'st, T, &'rf mut <T as Component>::Storage, &'rf 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.

Source

pub fn par_restrict_mut<'rf>( &'rf mut self, ) -> RestrictedStorage<'rf, 'st, T, &'rf mut <T as Component>::Storage, &'rf BitSet, MutableParallelRestriction>

Builds a mutable, parallel RestrictedStorage, does not allow mutably getting other components aside from the current iteration.

Source§

impl<'e, T, D> Storage<'e, T, D>
where T: Component, <T as Component>::Storage: Tracked, D: Deref<Target = MaskedStorage<T>>,

Source

pub fn channel(&self) -> &EventChannel<ComponentEvent>

Returns the event channel tracking modified components.

Source§

impl<'e, T, D> Storage<'e, T, D>
where T: Component, <T as Component>::Storage: Tracked, D: DerefMut<Target = MaskedStorage<T>>,

Source

pub fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>

Returns the event channel for insertions/removals/modifications of this storage’s components.

Source

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.

Source

pub fn flag(&mut self, event: ComponentEvent)

Flags an index with a ComponentEvent.

Source§

impl<'e, T, D> Storage<'e, T, D>
where T: Component, D: DerefMut<Target = MaskedStorage<T>>,

Source

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 });
 }
Source

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 Entriess. Otherwise the join will iterate over every single index of the bitset. If you want a join with all Entriess, 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
    }
}
Source§

impl<'e, T, D> Storage<'e, T, D>

Source

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>
where T: Component, D: Deref<Target = MaskedStorage<T>>,

Source

pub fn unprotected_storage(&self) -> &<T as Component>::Storage

Gets the wrapped storage.

Source

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.

Source

pub fn get(&self, e: Entity) -> Option<&T>

Tries to read the data associated with an Entity.

Source

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.

Source

pub fn is_empty(&self) -> bool

Checks whether this Storage is empty. This operation is very cheap.

Source

pub fn contains(&self, e: Entity) -> bool

Returns true if the storage has a component for this entity, and that entity is alive.

Source

pub fn mask(&self) -> &BitSet

Returns a reference to the bitset of this storage which allows filtering by the component type without actually getting the component.

Source§

impl<'e, T, D> Storage<'e, T, D>
where T: Component, D: DerefMut<Target = MaskedStorage<T>>,

Source

pub unsafe fn unprotected_storage_mut( &mut self, ) -> &mut <T as Component>::Storage

Gets mutable access to the wrapped storage.

This is unsafe because modifying the wrapped storage without also updating the mask bitset accordingly can result in illegal memory access.

Source

pub fn get_mut(&mut self, e: Entity) -> Option<&mut T>

Tries to mutate the data associated with an Entity.

Source

pub fn insert(&mut self, e: Entity, v: T) -> Result<Option<T>, Error>

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.

Source

pub fn remove(&mut self, e: Entity) -> Option<T>

Removes the data associated with an Entity.

Source

pub fn clear(&mut self)

Clears the contents of the storage.

Source

pub fn drain(&mut self) -> Drain<'_, T>

Creates a draining storage wrapper which can be .joined to get a draining iterator.

Trait Implementations§

Source§

impl<'a, 'b, T> GenericReadStorage for &'b Storage<'a, T, Fetch<'a, MaskedStorage<T>>>
where 'a: 'b, T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get( &self, entity: Entity, ) -> Option<&<&'b Storage<'a, T, Fetch<'a, MaskedStorage<T>>> as GenericReadStorage>::Component>

Get immutable access to an Entitys component
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, 'b, T> GenericReadStorage for &'b Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>
where 'a: 'b, T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get( &self, entity: Entity, ) -> Option<&<&'b Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericReadStorage>::Component>

Get immutable access to an Entitys component
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, T> GenericReadStorage for Storage<'a, T, Fetch<'a, MaskedStorage<T>>>
where T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get( &self, entity: Entity, ) -> Option<&<Storage<'a, T, Fetch<'a, MaskedStorage<T>>> as GenericReadStorage>::Component>

Get immutable access to an Entitys component
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, T> GenericReadStorage for Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>
where T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get( &self, entity: Entity, ) -> Option<&<Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericReadStorage>::Component>

Get immutable access to an Entitys component
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, 'b, T> GenericWriteStorage for &'b mut Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>
where 'a: 'b, T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get_mut( &mut self, entity: Entity, ) -> Option<&mut <&'b mut Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component>

Get mutable access to an Entitys component
Source§

fn insert( &mut self, entity: Entity, comp: <&'b mut Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component, ) -> Result<Option<<&'b mut Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component>, Error>

Insert a component for an Entity
Source§

fn remove(&mut self, entity: Entity)

Remove the component for an Entity
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, T> GenericWriteStorage for Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>
where T: Component,

Source§

type Component = T

The component type of the storage
Source§

fn get_mut( &mut self, entity: Entity, ) -> Option<&mut <Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component>

Get mutable access to an Entitys component
Source§

fn insert( &mut self, entity: Entity, comp: <Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component, ) -> Result<Option<<Storage<'a, T, FetchMut<'a, MaskedStorage<T>>> as GenericWriteStorage>::Component>, Error>

Insert a component for an Entity
Source§

fn remove(&mut self, entity: Entity)

Remove the component for an Entity
Source§

fn _private() -> Seal

Private function to seal the trait
Source§

impl<'a, 'e, T, D> Join for &'a Storage<'e, T, D>
where T: Component, D: Deref<Target = MaskedStorage<T>>,

Source§

type Type = &'a T

Type of joined components.
Source§

type Value = &'a <T as Component>::Storage

Type of joined storages.
Source§

type Mask = &'a BitSet

Type of joined bit mask.
Source§

unsafe fn open( self, ) -> (<&'a Storage<'e, T, D> as Join>::Mask, <&'a Storage<'e, T, D> as Join>::Value)

Open this join by returning the mask and the storages. Read more
Source§

unsafe fn get(v: &mut <&'a Storage<'e, T, D> as Join>::Value, i: u32) -> &'a T

Get a joined component value by a given index.
Source§

fn join(self) -> JoinIter<Self>
where Self: Sized,

Create a joined iterator over the contents.
Source§

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
Source§

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.
Source§

impl<'a, 'e, T, D> Join for &'a mut Storage<'e, T, D>
where T: Component, D: DerefMut<Target = MaskedStorage<T>>,

Source§

type Type = &'a mut T

Type of joined components.
Source§

type Value = &'a mut <T as Component>::Storage

Type of joined storages.
Source§

type Mask = &'a BitSet

Type of joined bit mask.
Source§

unsafe fn open( self, ) -> (<&'a mut Storage<'e, T, D> as Join>::Mask, <&'a mut Storage<'e, T, D> as Join>::Value)

Open this join by returning the mask and the storages. Read more
Source§

unsafe fn get( v: &mut <&'a mut Storage<'e, T, D> as Join>::Value, i: u32, ) -> &'a mut T

Get a joined component value by a given index.
Source§

fn join(self) -> JoinIter<Self>
where Self: Sized,

Create a joined iterator over the contents.
Source§

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
Source§

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.
Source§

impl<'a, 'e, T, D> Not for &'a Storage<'e, T, D>
where T: Component, D: Deref<Target = MaskedStorage<T>>,

Source§

type Output = AntiStorage<'a>

The resulting type after applying the ! operator.
Source§

fn not(self) -> <&'a Storage<'e, T, D> as Not>::Output

Performs the unary ! operation. Read more
Source§

impl<'a, T> SystemData<'a> for Storage<'a, T, Fetch<'a, MaskedStorage<T>>>
where T: Component,

Source§

fn setup(res: &mut Resources)

Sets up the system data for fetching it from the Resources.
Source§

fn fetch(res: &'a Resources) -> Storage<'a, T, Fetch<'a, MaskedStorage<T>>>

Fetches the system data from Resources. Note that this is only specified for one concrete lifetime 'a, you need to implement the SystemData trait for every possible lifetime.
Source§

fn reads() -> Vec<ResourceId>

Returns all read dependencies as fetched from Self::fetch. Read more
Source§

fn writes() -> Vec<ResourceId>

Returns all write dependencies as fetched from Self::fetch. Read more
Source§

impl<'a, T> SystemData<'a> for Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>
where T: Component,

Source§

fn setup(res: &mut Resources)

Sets up the system data for fetching it from the Resources.
Source§

fn fetch(res: &'a Resources) -> Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>

Fetches the system data from Resources. Note that this is only specified for one concrete lifetime 'a, you need to implement the SystemData trait for every possible lifetime.
Source§

fn reads() -> Vec<ResourceId>

Returns all read dependencies as fetched from Self::fetch. Read more
Source§

fn writes() -> Vec<ResourceId>

Returns all write dependencies as fetched from Self::fetch. Read more
Source§

impl<'a, T, D> DistinctStorage for Storage<'a, 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>
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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Any for T
where T: Any,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<'a, T> DynamicSystemData<'a> for T
where T: SystemData<'a>,

Source§

type Accessor = StaticAccessor<T>

The accessor of the SystemData, which specifies the read and write dependencies and does the fetching.
Source§

fn setup(_: &StaticAccessor<T>, res: &mut Resources)

Sets up Resources for fetching this system data.
Source§

fn fetch(_: &StaticAccessor<T>, res: &'a Resources) -> T

Creates a new resource bundle by fetching the required resources from the Resources struct. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Erased for T

Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

impl<T> Resource for T
where T: Any + Send + Sync,