Skip to main content

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        Self { client: fidl::client::sync::Client::new(channel) }
59    }
60
61    pub fn into_channel(self) -> fidl::Channel {
62        self.client.into_channel()
63    }
64
65    /// Waits until an event arrives and returns it. It is safe for other
66    /// threads to make concurrent requests while waiting for an event.
67    pub fn wait_for_event(&self, deadline: zx::MonotonicInstant) -> Result<PingEvent, fidl::Error> {
68        PingEvent::decode(self.client.wait_for_event::<PingMarker>(deadline)?)
69    }
70
71    /// Returns a string that is the concatenation of the input `ping`
72    /// a space and the string "pong".
73    pub fn r#ping(
74        &self,
75        mut ping: &str,
76        ___deadline: zx::MonotonicInstant,
77    ) -> Result<String, fidl::Error> {
78        let _response = self.client.send_query::<PingPingRequest, PingPingResponse, PingMarker>(
79            (ping,),
80            0x509ea7ec7cd43504,
81            fidl::encoding::DynamicFlags::empty(),
82            ___deadline,
83        )?;
84        Ok(_response.pong)
85    }
86}
87
88#[cfg(target_os = "fuchsia")]
89impl From<PingSynchronousProxy> for zx::NullableHandle {
90    fn from(value: PingSynchronousProxy) -> Self {
91        value.into_channel().into()
92    }
93}
94
95#[cfg(target_os = "fuchsia")]
96impl From<fidl::Channel> for PingSynchronousProxy {
97    fn from(value: fidl::Channel) -> Self {
98        Self::new(value)
99    }
100}
101
102#[cfg(target_os = "fuchsia")]
103impl fidl::endpoints::FromClient for PingSynchronousProxy {
104    type Protocol = PingMarker;
105
106    fn from_client(value: fidl::endpoints::ClientEnd<PingMarker>) -> Self {
107        Self::new(value.into_channel())
108    }
109}
110
111#[derive(Debug, Clone)]
112pub struct PingProxy {
113    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
114}
115
116impl fidl::endpoints::Proxy for PingProxy {
117    type Protocol = PingMarker;
118
119    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
120        Self::new(inner)
121    }
122
123    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
124        self.client.into_channel().map_err(|client| Self { client })
125    }
126
127    fn as_channel(&self) -> &::fidl::AsyncChannel {
128        self.client.as_channel()
129    }
130}
131
132impl PingProxy {
133    /// Create a new Proxy for test.ping/Ping.
134    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
135        let protocol_name = <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
136        Self { client: fidl::client::Client::new(channel, protocol_name) }
137    }
138
139    /// Get a Stream of events from the remote end of the protocol.
140    ///
141    /// # Panics
142    ///
143    /// Panics if the event stream was already taken.
144    pub fn take_event_stream(&self) -> PingEventStream {
145        PingEventStream { event_receiver: self.client.take_event_receiver() }
146    }
147
148    /// Returns a string that is the concatenation of the input `ping`
149    /// a space and the string "pong".
150    pub fn r#ping(
151        &self,
152        mut ping: &str,
153    ) -> fidl::client::QueryResponseFut<String, fidl::encoding::DefaultFuchsiaResourceDialect> {
154        PingProxyInterface::r#ping(self, ping)
155    }
156}
157
158impl PingProxyInterface for PingProxy {
159    type PingResponseFut =
160        fidl::client::QueryResponseFut<String, fidl::encoding::DefaultFuchsiaResourceDialect>;
161    fn r#ping(&self, mut ping: &str) -> Self::PingResponseFut {
162        fn _decode(
163            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
164        ) -> Result<String, fidl::Error> {
165            let _response = fidl::client::decode_transaction_body::<
166                PingPingResponse,
167                fidl::encoding::DefaultFuchsiaResourceDialect,
168                0x509ea7ec7cd43504,
169            >(_buf?)?;
170            Ok(_response.pong)
171        }
172        self.client.send_query_and_decode::<PingPingRequest, String>(
173            (ping,),
174            0x509ea7ec7cd43504,
175            fidl::encoding::DynamicFlags::empty(),
176            _decode,
177        )
178    }
179}
180
181pub struct PingEventStream {
182    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
183}
184
185impl std::marker::Unpin for PingEventStream {}
186
187impl futures::stream::FusedStream for PingEventStream {
188    fn is_terminated(&self) -> bool {
189        self.event_receiver.is_terminated()
190    }
191}
192
193impl futures::Stream for PingEventStream {
194    type Item = Result<PingEvent, fidl::Error>;
195
196    fn poll_next(
197        mut self: std::pin::Pin<&mut Self>,
198        cx: &mut std::task::Context<'_>,
199    ) -> std::task::Poll<Option<Self::Item>> {
200        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
201            &mut self.event_receiver,
202            cx
203        )?) {
204            Some(buf) => std::task::Poll::Ready(Some(PingEvent::decode(buf))),
205            None => std::task::Poll::Ready(None),
206        }
207    }
208}
209
210#[derive(Debug)]
211pub enum PingEvent {}
212
213impl PingEvent {
214    /// Decodes a message buffer as a [`PingEvent`].
215    fn decode(
216        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
217    ) -> Result<PingEvent, fidl::Error> {
218        let (bytes, _handles) = buf.split_mut();
219        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
220        debug_assert_eq!(tx_header.tx_id, 0);
221        match tx_header.ordinal {
222            _ => Err(fidl::Error::UnknownOrdinal {
223                ordinal: tx_header.ordinal,
224                protocol_name: <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
225            }),
226        }
227    }
228}
229
230/// A Stream of incoming requests for test.ping/Ping.
231pub struct PingRequestStream {
232    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
233    is_terminated: bool,
234}
235
236impl std::marker::Unpin for PingRequestStream {}
237
238impl futures::stream::FusedStream for PingRequestStream {
239    fn is_terminated(&self) -> bool {
240        self.is_terminated
241    }
242}
243
244impl fidl::endpoints::RequestStream for PingRequestStream {
245    type Protocol = PingMarker;
246    type ControlHandle = PingControlHandle;
247
248    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
249        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
250    }
251
252    fn control_handle(&self) -> Self::ControlHandle {
253        PingControlHandle { inner: self.inner.clone() }
254    }
255
256    fn into_inner(
257        self,
258    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
259    {
260        (self.inner, self.is_terminated)
261    }
262
263    fn from_inner(
264        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
265        is_terminated: bool,
266    ) -> Self {
267        Self { inner, is_terminated }
268    }
269}
270
271impl futures::Stream for PingRequestStream {
272    type Item = Result<PingRequest, fidl::Error>;
273
274    fn poll_next(
275        mut self: std::pin::Pin<&mut Self>,
276        cx: &mut std::task::Context<'_>,
277    ) -> std::task::Poll<Option<Self::Item>> {
278        let this = &mut *self;
279        if this.inner.check_shutdown(cx) {
280            this.is_terminated = true;
281            return std::task::Poll::Ready(None);
282        }
283        if this.is_terminated {
284            panic!("polled PingRequestStream after completion");
285        }
286        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
287            |bytes, handles| {
288                match this.inner.channel().read_etc(cx, bytes, handles) {
289                    std::task::Poll::Ready(Ok(())) => {}
290                    std::task::Poll::Pending => return std::task::Poll::Pending,
291                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
292                        this.is_terminated = true;
293                        return std::task::Poll::Ready(None);
294                    }
295                    std::task::Poll::Ready(Err(e)) => {
296                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
297                            e.into(),
298                        ))));
299                    }
300                }
301
302                // A message has been received from the channel
303                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
304
305                std::task::Poll::Ready(Some(match header.ordinal {
306                    0x509ea7ec7cd43504 => {
307                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
308                        let mut req = fidl::new_empty!(
309                            PingPingRequest,
310                            fidl::encoding::DefaultFuchsiaResourceDialect
311                        );
312                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<PingPingRequest>(&header, _body_bytes, handles, &mut req)?;
313                        let control_handle = PingControlHandle { inner: this.inner.clone() };
314                        Ok(PingRequest::Ping {
315                            ping: req.ping,
316
317                            responder: PingPingResponder {
318                                control_handle: std::mem::ManuallyDrop::new(control_handle),
319                                tx_id: header.tx_id,
320                            },
321                        })
322                    }
323                    _ => Err(fidl::Error::UnknownOrdinal {
324                        ordinal: header.ordinal,
325                        protocol_name: <PingMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
326                    }),
327                }))
328            },
329        )
330    }
331}
332
333#[derive(Debug)]
334pub enum PingRequest {
335    /// Returns a string that is the concatenation of the input `ping`
336    /// a space and the string "pong".
337    Ping { ping: String, responder: PingPingResponder },
338}
339
340impl PingRequest {
341    #[allow(irrefutable_let_patterns)]
342    pub fn into_ping(self) -> Option<(String, PingPingResponder)> {
343        if let PingRequest::Ping { ping, responder } = self {
344            Some((ping, responder))
345        } else {
346            None
347        }
348    }
349
350    /// Name of the method defined in FIDL
351    pub fn method_name(&self) -> &'static str {
352        match *self {
353            PingRequest::Ping { .. } => "ping",
354        }
355    }
356}
357
358#[derive(Debug, Clone)]
359pub struct PingControlHandle {
360    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
361}
362
363impl fidl::endpoints::ControlHandle for PingControlHandle {
364    fn shutdown(&self) {
365        self.inner.shutdown()
366    }
367
368    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
369        self.inner.shutdown_with_epitaph(status)
370    }
371
372    fn is_closed(&self) -> bool {
373        self.inner.channel().is_closed()
374    }
375    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
376        self.inner.channel().on_closed()
377    }
378
379    #[cfg(target_os = "fuchsia")]
380    fn signal_peer(
381        &self,
382        clear_mask: zx::Signals,
383        set_mask: zx::Signals,
384    ) -> Result<(), zx_status::Status> {
385        use fidl::Peered;
386        self.inner.channel().signal_peer(clear_mask, set_mask)
387    }
388}
389
390impl PingControlHandle {}
391
392#[must_use = "FIDL methods require a response to be sent"]
393#[derive(Debug)]
394pub struct PingPingResponder {
395    control_handle: std::mem::ManuallyDrop<PingControlHandle>,
396    tx_id: u32,
397}
398
399/// Set the the channel to be shutdown (see [`PingControlHandle::shutdown`])
400/// if the responder is dropped without sending a response, so that the client
401/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
402impl std::ops::Drop for PingPingResponder {
403    fn drop(&mut self) {
404        self.control_handle.shutdown();
405        // Safety: drops once, never accessed again
406        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
407    }
408}
409
410impl fidl::endpoints::Responder for PingPingResponder {
411    type ControlHandle = PingControlHandle;
412
413    fn control_handle(&self) -> &PingControlHandle {
414        &self.control_handle
415    }
416
417    fn drop_without_shutdown(mut self) {
418        // Safety: drops once, never accessed again due to mem::forget
419        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
420        // Prevent Drop from running (which would shut down the channel)
421        std::mem::forget(self);
422    }
423}
424
425impl PingPingResponder {
426    /// Sends a response to the FIDL transaction.
427    ///
428    /// Sets the channel to shutdown if an error occurs.
429    pub fn send(self, mut pong: &str) -> Result<(), fidl::Error> {
430        let _result = self.send_raw(pong);
431        if _result.is_err() {
432            self.control_handle.shutdown();
433        }
434        self.drop_without_shutdown();
435        _result
436    }
437
438    /// Similar to "send" but does not shutdown the channel if an error occurs.
439    pub fn send_no_shutdown_on_err(self, mut pong: &str) -> Result<(), fidl::Error> {
440        let _result = self.send_raw(pong);
441        self.drop_without_shutdown();
442        _result
443    }
444
445    fn send_raw(&self, mut pong: &str) -> Result<(), fidl::Error> {
446        self.control_handle.inner.send::<PingPingResponse>(
447            (pong,),
448            self.tx_id,
449            0x509ea7ec7cd43504,
450            fidl::encoding::DynamicFlags::empty(),
451        )
452    }
453}
454
455mod internal {
456    use super::*;
457}