ferrite_session/internal/protocol/
wrap.rs1use core::{
2 future::Future,
3 pin::Pin,
4};
5
6use crate::internal::base::*;
7
8pub trait Wrapper
9{
10 type Unwrap: Protocol;
11}
12
13pub struct Wrap<T>
14where
15 T: Wrapper,
16{
17 pub(crate) unwrap: Box<dyn HasWrapped<T>>,
18}
19
20pub trait HasWrapped<T>: Send + 'static
21{
22 fn unwrap(self: Box<Self>) -> <T::Unwrap as Protocol>::ClientEndpoint
23 where
24 T: Wrapper,
25 T::Unwrap: Protocol;
26}
27
28impl<T, W> HasWrapped<T> for W::ClientEndpoint
29where
30 T: Wrapper<Unwrap = W>,
31 W: Protocol,
32{
33 fn unwrap(self: Box<Self>) -> <T::Unwrap as Protocol>::ClientEndpoint
34 {
35 *self
36 }
37}
38
39impl<T> SealedProtocol for Wrap<T> where T: Wrapper {}
40
41impl<T> Protocol for Wrap<T>
42where
43 T: Wrapper,
44 T: Send + 'static,
45{
46 type ClientEndpoint = ReceiverOnce<Wrap<T>>;
47 type ProviderEndpoint = SenderOnce<Wrap<T>>;
48
49 fn create_endpoints() -> (Self::ProviderEndpoint, Self::ClientEndpoint)
50 {
51 once_channel()
52 }
53
54 fn forward(
55 client_end: Self::ClientEndpoint,
56 provider_end: Self::ProviderEndpoint,
57 ) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>>
58 {
59 Box::pin(async {
60 let payload = client_end.recv().await.unwrap();
61 provider_end.send(payload).unwrap();
62 })
63 }
64}