Components

Struct Components 

Source
pub struct Components { /* private fields */ }
Expand description

The set of all entities’ components.

Exists as a resource in the World by default.

Components is the store that holds every entity’s components. It is an archetypal storage, which means it is optimized for fast traversal and space-saving (in terms of CPU memory).

Use Components when you need to add or remove components from entities eagerly and immediately. For this you will need a mutable reference, which means it will block access to Components by other systems. If you don’t need to see the effects of your changes immediately you can use crate::Entity::insert_bundle, which works lazily (changes appear at the end of each frame), and within an async context crate::Entity::updates will return a future that completes when these changes have resolved.

Here’s an example of a system that creates and updates an entity immediately:

fn mk_ent(
    (mut entities, mut components): (ViewMut<Entities>, ViewMut<Components>),
) -> Result<(), GraphError> {
    let e = entities.create();
    components.insert_bundle(*e, (123, "123", 123.0));
    end()
}
let mut world = World::default();
world.add_subgraph(graph!(mk_ent));
world.tick().unwrap();

§A note about bundles

Component archetypes (the collection of unique components for a set of entities) are made up of bundles. Bundles are tuples that implement IsBundle. Each component element in a bundle must be unique and 'static. IsBundle is implemented by tuples sized 1 to 12.

Implementations§

Source§

impl Components

Source

pub fn insert_bundle<B: IsBundle>(&mut self, entity_id: usize, bundle: B)

Inserts the bundled components for the given entity.

§Panics
  • if the bundle’s types are not unique
use apecs::*;

let mut components = Components::default();
components.insert_bundle(0, ("zero", 0u32, 0.0f32));
Source

pub fn get_component<T: Send + Sync + 'static>( &self, entity_id: usize, ) -> Option<impl Deref<Target = T> + '_>

Returns a reference to a single component, if possible.

Source

pub fn get_component_mut<T: Send + Sync + 'static>( &mut self, entity_id: usize, ) -> Option<impl DerefMut<Target = T> + '_>

Returns a mutable reference to a single component, if possible.

Source

pub fn insert_component<T: Send + Sync + 'static>( &mut self, entity_id: usize, component: T, ) -> Option<T>

Insert a single component for the given entity.

Returns the previous component, if available.

use apecs::*;

let mut components = Components::default();
components.insert_component(0, "zero");
components.insert_component(0, 0u32);
components.insert_component(0, 0.0f32);
let prev = components.insert_component(0, "none");
assert_eq!(Some("zero"), prev);
Source

pub fn remove<B: IsBundle>(&mut self, entity_id: usize) -> Option<B>

Remove all components of the given entity and return them as a typed bundle.

§Warning

Any components not contained in B will be discarded.

§Panics

Panics if the component types are not unique.

use apecs::*;

let mut components = Components::default();
components.insert_bundle(0, ("zero", 0u32, 0.0f32));
let prev = components.remove::<(u32, f32)>(0);
assert_eq!(Some((0, 0.0)), prev);
Source

pub fn remove_component<T: Send + Sync + 'static>( &mut self, entity_id: usize, ) -> Option<T>

Remove a single component from the given entity, returning it if the entity had a component of that type.

use apecs::*;

let mut components = Components::default();
components.insert_bundle(0, ("zero", 0u32, 0.0f32));
let prev = components.remove_component::<&str>(0);
assert_eq!(Some("zero"), prev);
let dne = components.remove_component::<bool>(0);
assert_eq!(None, dne);
Source

pub fn len(&self) -> usize

Return the number of entities with archetypes.

Source

pub fn is_empty(&self) -> bool

Return whether any entities with archetypes exist in storage.

Source§

impl Components

Source

pub fn extend<B: IsBundle>( &mut self, extension: <B::MutBundle as IsQuery>::ExtensionColumns, )
where B::MutBundle: IsQuery,

Append to the existing set of archetypes in bulk.

This assumes the entries being inserted have unique ids and don’t already exist in the set.

use apecs::*;
let mut components = Components::default();
let a = Box::new((0..10_000).map(|id| Entry::new(id, id as u32)));
let b = Box::new((0..10_000).map(|id| Entry::new(id, id as f32)));
let c = Box::new((0..10_000).map(|id| Entry::new(id, format!("string{}", id))));
let d = Box::new((0..10_000).map(|id| Entry::new(id, id % 2 == 0)));
components.extend::<(u32, f32, String, bool)>((a, b, c, d));
assert_eq!(10_000, components.len());
Source

pub fn query<Q: IsQuery + 'static>(&mut self) -> QueryGuard<'_, Q>

Prepare a query.

use apecs::*;

let mut components = Components::default();
components.insert_bundle(0, ("zero", 0.0f32, 0u32));
components.insert_bundle(1, ("zero", 0.0f32, 0u32));
components.insert_bundle(2, ("zero", 0.0f32, 0u32));

for (f, u) in components.query::<(&f32, &u32)>().iter_mut() {
    assert_eq!(**f, **u as f32);
}

Trait Implementations§

Source§

impl Debug for Components

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Components

Source§

fn default() -> Self

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

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> IntoEither for T

Source§

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> SatisfyTraits<dyn None> for T

Source§

impl<T> SatisfyTraits<dyn Send> for T
where T: Send,

Source§

impl<T> SatisfyTraits<dyn Send + Sync> for T
where T: Send + Sync,

Source§

impl<T> SatisfyTraits<dyn Sync> for T
where T: Sync,