Skip to content

Commit 74f9054

Browse files
committed
More macro cleverness (4) avoid Y_rev
1 parent 69113be commit 74f9054

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/tuple_impl.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,16 @@ macro_rules! count_ident{
251251
macro_rules! ignore_ident{
252252
($id:ident, $($t:tt)*) => {$($t)*};
253253
}
254+
macro_rules! rev_for_each_ident{
255+
($m:ident, ) => {};
256+
($m:ident, $i0:ident, $($i:ident,)*) => {
257+
rev_for_each_ident!($m, $($i,)*);
258+
$m!($i0);
259+
};
260+
}
254261

255262
macro_rules! impl_tuple_collect {
256-
($($Y:ident),* ; $($Y_rev:ident),*) => (
263+
($($Y:ident),*) => (
257264
impl<A> TupleCollect for ($(ignore_ident!($Y, A),)*) {
258265
type Item = A;
259266
type Buffer = [Option<A>; count_ident!($($Y,)*) - 1];
@@ -302,39 +309,29 @@ macro_rules! impl_tuple_collect {
302309
count_ident!($($Y,)*)
303310
}
304311

305-
fn left_shift_push(&mut self, item: A) {
312+
fn left_shift_push(&mut self, mut item: A) {
306313
use std::mem::replace;
307314

308315
let &mut ($(ref mut $Y),*,) = self;
309-
$(
310-
let item = replace($Y_rev, item);
311-
)*
316+
macro_rules! replace_item{($i:ident) => {
317+
item = replace($i, item);
318+
}};
319+
rev_for_each_ident!(replace_item, $($Y,)*);
312320
drop(item);
313321
}
314322
}
315323
)
316324
}
317325

318-
// This snippet generates the twelve `impl_tuple_collect!` invocations:
319-
// use core::iter;
320-
// use itertools::Itertools;
321-
//
322-
// for i in 1..=12 {
323-
// println!("impl_tuple_collect!({idents}; {rev_idents});",
324-
// idents=('a'..='z').take(i).join(", "),
325-
// rev_idents=('a'..='z').take(i).collect_vec().into_iter().rev().join(", ")
326-
// );
327-
// }
328-
// It could probably be replaced by a bit more macro cleverness.
329-
impl_tuple_collect!(a; a);
330-
impl_tuple_collect!(a, b; b, a);
331-
impl_tuple_collect!(a, b, c; c, b, a);
332-
impl_tuple_collect!(a, b, c, d; d, c, b, a);
333-
impl_tuple_collect!(a, b, c, d, e; e, d, c, b, a);
334-
impl_tuple_collect!(a, b, c, d, e, f; f, e, d, c, b, a);
335-
impl_tuple_collect!(a, b, c, d, e, f, g; g, f, e, d, c, b, a);
336-
impl_tuple_collect!(a, b, c, d, e, f, g, h; h, g, f, e, d, c, b, a);
337-
impl_tuple_collect!(a, b, c, d, e, f, g, h, i; i, h, g, f, e, d, c, b, a);
338-
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j; j, i, h, g, f, e, d, c, b, a);
339-
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j, k; k, j, i, h, g, f, e, d, c, b, a);
340-
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j, k, l; l, k, j, i, h, g, f, e, d, c, b, a);
326+
impl_tuple_collect!(a);
327+
impl_tuple_collect!(a, b);
328+
impl_tuple_collect!(a, b, c);
329+
impl_tuple_collect!(a, b, c, d);
330+
impl_tuple_collect!(a, b, c, d, e);
331+
impl_tuple_collect!(a, b, c, d, e, f);
332+
impl_tuple_collect!(a, b, c, d, e, f, g);
333+
impl_tuple_collect!(a, b, c, d, e, f, g, h);
334+
impl_tuple_collect!(a, b, c, d, e, f, g, h, i);
335+
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j);
336+
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j, k);
337+
impl_tuple_collect!(a, b, c, d, e, f, g, h, i, j, k, l);

0 commit comments

Comments
 (0)