Skip to main content

ferrite_session/internal/protocol/channel/
send.rs

1use core::{
2  future::Future,
3  marker::PhantomData,
4  pin::Pin,
5};
6
7use crate::internal::base::*;
8
9pub struct SendChannel<A, B>(PhantomData<(A, B)>);
10
11impl<A, B> SealedProtocol for SendChannel<A, B> {}
12
13impl<A, B> Protocol for SendChannel<A, B>
14where
15  A: Protocol,
16  B: Protocol,
17{
18  type ClientEndpoint = (ReceiverOnce<A::ClientEndpoint>, B::ClientEndpoint);
19  type ProviderEndpoint = (SenderOnce<A::ClientEndpoint>, B::ProviderEndpoint);
20
21  fn create_endpoints() -> (Self::ProviderEndpoint, Self::ClientEndpoint)
22  {
23    let (chan_sender, chan_receiver) = once_channel();
24    let (provider_end, client_end) = B::create_endpoints();
25
26    ((chan_sender, provider_end), (chan_receiver, client_end))
27  }
28
29  fn forward(
30    client_end: Self::ClientEndpoint,
31    provider_end: Self::ProviderEndpoint,
32  ) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
33  {
34    let (chan_receiver, client_end_b) = client_end;
35    let (chan_sender, provider_end_b) = provider_end;
36
37    Box::pin(async {
38      let chan = chan_receiver.recv().await.unwrap();
39      chan_sender.send(chan).unwrap();
40
41      B::forward(client_end_b, provider_end_b).await;
42    })
43  }
44}
45
46impl<A, P, Q> RecApp<A> for SendChannel<P, Q>
47where
48  P: RecApp<A>,
49  Q: RecApp<A>,
50{
51  type Applied = SendChannel<P::Applied, Q::Applied>;
52}
53
54impl<P, Q, R> SharedRecApp<R> for SendChannel<P, Q>
55where
56  P: Protocol,
57  Q: SharedRecApp<R>,
58{
59  type Applied = SendChannel<P, Q::Applied>;
60}