fidl_test_ping/
fidl_test_ping.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_ping_common::*;
11use futures::future::{self, MaybeDone, TryFutureExt};
12use zx_status;
13
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
15pub struct PingMarker;
16
17impl fidl::endpoints::ProtocolMarker for PingMarker {
18    type Proxy = PingProxy;
19    type RequestStream = PingRequestStream;
20    #[cfg(target_os = "fuchsia")]
21    type SynchronousProxy = PingSynchronousProxy;
22
23    const DEBUG_NAME: &'static str = "test.ping.Ping";
24}
25impl fidl::endpoints::DiscoverableProtocolMarker for PingMarker {}
26
27pub trait PingProxyInterface: Send + Sync {
28    type PingResponseFut: std::future::Future<Output = Result<String, fidl::Error>> + Send;
29    fn r#ping(&self, ping: &str) -> Self::PingResponseFut;
30}
31#[derive(Debug)]
32#[cfg(target_os = "fuchsia")]
33pub struct PingSynchronousProxy {
34    client: fidl::client::sync::Client,
35}
36
37#[cfg(target_os = "fuchsia")]
38impl fidl::endpoints::SynchronousProxy for PingSynchronousProxy {
39    type Proxy = PingProxy;
40    type Protocol = PingMarker;
41
42    fn from_channel(inner: fidl::Channel) -> Self {
43        Self::new(inner)
44    }
45
46    fn into_channel(self) -> fidl::Channel {
47        self.client.into_channel()
48    }
49
50    fn as_channel(&self) -> &fidl::Channel {
51        self.client.as_channel()
52    }
53}
54
55#[cfg(target_os = "fuchsia")]
56impl PingSynchronousProxy {
57    pub fn new(channel: fidl::Channel) -> Self {
58        let protocol_name = <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
59        Self { client: fidl::client::sync::Client::new(channel, protocol_name) }
60    }
61
62    pub fn into_channel(self) -> fidl::Channel {
63        self.client.into_channel()
64    }
65
66    /// Waits until an event arrives and returns it. It is safe for other
67    /// threads to make concurrent requests while waiting for an event.
68    pub fn wait_for_event(&self, deadline: zx::MonotonicInstant) -> Result<PingEvent, fidl::Error> {
69        PingEvent::decode(self.client.wait_for_event(deadline)?)
70    }
71
72    /// Returns a string that is the concatenation of the input `ping`
73    /// a space and the string "pong".
74    pub fn r#ping(
75        &self,
76        mut ping: &str,
77        ___deadline: zx::MonotonicInstant,
78    ) -> Result<String, fidl::Error> {
79        let _response = self.client.send_query::<PingPingRequest, PingPingResponse>(
80            (ping,),
81            0x509ea7ec7cd43504,
82            fidl::encoding::DynamicFlags::empty(),
83            ___deadline,
84        )?;
85        Ok(_response.pong)
86    }
87}
88
89#[derive(Debug, Clone)]
90pub struct PingProxy {
91    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
92}
93
94impl fidl::endpoints::Proxy for PingProxy {
95    type Protocol = PingMarker;
96
97    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
98        Self::new(inner)
99    }
100
101    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
102        self.client.into_channel().map_err(|client| Self { client })
103    }
104
105    fn as_channel(&self) -> &::fidl::AsyncChannel {
106        self.client.as_channel()
107    }
108}
109
110impl PingProxy {
111    /// Create a new Proxy for test.ping/Ping.
112    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
113        let protocol_name = <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
114        Self { client: fidl::client::Client::new(channel, protocol_name) }
115    }
116
117    /// Get a Stream of events from the remote end of the protocol.
118    ///
119    /// # Panics
120    ///
121    /// Panics if the event stream was already taken.
122    pub fn take_event_stream(&self) -> PingEventStream {
123        PingEventStream { event_receiver: self.client.take_event_receiver() }
124    }
125
126    /// Returns a string that is the concatenation of the input `ping`
127    /// a space and the string "pong".
128    pub fn r#ping(
129        &self,
130        mut ping: &str,
131    ) -> fidl::client::QueryResponseFut<String, fidl::encoding::DefaultFuchsiaResourceDialect> {
132        PingProxyInterface::r#ping(self, ping)
133    }
134}
135
136impl PingProxyInterface for PingProxy {
137    type PingResponseFut =
138        fidl::client::QueryResponseFut<String, fidl::encoding::DefaultFuchsiaResourceDialect>;
139    fn r#ping(&self, mut ping: &str) -> Self::PingResponseFut {
140        fn _decode(
141            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
142        ) -> Result<String, fidl::Error> {
143            let _response = fidl::client::decode_transaction_body::<
144                PingPingResponse,
145                fidl::encoding::DefaultFuchsiaResourceDialect,
146                0x509ea7ec7cd43504,
147            >(_buf?)?;
148            Ok(_response.pong)
149        }
150        self.client.send_query_and_decode::<PingPingRequest, String>(
151            (ping,),
152            0x509ea7ec7cd43504,
153            fidl::encoding::DynamicFlags::empty(),
154            _decode,
155        )
156    }
157}
158
159pub struct PingEventStream {
160    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
161}
162
163impl std::marker::Unpin for PingEventStream {}
164
165impl futures::stream::FusedStream for PingEventStream {
166    fn is_terminated(&self) -> bool {
167        self.event_receiver.is_terminated()
168    }
169}
170
171impl futures::Stream for PingEventStream {
172    type Item = Result<PingEvent, 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(PingEvent::decode(buf))),
183            None => std::task::Poll::Ready(None),
184        }
185    }
186}
187
188#[derive(Debug)]
189pub enum PingEvent {}
190
191impl PingEvent {
192    /// Decodes a message buffer as a [`PingEvent`].
193    fn decode(
194        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
195    ) -> Result<PingEvent, 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: <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
203            }),
204        }
205    }
206}
207
208/// A Stream of incoming requests for test.ping/Ping.
209pub struct PingRequestStream {
210    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
211    is_terminated: bool,
212}
213
214impl std::marker::Unpin for PingRequestStream {}
215
216impl futures::stream::FusedStream for PingRequestStream {
217    fn is_terminated(&self) -> bool {
218        self.is_terminated
219    }
220}
221
222impl fidl::endpoints::RequestStream for PingRequestStream {
223    type Protocol = PingMarker;
224    type ControlHandle = PingControlHandle;
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        PingControlHandle { 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 PingRequestStream {
250    type Item = Result<PingRequest, 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 PingRequestStream 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                    0x509ea7ec7cd43504 => {
285                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
286                        let mut req = fidl::new_empty!(
287                            PingPingRequest,
288                            fidl::encoding::DefaultFuchsiaResourceDialect
289                        );
290                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<PingPingRequest>(&header, _body_bytes, handles, &mut req)?;
291                        let control_handle = PingControlHandle { inner: this.inner.clone() };
292                        Ok(PingRequest::Ping {
293                            ping: req.ping,
294
295                            responder: PingPingResponder {
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: <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
304                    }),
305                }))
306            },
307        )
308    }
309}
310
311#[derive(Debug)]
312pub enum PingRequest {
313    /// Returns a string that is the concatenation of the input `ping`
314    /// a space and the string "pong".
315    Ping { ping: String, responder: PingPingResponder },
316}
317
318impl PingRequest {
319    #[allow(irrefutable_let_patterns)]
320    pub fn into_ping(self) -> Option<(String, PingPingResponder)> {
321        if let PingRequest::Ping { ping, responder } = self {
322            Some((ping, responder))
323        } else {
324            None
325        }
326    }
327
328    /// Name of the method defined in FIDL
329    pub fn method_name(&self) -> &'static str {
330        match *self {
331            PingRequest::Ping { .. } => "ping",
332        }
333    }
334}
335
336#[derive(Debug, Clone)]
337pub struct PingControlHandle {
338    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
339}
340
341impl fidl::endpoints::ControlHandle for PingControlHandle {
342    fn shutdown(&self) {
343        self.inner.shutdown()
344    }
345    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
346        self.inner.shutdown_with_epitaph(status)
347    }
348
349    fn is_closed(&self) -> bool {
350        self.inner.channel().is_closed()
351    }
352    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
353        self.inner.channel().on_closed()
354    }
355
356    #[cfg(target_os = "fuchsia")]
357    fn signal_peer(
358        &self,
359        clear_mask: zx::Signals,
360        set_mask: zx::Signals,
361    ) -> Result<(), zx_status::Status> {
362        use fidl::Peered;
363        self.inner.channel().signal_peer(clear_mask, set_mask)
364    }
365}
366
367impl PingControlHandle {}
368
369#[must_use = "FIDL methods require a response to be sent"]
370#[derive(Debug)]
371pub struct PingPingResponder {
372    control_handle: std::mem::ManuallyDrop<PingControlHandle>,
373    tx_id: u32,
374}
375
376/// Set the the channel to be shutdown (see [`PingControlHandle::shutdown`])
377/// if the responder is dropped without sending a response, so that the client
378/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
379impl std::ops::Drop for PingPingResponder {
380    fn drop(&mut self) {
381        self.control_handle.shutdown();
382        // Safety: drops once, never accessed again
383        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
384    }
385}
386
387impl fidl::endpoints::Responder for PingPingResponder {
388    type ControlHandle = PingControlHandle;
389
390    fn control_handle(&self) -> &PingControlHandle {
391        &self.control_handle
392    }
393
394    fn drop_without_shutdown(mut self) {
395        // Safety: drops once, never accessed again due to mem::forget
396        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
397        // Prevent Drop from running (which would shut down the channel)
398        std::mem::forget(self);
399    }
400}
401
402impl PingPingResponder {
403    /// Sends a response to the FIDL transaction.
404    ///
405    /// Sets the channel to shutdown if an error occurs.
406    pub fn send(self, mut pong: &str) -> Result<(), fidl::Error> {
407        let _result = self.send_raw(pong);
408        if _result.is_err() {
409            self.control_handle.shutdown();
410        }
411        self.drop_without_shutdown();
412        _result
413    }
414
415    /// Similar to "send" but does not shutdown the channel if an error occurs.
416    pub fn send_no_shutdown_on_err(self, mut pong: &str) -> Result<(), fidl::Error> {
417        let _result = self.send_raw(pong);
418        self.drop_without_shutdown();
419        _result
420    }
421
422    fn send_raw(&self, mut pong: &str) -> Result<(), fidl::Error> {
423        self.control_handle.inner.send::<PingPingResponse>(
424            (pong,),
425            self.tx_id,
426            0x509ea7ec7cd43504,
427            fidl::encoding::DynamicFlags::empty(),
428        )
429    }
430}
431
432mod internal {
433    use super::*;
434}