Skip to content
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

Feature-Request: Add poll_next! macro, analogous to poll!. #2356

Closed
ghost opened this issue Feb 22, 2021 · 4 comments · Fixed by #2357
Closed

Feature-Request: Add poll_next! macro, analogous to poll!. #2356

ghost opened this issue Feb 22, 2021 · 4 comments · Fixed by #2357
Labels

Comments

@ghost
Copy link

ghost commented Feb 22, 2021

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))
    }
}
@ghost
Copy link
Author

ghost commented Feb 22, 2021

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

@taiki-e
Copy link
Member

taiki-e commented Feb 22, 2021

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

@taiki-e taiki-e added A-macro Area: macro related C-feature-request labels Feb 22, 2021
@ghost
Copy link
Author

ghost commented Feb 22, 2021

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.

@taiki-e
Copy link
Member

taiki-e commented Feb 22, 2021

I would be happy to accept adding documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant