futures_util/stream/try_stream/
try_next.rs

1use crate::stream::TryStreamExt;
2use core::pin::Pin;
3use futures_core::future::{FusedFuture, Future};
4use futures_core::stream::{FusedStream, TryStream};
5use futures_core::task::{Context, Poll};
6
7/// Future for the [`try_next`](super::TryStreamExt::try_next) method.
8#[derive(Debug)]
9#[must_use = "futures do nothing unless you `.await` or poll them"]
10pub struct TryNext<'a, St: ?Sized> {
11    stream: &'a mut St,
12}
13
14impl<St: ?Sized + Unpin> Unpin for TryNext<'_, St> {}
15
16impl<'a, St: ?Sized + TryStream + Unpin> TryNext<'a, St> {
17    pub(super) fn new(stream: &'a mut St) -> Self {
18        Self { stream }
19    }
20}
21
22impl<St: ?Sized + TryStream + Unpin + FusedStream> FusedFuture for TryNext<'_, St> {
23    fn is_terminated(&self) -> bool {
24        self.stream.is_terminated()
25    }
26}
27
28impl<St: ?Sized + TryStream + Unpin> Future for TryNext<'_, St> {
29    type Output = Result<Option<St::Ok>, St::Error>;
30
31    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
32        self.stream.try_poll_next_unpin(cx)?.map(Ok)
33    }
34}