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 5 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
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 15
// (inclusive) made of types that implement `Bufferable`
all_tuples!(impl_bufferable_for_tuple, 2, 15, 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
Loading