Skip to content

Commit dc95e40

Browse files
Avoid vector clone
Signed-off-by: Luca Della Vedova <lucadv@intrinsic.ai>
1 parent 2c2f0ba commit dc95e40

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ authors = ["Grey <mxgrey@intrinsic.ai>"]
99
[dependencies]
1010
bevy = "0.11"
1111
arrayvec = "*"
12+
itertools = "*"
1213
smallvec = "*"
1314
crossbeam = "*"
1415
futures = "0.3"

src/chain/unzip.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use bevy::utils::all_tuples;
1919

20+
use itertools::Itertools;
2021
use smallvec::SmallVec;
2122

2223
use crate::{
@@ -36,7 +37,7 @@ pub trait Unzippable: Sized {
3637
}
3738

3839
macro_rules! impl_unzippable_for_tuple {
39-
($($T:ident),*) => {
40+
($(($T:ident, $D:ident)),*) => {
4041
#[allow(non_snake_case)]
4142
impl<$($T: 'static + Send + Sync),*> Unzippable for ($($T,)*)
4243
{
@@ -75,20 +76,12 @@ macro_rules! impl_unzippable_for_tuple {
7576
.get_entity_mut(source).or_broken()?
7677
.take_input::<Self>()?;
7778

78-
// Targets is cloned to avoid borrow checker issues when
79-
// doing a mutable borrow of the world later
80-
let targets = world.get::<ForkTargetStorage>(source).or_broken()?.clone();
81-
// The compiler throws a warning when implementing this for
82-
// tuple sizes that wouldn't use the result of the first _idx = _idx + 1
83-
// so we add a leading underscore to suppress the warning
84-
let mut _idx = 0;
79+
let ($($D,)*) = world.get::<ForkTargetStorage>(source).or_broken()?.0.iter().copied().next_tuple().or_broken()?;
8580
let ($($T,)*) = inputs;
8681
$(
87-
let target = *targets.0.get(_idx).or_broken()?;
88-
if let Some(mut t_mut) = world.get_entity_mut(target) {
82+
if let Some(mut t_mut) = world.get_entity_mut($D) {
8983
t_mut.give_input(session, $T, roster)?;
9084
}
91-
_idx = _idx + 1;
9285
)*
9386
Ok(())
9487
}
@@ -102,9 +95,10 @@ macro_rules! impl_unzippable_for_tuple {
10295
}
10396
}
10497

105-
// Implements the `Unzippable` trait for all tuples between size 1 and 15
98+
// Implements the `Unzippable` trait for all tuples between size 1 and 12
10699
// (inclusive) made of 'static lifetime types that are `Send` and `Sync`
107-
all_tuples!(impl_unzippable_for_tuple, 1, 15, T);
100+
// D is a dummy type
101+
all_tuples!(impl_unzippable_for_tuple, 1, 12, T, D);
108102

109103
/// A trait for constructs that are able to perform a forking unzip of an
110104
/// unzippable chain. An unzippable chain is one whose response type contains a
@@ -134,5 +128,5 @@ macro_rules! impl_unzipbuilder_for_tuple {
134128
}
135129
}
136130

137-
// Implements the `UnzipBuilder` trait for all tuples between size 1 and 15
138-
all_tuples!(impl_unzipbuilder_for_tuple, 2, 15, A, F, U);
131+
// Implements the `UnzipBuilder` trait for all tuples between size 1 and 12
132+
all_tuples!(impl_unzipbuilder_for_tuple, 2, 12, A, F, U);

0 commit comments

Comments
 (0)