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:

self.storage
    .as_mut_ptr()
    .add(self.cursor)
    .cast::<T>()
    .write_unaligned(component);

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.

2 Likes

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