Skip to content

prefer static_map! over explicit unsafe in serde deserialisation #17

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Fuuzetsu
Copy link
Contributor

Instead of going over all the entries and checking if they are initialised and then going over them
again and unwrapping unsafely, we can just go over it once and report the error on first failure. The semantics should be exactly the same but we no
longer have an explicit unsafe in here: it's
moved behind the static_map! macro instead which can be considered more battle-hardened.

If we're accepting unsafe then it might make more
sense to do something like [MaybeUninit<T>; L::LENGTH] though something like the Builder is needed to make this ergonomic. Either way, I think the version in this commit at least hides the
magic.

PS: I checked if we can just replace
.unwrap_unchecked() with .unwrap() but it
seems that rustc is not able to notice that it
will never fail so it generates panic code anyway.

Instead of going over all the entries and checking
if they are initialised and then going over them
again and unwrapping unsafely, we can just go over
it once and report the error on first failure. The
semantics should be exactly the same but we no
longer have an explicit `unsafe` in here: it's
moved behind the `static_map!` macro instead which
can be considered more battle-hardened.

If we're accepting unsafe then it might make more
sense to do something like `[MaybeUninit<T>;
L::LENGTH]` though something like the `Builder` is
needed to make this ergonomic. Either way, I think
the version in this commit at least hides the
magic.

PS: I checked if we can just replace
`.unwrap_unchecked()` with `.unwrap()` but it
seems that rustc is not able to notice that it
will never fail so it generates panic code anyway.
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.

1 participant