Skip to main content

map_on

Macro map_on 

Source
macro_rules! map_on {
    ( ($type_name:tt), $mac:ident $(, $args:tt)* ) => { ... };
    ( ($first_type:tt, $($rest_type:tt),+), $mac:ident $(, $args:tt)* ) => { ... };
    ($tokens:tt, $($macro_arms:tt)+) => { ... };
    (@expand_tokens ($first:tt $(, $rest:tt)*)) => { ... };
    (@expand_tokens ($last:tt)) => { ... };
    (@expand_tokens ()) => { ... };
    ( ( $(($a:tt, $b:tt)),* $(,)? ), ($($params:tt)*) => $body:block ) => { ... };
    ( ($($types:tt),+), $mac:ident $(, $args:tt)* ) => { ... };
}
Expand description

A powerful macro to impl other macros for the given types.

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

use hexga_map_on::map_on;

trait Zero
{
    const ZERO : Self;
}

macro_rules! impl_zero {
    ($type_name:ty) => {
        impl Zero for $type_name
        {
            const ZERO : Self = 0 as Self;
        }
    };
}

map_on!
(
    (
        i8, i16, i32, i64, isize,
        u8, u16, u32, u64, usize,
        f32, f64
    ),
    impl_zero
);

// ^^ this call impl Zero for all the given type

assert_eq!(i32::ZERO  , 0);
assert_eq!(usize::ZERO, 0);
assert_eq!(f32::ZERO  , 0.);