Skip to main content

map_on_tuple

Macro map_on_tuple 

Source
macro_rules! map_on_tuple {
    ($macro_name:ident) => { ... };
    ( $($macro_arms:tt)+ ) => { ... };
}
Expand description

A powerful macro to impl other macros for tuple, up to 16 element (may change).

Can be used to impl trait to a lot of tuple using macro, where generic can’t.

§Examples

Using an existing macro:

use hexga_map_on::map_on_tuple;

trait Foo { fn foo(); }

impl Foo for i32 { fn foo() { println!("foo from i32"); } }
impl Foo for bool { fn foo() { println!("foo from bool"); } }

macro_rules! impl_foo {
    ( $( $len:literal => ( $( $idx:tt $typ:ident )+ ) )* ) => {
        $(
            #[cfg_attr(docsrs, doc(fake_variadic))]
            impl<$( $typ: Foo ),+> Foo for ( $( $typ ),+ ,) {
                fn foo() { println!("Foo from tuple size {}", $len); }
            }
        )*
    };
}

map_on_tuple!(impl_foo);

Using inline definition:

use hexga_map_on::map_on_tuple;

trait Foo { fn foo(); }

impl Foo for i32 { fn foo() { println!("foo from i32"); } }
impl Foo for bool { fn foo() { println!("foo from bool"); } }

map_on_tuple!(
    ( $( $len:literal => ( $( $idx:tt $typ:ident )+ ) )* ) => {
        $(
            #[cfg_attr(docsrs, doc(fake_variadic))]
            impl<$( $typ: Foo ),+> Foo for ( $( $typ ),+ ,) {
                fn foo() { println!("Foo from tuple size {}", $len); }
            }
        )*
    };
);