Type Definition 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 WriteStorage
s 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
.
Trait Implementations
sourceimpl<'a: 'b, 'b, T> GenericReadStorage for &'b WriteStorage<'a, T> where
T: Component,
impl<'a: 'b, 'b, T> GenericReadStorage for &'b WriteStorage<'a, T> where
T: Component,
sourceimpl<'a, T> GenericReadStorage for WriteStorage<'a, T> where
T: Component,
impl<'a, T> GenericReadStorage for WriteStorage<'a, T> where
T: Component,
sourceimpl<'a: 'b, 'b, T> GenericWriteStorage for &'b mut WriteStorage<'a, T> where
T: Component,
impl<'a: 'b, 'b, T> GenericWriteStorage for &'b mut WriteStorage<'a, T> where
T: Component,
type Component = T
type Component = T
The component type of the storage
sourcefn get_mut(&mut self, entity: Entity) -> Option<&'_ mut T>
fn get_mut(&mut self, entity: Entity) -> Option<&'_ mut T>
Get mutable access to an Entity
s component
sourcefn get_mut_or_default(&mut self, entity: Entity) -> Option<&'_ mut T> where
Self::Component: Default,
fn get_mut_or_default(&mut self, entity: Entity) -> Option<&'_ mut T> where
Self::Component: Default,
Get mutable access to an Entity
s component. If the component does not
exist, it is automatically created using Default::default()
. Read more
sourceimpl<'a, T> GenericWriteStorage for WriteStorage<'a, T> where
T: Component,
impl<'a, T> GenericWriteStorage for WriteStorage<'a, T> where
T: Component,
type Component = T
type Component = T
The component type of the storage
sourcefn get_mut(&mut self, entity: Entity) -> Option<&'_ mut T>
fn get_mut(&mut self, entity: Entity) -> Option<&'_ mut T>
Get mutable access to an Entity
s component
sourcefn get_mut_or_default(&mut self, entity: Entity) -> Option<&'_ mut T> where
Self::Component: Default,
fn get_mut_or_default(&mut self, entity: Entity) -> Option<&'_ mut T> where
Self::Component: Default,
Get mutable access to an Entity
s component. If the component does not
exist, it is automatically created using Default::default()
. Read more
sourceimpl<'a, T> SystemData<'a> for WriteStorage<'a, T> where
T: Component,
impl<'a, T> SystemData<'a> for WriteStorage<'a, T> where
T: Component,
sourcefn fetch(res: &'a World) -> Self
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. Read more
sourcefn reads() -> Vec<ResourceId>
fn reads() -> Vec<ResourceId>
Returns all read dependencies as fetched from Self::fetch
. Read more
sourcefn writes() -> Vec<ResourceId>
fn writes() -> Vec<ResourceId>
Returns all write dependencies as fetched from Self::fetch
. Read more