Combinatorial macro for match statement

Is it possible to somehow implement combinatorics for match using declarative macros?

let vec = match (p, q) {
    (1, 0) => {
        simulate::<1,0>()
    }
    (1, 1) => {
        simulate::<1,1>()
    }
    (1, 2) => {
        simulate::<1,2>()
    }

    ...

    (5, 4) => {
        simulate::<5,4>()
    }
    (5, 5) => {
        simulate::<5,5>()
    }
    _ => panic!("Wrong ARMA parameters"),
};

Here's a quick try: Rust Playground

macro_rules! pairs_2 {
    ($val:expr, $n:expr, [$($m:expr),* $(,)?], $default:expr) => {
        match $val {
            $(
                $m => simulate::<$n, $m>(),
            )*
            _ => $default,
        }
    }
}

macro_rules! pairs_1 {
    ($pair:expr, [$($n:expr),* $(,)?], $m:tt, $default:expr) => {
        match $pair.0 {
            $(
                $n => pairs_2!($pair.1, $n, $m, $default),
            )*
            _ => $default,
        }
    }
}

macro_rules! pairs {
    ($pair:expr, $nm:tt, $default:expr $(,)?) => {
        pairs_1!($pair, $nm, $nm, $default)
    };
}

pairs! {
    pair,
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    {
        println!("exiting");
        break;
    }
}
2 Likes