How to match array type from `ty` fragment in macro?

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.

ty, like most fragments, becomes opaque when forwarded. If you can't find a way to make it work with tt, you still may be able to make progress with a dose of repetition.

Yeah that's what I was afraid of. Thanks for confirming!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.