Struct specs::storage::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<'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 LendJoin-able structure that yields all indices, returning StorageEntry 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
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
    }
}
source

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

source

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

source

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

source

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

Returns the event channel tracking modified components.

source

pub fn event_emission(&self) -> bool

Returns the actual state of the event emission.

source§

impl<'e, T, D> Storage<'e, T, D>where T: Component, T::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

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>

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::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: Deref<Target = MaskedStorage<T>>, T::Storage: SliceAccess<T>,

source

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

source

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

source

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.

source

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.

source

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.

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, T, D: Clone> Clone for Storage<'a, T, D>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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

§

type Mask = &'a BitSet

Type of joined bit mask.
§

type Type = &'a T

Type of joined components.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get(v: &mut Self::Value, i: Index) -> &'a T

Get a joined component value by a given index. Read more
source§

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

Create a joined iterator over the contents.
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 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>>, T::Storage: SharedGetMutStorage<T>,

§

type Mask = &'a BitSet

Type of joined bit mask.
§

type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>

Type of joined components.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get(value: &mut Self::Value, id: Index) -> Self::Type

Get a joined component value by a given index. Read more
source§

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

Create a joined iterator over the contents.
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 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>where T: Component, D: Deref<Target = MaskedStorage<T>>,

§

type Mask = &'a BitSet

Type of joined bit mask.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get<'next>(v: &'next mut Self::Value, i: Index) -> &'a T

Get a joined component value by a given index. Read more
source§

fn lend_join(self) -> JoinLendIter<Self>where Self: Sized,

Create a joined lending iterator over the contents.
source§

fn maybe(self) -> MaybeJoin<Self>where Self: Sized,

Returns a structure that implements 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 more
source§

fn is_unconstrained() -> bool

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

§

type Mask = &'a BitSet

Type of joined bit mask.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get<'next>( value: &'next mut Self::Value, id: Index ) -> <Self as LendJoinඞType<'next>>::T

Get a joined component value by a given index. Read more
source§

fn lend_join(self) -> JoinLendIter<Self>where Self: Sized,

Create a joined lending iterator over the contents.
source§

fn maybe(self) -> MaybeJoin<Self>where Self: Sized,

Returns a structure that implements 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 more
source§

fn is_unconstrained() -> bool

If this 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> Not for &'a Storage<'e, T, D>where T: Component, D: Deref<Target = MaskedStorage<T>>,

§

type Output = AntiStorage<'a>

The resulting type after applying the ! operator.
source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
source§

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

§

type Mask = &'a BitSet

Type of joined bit mask.
§

type Type = &'a T

Type of joined components.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get(v: &Self::Value, i: Index) -> &'a T

Get a joined component value by a given index. Read more
source§

fn par_join(self) -> JoinParIter<Self>where Self: Sized,

Create a joined parallel iterator over the contents.
source§

fn is_unconstrained() -> bool

If this 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,

§

type Mask = &'a BitSet

Type of joined bit mask.
§

type Type = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'a>

Type of joined components.
§

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

Type of joined storages.
source§

unsafe fn open(self) -> (Self::Mask, Self::Value)

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

unsafe fn get(value: &Self::Value, id: Index) -> Self::Type

Get a joined component value by a given index. Read more
source§

fn par_join(self) -> JoinParIter<Self>where Self: Sized,

Create a joined parallel iterator over the contents.
source§

fn is_unconstrained() -> bool

If this 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> RepeatableLendGet for &'a Storage<'e, T, D>where T: Component, D: Deref<Target = MaskedStorage<T>>,

source§

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

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§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere T: SystemData<'a>,

§

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>, world: &mut World)

Sets up World for fetching this system data.
source§

fn fetch(_: &StaticAccessor<T>, world: &'a World) -> T

Creates a new resource bundle by fetching the required resources from the World 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 Twhere 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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> Event for Twhere T: Send + Sync + 'static,

source§

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