I'm trying to write a rust (meta-)function that maps some input type to some unrelated output type.
Coming from a C++ background, I would usually write it like this:
template<typename T>
struct f;
template<>
struct f<int> { using type = double; };
using input_type = int;
using output_type = f<input_type>::type;
My naive attempt to write the same in rust looks like this:
macro_rules! f {
($in: ty) => {
match $in {
i32 => f32,
}
}
}
type OutputType = f!(i32);
but, well, this fails to compile because the macro apparently doesn't return a type.
$ rustc typedef.rs
error: expected type, found keyword `match`
--> typedef.rs:3:5
|
3 | match $in {
| ^^^^^ expected type
...
9 | type OutputType = f!(i32);
| -------
| |
| this macro call doesn't expand to a type
| in this macro invocation
|
What's the idiomatic rust way to map one type to another?