Created
September 3, 2019 05:48
-
-
Save michaelsproul/91724686606a02cf17436340303fa5f9 to your computer and use it in GitHub Desktop.
Callin' a generic function with different type arguments based on runtime values
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! type_dispatch { | |
($function:ident, | |
($($arg:expr),*), | |
$base_ty:ident, | |
<$($param_ty:ty),*>, | |
[ $value:expr => primitive_type ] $($rest:tt)*) => { | |
match $value { | |
"bool" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* bool>, $($rest)*), | |
"uint8" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u8>, $($rest)*), | |
"uint16" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u16>, $($rest)*), | |
"uint32" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u32>, $($rest)*), | |
"uint64" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u64>, $($rest)*), | |
"uint128" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U128>, $($rest)*), | |
"uint256" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U256>, $($rest)*), | |
_ => { println!("unsupported: {}", $value); Ok(()) }, | |
} | |
}; | |
($function:ident, | |
($($arg:expr),*), | |
$base_ty:ident, | |
<$($param_ty:ty),*>, | |
[ $value:expr => typenum ] $($rest:tt)*) => { | |
match $value { | |
// DO YOU LIKE NUMBERS? | |
"0" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U0>, $($rest)*), | |
"1" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U1>, $($rest)*), | |
"2" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U2>, $($rest)*), | |
"3" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U3>, $($rest)*), | |
"4" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U4>, $($rest)*), | |
"5" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U5>, $($rest)*), | |
"6" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U6>, $($rest)*), | |
"7" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U7>, $($rest)*), | |
"8" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U8>, $($rest)*), | |
"16" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U16>, $($rest)*), | |
"32" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U32>, $($rest)*), | |
"64" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U64>, $($rest)*), | |
"128" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U128>, $($rest)*), | |
"256" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U256>, $($rest)*), | |
"512" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U512>, $($rest)*), | |
"1024" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U1024>, $($rest)*), | |
"2048" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U2048>, $($rest)*), | |
"4096" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U4096>, $($rest)*), | |
"8192" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* U8192>, $($rest)*), | |
_ => { println!("unsupported: {}", $value); Ok(()) }, | |
} | |
}; | |
($function:ident, ($($arg:expr),*), $base_type_name:ident, <$($param_ty:ty),*>,) => { | |
$function::<$base_type_name<$($param_ty),*>>($($arg),*) | |
} | |
} | |
// Example usage | |
fn main() { | |
type_dispatch!( | |
ssz_generic_test, | |
(&self.path), | |
FixedVector, | |
<>, | |
[elem_ty => primitive_type] | |
[length => typenum] | |
)?; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment