Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use macros for tuple implementations of various types #2

Merged
merged 14 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ authors = ["Grey <mxgrey@intrinsic.ai>"]
[dependencies]
bevy = "0.11"
arrayvec = "*"
itertools = "*"
smallvec = "*"
crossbeam = "*"
futures = "0.3"
Expand Down
45 changes: 18 additions & 27 deletions src/buffer/bufferable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/

use bevy::utils::all_tuples;
use smallvec::SmallVec;

use crate::{
Expand Down Expand Up @@ -76,36 +77,26 @@ impl<T: 'static + Send + Sync> Bufferable for Output<T> {
}
}

impl<T0, T1> Bufferable for (T0, T1)
where
T0: Bufferable,
T1: Bufferable,
{
type BufferType = (T0::BufferType, T1::BufferType);
fn as_buffer(self, builder: &mut Builder) -> Self::BufferType {
(
self.0.as_buffer(builder),
self.1.as_buffer(builder),
)
}
}

impl<T0, T1, T2> Bufferable for (T0, T1, T2)
where
T0: Bufferable,
T1: Bufferable,
T2: Bufferable,
{
type BufferType = (T0::BufferType, T1::BufferType, T2::BufferType);
fn as_buffer(self, builder: &mut Builder) -> Self::BufferType {
(
self.0.as_buffer(builder),
self.1.as_buffer(builder),
self.2.as_buffer(builder),
)
macro_rules! impl_bufferable_for_tuple {
($($T:ident),*) => {
#[allow(non_snake_case)]
impl<$($T: Bufferable),*> Bufferable for ($($T,)*)
{
type BufferType = ($($T::BufferType,)*);
fn as_buffer(self, builder: &mut Builder) -> Self::BufferType {
let ($($T,)*) = self;
($(
$T.as_buffer(builder),
)*)
}

}
}
}

// Implements the `Bufferable` trait for all tuples between size 2 and 12
// (inclusive) made of types that implement `Bufferable`
all_tuples!(impl_bufferable_for_tuple, 2, 12, T);

impl<T: Bufferable, const N: usize> Bufferable for [T; N] {
type BufferType = [T::BufferType; N];
Expand Down
140 changes: 52 additions & 88 deletions src/buffer/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

use bevy::prelude::{Entity, World};
use bevy::utils::all_tuples;

use smallvec::SmallVec;

Expand Down Expand Up @@ -121,100 +122,63 @@ impl<T: 'static + Send + Sync + Clone> Buffered for CloneFromBuffer<T> {
}
}

impl<T0, T1> Buffered for (T0, T1)
where
T0: Buffered,
T1: Buffered,
{
fn buffered_count(
&self,
session: Entity,
world: &World,
) -> Result<usize, OperationError> {
Ok([
self.0.buffered_count(session, world)?,
self.1.buffered_count(session, world)?,
].iter().copied().min().unwrap_or(0))
}
macro_rules! impl_buffered_for_tuple {
($($T:ident),*) => {
#[allow(non_snake_case)]
impl<$($T: Buffered),*> Buffered for ($($T,)*)
{
fn buffered_count(
&self,
session: Entity,
world: &World,
) -> Result<usize, OperationError> {
let ($($T,)*) = self;
Ok([
$(
$T.buffered_count(session, world)?,
)*
].iter().copied().min().unwrap_or(0))
}

type Item = (T0::Item, T1::Item);
fn pull(
&self,
session: Entity,
world: &mut World,
) -> Result<Self::Item, OperationError> {
let t0 = self.0.pull(session, world)?;
let t1 = self.1.pull(session, world)?;
Ok((t0, t1))
}
type Item = ($($T::Item),*);
fn pull(
&self,
session: Entity,
world: &mut World,
) -> Result<Self::Item, OperationError> {
let ($($T,)*) = self;
Ok(($(
$T.pull(session, world)?,
)*))
}

fn listen(
&self,
listener: Entity,
world: &mut World,
) -> OperationResult {
self.0.listen(listener, world)?;
self.1.listen(listener, world)?;
Ok(())
}
fn listen(
&self,
listener: Entity,
world: &mut World,
) -> OperationResult {
let ($($T,)*) = self;
$(
$T.listen(listener, world)?;
)*
Ok(())
}

fn as_input(&self) -> SmallVec<[Entity; 8]> {
let mut inputs = SmallVec::new();
inputs.extend(self.0.as_input());
inputs.extend(self.1.as_input());
inputs
fn as_input(&self) -> SmallVec<[Entity; 8]> {
let mut inputs = SmallVec::new();
let ($($T,)*) = self;
$(
inputs.extend($T.as_input());
)*
inputs
}
}
}
}

impl<T0, T1, T2> Buffered for (T0, T1, T2)
where
T0: Buffered,
T1: Buffered,
T2: Buffered,
{
fn buffered_count(
&self,
session: Entity,
world: &World,
) -> Result<usize, OperationError> {
Ok([
self.0.buffered_count(session, world)?,
self.1.buffered_count(session, world)?,
self.2.buffered_count(session, world)?,
].iter().copied().min().unwrap_or(0))
}

type Item = (T0::Item, T1::Item, T2::Item);
fn pull(
&self,
session: Entity,
world: &mut World,
) -> Result<Self::Item, OperationError> {
let t0 = self.0.pull(session, world)?;
let t1 = self.1.pull(session, world)?;
let t2 = self.2.pull(session, world)?;
Ok((t0, t1, t2))
}

fn listen(
&self,
listener: Entity,
world: &mut World,
) -> OperationResult {
self.0.listen(listener, world)?;
self.1.listen(listener, world)?;
self.2.listen(listener, world)?;
Ok(())
}

fn as_input(&self) -> SmallVec<[Entity; 8]> {
let mut inputs = SmallVec::new();
inputs.extend(self.0.as_input());
inputs.extend(self.1.as_input());
inputs.extend(self.2.as_input());
inputs
}
}
// Implements the `Buffered` trait for all tuples between size 2 and 12
// (inclusive) made of types that implement `Buffered`
all_tuples!(impl_buffered_for_tuple, 2, 12, T);

impl<T: Buffered, const N: usize> Buffered for [T; N] {
fn buffered_count(
Expand Down
110 changes: 52 additions & 58 deletions src/chain/fork_clone_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*
*/

use bevy::utils::all_tuples;

use crate::{
Chain, UnusedTarget, AddOperation, ForkClone, ForkTargetStorage, Builder,
Output,
Expand All @@ -30,65 +32,57 @@ pub trait ForkCloneBuilder<Response> {
) -> Self::Outputs;
}

impl<R, F0, U0, F1, U1> ForkCloneBuilder<R> for (F0, F1)
where
R: 'static + Send + Sync + Clone,
F0: FnOnce(Chain<R>) -> U0,
F1: FnOnce(Chain<R>) -> U1,
{
type Outputs = (U0, U1);
macro_rules! impl_forkclonebuilder_for_tuple {
($(($F:ident, $U:ident)),*) => {
#[allow(non_snake_case)]
impl<R: 'static + Send + Sync + Clone, $($F: FnOnce(Chain<R>) -> $U),*, $($U),*> ForkCloneBuilder<R> for ($($F,)*)
{
type Outputs = ($($U,)*);
fn build_fork_clone(
self,
source: Output<R>,
builder: &mut Builder,
) -> Self::Outputs {
let targets =
[
$(
{
// Variable is only used to make sure this cycle is repeated once
// for each instance of the $T type, but the type itself is not
// used.
#[allow(unused)]
let $F = std::marker::PhantomData::<$F>;
builder.commands.spawn(UnusedTarget).id()
},
)*
];

fn build_fork_clone(
self,
source: Output<R>,
builder: &mut Builder,
) -> Self::Outputs {
let target_0 = builder.commands.spawn(UnusedTarget).id();
let target_1 = builder.commands.spawn(UnusedTarget).id();

builder.commands.add(AddOperation::new(
Some(source.scope()),
source.id(),
ForkClone::<R>::new(
ForkTargetStorage::from_iter([target_0, target_1])
)
));

let u_0 = (self.0)(Chain::new(target_0, builder));
let u_1 = (self.1)(Chain::new(target_1, builder));
(u_0, u_1)
builder.commands.add(AddOperation::new(
Some(source.scope()),
source.id(),
ForkClone::<R>::new(
ForkTargetStorage::from_iter(targets)
)
));
let ($($F,)*) = self;
// The compiler throws a warning when implementing this for
// tuple sizes that wouldn't use the result of the first _idx = _idx + 1
// so we add a leading underscore to suppress the warning
let mut _idx = 0;
(
$(
{
let res = ($F)(Chain::new(targets[_idx], builder));
_idx = _idx + 1;
res
},
)*
)
}
}
}
}

impl<R, F0, U0, F1, U1, F2, U2> ForkCloneBuilder<R> for (F0, F1, F2)
where
R: 'static + Send + Sync + Clone,
F0: FnOnce(Chain<R>) -> U0,
F1: FnOnce(Chain<R>) -> U1,
F2: FnOnce(Chain<R>) -> U2,
{
type Outputs = (U0, U1, U2);

fn build_fork_clone(
self,
source: Output<R>,
builder: &mut Builder,
) -> Self::Outputs {
let target_0 = builder.commands.spawn(UnusedTarget).id();
let target_1 = builder.commands.spawn(UnusedTarget).id();
let target_2 = builder.commands.spawn(UnusedTarget).id();

builder.commands.add(AddOperation::new(
Some(source.scope()),
source.id(),
ForkClone::<R>::new(
ForkTargetStorage::from_iter([target_0, target_1, target_2])
)
));

let u_0 = (self.0)(Chain::new(target_0, builder));
let u_1 = (self.1)(Chain::new(target_1, builder));
let u_2 = (self.2)(Chain::new(target_2, builder));
(u_0, u_1, u_2)
}
}
// Implements the `ForkCloneBUilder` trait for all tuples between size 2 and 15
// (inclusive)
all_tuples!(impl_forkclonebuilder_for_tuple, 2, 15, F, U);
Copy link
Contributor

@mxgrey mxgrey Jul 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor nitpick, but let's cap the tuple size to 12 for all of the tuple implementations for the sake of consistency, so users don't get additionally confused by hitting different tuple limits for different functions. If someone wants more than 12 forked clones there are several ways they could still accomplish that with a limit of 12.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

903663c also found that there was a tuple with size 1 implemented manually and removed that to make the diff even more red c97a7fc

Loading