Writing `&[u8]` to `Box<[MaybeUninit<u8>]>`

I have a boxed slice of possibly uninitialized bytes, and I need to write a slice of bytes to a position in that boxed slice. How do I do this?

I'm refactoring code that was previously like this:


But now the memory I need to write is a slice of bytes and isn't a Sized type so I can't use ptr::write_unaligned.

1 Like

Take a look at how write_unaligned is implemented:

If what you have is a slice of bytes then it's written using alignment 1 anyway, so you can just copy it into place.


A perfect, I didn't think of checking the implementation. Thanks!

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.