tokio_stream/stream_ext/
try_next.rs1use crate::stream_ext::Next;
2use crate::Stream;
3
4use core::future::Future;
5use core::marker::PhantomPinned;
6use core::pin::Pin;
7use core::task::{Context, Poll};
8use pin_project_lite::pin_project;
9
10pin_project! {
11 #[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 #[pin]
25 _pin: PhantomPinned,
26 }
27}
28
29impl<'a, St: ?Sized> TryNext<'a, St> {
30 pub(super) fn new(stream: &'a mut St) -> Self {
31 Self {
32 inner: Next::new(stream),
33 _pin: PhantomPinned,
34 }
35 }
36}
37
38impl<T, E, St: ?Sized + Stream<Item = Result<T, E>> + Unpin> Future for TryNext<'_, St> {
39 type Output = Result<Option<T>, E>;
40
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}