Skip to content

Commit

Permalink
Add IntoIter derive for custom views
Browse files Browse the repository at this point in the history
  • Loading branch information
leudz committed Jun 15, 2024
1 parent 1aaecb4 commit 7548ba2
Show file tree
Hide file tree
Showing 10 changed files with 533 additions and 8 deletions.
7 changes: 2 additions & 5 deletions shipyard_proc/src/borrow_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ pub(crate) fn expand_borrow(
};

let mut gat_generics = generics.clone();
for generic in gat_generics.params.iter_mut() {
if let syn::GenericParam::Lifetime(lifetime) = generic {
lifetime.lifetime = parse_quote!('__view);
break;
}
if let Some(lifetime) = gat_generics.lifetimes_mut().next() {
lifetime.lifetime = parse_quote!('__view);
}

let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
Expand Down
483 changes: 483 additions & 0 deletions shipyard_proc/src/into_iter_expand.rs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions shipyard_proc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ extern crate proc_macro;
mod borrow_expand;
mod borrow_info_expand;
mod component_expand;
mod into_iter_expand;
mod label_expand;
mod world_borrow_expand;

use borrow_expand::expand_borrow;
use borrow_info_expand::expand_borrow_info;
use component_expand::{expand_component, expand_unique};
use into_iter_expand::expand_into_iter;
use label_expand::expand_label;
use world_borrow_expand::expand_world_borrow;

Expand Down Expand Up @@ -97,3 +99,16 @@ pub fn label(item: proc_macro::TokenStream) -> proc_macro::TokenStream {

expand_label(name, generics).into()
}

#[proc_macro_derive(IntoIter)]
pub fn into_iter(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse_macro_input!(item as syn::DeriveInput);

let name = input.ident;
let generics = input.generics;
let data = input.data;

expand_into_iter(name, generics, data)
.unwrap_or_else(|err| err.to_compile_error())
.into()
}
4 changes: 2 additions & 2 deletions src/iter/into_abstract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ pub trait IntoAbstract {
}
}

impl<'a, T: Component, Track: Tracking> IntoAbstract for &'a View<'a, T, Track> {
type AbsView = FullRawWindow<'a, T>;
impl<'a: 'b, 'b, T: Component, Track: Tracking> IntoAbstract for &'b View<'a, T, Track> {
type AbsView = FullRawWindow<'b, T>;

#[inline]
fn into_abstract(self) -> Self::AbsView {
Expand Down
7 changes: 7 additions & 0 deletions src/iter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ pub use par_iter::ParIter;
pub use par_mixed::ParMixed;
#[cfg(feature = "parallel")]
pub use par_tight::ParTight;
// used by proc macros
#[cfg(feature = "parallel")]
#[doc(hidden)]
pub use rayon::iter::{
plumbing::UnindexedConsumer as __UnindexedConsumer,
IndexedParallelIterator as __IndexedParallelIterator, ParallelIterator as __ParallelIterator,
};
pub use tight::Tight;
pub use with_id::{IntoWithId, LastId, WithId};
3 changes: 3 additions & 0 deletions src/iter/par_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ where
{
type Item = <Storage as AbstractMut>::Out;

#[inline]
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
Expand All @@ -36,6 +37,8 @@ where
ParIter::Mixed(mixed) => mixed.drive_unindexed(consumer),
}
}

#[inline]
fn opt_len(&self) -> Option<usize> {
match self {
ParIter::Tight(tight) => tight.opt_len(),
Expand Down
1 change: 1 addition & 0 deletions src/iter/par_mixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ where
{
type Item = <Mixed<Storage> as Iterator>::Item;

#[inline]
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
Expand Down
8 changes: 8 additions & 0 deletions src/iter/par_tight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ where
{
type Item = <Tight<Storage> as Iterator>::Item;

#[inline]
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge(self, consumer)
}

#[inline]
fn opt_len(&self) -> Option<usize> {
Some(self.len())
}
Expand All @@ -35,12 +38,17 @@ where
Storage: Clone + Send,
<Storage as AbstractMut>::Out: Send,
{
#[inline]
fn len(&self) -> usize {
self.0.len()
}

#[inline]
fn drive<C: Consumer<Self::Item>>(self, consumer: C) -> C::Result {
bridge(self, consumer)
}

#[inline]
fn with_producer<CB: ProducerCallback<Self::Item>>(self, callback: CB) -> CB::Output {
callback.callback(self.0)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub use scheduler::{
ScheduledWorkload, SystemModificator, Workload, WorkloadModificator, WorkloadSystem,
};
#[cfg(feature = "proc")]
pub use shipyard_proc::{Borrow, BorrowInfo, Component, Label, Unique, WorldBorrow};
pub use shipyard_proc::{Borrow, BorrowInfo, Component, IntoIter, Label, Unique, WorldBorrow};
pub use sparse_set::{
BulkAddEntity, SparseArray, SparseSet, SparseSetDrain, TupleAddComponent, TupleDelete,
TupleRemove,
Expand Down
11 changes: 11 additions & 0 deletions tests/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ fn check_derive() {

#[derive(Component, Unique)]
struct E;

#[derive(IntoIter)]
struct CustomView<'a> {
comp_a: shipyard::View<'a, A>,
v_comp_aa: shipyard::View<'a, A>,
comp_b: shipyard::ViewMut<'a, B>,
vm_comp_bb: shipyard::ViewMut<'a, B>,
}

#[derive(IntoIter)]
struct CustomView2<'a>(shipyard::View<'a, A>, shipyard::ViewMut<'a, B>);
}

/// Verify that the "default" attribute compiles.
Expand Down

0 comments on commit 7548ba2

Please sign in to comment.