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

Support for const variable in array init #2

Open
JoshMcguigan opened this issue Jan 16, 2019 · 5 comments
Open

Support for const variable in array init #2

JoshMcguigan opened this issue Jan 16, 2019 · 5 comments

Comments

@JoshMcguigan
Copy link
Owner

Currently, arr_macro does not support const variables in array initialization as shown below. This is because the macro needs to know the exact value of SIZE, but the compiler expands macros before doing name resolution.

const SIZE: usize = 3;
arr![None; SIZE];

See additional details at dtolnay/syn#101

@kaimast
Copy link

kaimast commented Jun 29, 2020

Is there a rust bug associated with this? All the issues linked seem to be closed or archived.

ColinFinck added a commit to ColinFinck/binread that referenced this issue Mar 27, 2021
This increases the minimum supported Rust version to 1.51.0.
I sadly had to introduce another unsafe statement, because:
* `Default` is only implemented for array lengths up to 32
  (rust-lang/rust#61415)
* arr_macro doesn't support const generics
  (JoshMcguigan/arr_macro#2)
* Direct initialization via [T; N] adds a trait bound
  BinRead: Copy
ColinFinck added a commit to ColinFinck/binread that referenced this issue Mar 27, 2021
This increases the minimum supported Rust version to 1.51.0.
I sadly had to introduce another unsafe statement, because:
* `Default` is only implemented for array lengths up to 32
  (rust-lang/rust#61415)
* arr_macro doesn't support const generics
  (JoshMcguigan/arr_macro#2)
* Direct initialization via [T; N] adds a trait bound
  `BinRead: Copy`
jam1garner pushed a commit to jam1garner/binread that referenced this issue Mar 28, 2021
This increases the minimum supported Rust version to 1.51.0.
I sadly had to introduce another unsafe statement, because:
* `Default` is only implemented for array lengths up to 32
  (rust-lang/rust#61415)
* arr_macro doesn't support const generics
  (JoshMcguigan/arr_macro#2)
* Direct initialization via [T; N] adds a trait bound
  `BinRead: Copy`
@daxpedda
Copy link

daxpedda commented Oct 5, 2021

Is there a rust bug associated with this?

rust-lang/rust#52393

@yegor256
Copy link

yegor256 commented Mar 1, 2022

@JoshMcguigan what is a possible workaround?

@DrRuhe
Copy link

DrRuhe commented Dec 7, 2022

I ran into this while using const generics to specify an array size and trying to fill that array with None without T being Copy or Default. I solved it by using this snippet:

let array: [Option<T>; N] = (0..N).map(|_| None).collect::<Vec<_>>().try_into().ok().unwrap();

The .ok() is needed to relax the Debug trait bound on T.

It is obviously not ideal but for my purposes a good enough workaround.

@PENGUINLIONG
Copy link

Is there a plan to support constant generic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants