Skip to content

Commit

Permalink
Changing EntityRaw to EntityDirect to make its functionality clearer …
Browse files Browse the repository at this point in the history
…and differentiate it from upcoming work
  • Loading branch information
recatek committed Oct 15, 2024
1 parent f9a039e commit b3b4614
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 169 deletions.
64 changes: 32 additions & 32 deletions macros/src/generate/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn generate_query_find(
if let Some(bound_params) = bound_params.get(&archetype.name) {
// Types and traits
let Archetype = format_ident!("{}", archetype.name);
let ArchetypeRaw = format_ident!("{}Raw", archetype.name);
let ArchetypeDirect = format_ident!("{}Direct", archetype.name);
let Type = bound_params
.iter()
.map(|p| to_type(p, &archetype))
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn generate_query_find(
None
}
}
#ArchetypeSelectInternalWorld::#ArchetypeRaw(#resolved_entity) => {
#ArchetypeSelectInternalWorld::#ArchetypeDirect(#resolved_entity) => {
// Alias the current archetype for use in the closure.
type MatchedArchetype = #Archetype;
// The closure needs to be made per-archetype because of OneOf types.
Expand Down Expand Up @@ -184,14 +184,14 @@ fn find_bind_mut(param: &ParseQueryParam) -> TokenStream {
ParseQueryParamType::EntityWild => {
quote!(view.entity)
}
ParseQueryParamType::EntityRaw(_) => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(view.index(), version))
ParseQueryParamType::EntityDirect(_) => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(view.index(), version))
}
ParseQueryParamType::EntityRawAny => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(view.index(), version).into())
ParseQueryParamType::EntityDirectAny => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(view.index(), version).into())
}
ParseQueryParamType::EntityRawWild => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(view.index(), version))
ParseQueryParamType::EntityDirectWild => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(view.index(), version))
}
ParseQueryParamType::OneOf(_) => {
panic!("must unpack OneOf first")
Expand All @@ -217,14 +217,14 @@ fn find_bind_borrow(param: &ParseQueryParam) -> TokenStream {
ParseQueryParamType::EntityWild => {
quote!(borrow.entity())
}
ParseQueryParamType::EntityRaw(_) => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(borrow.index(), version))
ParseQueryParamType::EntityDirect(_) => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(borrow.index(), version))
}
ParseQueryParamType::EntityRawAny => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(borrow.index(), version).into())
ParseQueryParamType::EntityDirectAny => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(borrow.index(), version).into())
}
ParseQueryParamType::EntityRawWild => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(borrow.index(), version))
ParseQueryParamType::EntityDirectWild => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(borrow.index(), version))
}
ParseQueryParamType::OneOf(_) => {
panic!("must unpack OneOf first")
Expand Down Expand Up @@ -486,14 +486,14 @@ fn iter_bind_mut(param: &ParseQueryParam) -> TokenStream {
ParseQueryParamType::EntityWild => {
quote!(&slices.entity[idx])
}
ParseQueryParamType::EntityRaw(_) => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version))
ParseQueryParamType::EntityDirect(_) => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version))
}
ParseQueryParamType::EntityRawAny => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version).into())
ParseQueryParamType::EntityDirectAny => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version).into())
}
ParseQueryParamType::EntityRawWild => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version))
ParseQueryParamType::EntityDirectWild => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version))
}
ParseQueryParamType::OneOf(_) => {
panic!("must unpack OneOf first")
Expand All @@ -519,14 +519,14 @@ fn iter_bind_borrow(param: &ParseQueryParam) -> TokenStream {
ParseQueryParamType::EntityWild => {
quote!(&archetype.get_slice_entities()[idx])
}
ParseQueryParamType::EntityRaw(_) => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version))
ParseQueryParamType::EntityDirect(_) => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version))
}
ParseQueryParamType::EntityRawAny => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version).into())
ParseQueryParamType::EntityDirectAny => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version).into())
}
ParseQueryParamType::EntityRawWild => {
quote!(&::gecs::__internal::new_entity_raw::<MatchedArchetype>(idx, version))
ParseQueryParamType::EntityDirectWild => {
quote!(&::gecs::__internal::new_entity_direct::<MatchedArchetype>(idx, version))
}
ParseQueryParamType::OneOf(_) => {
panic!("must unpack OneOf first")
Expand All @@ -547,9 +547,9 @@ fn to_type(param: &ParseQueryParam, archetype: &DataArchetype) -> TokenStream {
ParseQueryParamType::Entity(ident) => quote!(Entity<#ident>),
ParseQueryParamType::EntityAny => quote!(EntityAny),
ParseQueryParamType::EntityWild => quote!(Entity<#archetype_name>),
ParseQueryParamType::EntityRaw(ident) => quote!(EntityRaw<#ident>),
ParseQueryParamType::EntityRawAny => quote!(EntityRawAny),
ParseQueryParamType::EntityRawWild => quote!(EntityRaw<#archetype_name>),
ParseQueryParamType::EntityDirect(ident) => quote!(EntityDirect<#ident>),
ParseQueryParamType::EntityDirectAny => quote!(EntityDirectAny),
ParseQueryParamType::EntityDirectWild => quote!(EntityDirect<#archetype_name>),
ParseQueryParamType::OneOf(_) => panic!("must unpack OneOf first"),
}
}
Expand Down Expand Up @@ -602,13 +602,13 @@ fn bind_query_params(
ParseQueryParamType::EntityAny => {
bound.push(param.clone()); // Always matches
}
ParseQueryParamType::EntityRawAny => {
ParseQueryParamType::EntityDirectAny => {
bound.push(param.clone()); // Always matches
}
ParseQueryParamType::EntityWild => {
bound.push(param.clone()); // Always matches
}
ParseQueryParamType::EntityRawWild => {
ParseQueryParamType::EntityDirectWild => {
bound.push(param.clone()); // Always matches
}
ParseQueryParamType::Component(name) => {
Expand All @@ -625,7 +625,7 @@ fn bind_query_params(
continue; // No need to check more
}
}
ParseQueryParamType::EntityRaw(name) => {
ParseQueryParamType::EntityDirect(name) => {
if param.is_cfg_enabled == false || archetype.name == name.to_string() {
bound.push(param.clone());
} else {
Expand Down
58 changes: 29 additions & 29 deletions macros/src/generate/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {

let ArchetypeSelectId = format_ident!("ArchetypeSelectId");
let ArchetypeSelectEntity = format_ident!("ArchetypeSelectEntity");
let ArchetypeSelectEntityRaw = format_ident!("ArchetypeSelectEntityRaw");
let ArchetypeSelectEntityDirect = format_ident!("ArchetypeSelectEntityDirect");

let ArchetypeSelectInternalWorld =
format_ident!("__ArchetypeSelectInternal{}", world_data.name);
Expand All @@ -33,10 +33,10 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
.iter()
.map(|archetype| format_ident!("{}", archetype.name))
.collect::<Vec<_>>();
let ArchetypeRaw = world_data
let ArchetypeDirect = world_data
.archetypes
.iter()
.map(|archetype| format_ident!("{}Raw", archetype.name))
.map(|archetype| format_ident!("{}Direct", archetype.name))
.collect::<Vec<_>>();

// Variables and fields
Expand Down Expand Up @@ -80,7 +80,7 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
#WorldCapacity,
#ArchetypeSelectId,
#ArchetypeSelectEntity,
#ArchetypeSelectEntityRaw
#ArchetypeSelectEntityDirect
};

#[doc(hidden)]
Expand Down Expand Up @@ -215,15 +215,15 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
}

#[derive(Clone, Copy)]
pub enum #ArchetypeSelectEntityRaw {
#( #Archetype(EntityRaw<#Archetype>), )*
pub enum #ArchetypeSelectEntityDirect {
#( #Archetype(EntityDirect<#Archetype>), )*
}

// Combined dispatch table for resolving both entity key types.
#[doc(hidden)]
pub enum #ArchetypeSelectInternalWorld {
#( #Archetype(Entity<#Archetype>), )*
#( #ArchetypeRaw(EntityRaw<#Archetype>), )*
#( #ArchetypeDirect(EntityDirect<#Archetype>), )*
}

// Resolve dispatch implementation ----------------------------------------------------
Expand All @@ -250,24 +250,24 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
}
}

impl From<EntityRaw<#Archetype>> for #ArchetypeSelectId {
impl From<EntityDirect<#Archetype>> for #ArchetypeSelectId {
#[inline(always)]
fn from(entity: EntityRaw<#Archetype>) -> Self {
fn from(entity: EntityDirect<#Archetype>) -> Self {
#ArchetypeSelectId::#Archetype
}
}

impl From<EntityRaw<#Archetype>> for #ArchetypeSelectEntityRaw {
impl From<EntityDirect<#Archetype>> for #ArchetypeSelectEntityDirect {
#[inline(always)]
fn from(entity: EntityRaw<#Archetype>) -> Self {
#ArchetypeSelectEntityRaw::#Archetype(entity)
fn from(entity: EntityDirect<#Archetype>) -> Self {
#ArchetypeSelectEntityDirect::#Archetype(entity)
}
}

impl From<&EntityRaw<#Archetype>> for #ArchetypeSelectEntityRaw {
impl From<&EntityDirect<#Archetype>> for #ArchetypeSelectEntityDirect {
#[inline(always)]
fn from(entity: &EntityRaw<#Archetype>) -> Self {
#ArchetypeSelectEntityRaw::#Archetype(*entity)
fn from(entity: &EntityDirect<#Archetype>) -> Self {
#ArchetypeSelectEntityDirect::#Archetype(*entity)
}
}

Expand All @@ -278,10 +278,10 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
}
}

impl From<EntityRaw<#Archetype>> for #ArchetypeSelectInternalWorld {
impl From<EntityDirect<#Archetype>> for #ArchetypeSelectInternalWorld {
#[inline(always)]
fn from(entity: EntityRaw<#Archetype>) -> Self {
#ArchetypeSelectInternalWorld::#ArchetypeRaw(entity)
fn from(entity: EntityDirect<#Archetype>) -> Self {
#ArchetypeSelectInternalWorld::#ArchetypeDirect(entity)
}
}

Expand Down Expand Up @@ -370,17 +370,17 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
}
}

impl TryFrom<EntityRawAny> for #ArchetypeSelectEntityRaw {
impl TryFrom<EntityDirectAny> for #ArchetypeSelectEntityDirect {
type Error = EcsError;

#[inline(always)]
fn try_from(entity: EntityRawAny) -> Result<Self, EcsError> {
fn try_from(entity: EntityDirectAny) -> Result<Self, EcsError> {
match entity.archetype_id() {
#(
#Archetype::ARCHETYPE_ID => {
// We can use from_any_unchecked because we just checked the archetype
Ok(#ArchetypeSelectEntityRaw::#Archetype(
EntityRaw::<#Archetype>::from_any_unchecked(entity))
Ok(#ArchetypeSelectEntityDirect::#Archetype(
EntityDirect::<#Archetype>::from_any_unchecked(entity))
)
},
)*
Expand Down Expand Up @@ -408,17 +408,17 @@ pub fn generate_world(world_data: &DataWorld, raw_input: &str) -> TokenStream {
}
}

impl TryFrom<EntityRawAny> for #ArchetypeSelectInternalWorld {
impl TryFrom<EntityDirectAny> for #ArchetypeSelectInternalWorld {
type Error = EcsError;

#[inline(always)]
fn try_from(entity: EntityRawAny) -> Result<Self, EcsError> {
fn try_from(entity: EntityDirectAny) -> Result<Self, EcsError> {
match entity.archetype_id() {
#(
#Archetype::ARCHETYPE_ID => {
// We can use from_any_unchecked because we just checked the archetype
Ok(#ArchetypeSelectInternalWorld::#ArchetypeRaw(
EntityRaw::<#Archetype>::from_any_unchecked(entity))
Ok(#ArchetypeSelectInternalWorld::#ArchetypeDirect(
EntityDirect::<#Archetype>::from_any_unchecked(entity))
)
},
)*
Expand Down Expand Up @@ -873,14 +873,14 @@ fn section_archetype(archetype_data: &DataArchetype) -> TokenStream {
}
}

impl<'a> ArchetypeCanResolve<'a, #ArchetypeView<'a>, EntityRaw<#Archetype>> for #Archetype {
impl<'a> ArchetypeCanResolve<'a, #ArchetypeView<'a>, EntityDirect<#Archetype>> for #Archetype {
#[inline(always)]
fn resolve_for(&self, key: EntityRaw<#Archetype>) -> Option<usize> {
fn resolve_for(&self, key: EntityDirect<#Archetype>) -> Option<usize> {
self.data.resolve(key)
}

#[inline(always)]
fn resolve_view(&'a mut self, key: EntityRaw<#Archetype>) -> Option<#ArchetypeView<'a>> {
fn resolve_view(&'a mut self, key: EntityDirect<#Archetype>) -> Option<#ArchetypeView<'a>> {
self.data.get_view_mut(key)
}
}
Expand Down
36 changes: 18 additions & 18 deletions macros/src/parse/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mod kw {

syn::custom_keyword!(Entity);
syn::custom_keyword!(EntityAny);
syn::custom_keyword!(EntityRaw);
syn::custom_keyword!(EntityRawAny);
syn::custom_keyword!(EntityDirect);
syn::custom_keyword!(EntityDirectAny);
syn::custom_keyword!(OneOf);
}

Expand Down Expand Up @@ -61,9 +61,9 @@ pub enum ParseQueryParamType {
Entity(Ident),
EntityAny,
EntityWild, // Entity<_>
EntityRaw(Ident),
EntityRawAny,
EntityRawWild,
EntityDirect(Ident),
EntityDirectAny,
EntityDirectWild,
OneOf(Box<[Ident]>),
}

Expand Down Expand Up @@ -188,9 +188,9 @@ impl Parse for ParseQueryParam {
ParseQueryParamType::Entity(_)
| ParseQueryParamType::EntityAny
| ParseQueryParamType::EntityWild
| ParseQueryParamType::EntityRaw(_)
| ParseQueryParamType::EntityRawAny
| ParseQueryParamType::EntityRawWild
| ParseQueryParamType::EntityDirect(_)
| ParseQueryParamType::EntityDirectAny
| ParseQueryParamType::EntityDirectWild
if is_mut =>
{
Err(syn::Error::new(
Expand Down Expand Up @@ -230,32 +230,32 @@ impl Parse for ParseQueryParamType {
} else {
Err(lookahead.error())
}
} else if lookahead.peek(kw::EntityRaw) {
// EntityRaw<...>
input.parse::<kw::EntityRaw>()?;
} else if lookahead.peek(kw::EntityDirect) {
// EntityDirect<...>
input.parse::<kw::EntityDirect>()?;
input.parse::<Lt>()?;

// EntityRaw<A> or EntityRaw<_>
// EntityDirect<A> or EntityDirect<_>
let lookahead = input.lookahead1();
if lookahead.peek(Token![_]) {
input.parse::<Token![_]>()?;
input.parse::<Gt>()?;
Ok(ParseQueryParamType::EntityRawWild)
Ok(ParseQueryParamType::EntityDirectWild)
} else if lookahead.peek(Ident) {
let archetype = input.parse::<Ident>()?;
input.parse::<Gt>()?;
Ok(ParseQueryParamType::EntityRaw(archetype))
Ok(ParseQueryParamType::EntityDirect(archetype))
} else {
Err(lookahead.error())
}
} else if lookahead.peek(kw::EntityAny) {
// EntityAny
input.parse::<kw::EntityAny>()?;
Ok(ParseQueryParamType::EntityAny)
} else if lookahead.peek(kw::EntityRawAny) {
// EntityRawAny
input.parse::<kw::EntityRawAny>()?;
Ok(ParseQueryParamType::EntityRawAny)
} else if lookahead.peek(kw::EntityDirectAny) {
// EntityDirectAny
input.parse::<kw::EntityDirectAny>()?;
Ok(ParseQueryParamType::EntityDirectAny)
} else if lookahead.peek(kw::OneOf) {
// OneOf<A, B, C>
input.parse::<kw::OneOf>()?;
Expand Down
6 changes: 3 additions & 3 deletions src/archetype/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::archetype::iter::*;
use crate::archetype::slices::*;
use crate::archetype::slot::{Slot, SlotIndex};
use crate::archetype::view::*;
use crate::entity::{Entity, EntityRaw};
use crate::entity::{Entity, EntityDirect};
use crate::index::{TrimmedIndex, MAX_DATA_CAPACITY};
use crate::traits::{Archetype, EntityKey, StorageCanResolve};
use crate::util::{debug_checked_assume, num_assert_leq};
Expand Down Expand Up @@ -503,9 +503,9 @@ macro_rules! declare_storage_dynamic_n {
}
}

impl<A: Archetype, #(T~I,)*> StorageCanResolve<EntityRaw<A>> for $name<A, #(T~I,)*> {
impl<A: Archetype, #(T~I,)*> StorageCanResolve<EntityDirect<A>> for $name<A, #(T~I,)*> {
#[inline(always)]
fn resolve_for(&self, raw: EntityRaw<A>) -> Option<usize> {
fn resolve_for(&self, raw: EntityDirect<A>) -> Option<usize> {
let dense_index = raw.dense_index().into();

// We need to guarantee that the resulting index is in bounds.
Expand Down
Loading

0 comments on commit b3b4614

Please sign in to comment.