I have a long-lived BytesMut buffer that I use for encoding messages before sending them over a socket. The flow is something like:
let mut buf = BytesMut::with_capacity(…);
let mut socket: impl Sink;
loop {
{ // this step may happen 0.. times during any given iteration
let msg: CustomMessageType = todo!();
encode(&msg, &mut buf);
socket.feed(buf.split()).await?;
}
socket.flush().await?;
}
Can I assume that the BytesMut will re-use the same allocation over and over again? As far as I understand, as long as all feed calls combined don't exceed the original capacity, it should be able to reclaim the old space?