What is let dst = unsafe { &mut *(dst as *mut _ as *mut [MaybeUninit<u8>]) };
doing?
#3121
-
I came across following code: tokio/tokio/src/io/util/read_buf.rs Lines 53 to 57 in d786553 Can someone please briefly explain what this is doing? I feel especially threatened by line 54... Thought: If it helps, I think the type of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
The 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
|
Beta Was this translation helpful? Give feedback.
-
You may find the documentation for Essentially, |
Beta Was this translation helpful? Give feedback.
The
&mut UninitSlice
type is a special kind ofu8
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 onunfilled_mut
, so the implementer ofpoll_read
can…