I'm new to async Rust and for the last few days I'm digging into internals of different async frameworks. I'm puzzled why AsyncRead
(and similar traits) has no async methods but rely on poll functions like poll_read
? It also seems like frameworks like Tokio don't use Future
trait that much. Are there any reasons beyond historical behind that?
Rust's async it not centered around Future
trait, but around Waker
(in Context
) and Poll
. For AsyncRead
, you can poll with different buf: &mut ReadBuf<'_>
every time. And it will not borrow the buffer until it is done reading, causing a number of problems with borrow checker, as async fn read(buf: &mut ReadBuf<'_>)
would have (I can elaborate why if you wish).
So the answer is flexibility and api composability.
1 Like
Thank you. Indeed the borrowing argument is the most clear for me.
Have a look at this essay on AsyncIterator
.
1 Like