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
use super::shared_to_linear::SharedToLinear;
use crate::internal::base::*;

pub trait HasSharedRecApp<F, A>: Send + 'static
{
  fn get_applied(self: Box<Self>) -> <F::Applied as Protocol>::ClientEndpoint
  where
    F: SharedRecApp<A>,
    F::Applied: Protocol;
}

impl<F, A, FA, E> HasSharedRecApp<F, A> for E
where
  F: 'static,
  A: 'static,
  E: Send + 'static,
  FA: Protocol<ClientEndpoint = E>,
  F: SharedRecApp<A, Applied = FA>,
{
  fn get_applied(self: Box<Self>) -> <F::Applied as Protocol>::ClientEndpoint
  {
    *self
  }
}

pub struct LinearToShared<F>
{
  pub(crate) linear:
    Box<dyn HasSharedRecApp<F, SharedToLinear<LinearToShared<F>>>>,
}

impl<F> SealedSharedProtocol for LinearToShared<F> {}

impl<F> SharedProtocol for LinearToShared<F>
where
  F: Protocol,
  F: SharedRecApp<SharedToLinear<LinearToShared<F>>>,
  F::Applied: Protocol,
{
}

impl<F, T, E> ForwardChannel for LinearToShared<F>
where
  F: SharedRecApp<SharedToLinear<LinearToShared<F>>, Applied = T>,
  F: Send + 'static,
  T: Protocol<ClientEndpoint = E>,
  E: ForwardChannel,
{
  fn forward_to(
    self,
    sender: OpaqueSender,
    receiver: OpaqueReceiver,
  )
  {
    self.linear.get_applied().forward_to(sender, receiver)
  }

  fn forward_from(
    sender: OpaqueSender,
    receiver: OpaqueReceiver,
  ) -> Self
  {
    LinearToShared {
      linear: Box::new(E::forward_from(sender, receiver)),
    }
  }
}