1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use core::task;
use std::{
    pin::Pin,
    task::{Context, Poll},
};

use futures_core::Stream;
use pin_project::pin_project;

use crate::LendingStream;

/// The iterator returned from `AsyncIterator::lend`.
#[derive(Debug)]
#[pin_project]
pub struct Lend<I: Stream>(#[pin] I);

impl<I: Stream> Lend<I> {
    pub(crate) fn new(i: I) -> Self {
        Self(i)
    }
}

impl<I: Stream + Unpin> LendingStream for Lend<I> {
    type Item<'a> = (&'a I, I::Item)
    where
        Self: 'a;

    fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item<'_>>> {
        let item = task::ready!(Pin::new(&mut self.0).poll_next(cx));
        Poll::Ready(item.map(move |item| (&self.0, item)))
    }
}