Suppose I have some closure, A, with a mutable reference to some byte slice
&mut [u8]. In closure A, to obtain a pointer to the byte slice, I just coerce the mutable reference into a mutable pointer.
When reading data from UDP sockets, we must supply a mutable byte slice to be filled. Suppose the slice I supply has
k length (e.g., 10000). When a packet gets written to the byte slice, it may only fill up
j < k bytes (e.g., 1400). If I want to pass this data into another closure, B, I have to heap allocate the bytes and pass a pointer to closure B (e.g.,
BytesMut does this implicitly when i
clone_from_slice). However, I would like to save a clone. So, i have an idea that I'd like others to help me with.
Instead of supplying an
&mut [u8] directly, what if we pre-allocated a
BytesMut structure, and then passed a
&mut [u8] (which is a reference to some heap-allocated subset of the slice in the
BytesMut) to the udp socket. Then, after we read
j bytes, we advance the cursor,
take() the new bytes (to create a
Bytes object), then pass the
Bytes outside of closure A. The next step would be mutating "where" the
&mut [u8] points to. Could we do this by saving a
*mut &mut [u8], and dereferencing that and updating "where" the
&mut [u8] points to? Can we change "where" a reference points to, even though it was already passed into a closure outside of A? Also, suppose there is a single event loop, so we don't have to worry much about synchronization.
The purpose, in doing this, would be to reduce a single clone.