ferrite_session/internal/base/rec/
impls.rs

1use core::{
2  future::Future,
3  pin::Pin,
4};
5
6use super::{
7  traits::{
8    HasRecApp,
9    HasRecEndpoint,
10    RecApp,
11    SharedRecApp,
12  },
13  types::{
14    RecEndpoint,
15    RecRow,
16    RecX,
17    Release,
18    SharedRecRow,
19  },
20};
21use crate::internal::{
22  base::{
23    channel::{
24      once_channel,
25      ReceiverOnce,
26      SenderOnce,
27    },
28    protocol::{
29      Protocol,
30      SealedProtocol,
31    },
32  },
33  functional::*,
34};
35
36impl<T, F, A> HasRecApp<F, A> for T
37where
38  F: 'static,
39  A: 'static,
40  T: Send + 'static,
41  F: RecApp<A, Applied = T>,
42{
43  fn get_applied(self: Box<T>) -> Box<T>
44  {
45    self
46  }
47}
48
49impl<C, F> RecApp<C> for RecX<(), F>
50where
51  C: Send,
52  F: RecApp<(RecX<C, F>, C)>,
53{
54  type Applied = RecX<C, F>;
55}
56
57impl<C, A> RecApp<(A, C)> for Z
58where
59  A: Send,
60  C: Send,
61{
62  type Applied = A;
63}
64
65impl<C, A, N> RecApp<(A, C)> for S<N>
66where
67  N: Send,
68  C: Send,
69  A: Send,
70  N: RecApp<C>,
71{
72  type Applied = N::Applied;
73}
74
75impl<A> RecApp<A> for ()
76{
77  type Applied = ();
78}
79
80impl<A, X, Y> RecApp<A> for (X, Y)
81where
82  X: RecApp<A>,
83  Y: RecApp<A>,
84{
85  type Applied = (X::Applied, Y::Applied);
86}
87
88impl<X> SharedRecApp<X> for Release
89{
90  type Applied = X;
91}
92
93impl<R> SharedRecApp<R> for ()
94{
95  type Applied = ();
96}
97
98impl<P, Q, R> SharedRecApp<R> for (P, Q)
99where
100  P: SharedRecApp<R>,
101  Q: SharedRecApp<R>,
102{
103  type Applied = (P::Applied, Q::Applied);
104}
105
106impl<X, F> SharedRecApp<X> for RecX<(), F>
107where
108  F: SharedRecApp<X>,
109{
110  type Applied = RecX<(), F::Applied>;
111}
112
113impl<F, C, E> HasRecEndpoint<F, C> for E
114where
115  E: Send + 'static,
116  F: RecApp<C>,
117  F::Applied: Protocol<ClientEndpoint = E>,
118{
119  fn get_applied(self: Box<Self>) -> Box<Self>
120  {
121    self
122  }
123}
124
125impl<C, F> SealedProtocol for RecX<C, F> {}
126
127impl<C, F> Protocol for RecX<C, F>
128where
129  C: Send + 'static,
130  F: Protocol,
131  F: RecApp<(RecX<C, F>, C)>,
132{
133  type ClientEndpoint = ReceiverOnce<RecEndpoint<F, (RecX<C, F>, C)>>;
134  type ProviderEndpoint = SenderOnce<RecEndpoint<F, (RecX<C, F>, C)>>;
135
136  fn create_endpoints() -> (Self::ProviderEndpoint, Self::ClientEndpoint)
137  {
138    once_channel()
139  }
140
141  fn forward(
142    client_end: Self::ClientEndpoint,
143    provider_end: Self::ProviderEndpoint,
144  ) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
145  {
146    Box::pin(async {
147      let endpoint = client_end.recv().await.unwrap();
148      provider_end.send(endpoint).unwrap();
149    })
150  }
151}
152
153impl SealedProtocol for Release {}
154
155impl Protocol for Release
156{
157  type ClientEndpoint = ();
158  type ProviderEndpoint = ();
159
160  fn create_endpoints() -> (Self::ProviderEndpoint, Self::ClientEndpoint)
161  {
162    ((), ())
163  }
164
165  fn forward(
166    _client_end: Self::ClientEndpoint,
167    _provider_end: Self::ProviderEndpoint,
168  ) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
169  {
170    Box::pin(async {})
171  }
172}
173
174impl<R, Row1, Row2, Row3> ToRow for RecRow<R, Row1>
175where
176  R: Send,
177  Row1: ToRow<Row = Row2>,
178  Row2: RecApp<R, Applied = Row3>,
179  Row3: RowCon,
180{
181  type Row = Row3;
182}
183
184impl<R, Row1, Row2, Row3> ToRow for SharedRecRow<R, Row1>
185where
186  R: Send,
187  Row1: ToRow<Row = Row2>,
188  Row2: SharedRecApp<R, Applied = Row3>,
189  Row3: RowCon,
190{
191  type Row = Row3;
192}