ComponentVec

Struct ComponentVec 

Source
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>

Source

pub fn new() -> Self

Constructs a new empty ComponentVec<T>.

The vector will not allocate until elements are pushed onto it.

Source

pub fn len(&self) -> usize

Returns the number of elements in the vector, also referred to as its ‘length’.

Source

pub fn is_empty(&self) -> bool

Returns true if the vector contains no elements.

Source

pub fn contains(&self, entity: Entity) -> bool

Returns true if the vector contains a component for the entity.

Source

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.

Source

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.

Source

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.

Source

pub fn remove(&mut self, entity: Entity) -> Option<T>

Removes a component from the entity and returns the component.

Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted into the vector.

Source

pub fn iter(&self) -> Iter<'_, T>

Returns an iterator that yields entities and their components as references.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator that yields entities and their components as mutable references.

Trait Implementations§

Source§

impl<T> Default for ComponentVec<T>

Source§

fn default() -> ComponentVec<T>

Returns the “default value” for a type. Read more
Source§

impl<'a, T> IntoIterator for &'a ComponentVec<T>

Source§

type IntoIter = Zip<Copied<Iter<'a, Entity>>, Iter<'a, T>>

Which kind of iterator are we turning this into?
Source§

type Item = (Entity, &'a T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoIterator for &'a mut ComponentVec<T>

Source§

type IntoIter = Zip<Copied<Iter<'a, Entity>>, IterMut<'a, T>>

Which kind of iterator are we turning this into?
Source§

type Item = (Entity, &'a mut T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, T> IntoQuery<'a> for &'a ComponentVec<T>
where T: 'a,

Source§

type Query = Zip<Copied<Iter<'a, Entity>>, Iter<'a, T>>

The type of the query.
Source§

fn into_query(self) -> Self::Query

Creates a query from a value.
Source§

impl<'a, T> IntoQuery<'a> for &'a mut ComponentVec<T>
where T: 'a,

Source§

type Query = Zip<Copied<Iter<'a, Entity>>, IterMut<'a, T>>

The type of the query.
Source§

fn into_query(self) -> Self::Query

Creates a query from a value.
Source§

impl<'a, T> IntoRefOrMut<'a> for &'a ComponentVec<T>

Source§

type Components = &'a [T]

The type of slice containing the components. Either a shared or a mutable reference.
Source§

fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>

Converts self into a RefOrMut.
Source§

impl<'a, T> IntoRefOrMut<'a> for &'a mut ComponentVec<T>

Source§

type Components = &'a mut [T]

The type of slice containing the components. Either a shared or a mutable reference.
Source§

fn into_ref_or_mut(self) -> RefOrMut<'a, Self::Components>

Converts self into a RefOrMut.

Auto Trait Implementations§

§

impl<T> Freeze for ComponentVec<T>

§

impl<T> RefUnwindSafe for ComponentVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for ComponentVec<T>
where T: Send,

§

impl<T> Sync for ComponentVec<T>
where T: Sync,

§

impl<T> Unpin for ComponentVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for ComponentVec<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. 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 T
where 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.

Source§

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

Source§

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

Source§

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.