Futures-rs: Feature-Request: Add poll_next! macro, analogous to poll!.

Created on 22 Feb 2021  路  4Comments  路  Source: rust-lang/futures-rs

For futures there is a convenient poll! macro that allows polling a future once inside of an async context. I couldn't find an equivalent for the poll_next method of streams. This would be really useful in my opinion.

This could be done somewhat like this:

use futures::{Stream, StreamExt};
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

fn poll_next<StreamType>(stream: StreamType) -> PollNextOnce<StreamType> {
    PollNextOnce {
        stream,
    }
}

struct PollNextOnce<StreamType> {
    stream: StreamType,
}

impl<StreamType> Future for PollNextOnce<StreamType>
where
    StreamType: Stream + Unpin,
{
    type Output = Poll<Option<StreamType::Item>>;

    fn poll(self: Pin<&mut Self>, context: &mut Context) -> Poll<Self::Output> {
        Poll::Ready(self.get_mut().stream.poll_next_unpin(context))
    }
}
A-macro C-feature-request docs

Most helpful comment

I guess you can use poll!(stream.next()).

Thanks for the tip. I initially thought this would be problematic in the Poll::Pending case, but since stream.next() only takes a mutable reference, this problem doesn't exist.

This would make the implementation of a poll_next! macro much easier as it would only need to wrap poll!(stream.next()).

In case a decision is made against the inclusion of poll_next!(..), the documentation of poll!(...) could at least be updated to mention this pattern.

All 4 comments

I would be willing to make a pull request if this feature is considered to be added.

I guess you can use poll!(stream.next()).

I guess you can use poll!(stream.next()).

Thanks for the tip. I initially thought this would be problematic in the Poll::Pending case, but since stream.next() only takes a mutable reference, this problem doesn't exist.

This would make the implementation of a poll_next! macro much easier as it would only need to wrap poll!(stream.next()).

In case a decision is made against the inclusion of poll_next!(..), the documentation of poll!(...) could at least be updated to mention this pattern.

I would be happy to accept adding documentation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

olegnn picture olegnn  路  5Comments

jedisct1 picture jedisct1  路  3Comments

seanmonstar picture seanmonstar  路  4Comments

jannickj picture jannickj  路  3Comments

jimblandy picture jimblandy  路  5Comments