fidl_next_protocol/
transport.rs1use core::error::Error;
6use core::future::Future;
7use core::pin::Pin;
8use core::task::{Context, Poll};
9
10use fidl_next_codec::{Decoder, Encoder};
11
12pub trait Transport {
34 type Error: Error + Send + Sync + 'static;
36
37 fn split(self) -> (Self::Sender, Self::Receiver);
39
40 type Sender: Send + Sync + Clone;
43 type SendBuffer: Encoder + Send;
45 type SendFutureState: Send;
47
48 fn acquire(sender: &Self::Sender) -> Self::SendBuffer;
50 fn begin_send(sender: &Self::Sender, buffer: Self::SendBuffer) -> Self::SendFutureState;
54 fn poll_send(
56 future: Pin<&mut Self::SendFutureState>,
57 cx: &mut Context<'_>,
58 sender: &Self::Sender,
59 ) -> Poll<Result<(), Self::Error>>;
60 fn close(sender: &Self::Sender);
62
63 type Receiver: Send;
65 type RecvFutureState: Send;
67 type RecvBuffer: Decoder + Send;
69
70 fn begin_recv(receiver: &mut Self::Receiver) -> Self::RecvFutureState;
74 fn poll_recv(
76 future: Pin<&mut Self::RecvFutureState>,
77 cx: &mut Context<'_>,
78 receiver: &mut Self::Receiver,
79 ) -> Poll<Result<Option<Self::RecvBuffer>, Self::Error>>;
80}
81
82pub trait TransportExt: Transport {
84 fn send(sender: &Self::Sender, buffer: Self::SendBuffer) -> SendFuture<'_, Self> {
86 let future_state = Self::begin_send(sender, buffer);
87 SendFuture { sender, future_state }
88 }
89
90 fn recv(receiver: &mut Self::Receiver) -> RecvFuture<'_, Self> {
92 let future_state = Self::begin_recv(receiver);
93 RecvFuture { receiver, future_state }
94 }
95}
96
97impl<T: Transport + ?Sized> TransportExt for T {}
98
99#[must_use = "futures do nothing unless polled"]
101pub struct SendFuture<'s, T: Transport + ?Sized> {
102 sender: &'s T::Sender,
103 future_state: T::SendFutureState,
104}
105
106impl<T: Transport> Future for SendFuture<'_, T> {
107 type Output = Result<(), T::Error>;
108
109 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
110 let this = unsafe { Pin::into_inner_unchecked(self) };
111 let future_state = unsafe { Pin::new_unchecked(&mut this.future_state) };
112 T::poll_send(future_state, cx, this.sender)
113 }
114}
115
116#[must_use = "futures do nothing unless polled"]
118pub struct RecvFuture<'r, T: Transport + ?Sized> {
119 receiver: &'r mut T::Receiver,
120 future_state: T::RecvFutureState,
121}
122
123impl<T: Transport> Future for RecvFuture<'_, T> {
124 type Output = Result<Option<T::RecvBuffer>, T::Error>;
125
126 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
127 let this = unsafe { Pin::into_inner_unchecked(self) };
128 let future_state = unsafe { Pin::new_unchecked(&mut this.future_state) };
129 T::poll_recv(future_state, cx, this.receiver)
130 }
131}