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
use std::{any::Any, fmt::Debug, marker::PhantomData};

use crate::{Rx, Tx};
use anyhow::{bail, Result};

pub type Msg = Box<dyn Any + Send + 'static>;

pub trait Transport: Clone {
    type Rx;
    type Tx;

    /// Create a pair of channels, the first A->B, the second B->A
    fn make_channel(&self) -> (Self::Tx, Self::Rx);
    fn recv(&self, rx: &mut Self::Rx) -> Result<Msg>;
    fn send(&self, tx: &mut Self::Tx, value: Msg) -> Result<()>;
    fn choice(&self, rx1: &mut Self::Rx, rx2: &mut Self::Rx) -> Result<Either>;
}

pub enum Either {
    Left(Msg),
    Right(Msg),
    Both(Msg),
}

pub struct Channel<To, Tr: Transport> {
    pub(crate) tx: Tr::Tx,
    pub(crate) rx: Tr::Rx,
    pub(crate) tr: Tr,
    _ph: PhantomData<To>,
}

impl<To, Tr> Clone for Channel<To, Tr>
where
    Tr: Transport,
    Tr::Rx: Clone,
    Tr::Tx: Clone,
{
    fn clone(&self) -> Self {
        Self {
            tx: self.tx.clone(),
            rx: self.rx.clone(),
            tr: self.tr.clone(),
            _ph: PhantomData,
        }
    }
}

impl<To, Tr: Transport> Channel<To, Tr> {
    pub(crate) fn new(tr: Tr, tx: Tr::Tx, rx: Tr::Rx) -> Self {
        Self {
            tx,
            rx,
            tr,
            _ph: PhantomData,
        }
    }

    pub fn recv<T: Debug + 'static, Cont>(
        &mut self,
        protocol: Rx<To, T, Cont>,
    ) -> Result<(T, Cont)> {
        let value = self.tr.recv(&mut self.rx)?;
        let value = match value.downcast::<T>() {
            Ok(v) => v,
            Err(v) => bail!("got unexpected message {:?}", v),
        };
        Ok((*value, protocol.cont))
    }

    pub fn send<T: Send + 'static, Cont>(
        &mut self,
        protocol: Tx<To, T, Cont>,
        value: T,
    ) -> Result<Cont> {
        self.tr.send(&mut self.tx, Box::new(value))?;
        Ok(protocol.cont)
    }
}