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 #[cfg(feature = "fuchsia")] T: Transport = zx::Channel,
59 #[cfg(not(feature = "fuchsia"))] T: Transport,
60 > {
61 #[pin]
62 state: $state<$($lifetime,)? T>,
63 }
64
65 impl<$($lifetime,)? T: Transport> $name<$($lifetime,)? T> {
66 #[doc = concat!("Returns a `", stringify!($name), "` wrapping the given result.")]
67 pub fn from_untyped(result: Result<$future, EncodeError>) -> Self {
68 Self {
69 state: match result {
70 Err(error) => $state::EncodeError(error),
71 Ok(future) => $state::Sending(future),
72 },
73 }
74 }
75
76 pub fn encode(self) -> Result<$encoded<$($lifetime,)? T>, Error<T::Error>> {
80 Ok($encoded {
81 state: match self.state {
82 $state::EncodeError(error) => return Err(Error::Encode(error)),
83 state => state,
84 },
85 })
86 }
87 }
88
89 impl<'a, T: Transport> Future for $name<$($lifetime,)? T> {
90 type Output = Result<(), Error<T::Error>>;
91
92 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
93 self.project().state.poll_state(cx)
94 }
95 }
96
97 #[doc = concat!("An encoded `", stringify!($name), "`.")]
98 #[must_use = "futures do nothing unless polled"]
102 #[pin_project]
103 pub struct $encoded<
104 $($lifetime,)?
105 #[cfg(feature = "fuchsia")] T: Transport = zx::Channel,
106 #[cfg(not(feature = "fuchsia"))] T: Transport,
107 > {
108 #[pin]
109 state: $state<$($lifetime,)? T>,
110 }
111
112 impl<$($lifetime,)? T: Transport> Future for $encoded<$($lifetime,)? T> {
113 type Output = Result<(), Error<T::Error>>;
114
115 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
116 self.project().state.poll_state(cx)
117 }
118 }
119 }
120}
121
122define_send_future! {
123 SendFuture<'a, T: Transport>(fidl_next_protocol::SendFuture<'a, T>) => EncodedSendFuture {
125 state = SendFutureState,
126 proj = SendFutureProj,
127 own = SendFutureOwn,
128 }
129}
130
131define_send_future! {
132 RespondFuture<T: Transport>(fidl_next_protocol::RespondFuture<T>) => EncodedRespondFuture {
134 state = RespondFutureState,
135 proj = RespondFutureProj,
136 own = RespondFutureOwn,
137 }
138}