Add read/write implems for generic arrays#235
Add read/write implems for generic arrays#235sharksforarms merged 1 commit intosharksforarms:masterfrom xlambein:master
Conversation
| #[allow(clippy::uninit_assumed_init)] | ||
| // This is safe because we initialize the array immediately after, | ||
| // and never return it in case of error | ||
| let mut slice: [T; N] = unsafe { MaybeUninit::uninit().assume_init() }; |
There was a problem hiding this comment.
The reason for this is because of potential performance impacts of default'ing here? let mut slice: [$typ; N] = [<$typ>::default(); N];
There was a problem hiding this comment.
Using default() requires that T implements Default, which imo is an unnecessary restriction. To me the safe alternative would be to do:
let mut v = Vec::with_capacity(N);
let mut rest = input;
for i = 0..N {
let (new_rest, value) = T::read(rest, ctx)?;
v.push(value);
rest = new_rest;
}
Ok((rest, v.try_into().unwrap()))That makes a few unnecessary allocations and checks, which are avoided with the unsafe section. It doesn't seem very critical, so if you'd rather do that I think it's fine! :-)
|
Hey @xlambein sorry this took so long, this PR looks good to me. |
|
Released in 0.12.4, thanks @xlambein and sorry for the delay |
|
Oh, I missed the previous comment. Thanks a lot for merging! And no worries about the delay, thanks for maintaining this project! |
|
i.r. to the use of unsafe, we could utilize this: rust-lang/rust#75644 once it's released! |
|
Tracking issue: rust-lang/rust#89379 |
Hey! Thanks for the great project :-)
I needed to (de)serialize arrays of structs that implement
DekuRead/Write, so I went ahead and added those trait implementations. They also supersede the implementations for all theuX/iX/fXarray/slices, so I removed those.There's a bit of
unsafecode to avoid doing unnecessary allocations.