Question mark operator converts Result<...> to Poll>

⚓ Rust    📅 2026-02-27    👤 surdeus    👁️ 1      

surdeus

Hello there, I came across this implementation in tokio_util. And I'm wondering why the question mark (?) here works.

fn poll_write(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, io::Error>> {
        let mut this = self.project();

        ready!(this.inner.as_mut().poll_ready(cx).map_err(Into::into))?;
        match this.inner.as_mut().start_send(buf) {
            Ok(()) => Poll::Ready(Ok(buf.len())),
            Err(e) => Poll::Ready(Err(e.into())),
        }
    }

From my understanding, ready! macro extracts the Result inside of the Poll, so applying ? would result in returning a Result on error. But the function expects a Poll as return type. Shouldn't this line be written as:

ready!(this.inner.as_mut().poll_ready(cx).map_err(Into::into)?);

Both versions compile, but can you please explain why the original version works?

Thank you in advance.

3 posts - 2 participants

Read full topic

🏷️ Rust_feed