#Recursive Declarative Macro Help

4 messages · Page 1 of 1 (latest)

rotund summit
#

So I have a set of macro I'm using to tell me the count of the number of types. I'm trying to emulate the query format in the legion ecs and am looking through their github trying to learn. I have it working for all sizes greater than 1. However, this won't work for single sizes. Any ideas why?


macro_rules! count {
    ($a: tt, $($b:tt)*)=>{
        {
            1+count!($($b)*)
        }
    };
    ($a: tt) => { 1 }; 
    () => { 0 }
}

macro_rules! tuple_print {
    ($head_ty:ident) => {
        implement_tuple_print!($head_ty);
    };
    ($head_ty:ident, $( $tail_ty:ident ),*) => (
        implement_tuple_print!($head_ty, $( $tail_ty ),*);
        tuple_print!($( $tail_ty ),*);
    );
}

macro_rules! implement_tuple_print {
    ($( $ty: ident),*)=> {
        impl<$( $ty ),*> PrintTuple for ( $( $ty, )* ) {
            fn print() {
                println!("Tuple: {}",count!($( $ty ),*));
            }
        }
    };
    () => {}
}


tuple_print!(A, B, C, D, E, F, G, H);

fn main() {
     //Prints 3
    <(u32,f32,u64)>::print();
    //Prints 2
    <(u32, f32)>::print();
    //Doesn't work ... however, does work in the legion ecs
    <(u32)>::print();
}
torn hinge
#
- <(u32)>::print();
+ <(u32,)>::print();
#

i.e., (T) is not a tuple but (T,) is