MaybeUninit and hex::decode_to_slice

Is it possible, using the current stable Rust, to decode a hex string onto a [u8; 32] without a dummy initialization? The hex crate has a hex::decode_to_slice() and I've been staring at MaybeUninit, but it looks like the useful candidates are nightly-only.

1 Like

You can inline (unstable) MaybeUninit array helper functions yourself. Most of them are just wrapper around transmute and pointer cast for clarity.

3 Likes

hex only accepts references to initialized slices. There is no way around initializing without getting UB.

You are saving three instructions with this optimization on x86_64. You will probably gain much more by spending time on a specialized hex decoding for your size.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.