1 use crate::stream_ext::Next;
2 use crate::Stream;
3 
4 use core::future::Future;
5 use core::marker::PhantomPinned;
6 use core::pin::Pin;
7 use core::task::{Context, Poll};
8 use pin_project_lite::pin_project;
9 
10 pin_project! {
11     /// Future for the [`try_next`](super::StreamExt::try_next) method.
12     ///
13     /// # Cancel safety
14     ///
15     /// This method is cancel safe. It only
16     /// holds onto a reference to the underlying stream,
17     /// so dropping it will never lose a value.
18     #[derive(Debug)]
19     #[must_use = "futures do nothing unless you `.await` or poll them"]
20     pub struct TryNext<'a, St: ?Sized> {
21         #[pin]
22         inner: Next<'a, St>,
23         // Make this future `!Unpin` for compatibility with async trait methods.
24         #[pin]
25         _pin: PhantomPinned,
26     }
27 }
28 
29 impl<'a, St: ?Sized> TryNext<'a, St> {
new(stream: &'a mut St) -> Self30     pub(super) fn new(stream: &'a mut St) -> Self {
31         Self {
32             inner: Next::new(stream),
33             _pin: PhantomPinned,
34         }
35     }
36 }
37 
38 impl<T, E, St: ?Sized + Stream<Item = Result<T, E>> + Unpin> Future for TryNext<'_, St> {
39     type Output = Result<Option<T>, E>;
40 
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>41     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
42         let me = self.project();
43         me.inner.poll(cx).map(Option::transpose)
44     }
45 }
46