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

Implement `[value; N]` syntax support for `array_vec!` #118

Closed slightlyoutofphase closed 4 years ago

slightlyoutofphase commented 4 years ago

Closes #117. I also improved one of the other variants of the macro a little bit while I was at it.

Nemo157 commented 4 years ago

Implementation looks fine, but it seems weird to support this syntax for array_vec! and not tiny_vec!.

slightlyoutofphase commented 4 years ago

Implementation looks fine, but it seems weird to support this syntax for array_vec! and not tiny_vec!.

I can add that as well, if you'd like.

slightlyoutofphase commented 4 years ago

Ok, added it for tiny_vec! as well, and added a test.

Nemo157 commented 4 years ago
// Given that this works:
let vec: TinyVec<[f32; 4]> = tiny_vec![1.1, 1.1, 1.1];

// Should this work:
let vec: TinyVec<[f32; 4]> = tiny_vec![1.1; 3];
slightlyoutofphase commented 4 years ago

For both ArrayVec and TinyVec?

Lokathor commented 4 years ago

hmm, that's a very good question.

Nemo157 commented 4 years ago

The first form works for both, so if it's possible (/easy enough) it seems good to have the second form for both.

Lokathor commented 4 years ago

@slightlyoutofphase Hi, looks like we all forgot about this a bit.

did you want to add that one final macro variant to this PR? Or I can just merge it now.

Lokathor commented 4 years ago

oh, forgot to say at the time. near as i can tell it looks like the requested style was already implemented by the time it was asked.

Nemo157 commented 4 years ago
8 |     let vec: TinyVec<[f32; 4]> = tiny_vec![1.1; 3];
  |              -----------------   ^^^^^^^^^^^^^^^^^ expected an array with a fixed size of 4 elements, found one with 3 elements
  |              |
  |              expected due to this

(I also just realised that the inverse could be supported by tiny_vec! (and not array_vec!): let vec: TinyVec<[f32; 4]> = tiny_vec![1.1; 5];, going directly to an allocation).

slightlyoutofphase commented 3 years ago

Hey guys, sorry I didn't get back to you. Basically, after playing around with a bunch of things, I found that it isn't quite possible to make the code example given by @Nemo157, that is, this:

// Given that this works:
let vec: TinyVec<[f32; 4]> = tiny_vec![1.1, 1.1, 1.1];

// Should this work:
let vec: TinyVec<[f32; 4]> = tiny_vec![1.1; 3];

work at the moment. The compiler isn't quite smart enough currently to see through the ambiguity such that both versions can exist and work in their appropriate contexts at the same time.

If it gets to the point where I think this can be done though, I'll for sure come back and add it for you if you want.