From 5d5e9dfcb426f23397b5ce2044510b887a39ed69 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Thu, 26 Sep 2019 21:52:48 +0200 Subject: [PATCH] Add and use for_each_tuple! --- src/for_each_tuple.rs | 15 ++++++++ src/lib.rs | 3 ++ src/symbols/list.rs | 82 ++++++------------------------------------- 3 files changed, 28 insertions(+), 72 deletions(-) create mode 100644 src/for_each_tuple.rs diff --git a/src/for_each_tuple.rs b/src/for_each_tuple.rs new file mode 100644 index 0000000..25c5f1e --- /dev/null +++ b/src/for_each_tuple.rs @@ -0,0 +1,15 @@ +macro_rules! for_each_tuple_ { + ( $m:ident !! ) => ( + $m! { } + ); + ( $m:ident !! $h:ident, $($t:ident,)* ) => ( + $m! { $h $($t)* } + for_each_tuple_! { $m !! $($t,)* } + ); +} +#[macro_export] +macro_rules! for_each_tuple { + ( $m:ident ) => ( + for_each_tuple_! { $m !! A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, } + ); +} diff --git a/src/lib.rs b/src/lib.rs index b3186a7..9223116 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -21,6 +21,9 @@ missing_debug_implementations extern crate regex; extern crate users; +#[macro_use] +mod for_each_tuple; + pub mod bin; pub mod build; pub mod command_runner; diff --git a/src/symbols/list.rs b/src/symbols/list.rs index 19c6ae0..d9d809c 100644 --- a/src/symbols/list.rs +++ b/src/symbols/list.rs @@ -75,80 +75,18 @@ impl<'a> Symbol for List<'a> { } } -impl<'a, A: 'a + Symbol, B: 'a + Symbol> From<(A, B)> for List<'a> { - fn from((a, b): (A, B)) -> Self { - Self::new(vec![Box::new(a), Box::new(b)]) - } -} - -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol> From<(A, B, C)> for List<'a> { - fn from((a, b, c): (A, B, C)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol> From<(A, B, C, D)> for List<'a> { - fn from((a, b, c, d): (A, B, C, D)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol> From<(A, B, C, D, E)> for List<'a> { - fn from((a, b, c, d, e): (A, B, C, D, E)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol> From<(A, B, C, D, E, F)> for List<'a> { - fn from((a, b, c, d, e, f): (A, B, C, D, E, F)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol> From<(A, B, C, D, E, F, G)> for List<'a> { - fn from((a, b, c, d, e, f, g): (A, B, C, D, E, F, G)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol> From<(A, B, C, D, E, F, G, H)> for List<'a> { - fn from((a, b, c, d, e, f, g, h): (A, B, C, D, E, F, G, H)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K)> for List<'a> { - fn from((a, b, c, d, e, f, g, h, i, j, k): (A, B, C, D, E, F, G, H, I, J, K)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L)> for List<'a> { - fn from((a, b, c, d, e, f, g, h, i, j, k, l): (A, B, C, D, E, F, G, H, I, J, K, L)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l)]) - } -} - -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol, M: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L, M)> for List<'a> { - fn from((a, b, c, d, e, f, g, h, i, j, k, l, m): (A, B, C, D, E, F, G, H, I, J, K, L, M)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l), Box::new(m)]) - } +macro_rules! from_tuple { + ( $($name:ident)* ) => ( + #[allow(non_snake_case)] + impl<'a, $($name: 'a + Symbol,)*> From<($($name,)*)> for List<'a> { + fn from(($($name,)*): ($($name,)*)) -> Self { + Self::new(vec![$(Box::new($name),)*]) + } + } + ); } -#[rustfmt::skip] -impl<'a, A: 'a + Symbol, B: 'a + Symbol, C: 'a + Symbol, D: 'a + Symbol, E: 'a + Symbol, F: 'a + Symbol, G: 'a + Symbol, H: 'a + Symbol, I: 'a + Symbol, J: 'a + Symbol, K: 'a + Symbol, L: 'a + Symbol, M: 'a + Symbol, N: 'a + Symbol> From<(A, B, C, D, E, F, G, H, I, J, K, L, M, N)> for List<'a> { - fn from((a, b, c, d, e, f, g, h, i, j, k, l, m, n): (A, B, C, D, E, F, G, H, I, J, K, L, M, N)) -> Self { - Self::new(vec![Box::new(a), Box::new(b), Box::new(c), Box::new(d), Box::new(e), Box::new(f), Box::new(g), Box::new(h), Box::new(i), Box::new(j), Box::new(k), Box::new(l), Box::new(m), Box::new(n)]) - } -} +for_each_tuple!(from_tuple); struct SymbolListAction<'a> { runner: &'a dyn SymbolRunner,