pub struct ComponentVec<T> { /* private fields */ }Expand description
A densely packed growable array for associating entities with components.
§Examples
use checs::ComponentVec;
let mut entities = checs::entity::Allocator::new();
let e0 = entities.alloc();
let e1 = entities.alloc();
let e2 = entities.alloc();
let e3 = entities.alloc();
let mut vec = ComponentVec::new();
vec.insert(e0, 42);
vec.insert(e1, 17);
vec.insert(e2, 99);
assert_eq!(vec.len(), 3);
assert_eq!(vec.get(e0), Some(&42));
assert_eq!(vec.contains(e1), true);
assert_eq!(vec.get_mut(e2), Some(&mut 99));
assert_eq!(vec.get(e3), None);
assert_eq!(vec.remove(e2), Some(99));
assert_eq!(vec.get(e2), None);
for (entity, int) in &vec {
println!("{entity:?}: {int}");
}Implementations§
Source§impl<T> ComponentVec<T>
impl<T> ComponentVec<T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Constructs a new empty ComponentVec<T>.
The vector will not allocate until elements are pushed onto it.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the vector, also referred to as its ‘length’.
Sourcepub fn contains(&self, entity: Entity) -> bool
pub fn contains(&self, entity: Entity) -> bool
Returns true if the vector contains a component for the entity.
Sourcepub fn get(&self, entity: Entity) -> Option<&T>
pub fn get(&self, entity: Entity) -> Option<&T>
Returns a reference to the entity’s component, or None if the entity does not have that
component.
Sourcepub fn get_mut(&mut self, entity: Entity) -> Option<&mut T>
pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T>
Returns a mutable reference to an entity’s component, or None if the entity does not
have that component.
Sourcepub fn insert(&mut self, entity: Entity, component: T) -> Option<T>
pub fn insert(&mut self, entity: Entity, component: T) -> Option<T>
Adds a component to the entity.
If the entity already had that component its value is updated and the old value is
returned.
Sourcepub fn remove(&mut self, entity: Entity) -> Option<T>
pub fn remove(&mut self, entity: Entity) -> Option<T>
Removes a component from the entity and returns the component.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted into the vector.
Trait Implementations§
Source§impl<T> Default for ComponentVec<T>
impl<T> Default for ComponentVec<T>
Source§fn default() -> ComponentVec<T>
fn default() -> ComponentVec<T>
Source§impl<'a, T> IntoIterator for &'a ComponentVec<T>
impl<'a, T> IntoIterator for &'a ComponentVec<T>
Source§impl<'a, T> IntoIterator for &'a mut ComponentVec<T>
impl<'a, T> IntoIterator for &'a mut ComponentVec<T>
Source§impl<'a, T> IntoQuery<'a> for &'a ComponentVec<T>where
T: 'a,
impl<'a, T> IntoQuery<'a> for &'a ComponentVec<T>where
T: 'a,
Source§impl<'a, T> IntoQuery<'a> for &'a mut ComponentVec<T>where
T: 'a,
impl<'a, T> IntoQuery<'a> for &'a mut ComponentVec<T>where
T: 'a,
Source§impl<'a, T> IntoRefOrMut<'a> for &'a ComponentVec<T>
impl<'a, T> IntoRefOrMut<'a> for &'a ComponentVec<T>
Source§type Components = &'a [T]
type Components = &'a [T]
Source§fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>
fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>
self into a RefOrMut.Source§impl<'a, T> IntoRefOrMut<'a> for &'a mut ComponentVec<T>
impl<'a, T> IntoRefOrMut<'a> for &'a mut ComponentVec<T>
Source§type Components = &'a mut [T]
type Components = &'a mut [T]
Source§fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>
fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>
self into a RefOrMut.