I'm trying to write code like the following:
macro_rules! foo {
($ty:ty) => {
// Do other stuff
foo!(@inner $ty);
};
(@inner [$ty:ty]) => { println!("slice!") }; // Do slice stuff
(@inner [$ty:ty; $n:expr]) => { println!("array!") }; // Do array stuff
(@inner $ty:ty) => {}; // Do nothing
}
fn main() {
// Don't need to support passing `core::cell::Cell<u8>` to the macro because I can just
// `use` it first and then pass `Cell<u8>` if need be.
use core::cell::Cell;
foo!(Cell<u8>);
foo!([u8]); // Should print "slice", but doesn't.
foo!([u8; 2]); // Should print "array", but doesn't.
}
The idea is that, as part of this macro, I need to be able to distinguish between slice types, array types, and types that are neither of these. I assume that the ty
fragment type is opaque from a syntax perspective, which is why this isn't working. Is there any way I can restructure things so that it does work? I do need to support arbitrary types, so I can't just change the $ty:ty
in the first arm to $ty:tt
or something like that.