Skip to content

Commit 0d5f4a3

Browse files
committed
More macro cleverness (1) avoid explicitly passing number of arguments
1 parent af54286 commit 0d5f4a3

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/tuple_impl.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,16 @@ pub trait TupleCollect: Sized {
244244
fn left_shift_push(&mut self, item: Self::Item);
245245
}
246246

247+
macro_rules! count_ident{
248+
() => {0};
249+
($i0:ident, $($i:ident,)*) => {1 + count_ident!($($i,)*)};
250+
}
251+
247252
macro_rules! impl_tuple_collect {
248-
($N:expr; $A:ident ; $($X:ident),* ; $($Y:ident),* ; $($Y_rev:ident),*) => (
253+
($A:ident ; $($X:ident),* ; $($Y:ident),* ; $($Y_rev:ident),*) => (
249254
impl<$A> TupleCollect for ($($X),*,) {
250255
type Item = $A;
251-
type Buffer = [Option<$A>; $N - 1];
256+
type Buffer = [Option<$A>; count_ident!($($Y,)*) - 1];
252257

253258
#[allow(unused_assignments, unused_mut)]
254259
fn collect_from_iter<I>(iter: I, buf: &mut Self::Buffer) -> Option<Self>
@@ -291,7 +296,7 @@ macro_rules! impl_tuple_collect {
291296
}
292297

293298
fn num_items() -> usize {
294-
$N
299+
count_ident!($($Y,)*)
295300
}
296301

297302
fn left_shift_push(&mut self, item: $A) {
@@ -312,23 +317,22 @@ macro_rules! impl_tuple_collect {
312317
// use itertools::Itertools;
313318
//
314319
// for i in 1..=12 {
315-
// println!("impl_tuple_collect!({arity}; A; {ty}; {idents}; {rev_idents});",
316-
// arity=i,
320+
// println!("impl_tuple_collect!(A; {ty}; {idents}; {rev_idents});",
317321
// ty=iter::repeat("A").take(i).join(", "),
318322
// idents=('a'..='z').take(i).join(", "),
319323
// rev_idents=('a'..='z').take(i).collect_vec().into_iter().rev().join(", ")
320324
// );
321325
// }
322326
// It could probably be replaced by a bit more macro cleverness.
323-
impl_tuple_collect!(1; A; A; a; a);
324-
impl_tuple_collect!(2; A; A, A; a, b; b, a);
325-
impl_tuple_collect!(3; A; A, A, A; a, b, c; c, b, a);
326-
impl_tuple_collect!(4; A; A, A, A, A; a, b, c, d; d, c, b, a);
327-
impl_tuple_collect!(5; A; A, A, A, A, A; a, b, c, d, e; e, d, c, b, a);
328-
impl_tuple_collect!(6; A; A, A, A, A, A, A; a, b, c, d, e, f; f, e, d, c, b, a);
329-
impl_tuple_collect!(7; A; A, A, A, A, A, A, A; a, b, c, d, e, f, g; g, f, e, d, c, b, a);
330-
impl_tuple_collect!(8; A; A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h; h, g, f, e, d, c, b, a);
331-
impl_tuple_collect!(9; A; A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i; i, h, g, f, e, d, c, b, a);
332-
impl_tuple_collect!(10; A; A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j; j, i, h, g, f, e, d, c, b, a);
333-
impl_tuple_collect!(11; A; A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k; k, j, i, h, g, f, e, d, c, b, a);
334-
impl_tuple_collect!(12; A; A, A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k, l; l, k, j, i, h, g, f, e, d, c, b, a);
327+
impl_tuple_collect!(A; A; a; a);
328+
impl_tuple_collect!(A; A, A; a, b; b, a);
329+
impl_tuple_collect!(A; A, A, A; a, b, c; c, b, a);
330+
impl_tuple_collect!(A; A, A, A, A; a, b, c, d; d, c, b, a);
331+
impl_tuple_collect!(A; A, A, A, A, A; a, b, c, d, e; e, d, c, b, a);
332+
impl_tuple_collect!(A; A, A, A, A, A, A; a, b, c, d, e, f; f, e, d, c, b, a);
333+
impl_tuple_collect!(A; A, A, A, A, A, A, A; a, b, c, d, e, f, g; g, f, e, d, c, b, a);
334+
impl_tuple_collect!(A; A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h; h, g, f, e, d, c, b, a);
335+
impl_tuple_collect!(A; A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i; i, h, g, f, e, d, c, b, a);
336+
impl_tuple_collect!(A; A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j; j, i, h, g, f, e, d, c, b, a);
337+
impl_tuple_collect!(A; A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k; k, j, i, h, g, f, e, d, c, b, a);
338+
impl_tuple_collect!(A; A, A, A, A, A, A, A, A, A, A, A, A; a, b, c, d, e, f, g, h, i, j, k, l; l, k, j, i, h, g, f, e, d, c, b, a);

0 commit comments

Comments
 (0)