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
use core::{
  future::Future,
  pin::Pin,
};

use crate::internal::base::*;

pub trait Wrapper
{
  type Unwrap: Protocol;
}

pub struct Wrap<T>
where
  T: Wrapper,
{
  pub(crate) unwrap: Box<dyn HasWrapped<T>>,
}

pub trait HasWrapped<T>: Send + 'static
{
  fn unwrap(self: Box<Self>) -> <T::Unwrap as Protocol>::ClientEndpoint
  where
    T: Wrapper,
    T::Unwrap: Protocol;
}

impl<T, W> HasWrapped<T> for W::ClientEndpoint
where
  T: Wrapper<Unwrap = W>,
  W: Protocol,
{
  fn unwrap(self: Box<Self>) -> <T::Unwrap as Protocol>::ClientEndpoint
  {
    *self
  }
}

impl<T> SealedProtocol for Wrap<T> where T: Wrapper {}

impl<T> Protocol for Wrap<T>
where
  T: Wrapper,
  T: Send + 'static,
{
  type ClientEndpoint = ReceiverOnce<Wrap<T>>;
  type ProviderEndpoint = SenderOnce<Wrap<T>>;

  fn create_endpoints() -> (Self::ProviderEndpoint, Self::ClientEndpoint)
  {
    once_channel()
  }

  fn forward(
    client_end: Self::ClientEndpoint,
    provider_end: Self::ProviderEndpoint,
  ) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
  {
    Box::pin(async {
      let payload = client_end.recv().await.unwrap();
      provider_end.send(payload).unwrap();
    })
  }
}