Lokathor / tinyvec

Just, really the littlest Vec you could need. So smol.
https://docs.rs/tinyvec
Apache License 2.0
648 stars 49 forks source link

array_vec! macro cannot work with tuple element #164

Open YoshieraHuang opened 2 years ago

YoshieraHuang commented 2 years ago

This expression tries to construct an ArrayVec with element type (f32, f32) but cannot compile:

let a: ArrayVec<[(f32,f32); 3]> = array_vec!((2.0, 2.0), (2.0, 2.0), (2.0, 2.0));

The error goes like:

expected type, found `2.0`
expected type

Meanwhile, the vec! macro can handle this well.

Lokathor commented 2 years ago

The problem here is that array_vec! has several ways it can be invoked.

macro_rules! array_vec {
    ($array_type:ty => $($elem:expr),* $(,)?) => { ... };
    ($array_type:ty) => { ... };
    ($($elem:expr),*) => { ... };
    ($elem:expr; $n:expr) => { ... };
    () => { ... };
}

Unfortunately, in this situation it seems to be doing a partial match on the 2nd case, ($array_type:ty) => { ... };, which then fails because that's not actually what you wanted.

I don't think there's an easy fix here, because changing the ordering of the match arms can break other people's code instead.

I'll link this issue in the discord and perhaps a macro expert can swoop in and solve the problem.

In the mean time, array_vec!([(f32,f32); 3] => (2.0, 2.0), (2.0, 2.0), (2.0, 2.0)) is more verbose but does work how you want (playground example).