| 1 | use futures_core::Stream; |
| 2 | use std::future::Future; |
| 3 | use std::pin::Pin; |
| 4 | use std::task::{Context, Poll}; |
| 5 | |
| 6 | // This is equivalent to the `futures::StreamExt::next` method. |
| 7 | // But we want to make this crate dependency as small as possible, so we define our `next` function. |
| 8 | #[doc (hidden)] |
| 9 | pub fn next<S>(stream: &mut S) -> impl Future<Output = Option<S::Item>> + '_ |
| 10 | where |
| 11 | S: Stream + Unpin, |
| 12 | { |
| 13 | Next { stream } |
| 14 | } |
| 15 | |
| 16 | #[derive(Debug)] |
| 17 | struct Next<'a, S> { |
| 18 | stream: &'a mut S, |
| 19 | } |
| 20 | |
| 21 | impl<S> Unpin for Next<'_, S> where S: Unpin {} |
| 22 | |
| 23 | impl<S> Future for Next<'_, S> |
| 24 | where |
| 25 | S: Stream + Unpin, |
| 26 | { |
| 27 | type Output = Option<S::Item>; |
| 28 | |
| 29 | fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 30 | Pin::new(&mut self.stream).poll_next(cx) |
| 31 | } |
| 32 | } |
| 33 | |