-
Notifications
You must be signed in to change notification settings - Fork 18
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
Implement BinRead
for all array lengths using const generics.
#41
Conversation
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`
binread/src/binread_impls.rs
Outdated
} | ||
} | ||
)* | ||
let mut arr: [B; N] = unsafe { core::mem::MaybeUninit::uninit().assume_init() }; |
There was a problem hiding this comment.
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]
There was a problem hiding this comment.
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.
* 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
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 (Use const generics for arrayDefault
impl rust-lang/rust#61415)const
variable in array init JoshMcguigan/arr_macro#2)BinRead: Copy