heapless
heapless copied to clipboard
Add vec macros
Add vec and vec_with_cap macros
Resolves #344
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.
That's ambiguous though in the case of vec![1; 3].
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.
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.