Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement array! and array_from! #1

Open
peterjoel opened this issue Dec 9, 2020 · 0 comments
Open

Implement array! and array_from! #1

peterjoel opened this issue Dec 9, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@peterjoel
Copy link
Owner

peterjoel commented Dec 9, 2020

Array initialiizer macros.

Usage / test cases:

  • array![0,1,2,3] => [0,1,2,3]
  • array![..(0..=3)] => compile error - size must be given when using spread operator
  • array_from![0,1,2,3] => [0.into(), 1.into(), 2.into(), 3.into()] (and the same treatment for all examples below)
  • array![1; 4] => [1, 1, 1, 1] (not [1; 4] because we don't want to require Copy)
  • array![1; non_constant_var] => compile error - length must be constant
  • array![0, 1; 4] => Need to decide on one of:
    • panic - not enough elements
    • compile error - not enough elements (I'm currently favouring this option)
    • [0,1,0,1] (repeat the sequence)
    • [0,1,1,1] (repeat final element)
  • array![..(0..100); 4] => [0,1,2,3] (no error on too many elements when the last arg is a spread)
  • array![0, 1, 2, 3, ..(0..100); 4] => panic - too many elements (the array was already "full" before the final spread)
  • array![0, 1, ..(2..5), ..(0..100); 4] => panic - too many elements (the array was already "full" before the final spread)
  • array![0, 1, ..(2..); 4] => [0,1,2,3]
  • array![..(0..2); 4] => panic - not enough elements
  • array![0, ..(1..2); 4] => panic - not enough elements

Implementation:

  • This will require a little refactoring of VecInput so that its internals can be re-used.
  • It should be ok for the macro to accept any expression for the length. If the expression is not constant, the generated code should produce a suitable error without extra effort.
  • special cases for:
    • array![0,1,2,3] (no length specified)
    • array![1; 4] (repeat given element)
  • the other variants can write to a [MaybeUninit<T>; n] from a sequence of iterators.
  • If the length is larger than the number of provided elements, it often will not be possible to detect until runtime. It should:
    • panic, with a suitable error message
    • before panic, clean up the partially initialized array to avoid memory leaks.
@peterjoel peterjoel added enhancement New feature or request help wanted Extra attention is needed labels Dec 9, 2020
@peterjoel peterjoel changed the title Add array initializer macros Implement array! and array_from! Dec 9, 2020
@peterjoel peterjoel removed the help wanted Extra attention is needed label Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant