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
use derive_more::{Display, Error};
use std::{
    future::Future,
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

use crate::{
    mpsc::MPSC,
    pod::{Pod, Queue},
    MultiSender,
};

#[derive(Debug, Display, Error)]
pub struct SenderGone;

pub struct Mailbox<T> {
    pod: Arc<Pod<T>>,
}

impl<T: Queue> Mailbox<T> {
    pub(crate) fn new(pod: Arc<Pod<T>>) -> Self {
        Self { pod }
    }

    #[allow(clippy::should_implement_trait)]
    pub fn next(&mut self) -> MailboxFuture<'_, T> {
        MailboxFuture { mb: self }
    }
}

impl<Msg> Mailbox<MPSC<Msg>> {
    pub fn me(&self) -> MultiSender<Msg> {
        MultiSender {
            pod: self.pod.clone(),
        }
    }
}

pub struct MailboxFuture<'a, T> {
    mb: &'a mut Mailbox<T>,
}

impl<T: Queue> Future for MailboxFuture<'_, T> {
    type Output = Result<T::Msg, SenderGone>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let q = &self.mb.pod.queue;
        match q.poll(cx) {
            Poll::Ready(Some(t)) => Poll::Ready(Ok(t)),
            Poll::Ready(None) => Poll::Ready(Err(SenderGone)),
            Poll::Pending => Poll::Pending,
        }
    }
}