pub struct Storage<C, D: UnprotectedStorage<C>, I> { /* private fields */ }Expand description
A storage for components managed with specs_static::Id instead of Entity.
This Storage behaves very similar to specs’ Storage.
§Registering
These component storages also have to be registered. This can be done using the WorldExt
trait and its register_tile_comp method.
Implementations§
Source§impl<C, D, I> Storage<C, D, I>
impl<C, D, I> Storage<C, D, I>
Sourcepub fn get(&self, id: I) -> Option<&C>
pub fn get(&self, id: I) -> Option<&C>
Tries to retrieve a component by its Id.
This will only check whether a component is inserted or not, without doing
any liveness checks for the id.
Examples found in repository?
examples/basic.rs (line 63)
62 fn run(&mut self, (tiles, materials): Self::SystemData) {
63 if let Some(mat) = materials.get(tiles.id(3, 4)) {
64 println!("The material at (3, 4) is {:?}.", mat);
65 }
66
67 let num_water: usize = (&*materials)
68 .join()
69 .map(|mat| match *mat {
70 Material::Water => 1,
71 _ => 0,
72 })
73 .sum();
74
75 println!("There are {} tiles with water.", num_water);
76 }Sourcepub fn get_mut(&mut self, id: I) -> Option<&mut C>
pub fn get_mut(&mut self, id: I) -> Option<&mut C>
Tries to retrieve a component mutably by its Id.
This will only check whether a component is inserted or not, without doing
any liveness checks for the id.
Sourcepub fn insert(&mut self, id: I, comp: C) -> Option<C>
pub fn insert(&mut self, id: I, comp: C) -> Option<C>
Inserts comp at id. If there already was a value, it will be returned.
In contrast to entities, there are no invalid ids.
Examples found in repository?
examples/basic.rs (line 94)
79fn main() {
80 let mut d = DispatcherBuilder::new().with(Sys, "sys", &[]).build();
81 let mut w = World::new();
82
83 // Use method provided by `WorldExt`.
84 w.add_resource(Tiles::new(8, 8));
85 w.register_tile_comp::<Material, TileId>();
86
87 // Initialize
88
89 {
90 let tiles = w.read_resource::<Tiles>();
91 let mut materials: TileCompsMut<Material> = SystemData::fetch(&w.res);
92
93 for tile_id in tiles.iter_all() {
94 materials.insert(tile_id, Material::Dirt);
95 }
96
97 materials.insert(tiles.id(1, 5), Material::Grass);
98 materials.insert(tiles.id(2, 5), Material::Grass);
99 materials.insert(tiles.id(3, 4), Material::Water);
100 materials.insert(tiles.id(3, 7), Material::Water);
101 }
102
103 // ---
104
105 d.dispatch(&mut w.res);
106}Trait Implementations§
Source§impl<C, D, I> Drop for Storage<C, D, I>where
D: UnprotectedStorage<C>,
impl<C, D, I> Drop for Storage<C, D, I>where
D: UnprotectedStorage<C>,
Source§impl<'a, C, D, I> Join for &'a Storage<C, D, I>where
D: UnprotectedStorage<C>,
impl<'a, C, D, I> Join for &'a Storage<C, D, I>where
D: UnprotectedStorage<C>,
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
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: u32) -> Self::Type
unsafe fn get(value: &mut Self::Value, id: u32) -> Self::Type
Get a joined component value by a given index.
Source§fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
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 moreSource§fn is_unconstrained() -> bool
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, C, D, I> Join for &'a mut Storage<C, D, I>where
D: UnprotectedStorage<C>,
impl<'a, C, D, I> Join for &'a mut Storage<C, D, I>where
D: UnprotectedStorage<C>,
Source§unsafe fn open(self) -> (Self::Mask, Self::Value)
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: u32) -> Self::Type
unsafe fn get(value: &mut Self::Value, id: u32) -> Self::Type
Get a joined component value by a given index.
Source§fn maybe(self) -> MaybeJoin<Self>where
Self: Sized,
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 moreSource§fn is_unconstrained() -> bool
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<C, D, I> Tracked for Storage<C, D, I>
impl<C, D, I> Tracked for Storage<C, D, I>
Source§fn channel(&self) -> &EventChannel<ComponentEvent>
fn channel(&self) -> &EventChannel<ComponentEvent>
Event channel tracking modified/inserted/removed components.
Source§fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>
fn channel_mut(&mut self) -> &mut EventChannel<ComponentEvent>
Mutable event channel tracking modified/inserted/removed components.
Auto Trait Implementations§
impl<C, D, I> Freeze for Storage<C, D, I>where
D: Freeze,
impl<C, D, I> RefUnwindSafe for Storage<C, D, I>
impl<C, D, I> Send for Storage<C, D, I>
impl<C, D, I> Sync for Storage<C, D, I>
impl<C, D, I> Unpin for Storage<C, D, I>
impl<C, D, I> UnwindSafe for Storage<C, D, I>
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
Mutably borrows from an owned value. Read more
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>
Converts
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>
Converts
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> TryDefault for Twhere
T: Default,
impl<T> TryDefault for Twhere
T: Default,
Source§fn try_default() -> Result<T, String>
fn try_default() -> Result<T, String>
Tries to create the default.
Source§fn unwrap_default() -> Self
fn unwrap_default() -> Self
Calls
try_default and panics on an error case.