should use poll to implement the async function, but cannot use the async function to implement poll
Each call to async actually regenerates a temporary future. If the future is pending, it does not save the pending state, but simply drop it. This is incorrect behavior
example:
async fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let len = self.socket.write(buf).await?; // ready here
self.socket.flush().await?; // pending here, but this buf is already write to inner socket, it will cause the caller to think that the send was not successful, and try to send again
ok(len)
}
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<io::Result<usize>> {
let task = pin_mut!(self.write(buf));
task.poll(cx)
}
right way:
don't use async, manage state by yourself
should use poll to implement the async function, but cannot use the async function to implement poll
Each call to async actually regenerates a temporary future. If the future is pending, it does not save the pending state, but simply drop it. This is incorrect behavior
example:
right way: don't use async, manage state by yourself