Type Alias specs::storage::WriteStorage

source ·
pub type WriteStorage<'a, T> = Storage<'a, T, FetchMut<'a, MaskedStorage<T>>>;
Expand description

A storage with read and write access.

Additionally to what ReadStorage can do a storage with mutable access allows:

Aliasing

It is strictly disallowed to fetch both a ReadStorage and a WriteStorage of the same component. Because Specs uses interior mutability for its resources, we can’t check this at compile time. If you try to do this, you will get a panic.

It is also disallowed to fetch multiple WriteStorages for the same component.

Retrieve components mutably

This works just like Storage::get, but returns a mutable reference:

let entity = world.create_entity().with(Pos(2.0)).build();

assert_eq!(pos_storage.get_mut(entity), Some(&mut Pos(2.0)));
if let Some(pos) = pos_storage.get_mut(entity) {
    *pos = Pos(4.5);
}

assert_eq!(pos_storage.get(entity), Some(&Pos(4.5)));

Inserting and removing components

You can insert components using Storage::insert and remove them again with Storage::remove.

let entity = world.create_entity().with(Pos(0.1)).build();

if let Ok(Some(p)) = pos_storage.insert(entity, Pos(4.0)) {
    println!("Overwrote {:?} with a new position", p);
}

There’s also an Entry-API similar to the one provided by std::collections::HashMap.

Aliased Type§

struct WriteStorage<'a, T> { /* private fields */ }

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: 'b, 'b, T> GenericReadStorage for &'b WriteStorage<'a, T>where T: Component,

§

type Component = T

The component type of the storage
source§

fn get(&self, entity: Entity) -> Option<&Self::Component>

Get immutable access to an Entitys component
source§

fn _private() -> Seal

Private function to seal the trait
source§

impl<'a, T> GenericReadStorage for WriteStorage<'a, T>where T: Component,

§

type Component = T

The component type of the storage
source§

fn get(&self, entity: Entity) -> Option<&Self::Component>

Get immutable access to an Entitys component
source§

fn _private() -> Seal

Private function to seal the trait
source§

impl<'a: 'b, 'b, T> GenericWriteStorage for &'b mut WriteStorage<'a, T>where T: Component,

§

type AccessMut<'c> = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'c> where Self: 'c

The wrapper through with mutable access of a component is performed.
§

type Component = T

The component type of the storage
source§

fn get_mut( &mut self, entity: Entity ) -> Option<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>

Get mutable access to an Entitys component
source§

fn get_mut_or_default( &mut self, entity: Entity ) -> Option<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>where Self::Component: Default,

Get mutable access to an Entitys component. If the component does not exist, it is automatically created using Default::default(). Read more
source§

fn insert( &mut self, entity: Entity, comp: Self::Component ) -> InsertResult<Self::Component>

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 WriteStorage<'a, T>where T: Component,

§

type AccessMut<'b> = <<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'b> where Self: 'b

The wrapper through with mutable access of a component is performed.
§

type Component = T

The component type of the storage
source§

fn get_mut( &mut self, entity: Entity ) -> Option<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>

Get mutable access to an Entitys component
source§

fn get_mut_or_default( &mut self, entity: Entity ) -> Option<<<T as Component>::Storage as UnprotectedStorage<T>>::AccessMut<'_>>where Self::Component: Default,

Get mutable access to an Entitys component. If the component does not exist, it is automatically created using Default::default(). Read more
source§

fn insert( &mut self, entity: Entity, comp: Self::Component ) -> InsertResult<Self::Component>

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> SystemData<'a> for WriteStorage<'a, T>where T: Component,

source§

fn setup(res: &mut World)

Sets up the system data for fetching it from the World.
source§

fn fetch(res: &'a World) -> Self

Fetches the system data from World. 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