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 BinRead for all array lengths using const generics. #41

Merged
merged 5 commits into from
Mar 28, 2021

Conversation

ColinFinck
Copy link
Contributor

This increases the minimum supported Rust version to 1.51.0.
I sadly had to introduce another unsafe statement, because:

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`
}
}
)*
let mut arr: [B; N] = unsafe { core::mem::MaybeUninit::uninit().assume_init() };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only safe to .assume_init() immediately on a MaybeUninit<[T; N]> if T is itself MaybeUninit<U> so the above line should look something like:

let mut arr: [MabyeUninit<B>; N] = unsafe { core::mem::MaybeUninit::uninit().assume_init() };

then each item should be assigned MaybeUninit::new(some_val), and then the entire array should be transmuted to [B; N]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed that, restored the old implementation, and put my new code behind a feature gate "const_generics".
Previous Rust compatibility is preserved while Rust 1.51+ users can benefit from const generics :)

Feel free to merge this PR or squash all commits into one.

@jam1garner jam1garner merged commit 0c09364 into jam1garner:master Mar 28, 2021
jam1garner pushed a commit to jam1garner/binrw that referenced this pull request Mar 28, 2021
* Implement `BinRead` for all array lengths using const generics.
* Fix array initialization via `MaybeUninit`.
* Restore the old code and put the new one behind a feature gate.
* Use the array-init crate over manual unsafe initialization.

Ported from jam1garner/binread#41
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

Successfully merging this pull request may close these issues.

2 participants