Skip to content

Why is it not allowed to cast a &mut Pod or &mut [u8] to a &mut AnyBitPattern ? #98

Answered by Lokathor
agausmann asked this question in Q&A
Discussion options

You must be logged in to vote

reinterpreting a byte buffer into a mutably referenced struct that may contain padding

This specific pattern, of going from all initialized to a view where not all are initialized, has a slight hiccup. If it's an immutable view that's no problem, you're only looking, so it's okay to not look at some of the bytes.

The mutable case is basically as you guessed: If it's a mutable view than a write will actually deinitialize some bytes, and then when you "undo" the &mut HasPadding to have just &mut [u8] again, there's some uninit memory in your byte slice.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by agausmann
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants