Skip to content

What is let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) }; doing? #3121

Answered by Darksonn
Urhengulas asked this question in Q&A
Discussion options

You must be logged in to vote

The &mut UninitSlice type is a special kind of u8 slice that allows the memory behind it to be uninitialized, but doesn't allow you to de-initialize any memory behind the slice if that memory was already initialized. The unsafe block casts it to an &mut [MaybeUninit<u8>], which is a different kind of slice. The &mut [MaybeUninit<u8>] type also allows the memory behind it to be uninitialized, but allows you to de-initialize memory behind it.

The unsafe block is a promise that we don't de-initialize any memory in the slice that was already initialized. This is correct to do in this case because the ReadBuf type has the following guarantee on unfilled_mut, so the implementer of poll_read can…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@Urhengulas
Comment options

Answer selected by hawkw
Comment options

You must be logged in to vote
1 reply
@Urhengulas
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants