fidl_test_echo/
fidl_test_echo.rs

1// WARNING: This file is machine generated by fidlgen.
2
3#![warn(clippy::all)]
4#![allow(unused_parens, unused_mut, unused_imports, nonstandard_style)]
5
6use bitflags::bitflags;
7use fidl::client::QueryResponseFut;
8use fidl::encoding::{MessageBufFor, ProxyChannelBox, ResourceDialect};
9use fidl::endpoints::{ControlHandle as _, Responder as _};
10pub use fidl_test_echo_common::*;
11use futures::future::{self, MaybeDone, TryFutureExt};
12use zx_status;
13
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
15pub struct EchoMarker;
16
17impl fidl::endpoints::ProtocolMarker for EchoMarker {
18    type Proxy = EchoProxy;
19    type RequestStream = EchoRequestStream;
20    #[cfg(target_os = "fuchsia")]
21    type SynchronousProxy = EchoSynchronousProxy;
22
23    const DEBUG_NAME: &'static str = "test.echo.Echo";
24}
25impl fidl::endpoints::DiscoverableProtocolMarker for EchoMarker {}
26
27pub trait EchoProxyInterface: Send + Sync {
28    type EchoStringResponseFut: std::future::Future<Output = Result<Option<String>, fidl::Error>>
29        + Send;
30    fn r#echo_string(&self, value: Option<&str>) -> Self::EchoStringResponseFut;
31}
32#[derive(Debug)]
33#[cfg(target_os = "fuchsia")]
34pub struct EchoSynchronousProxy {
35    client: fidl::client::sync::Client,
36}
37
38#[cfg(target_os = "fuchsia")]
39impl fidl::endpoints::SynchronousProxy for EchoSynchronousProxy {
40    type Proxy = EchoProxy;
41    type Protocol = EchoMarker;
42
43    fn from_channel(inner: fidl::Channel) -> Self {
44        Self::new(inner)
45    }
46
47    fn into_channel(self) -> fidl::Channel {
48        self.client.into_channel()
49    }
50
51    fn as_channel(&self) -> &fidl::Channel {
52        self.client.as_channel()
53    }
54}
55
56#[cfg(target_os = "fuchsia")]
57impl EchoSynchronousProxy {
58    pub fn new(channel: fidl::Channel) -> Self {
59        let protocol_name = <EchoMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
60        Self { client: fidl::client::sync::Client::new(channel, protocol_name) }
61    }
62
63    pub fn into_channel(self) -> fidl::Channel {
64        self.client.into_channel()
65    }
66
67    /// Waits until an event arrives and returns it. It is safe for other
68    /// threads to make concurrent requests while waiting for an event.
69    pub fn wait_for_event(&self, deadline: zx::MonotonicInstant) -> Result<EchoEvent, fidl::Error> {
70        EchoEvent::decode(self.client.wait_for_event(deadline)?)
71    }
72
73    pub fn r#echo_string(
74        &self,
75        mut value: Option<&str>,
76        ___deadline: zx::MonotonicInstant,
77    ) -> Result<Option<String>, fidl::Error> {
78        let _response = self.client.send_query::<EchoEchoStringRequest, EchoEchoStringResponse>(
79            (value,),
80            0x1e16cc1b194790f6,
81            fidl::encoding::DynamicFlags::empty(),
82            ___deadline,
83        )?;
84        Ok(_response.response)
85    }
86}
87
88#[derive(Debug, Clone)]
89pub struct EchoProxy {
90    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
91}
92
93impl fidl::endpoints::Proxy for EchoProxy {
94    type Protocol = EchoMarker;
95
96    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
97        Self::new(inner)
98    }
99
100    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
101        self.client.into_channel().map_err(|client| Self { client })
102    }
103
104    fn as_channel(&self) -> &::fidl::AsyncChannel {
105        self.client.as_channel()
106    }
107}
108
109impl EchoProxy {
110    /// Create a new Proxy for test.echo/Echo.
111    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
112        let protocol_name = <EchoMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
113        Self { client: fidl::client::Client::new(channel, protocol_name) }
114    }
115
116    /// Get a Stream of events from the remote end of the protocol.
117    ///
118    /// # Panics
119    ///
120    /// Panics if the event stream was already taken.
121    pub fn take_event_stream(&self) -> EchoEventStream {
122        EchoEventStream { event_receiver: self.client.take_event_receiver() }
123    }
124
125    pub fn r#echo_string(
126        &self,
127        mut value: Option<&str>,
128    ) -> fidl::client::QueryResponseFut<Option<String>, fidl::encoding::DefaultFuchsiaResourceDialect>
129    {
130        EchoProxyInterface::r#echo_string(self, value)
131    }
132}
133
134impl EchoProxyInterface for EchoProxy {
135    type EchoStringResponseFut = fidl::client::QueryResponseFut<
136        Option<String>,
137        fidl::encoding::DefaultFuchsiaResourceDialect,
138    >;
139    fn r#echo_string(&self, mut value: Option<&str>) -> Self::EchoStringResponseFut {
140        fn _decode(
141            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
142        ) -> Result<Option<String>, fidl::Error> {
143            let _response = fidl::client::decode_transaction_body::<
144                EchoEchoStringResponse,
145                fidl::encoding::DefaultFuchsiaResourceDialect,
146                0x1e16cc1b194790f6,
147            >(_buf?)?;
148            Ok(_response.response)
149        }
150        self.client.send_query_and_decode::<EchoEchoStringRequest, Option<String>>(
151            (value,),
152            0x1e16cc1b194790f6,
153            fidl::encoding::DynamicFlags::empty(),
154            _decode,
155        )
156    }
157}
158
159pub struct EchoEventStream {
160    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
161}
162
163impl std::marker::Unpin for EchoEventStream {}
164
165impl futures::stream::FusedStream for EchoEventStream {
166    fn is_terminated(&self) -> bool {
167        self.event_receiver.is_terminated()
168    }
169}
170
171impl futures::Stream for EchoEventStream {
172    type Item = Result<EchoEvent, fidl::Error>;
173
174    fn poll_next(
175        mut self: std::pin::Pin<&mut Self>,
176        cx: &mut std::task::Context<'_>,
177    ) -> std::task::Poll<Option<Self::Item>> {
178        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
179            &mut self.event_receiver,
180            cx
181        )?) {
182            Some(buf) => std::task::Poll::Ready(Some(EchoEvent::decode(buf))),
183            None => std::task::Poll::Ready(None),
184        }
185    }
186}
187
188#[derive(Debug)]
189pub enum EchoEvent {}
190
191impl EchoEvent {
192    /// Decodes a message buffer as a [`EchoEvent`].
193    fn decode(
194        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
195    ) -> Result<EchoEvent, fidl::Error> {
196        let (bytes, _handles) = buf.split_mut();
197        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
198        debug_assert_eq!(tx_header.tx_id, 0);
199        match tx_header.ordinal {
200            _ => Err(fidl::Error::UnknownOrdinal {
201                ordinal: tx_header.ordinal,
202                protocol_name: <EchoMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
203            }),
204        }
205    }
206}
207
208/// A Stream of incoming requests for test.echo/Echo.
209pub struct EchoRequestStream {
210    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
211    is_terminated: bool,
212}
213
214impl std::marker::Unpin for EchoRequestStream {}
215
216impl futures::stream::FusedStream for EchoRequestStream {
217    fn is_terminated(&self) -> bool {
218        self.is_terminated
219    }
220}
221
222impl fidl::endpoints::RequestStream for EchoRequestStream {
223    type Protocol = EchoMarker;
224    type ControlHandle = EchoControlHandle;
225
226    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
227        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
228    }
229
230    fn control_handle(&self) -> Self::ControlHandle {
231        EchoControlHandle { inner: self.inner.clone() }
232    }
233
234    fn into_inner(
235        self,
236    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
237    {
238        (self.inner, self.is_terminated)
239    }
240
241    fn from_inner(
242        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
243        is_terminated: bool,
244    ) -> Self {
245        Self { inner, is_terminated }
246    }
247}
248
249impl futures::Stream for EchoRequestStream {
250    type Item = Result<EchoRequest, fidl::Error>;
251
252    fn poll_next(
253        mut self: std::pin::Pin<&mut Self>,
254        cx: &mut std::task::Context<'_>,
255    ) -> std::task::Poll<Option<Self::Item>> {
256        let this = &mut *self;
257        if this.inner.check_shutdown(cx) {
258            this.is_terminated = true;
259            return std::task::Poll::Ready(None);
260        }
261        if this.is_terminated {
262            panic!("polled EchoRequestStream after completion");
263        }
264        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
265            |bytes, handles| {
266                match this.inner.channel().read_etc(cx, bytes, handles) {
267                    std::task::Poll::Ready(Ok(())) => {}
268                    std::task::Poll::Pending => return std::task::Poll::Pending,
269                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
270                        this.is_terminated = true;
271                        return std::task::Poll::Ready(None);
272                    }
273                    std::task::Poll::Ready(Err(e)) => {
274                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
275                            e.into(),
276                        ))))
277                    }
278                }
279
280                // A message has been received from the channel
281                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
282
283                std::task::Poll::Ready(Some(match header.ordinal {
284                    0x1e16cc1b194790f6 => {
285                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
286                        let mut req = fidl::new_empty!(
287                            EchoEchoStringRequest,
288                            fidl::encoding::DefaultFuchsiaResourceDialect
289                        );
290                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<EchoEchoStringRequest>(&header, _body_bytes, handles, &mut req)?;
291                        let control_handle = EchoControlHandle { inner: this.inner.clone() };
292                        Ok(EchoRequest::EchoString {
293                            value: req.value,
294
295                            responder: EchoEchoStringResponder {
296                                control_handle: std::mem::ManuallyDrop::new(control_handle),
297                                tx_id: header.tx_id,
298                            },
299                        })
300                    }
301                    _ => Err(fidl::Error::UnknownOrdinal {
302                        ordinal: header.ordinal,
303                        protocol_name: <EchoMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
304                    }),
305                }))
306            },
307        )
308    }
309}
310
311#[derive(Debug)]
312pub enum EchoRequest {
313    EchoString { value: Option<String>, responder: EchoEchoStringResponder },
314}
315
316impl EchoRequest {
317    #[allow(irrefutable_let_patterns)]
318    pub fn into_echo_string(self) -> Option<(Option<String>, EchoEchoStringResponder)> {
319        if let EchoRequest::EchoString { value, responder } = self {
320            Some((value, responder))
321        } else {
322            None
323        }
324    }
325
326    /// Name of the method defined in FIDL
327    pub fn method_name(&self) -> &'static str {
328        match *self {
329            EchoRequest::EchoString { .. } => "echo_string",
330        }
331    }
332}
333
334#[derive(Debug, Clone)]
335pub struct EchoControlHandle {
336    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
337}
338
339impl fidl::endpoints::ControlHandle for EchoControlHandle {
340    fn shutdown(&self) {
341        self.inner.shutdown()
342    }
343    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
344        self.inner.shutdown_with_epitaph(status)
345    }
346
347    fn is_closed(&self) -> bool {
348        self.inner.channel().is_closed()
349    }
350    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
351        self.inner.channel().on_closed()
352    }
353
354    #[cfg(target_os = "fuchsia")]
355    fn signal_peer(
356        &self,
357        clear_mask: zx::Signals,
358        set_mask: zx::Signals,
359    ) -> Result<(), zx_status::Status> {
360        use fidl::Peered;
361        self.inner.channel().signal_peer(clear_mask, set_mask)
362    }
363}
364
365impl EchoControlHandle {}
366
367#[must_use = "FIDL methods require a response to be sent"]
368#[derive(Debug)]
369pub struct EchoEchoStringResponder {
370    control_handle: std::mem::ManuallyDrop<EchoControlHandle>,
371    tx_id: u32,
372}
373
374/// Set the the channel to be shutdown (see [`EchoControlHandle::shutdown`])
375/// if the responder is dropped without sending a response, so that the client
376/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
377impl std::ops::Drop for EchoEchoStringResponder {
378    fn drop(&mut self) {
379        self.control_handle.shutdown();
380        // Safety: drops once, never accessed again
381        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
382    }
383}
384
385impl fidl::endpoints::Responder for EchoEchoStringResponder {
386    type ControlHandle = EchoControlHandle;
387
388    fn control_handle(&self) -> &EchoControlHandle {
389        &self.control_handle
390    }
391
392    fn drop_without_shutdown(mut self) {
393        // Safety: drops once, never accessed again due to mem::forget
394        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
395        // Prevent Drop from running (which would shut down the channel)
396        std::mem::forget(self);
397    }
398}
399
400impl EchoEchoStringResponder {
401    /// Sends a response to the FIDL transaction.
402    ///
403    /// Sets the channel to shutdown if an error occurs.
404    pub fn send(self, mut response: Option<&str>) -> Result<(), fidl::Error> {
405        let _result = self.send_raw(response);
406        if _result.is_err() {
407            self.control_handle.shutdown();
408        }
409        self.drop_without_shutdown();
410        _result
411    }
412
413    /// Similar to "send" but does not shutdown the channel if an error occurs.
414    pub fn send_no_shutdown_on_err(self, mut response: Option<&str>) -> Result<(), fidl::Error> {
415        let _result = self.send_raw(response);
416        self.drop_without_shutdown();
417        _result
418    }
419
420    fn send_raw(&self, mut response: Option<&str>) -> Result<(), fidl::Error> {
421        self.control_handle.inner.send::<EchoEchoStringResponse>(
422            (response,),
423            self.tx_id,
424            0x1e16cc1b194790f6,
425            fidl::encoding::DynamicFlags::empty(),
426        )
427    }
428}
429
430mod internal {
431    use super::*;
432}