-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AsyncWrite::poll_write_buf is not invoked when using write_buf on a object that needs dereferencing #2610
Comments
Thanks! To fix it, you should modify this part of the code. Do you want to open a PR with that too? |
Add the implementation of poll_{read,write}_buf in deref_async_{read, write} macros so that the underlying poll_{read,write}_buf functions are correctly called. Fixes: tokio-rs#2610
The compilation failure in #2611 explains why the buf version is not implemented: It is a generic function, so it cannot be called on a trait object. Sorry, this is not possible to fix due to how the Rust programming language works. |
Sad! I realized that I can have a workaround in my code by creating a minimal clone of #[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct WriteVec<'a, W, B> {
writer: &'a mut W,
buf: &'a mut B,
}
pub trait AsyncWriteVec {
fn poll_write_vec<B: Buf>(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut B,
) -> Poll<io::Result<usize>>;
fn write_vec <'a, B>(&'a mut self, src: &'a mut B) -> WriteVec<'a, Self, B>
where
Self: Sized,
B: Buf,
{
WriteVec { writer: self, buf: src }
}
}
impl<W, B> Future for WriteVec<'_, W, B>
where
W: AsyncWriteVec,
B: Buf,
{
type Output = io::Result<usize>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<usize>> {
// safety: no data is moved from self
unsafe {
let me = self.get_unchecked_mut();
Pin::new_unchecked(&mut *me.writer).poll_write_vec(cx, &mut me.buf)
}
}
} and impl AsyncWriteVec for MockWrite {
fn poll_write_vec<B: Buf>(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut B,
) -> Poll<io::Result<usize>> {
self.poll_write_buf(cx, buf)
}
} Hopefully the snippet above could help others if they encounter the same issue. |
BTW, this is a The correct projection is |
Filed #2612 as I confirmed that |
Version
tokio v0.2.21
Platform
Linux
Description
I noticed that poll_write is called instead of
poll_write_buf
when I callwrite_buf
on a Boxed object.I tried this code:
I expected to see this happen: (output of the code)
Instead, this happened:
The same issue applies to the
read_buf
inAsyncReadExt
as well.This issue may not impact the correctness of
write_buf
/read_buf
but it may impact its performance since it disables vectored IO.I believe I have a fix for it.
The text was updated successfully, but these errors were encountered: