-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
Is your feature request related to a problem? Please describe.
It would be nice if there was a better way to advance the "parent" buffer after using .take(). Consider the current implementation of AsyncRead
for Take
: https://docs.rs/tokio/1.46.1/src/tokio/io/util/take.rs.html#79
let n = b.filled().len();
// We need to update the original ReadBuf
unsafe {
buf.assume_init(n);
}
buf.advance(n);
Describe the solution you'd like
One idea would be some sort of API to close the "take" operation by "putting" the sub-buffer back:
buf.put(b); // or maybe put_take() or something?
That could consume b
(which would give the API a really nice "bookends" feel), but it wouldn't need to, necessarily: a reference would be fine. The main thing it would do is to ensure that the b
buffer is actually a result of calling buf.take()
(ie: the referred memory regions are a sub-slice) and use the initialized/filled regions of b
to update buf
.
Describe alternatives you've considered
I really dislike unsafe code, so right now I'm just calling buf.initialize_unfilled()
at the start and then later I can buf.advance(n)
, which is safe. This is suboptimal because of the unnecessary initialization.
Another possibility would be to update the .put_slice()
API to notice if the slice being "put" is a subset of the existing buffer and behave accordingly. It feels a bit magic, though, so I think a dedicated API would be better...