Skip to content

Why exactly can't you use this crate with types with padding bytes? #86

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

You must be logged in to vote

Since a type's size is always a multiple of its alignment, then if the value is aligned to 16 but the fields are only a total of 12 bytes (say a vec3), then there will be 4 bytes of padding inserted to bring the type's size to a multiple of 16 (in this case 16*1). If you transmute that value into a byte array ([u8; 16]) array, then the first 12 bytes would be initialized and the last 4 bytes would be unitialized. In Rust you aren't allowed to convert uninitialized memory into normal data types (you have to use MaybeUninit).

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@laptou
Comment options

@Lokathor
Comment options

Answer selected by laptou
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