fidl_next_bind/future/
send.rs1use core::future::Future;
6use core::pin::Pin;
7use core::task::{Context, Poll, ready};
8
9use fidl_next_codec::EncodeError;
10use fidl_next_protocol::Transport;
11use pin_project::pin_project;
12
13use crate::Error;
14
15macro_rules! define_send_future {
16 (
17 $(#[$metas:meta])*
18 $name:ident<$($lifetime:lifetime,)? T: Transport>($future:ty) => $encoded:ident {
19 state = $state:ident,
20 proj = $proj:ident,
21 own = $own:ident,
22 }
23 ) => {
24 #[pin_project(project = $proj, project_replace = $own)]
25 enum $state<$($lifetime,)? T: Transport> {
26 EncodeError(EncodeError),
27 Sending(#[pin] $future),
28 Finished,
29 }
30
31 impl<$($lifetime,)? T: Transport> $state<$($lifetime,)? T> {
32 fn poll_state(
33 mut self: Pin<&mut Self>,
34 cx: &mut Context<'_>,
35 ) -> Poll<Result<(), Error<T::Error>>> {
36 match self.as_mut().project() {
37 $proj::EncodeError(_) => {
38 let state = self.project_replace(Self::Finished);
39 let $own::EncodeError(error) = state else {
40 unreachable!();
41 };
42 Poll::Ready(Err(Error::Encode(error)))
43 }
44 $proj::Sending(future) => match ready!(future.poll(cx)) {
45 Ok(()) => Poll::Ready(Ok(())),
46 Err(error) => Poll::Ready(Err(Error::Protocol(error))),
47 },
48 $proj::Finished => panic!("State polled after completing"),
49 }
50 }
51 }
52
53 $(#[$metas])*
54 #[must_use = "futures do nothing unless polled"]
55 #[pin_project]
56 pub struct $name<
57 $($lifetime,)?
58 T: Transport,
59 > {
60 #[pin]
61 state: $state<$($lifetime,)? T>,
62 }
63
64 impl<$($lifetime,)? T: Transport> $name<$($lifetime,)? T> {
65 #[doc = concat!("Returns a `", stringify!($name), "` wrapping the given result.")]
66 pub fn from_untyped(result: Result<$future, EncodeError>) -> Self {
67 Self {
68 state: match result {
69 Err(error) => $state::EncodeError(error),
70 Ok(future) => $state::Sending(future),
71 },
72 }
73 }
74
75 pub fn encode(self) -> Result<$encoded<$($lifetime,)? T>, Error<T::Error>> {
79 Ok($encoded {
80 state: match self.state {
81 $state::EncodeError(error) => return Err(Error::Encode(error)),
82 state => state,
83 },
84 })
85 }
86 }
87
88 impl<'a, T: Transport> Future for $name<$($lifetime,)? T> {
89 type Output = Result<(), Error<T::Error>>;
90
91 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
92 self.project().state.poll_state(cx)
93 }
94 }
95
96 #[doc = concat!("An encoded `", stringify!($name), "`.")]
97 #[must_use = "futures do nothing unless polled"]
101 #[pin_project]
102 pub struct $encoded<
103 $($lifetime,)?
104 T: Transport,
105 > {
106 #[pin]
107 state: $state<$($lifetime,)? T>,
108 }
109
110 impl<$($lifetime,)? T: Transport> Future for $encoded<$($lifetime,)? T> {
111 type Output = Result<(), Error<T::Error>>;
112
113 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
114 self.project().state.poll_state(cx)
115 }
116 }
117 }
118}
119
120define_send_future! {
121 SendFuture<'a, T: Transport>(fidl_next_protocol::SendFuture<'a, T>) => EncodedSendFuture {
123 state = SendFutureState,
124 proj = SendFutureProj,
125 own = SendFutureOwn,
126 }
127}
128
129define_send_future! {
130 RespondFuture<T: Transport>(fidl_next_protocol::RespondFuture<T>) => EncodedRespondFuture {
132 state = RespondFutureState,
133 proj = RespondFutureProj,
134 own = RespondFutureOwn,
135 }
136}