heapless icon indicating copy to clipboard operation
heapless copied to clipboard

Add vec macros

Open YuhanLiin opened this issue 1 year ago • 4 comments

Add vec and vec_with_cap macros Resolves #344

YuhanLiin avatar Jan 10 '24 05:01 YuhanLiin

I think instead of vec_with_cap, an API like

vec![1, 2, 3]    // Vec::<_, 3>::from([1, 2, 3])
vec![1; 3]       // Vec::<_, 3>::from([1; 3])
vec![1, 2, 3; 6] // Vec::<_, 6>::from([1, 2, 3])
vec![1; 3; 6]    // Vec::<_, 6>::from([1; 3])

would work.

reitermarkus avatar Jan 16 '24 06:01 reitermarkus

That's ambiguous though in the case of vec![1; 3].

birkenfeld avatar Jan 16 '24 09:01 birkenfeld

Good point. In that case, maybe : as separator would work, e.g. vec![1: 3] vs vec![1; 3: 3]. This also slightly matches the general : syntax for specifying types.

Then again, I think

let vec: Vec<_, 6> = vec![1; 3];

should work when using Vec::from_array, right? So supporting capacity in the macro may not be necessary, to avoid deviating from how the macro works in std.

reitermarkus avatar Jan 21 '24 01:01 reitermarkus

If we don't go with the vec![1; 3: 3] version with explicit capacity, then we won't be able to use the macro inline in a lot of places, such as somefunction(vec![0; 3]). It'll need to be in a declaration statement with an explicit type like let v: Vec<_, 3> = vec![...], otherwise the compiler might not know the capacity, leading to ambiguity. I don't know if we care about that.

YuhanLiin avatar Jan 26 '24 04:01 YuhanLiin