Skip to content

Commit

Permalink
Adding helper functions for extracting a component by type from a tup…
Browse files Browse the repository at this point in the history
…le of an archetype's components
  • Loading branch information
recatek committed Jan 20, 2025
1 parent bc149e9 commit 0f21486
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
16 changes: 15 additions & 1 deletion macros/src/generate/world.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use proc_macro2::TokenStream;
use proc_macro2::{TokenStream, Literal};
use quote::{format_ident, quote};
use xxhash_rust::xxh3::xxh3_128;

Expand Down Expand Up @@ -686,6 +686,10 @@ fn section_archetype(archetype_data: &DataArchetype) -> TokenStream {
.iter()
.map(|component| component.id)
.collect::<Vec<_>>();
let component_index = (0..archetype_data.components.len())
.into_iter()
.map(|idx| Literal::usize_unsuffixed(idx))
.collect::<Vec<_>>();

// Types and traits
let Archetype = format_ident!("{}", archetype_data.name);
Expand Down Expand Up @@ -886,6 +890,16 @@ fn section_archetype(archetype_data: &DataArchetype) -> TokenStream {
fn resolve_borrow_component_mut<'a>(borrow: &'a #ArchetypeBorrow<'a>) -> RefMut<'a, #Component> {
borrow.0.#borrow_component_mut()
}

#[inline(always)]
fn resolve_extract(components: &Self::Components) -> &#Component {
&components.#component_index
}

#[inline(always)]
fn resolve_extract_mut(components: &mut Self::Components) -> &mut #Component {
&mut components.#component_index
}
}
)*

Expand Down
69 changes: 69 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ where

/// A tuple of the components in this archetype.
type Components;

/// The slices type when accessing all of this archetype's slices simultaneously.
type Slices<'a>
where
Expand Down Expand Up @@ -597,6 +598,70 @@ where
<Self as ArchetypeHas<C>>::resolve_borrow_slice_mut(self)
}

/// Extracts a component reference by type from this archetype's component tuple.
///
/// # Examples
///
/// ```rust
/// use gecs::prelude::*;
///
/// pub struct CompA(u32);
/// pub struct CompB(u32);
///
/// ecs_world! {
/// ecs_archetype!(ArchFoo, CompA, CompB);
/// }
///
/// fn main() {
/// let mut world = EcsWorld::default();
///
/// let entity_a = world.create::<ArchFoo>((CompA(1), CompB(2),));
/// let components = world.destroy(entity_a).unwrap();
///
/// assert_eq!(ArchFoo::extract::<CompB>(&components).0, 2)
/// }
/// ```
#[inline(always)]
fn extract<C>(components: &Self::Components) -> &C
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_extract(components)
}

/// Extracts a mutable component reference by type from this archetype's component tuple.
///
/// # Examples
///
/// ```rust
/// use gecs::prelude::*;
///
/// pub struct CompA(u32);
/// pub struct CompB(u32);
///
/// ecs_world! {
/// ecs_archetype!(ArchFoo, CompA, CompB);
/// }
///
/// fn main() {
/// let mut world = EcsWorld::default();
///
/// let entity_a = world.create::<ArchFoo>((CompA(1), CompB(2),));
/// let mut components = world.destroy(entity_a).unwrap();
///
/// ArchFoo::extract_mut::<CompB>(&mut components).0 += 1;
///
/// assert_eq!(ArchFoo::extract::<CompB>(&components).0, 3)
/// }
/// ```
#[inline(always)]
fn extract_mut<C>(components: &mut Self::Components) -> &mut C
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_extract_mut(components)
}

/// Returns an iterator over all the entities created since the last time entity events were
/// cleared on the world or on this specific archetype. This list has no ordering guarantees.
/// Note that entities appear in this list even if they have since been destroyed.
Expand Down Expand Up @@ -770,6 +835,10 @@ pub trait ArchetypeHas<C>: Archetype {
fn resolve_borrow_component<'a>(borrow: &'a Self::Borrow<'a>) -> Ref<'a, C>;
#[doc(hidden)]
fn resolve_borrow_component_mut<'a>(borrow: &'a Self::Borrow<'a>) -> RefMut<'a, C>;
#[doc(hidden)]
fn resolve_extract(components: &Self::Components) -> &C;
#[doc(hidden)]
fn resolve_extract_mut(components: &mut Self::Components) -> &mut C;
}

// NOTE: There's no point in trying to make a View/ViewMut split because each column is in a
Expand Down

0 comments on commit 0f21486

Please sign in to comment.