|
16 | 16 | */ |
17 | 17 |
|
18 | 18 | use bevy::prelude::{Entity, World}; |
| 19 | +use bevy::utils::all_tuples; |
19 | 20 |
|
20 | 21 | use smallvec::SmallVec; |
21 | 22 |
|
@@ -121,100 +122,63 @@ impl<T: 'static + Send + Sync + Clone> Buffered for CloneFromBuffer<T> { |
121 | 122 | } |
122 | 123 | } |
123 | 124 |
|
124 | | -impl<T0, T1> Buffered for (T0, T1) |
125 | | -where |
126 | | - T0: Buffered, |
127 | | - T1: Buffered, |
128 | | -{ |
129 | | - fn buffered_count( |
130 | | - &self, |
131 | | - session: Entity, |
132 | | - world: &World, |
133 | | - ) -> Result<usize, OperationError> { |
134 | | - Ok([ |
135 | | - self.0.buffered_count(session, world)?, |
136 | | - self.1.buffered_count(session, world)?, |
137 | | - ].iter().copied().min().unwrap_or(0)) |
138 | | - } |
| 125 | +macro_rules! impl_buffered_for_tuple { |
| 126 | + ($($T:ident),*) => { |
| 127 | + #[allow(non_snake_case)] |
| 128 | + impl<$($T: Buffered),*> Buffered for ($($T,)*) |
| 129 | + { |
| 130 | + fn buffered_count( |
| 131 | + &self, |
| 132 | + session: Entity, |
| 133 | + world: &World, |
| 134 | + ) -> Result<usize, OperationError> { |
| 135 | + let ($($T,)*) = self; |
| 136 | + Ok([ |
| 137 | + $( |
| 138 | + $T.buffered_count(session, world)?, |
| 139 | + )* |
| 140 | + ].iter().copied().min().unwrap_or(0)) |
| 141 | + } |
139 | 142 |
|
140 | | - type Item = (T0::Item, T1::Item); |
141 | | - fn pull( |
142 | | - &self, |
143 | | - session: Entity, |
144 | | - world: &mut World, |
145 | | - ) -> Result<Self::Item, OperationError> { |
146 | | - let t0 = self.0.pull(session, world)?; |
147 | | - let t1 = self.1.pull(session, world)?; |
148 | | - Ok((t0, t1)) |
149 | | - } |
| 143 | + type Item = ($($T::Item),*); |
| 144 | + fn pull( |
| 145 | + &self, |
| 146 | + session: Entity, |
| 147 | + world: &mut World, |
| 148 | + ) -> Result<Self::Item, OperationError> { |
| 149 | + let ($($T,)*) = self; |
| 150 | + Ok(($( |
| 151 | + $T.pull(session, world)?, |
| 152 | + )*)) |
| 153 | + } |
150 | 154 |
|
151 | | - fn listen( |
152 | | - &self, |
153 | | - listener: Entity, |
154 | | - world: &mut World, |
155 | | - ) -> OperationResult { |
156 | | - self.0.listen(listener, world)?; |
157 | | - self.1.listen(listener, world)?; |
158 | | - Ok(()) |
159 | | - } |
| 155 | + fn listen( |
| 156 | + &self, |
| 157 | + listener: Entity, |
| 158 | + world: &mut World, |
| 159 | + ) -> OperationResult { |
| 160 | + let ($($T,)*) = self; |
| 161 | + $( |
| 162 | + $T.listen(listener, world)?; |
| 163 | + )* |
| 164 | + Ok(()) |
| 165 | + } |
160 | 166 |
|
161 | | - fn as_input(&self) -> SmallVec<[Entity; 8]> { |
162 | | - let mut inputs = SmallVec::new(); |
163 | | - inputs.extend(self.0.as_input()); |
164 | | - inputs.extend(self.1.as_input()); |
165 | | - inputs |
| 167 | + fn as_input(&self) -> SmallVec<[Entity; 8]> { |
| 168 | + let mut inputs = SmallVec::new(); |
| 169 | + let ($($T,)*) = self; |
| 170 | + $( |
| 171 | + inputs.extend($T.as_input()); |
| 172 | + )* |
| 173 | + inputs |
| 174 | + } |
| 175 | + } |
166 | 176 | } |
167 | 177 | } |
168 | 178 |
|
169 | | -impl<T0, T1, T2> Buffered for (T0, T1, T2) |
170 | | -where |
171 | | - T0: Buffered, |
172 | | - T1: Buffered, |
173 | | - T2: Buffered, |
174 | | -{ |
175 | | - fn buffered_count( |
176 | | - &self, |
177 | | - session: Entity, |
178 | | - world: &World, |
179 | | - ) -> Result<usize, OperationError> { |
180 | | - Ok([ |
181 | | - self.0.buffered_count(session, world)?, |
182 | | - self.1.buffered_count(session, world)?, |
183 | | - self.2.buffered_count(session, world)?, |
184 | | - ].iter().copied().min().unwrap_or(0)) |
185 | | - } |
186 | | - |
187 | | - type Item = (T0::Item, T1::Item, T2::Item); |
188 | | - fn pull( |
189 | | - &self, |
190 | | - session: Entity, |
191 | | - world: &mut World, |
192 | | - ) -> Result<Self::Item, OperationError> { |
193 | | - let t0 = self.0.pull(session, world)?; |
194 | | - let t1 = self.1.pull(session, world)?; |
195 | | - let t2 = self.2.pull(session, world)?; |
196 | | - Ok((t0, t1, t2)) |
197 | | - } |
198 | | - |
199 | | - fn listen( |
200 | | - &self, |
201 | | - listener: Entity, |
202 | | - world: &mut World, |
203 | | - ) -> OperationResult { |
204 | | - self.0.listen(listener, world)?; |
205 | | - self.1.listen(listener, world)?; |
206 | | - self.2.listen(listener, world)?; |
207 | | - Ok(()) |
208 | | - } |
209 | | - |
210 | | - fn as_input(&self) -> SmallVec<[Entity; 8]> { |
211 | | - let mut inputs = SmallVec::new(); |
212 | | - inputs.extend(self.0.as_input()); |
213 | | - inputs.extend(self.1.as_input()); |
214 | | - inputs.extend(self.2.as_input()); |
215 | | - inputs |
216 | | - } |
217 | | -} |
| 179 | +// Implements the `Buffered` trait for all tuples between size 2 and 12 |
| 180 | +// (inclusive) made of types that implement `Buffered` |
| 181 | +all_tuples!(impl_buffered_for_tuple, 2, 12, T); |
218 | 182 |
|
219 | 183 | impl<T: Buffered, const N: usize> Buffered for [T; N] { |
220 | 184 | fn buffered_count( |
|
0 commit comments