-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Add read/write implems for generic arrays #235
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
/fX
array/slices, so I removed those.There's a bit of
unsafe
code to avoid doing unnecessary allocations.