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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::pin::Pin;
use std::task::{Context, Poll};

use futures::{Future, Sink, SinkExt};
use futures::channel::oneshot;

use super::{Error, ErrorType, Executor};

pub struct Spawner<'a, Item> {
    sink: &'a Executor,
    item: Option<Item>,
}

impl<Item> Unpin for Spawner<'_, Item> {}

impl<'a, Item> Spawner<'a, Item> {
    #[inline]
    pub(crate) fn new(sink: &'a Executor, item: Item) -> Self {
        Self {
            sink,
            item: Some(item),
        }
    }

    // #[inline]
    // pub fn exec(mut self, exec: &'a Executor) -> Self {
    //     self.sink = exec;
    //     self
    // }

    #[inline]
    pub async fn result(mut self) -> Result<Item::Output, Error<Item>>
        where
            Item: Future + Send + 'static,
            Item::Output: Send + 'static,
    {
        let task = self.item.take().expect("polled Feed after completion");
        if self.sink.is_closed() {
            return Err(Error::SendError(ErrorType::Closed(Some(task))));
        }

        let (res_tx, res_rx) = oneshot::channel();
        let waiting_count = self.sink.waiting_count.clone();
        let task = async move {
            waiting_count.dec();
            let output = task.await;
            if let Err(_e) = res_tx.send(output) {
                log::warn!("send result failed");
            }
        };
        self.sink.waiting_count.inc();

        if let Err(e) = self.sink.tx.clone().send(Box::new(Box::pin(task))).await {
            self.sink.waiting_count.dec();
            return Err(Error::from(e));
        }
        res_rx.await.map_err(|_| Error::RecvResultError)
    }
}

impl<Item> Future for Spawner<'_, Item>
    where
        Item: Future + Send + 'static,
        Item::Output: Send + 'static,
{
    type Output = Result<(), Error<Item>>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        let task = this.item.take().expect("polled Feed after completion");
        if this.sink.is_closed() {
            return Poll::Ready(Err(Error::SendError(ErrorType::Closed(Some(task)))));
        }
        let mut tx = this.sink.tx.clone();
        let mut sink = Pin::new(&mut tx);
        futures::ready!(sink.as_mut().poll_ready(cx))?;
        let waiting_count = this.sink.waiting_count.clone();
        let task = async move {
            waiting_count.dec();
            let _ = task.await;
        };
        this.sink.waiting_count.inc();
        sink.as_mut()
            .start_send(Box::new(Box::pin(task)))
            .map_err(|e| {
                this.sink.waiting_count.dec();
                e
            })?;
        Poll::Ready(Ok(()))
    }
}