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))
}
}
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.
Most helpful comment
Thanks for the tip. I initially thought this would be problematic in the
Poll::Pendingcase, but sincestream.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 wrappoll!(stream.next()).In case a decision is made against the inclusion of
poll_next!(..), the documentation ofpoll!(...)could at least be updated to mention this pattern.