Skip to content

Commit

Permalink
Switch to on demand tracking
Browse files Browse the repository at this point in the history
Instead of being on the component type, tracking is set on the view
  • Loading branch information
leudz committed Feb 3, 2023
1 parent f6adbc2 commit 0e2d67a
Show file tree
Hide file tree
Showing 96 changed files with 3,013 additions and 3,949 deletions.
75 changes: 8 additions & 67 deletions shipyard_proc/src/component_expand.rs
Original file line number Diff line number Diff line change
@@ -1,77 +1,18 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Error, Result};

pub(crate) fn expand_component(
name: syn::Ident,
generics: syn::Generics,
attribute_input: Option<&syn::Attribute>,
) -> Result<TokenStream> {
let tracking = if let Some(tracking_attr) = attribute_input {
let tracking: syn::Ident = tracking_attr.parse_args().map_err(|_| {
Error::new_spanned(
&tracking_attr.tokens,
"Track should be one of: Untracked, Insertion, Modification, Deletion, Removal or All.",
)
})?;

let tracking_name = tracking.to_string();

match tracking_name.as_str() {
"Untracked" | "Insertion" | "Modification" | "Deletion" | "Removal" | "All" => {}
_ => return Err(Error::new_spanned(
&tracking,
"Track should be one of: Untracked, Insertion, Modification, Deletion, Removal or All.",
)),
}

quote!(#tracking)
} else {
quote!(Untracked)
};

pub(crate) fn expand_component(name: syn::Ident, generics: syn::Generics) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

Ok(quote!(
impl #impl_generics ::shipyard::Component for #name #ty_generics #where_clause {
type Tracking = ::shipyard::track::#tracking;
}
))
quote!(
impl #impl_generics ::shipyard::Component for #name #ty_generics #where_clause {}
)
}

pub(crate) fn expand_unique(
name: syn::Ident,
generics: syn::Generics,
attribute_input: Option<&syn::Attribute>,
) -> Result<TokenStream> {
let tracking = if let Some(tracking_attr) = attribute_input {
let tracking: syn::Ident = tracking_attr.parse_args().map_err(|_| {
Error::new_spanned(
&tracking_attr.tokens,
"Track should be one of: Untracked, Insertion, Modification, Deletion, Removal or All.",
)
})?;

let tracking_name = tracking.to_string();

match tracking_name.as_str() {
"Untracked" | "Insertion" | "Modification" | "Deletion" | "Removal" | "All" => {}
_ => return Err(Error::new_spanned(
&tracking,
"Track should be one of: Untracked, Insertion, Modification, Deletion, Removal or All.",
)),
}

quote!(#tracking)
} else {
quote!(Untracked)
};

pub(crate) fn expand_unique(name: syn::Ident, generics: syn::Generics) -> TokenStream {
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

Ok(quote!(
impl #impl_generics ::shipyard::Unique for #name #ty_generics #where_clause {
type Tracking = ::shipyard::track::#tracking;
}
))
quote!(
impl #impl_generics ::shipyard::Unique for #name #ty_generics #where_clause {}
)
}
26 changes: 2 additions & 24 deletions shipyard_proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,7 @@ pub fn component(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let name = input.ident;
let generics = input.generics;

let attribute_input: Option<&syn::Attribute> = input
.attrs
.iter()
.filter(|attr| match attr.style {
syn::AttrStyle::Outer => true,
syn::AttrStyle::Inner(_) => false,
})
.find(|attr| attr.path.get_ident().map(ToString::to_string) == Some("track".to_string()));

expand_component(name, generics, attribute_input)
.unwrap_or_else(|err| err.to_compile_error())
.into()
expand_component(name, generics).into()
}

#[proc_macro_derive(Unique, attributes(track))]
Expand All @@ -38,18 +27,7 @@ pub fn unique(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let name = input.ident;
let generics = input.generics;

let attribute_input: Option<&syn::Attribute> = input
.attrs
.iter()
.filter(|attr| match attr.style {
syn::AttrStyle::Outer => true,
syn::AttrStyle::Inner(_) => false,
})
.find(|attr| attr.path.get_ident().map(ToString::to_string) == Some("track".to_string()));

expand_unique(name, generics, attribute_input)
.unwrap_or_else(|err| err.to_compile_error())
.into()
expand_unique(name, generics).into()
}

#[proc_macro_derive(Borrow, attributes(shipyard))]
Expand Down
4 changes: 2 additions & 2 deletions src/add_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl AddComponent for () {
fn add_component_unchecked(&mut self, _: EntityId, _: Self::Component) {}
}

impl<T: Component> AddComponent for ViewMut<'_, T> {
impl<T: Component, const TRACK: u32> AddComponent for ViewMut<'_, T, TRACK> {
type Component = T;

#[inline]
Expand All @@ -46,7 +46,7 @@ impl<T: Component> AddComponent for ViewMut<'_, T> {
}
}

impl<T: Component> AddComponent for &mut ViewMut<'_, T> {
impl<T: Component, const TRACK: u32> AddComponent for &mut ViewMut<'_, T, TRACK> {
type Component = T;

#[inline]
Expand Down
4 changes: 2 additions & 2 deletions src/add_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl AddEntity for () {
fn add_entity(_: &mut Self, _: EntityId, _: Self::Component) {}
}

impl<T: Component> AddEntity for ViewMut<'_, T> {
impl<T: Component, const TRACK: u32> AddEntity for ViewMut<'_, T, TRACK> {
type Component = T;

#[inline]
Expand All @@ -27,7 +27,7 @@ impl<T: Component> AddEntity for ViewMut<'_, T> {
}
}

impl<T: Component> AddEntity for &mut ViewMut<'_, T> {
impl<T: Component, const TRACK: u32> AddEntity for &mut ViewMut<'_, T, TRACK> {
type Component = T;

#[inline]
Expand Down
72 changes: 36 additions & 36 deletions src/all_storages/custom_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ pub trait CustomStorageAccess {
///
/// [`RefMut`]: crate::atomic_refcell::RefMut
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Sync,
F: FnOnce() -> S;
Expand All @@ -159,22 +159,22 @@ pub trait CustomStorageAccess {
/// [`RefMut`]: crate::atomic_refcell::RefMut
/// [`StorageId`]: crate::storage::StorageId
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Sync,
F: FnOnce() -> S;
/// Returns a [`RefMut`] to the requested `S` storage and create it if it does not exist.
///
/// [`RefMut`]: crate::atomic_refcell::RefMut
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_sync_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_sync_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Send,
F: FnOnce() -> S;
Expand All @@ -183,22 +183,22 @@ pub trait CustomStorageAccess {
/// [`RefMut`]: crate::atomic_refcell::RefMut
/// [`StorageId`]: crate::storage::StorageId
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_sync_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_sync_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Send,
F: FnOnce() -> S;
/// Returns a [`RefMut`] to the requested `S` storage and create it if it does not exist.
///
/// [`RefMut`]: crate::atomic_refcell::RefMut
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_sync_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_sync_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage,
F: FnOnce() -> S;
Expand All @@ -207,11 +207,11 @@ pub trait CustomStorageAccess {
/// [`RefMut`]: crate::atomic_refcell::RefMut
/// [`StorageId`]: crate::storage::StorageId
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_sync_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_sync_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage,
F: FnOnce() -> S;
Expand Down Expand Up @@ -614,22 +614,22 @@ impl CustomStorageAccess for AllStorages {
}
#[cfg(feature = "thread_local")]
#[inline]
fn custom_storage_or_insert_non_send_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Sync,
F: FnOnce() -> S,
{
self.custom_storage_or_insert_non_send_mut_by_id(StorageId::of::<S>(), f)
}
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Sync,
F: FnOnce() -> S,
Expand Down Expand Up @@ -681,22 +681,22 @@ impl CustomStorageAccess for AllStorages {
}
#[cfg(feature = "thread_local")]
#[inline]
fn custom_storage_or_insert_non_sync_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_sync_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Send,
F: FnOnce() -> S,
{
self.custom_storage_or_insert_non_sync_mut_by_id(StorageId::of::<S>(), f)
}
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_sync_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_sync_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage + Send,
F: FnOnce() -> S,
Expand Down Expand Up @@ -740,22 +740,22 @@ impl CustomStorageAccess for AllStorages {
}
#[cfg(feature = "thread_local")]
#[inline]
fn custom_storage_or_insert_non_send_sync_mut<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_sync_mut<S, F>(
&self,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage,
F: FnOnce() -> S,
{
self.custom_storage_or_insert_non_send_sync_mut_by_id(StorageId::of::<S>(), f)
}
#[cfg(feature = "thread_local")]
fn custom_storage_or_insert_non_send_sync_mut_by_id<'a, S, F>(
&'a self,
fn custom_storage_or_insert_non_send_sync_mut_by_id<S, F>(
&self,
storage_id: StorageId,
f: F,
) -> Result<RefMut<'a, &'a mut S>, error::GetStorage>
) -> Result<RefMut<'_, &'_ mut S>, error::GetStorage>
where
S: 'static + Storage,
F: FnOnce() -> S,
Expand Down
2 changes: 1 addition & 1 deletion src/all_storages/delete_any/custom_delete_any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl CustomDeleteAny for () {
fn delete_any(&mut self, _: &mut HashSet<EntityId>, _current: u32) {}
}

impl<T: Component> CustomDeleteAny for SparseSet<T, T::Tracking> {
impl<T: Component> CustomDeleteAny for SparseSet<T> {
#[inline]
fn delete_any(&mut self, ids: &mut HashSet<EntityId>, current: u32) {
ids.extend(&self.dense);
Expand Down
Loading

0 comments on commit 0e2d67a

Please sign in to comment.