Skip to main content

ferrite_session/internal/protocol/choice/
external_choice.rs

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