I almost get trapped in an error

I need something like

fn foo() -> std::task::Poll<Result<(),std::io::Error>>{
...
}

fn bar() -> Result<u32,std::io::Error> {
    foo()?;

   /* do something else */
}

I quickly wrote something like the above, and then get suppised by “what? a Poll is Try?” in review.

Then I ask my self: what does it do when foo is returning Pending? Is it returning std::io::Error with ErrorKind::WouldBlock? A few moments I assemed that but finally I get back to the document and figured out the result will be a Pending get ignored…

So it has to be

fn bar() -> Result<u32, std::io::Error> {
    if let Poll::Pending = foo() {
        Err(std::io::Error::from(std::io::ErrorKind::WouldBlock))
    }
    /* do something else */
}

Thinking how the Rust team will see from this kind of potential error…

At least, as a Result value will warn you if you didn’t use it, the Poll value should be the same, so I should be given a warning, right?

I think the issue here is just a missing #[must_use] on Poll, e.g. a quick test with it gives a decent error.

1 Like

In addition to must_use, should note that the futures crate provides a ready! macro; ready!(foo()?) does what you thought foo()? would.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.