fidl_test_fidl_connector/
fidl_test_fidl_connector.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 _};
10use futures::future::{self, MaybeDone, TryFutureExt};
11use zx_status;
12
13#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
14#[repr(C)]
15pub struct TestPingResponse {
16    pub gen: u32,
17}
18
19impl fidl::Persistable for TestPingResponse {}
20
21#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
22pub struct TestMarker;
23
24impl fidl::endpoints::ProtocolMarker for TestMarker {
25    type Proxy = TestProxy;
26    type RequestStream = TestRequestStream;
27    #[cfg(target_os = "fuchsia")]
28    type SynchronousProxy = TestSynchronousProxy;
29
30    const DEBUG_NAME: &'static str = "test.fidl.connector.Test";
31}
32impl fidl::endpoints::DiscoverableProtocolMarker for TestMarker {}
33
34pub trait TestProxyInterface: Send + Sync {
35    type PingResponseFut: std::future::Future<Output = Result<u32, fidl::Error>> + Send;
36    fn r#ping(&self) -> Self::PingResponseFut;
37    type DisconnectResponseFut: std::future::Future<Output = Result<(), fidl::Error>> + Send;
38    fn r#disconnect(&self) -> Self::DisconnectResponseFut;
39}
40#[derive(Debug)]
41#[cfg(target_os = "fuchsia")]
42pub struct TestSynchronousProxy {
43    client: fidl::client::sync::Client,
44}
45
46#[cfg(target_os = "fuchsia")]
47impl fidl::endpoints::SynchronousProxy for TestSynchronousProxy {
48    type Proxy = TestProxy;
49    type Protocol = TestMarker;
50
51    fn from_channel(inner: fidl::Channel) -> Self {
52        Self::new(inner)
53    }
54
55    fn into_channel(self) -> fidl::Channel {
56        self.client.into_channel()
57    }
58
59    fn as_channel(&self) -> &fidl::Channel {
60        self.client.as_channel()
61    }
62}
63
64#[cfg(target_os = "fuchsia")]
65impl TestSynchronousProxy {
66    pub fn new(channel: fidl::Channel) -> Self {
67        let protocol_name = <TestMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
68        Self { client: fidl::client::sync::Client::new(channel, protocol_name) }
69    }
70
71    pub fn into_channel(self) -> fidl::Channel {
72        self.client.into_channel()
73    }
74
75    /// Waits until an event arrives and returns it. It is safe for other
76    /// threads to make concurrent requests while waiting for an event.
77    pub fn wait_for_event(&self, deadline: zx::MonotonicInstant) -> Result<TestEvent, fidl::Error> {
78        TestEvent::decode(self.client.wait_for_event(deadline)?)
79    }
80
81    pub fn r#ping(&self, ___deadline: zx::MonotonicInstant) -> Result<u32, fidl::Error> {
82        let _response = self.client.send_query::<fidl::encoding::EmptyPayload, TestPingResponse>(
83            (),
84            0x23aa1eb7d24346cf,
85            fidl::encoding::DynamicFlags::empty(),
86            ___deadline,
87        )?;
88        Ok(_response.gen)
89    }
90
91    pub fn r#disconnect(&self, ___deadline: zx::MonotonicInstant) -> Result<(), fidl::Error> {
92        let _response =
93            self.client.send_query::<fidl::encoding::EmptyPayload, fidl::encoding::EmptyPayload>(
94                (),
95                0x5a1b547382bf672e,
96                fidl::encoding::DynamicFlags::empty(),
97                ___deadline,
98            )?;
99        Ok(_response)
100    }
101}
102
103#[derive(Debug, Clone)]
104pub struct TestProxy {
105    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
106}
107
108impl fidl::endpoints::Proxy for TestProxy {
109    type Protocol = TestMarker;
110
111    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
112        Self::new(inner)
113    }
114
115    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
116        self.client.into_channel().map_err(|client| Self { client })
117    }
118
119    fn as_channel(&self) -> &::fidl::AsyncChannel {
120        self.client.as_channel()
121    }
122}
123
124impl TestProxy {
125    /// Create a new Proxy for test.fidl.connector/Test.
126    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
127        let protocol_name = <TestMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
128        Self { client: fidl::client::Client::new(channel, protocol_name) }
129    }
130
131    /// Get a Stream of events from the remote end of the protocol.
132    ///
133    /// # Panics
134    ///
135    /// Panics if the event stream was already taken.
136    pub fn take_event_stream(&self) -> TestEventStream {
137        TestEventStream { event_receiver: self.client.take_event_receiver() }
138    }
139
140    pub fn r#ping(
141        &self,
142    ) -> fidl::client::QueryResponseFut<u32, fidl::encoding::DefaultFuchsiaResourceDialect> {
143        TestProxyInterface::r#ping(self)
144    }
145
146    pub fn r#disconnect(
147        &self,
148    ) -> fidl::client::QueryResponseFut<(), fidl::encoding::DefaultFuchsiaResourceDialect> {
149        TestProxyInterface::r#disconnect(self)
150    }
151}
152
153impl TestProxyInterface for TestProxy {
154    type PingResponseFut =
155        fidl::client::QueryResponseFut<u32, fidl::encoding::DefaultFuchsiaResourceDialect>;
156    fn r#ping(&self) -> Self::PingResponseFut {
157        fn _decode(
158            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
159        ) -> Result<u32, fidl::Error> {
160            let _response = fidl::client::decode_transaction_body::<
161                TestPingResponse,
162                fidl::encoding::DefaultFuchsiaResourceDialect,
163                0x23aa1eb7d24346cf,
164            >(_buf?)?;
165            Ok(_response.gen)
166        }
167        self.client.send_query_and_decode::<fidl::encoding::EmptyPayload, u32>(
168            (),
169            0x23aa1eb7d24346cf,
170            fidl::encoding::DynamicFlags::empty(),
171            _decode,
172        )
173    }
174
175    type DisconnectResponseFut =
176        fidl::client::QueryResponseFut<(), fidl::encoding::DefaultFuchsiaResourceDialect>;
177    fn r#disconnect(&self) -> Self::DisconnectResponseFut {
178        fn _decode(
179            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
180        ) -> Result<(), fidl::Error> {
181            let _response = fidl::client::decode_transaction_body::<
182                fidl::encoding::EmptyPayload,
183                fidl::encoding::DefaultFuchsiaResourceDialect,
184                0x5a1b547382bf672e,
185            >(_buf?)?;
186            Ok(_response)
187        }
188        self.client.send_query_and_decode::<fidl::encoding::EmptyPayload, ()>(
189            (),
190            0x5a1b547382bf672e,
191            fidl::encoding::DynamicFlags::empty(),
192            _decode,
193        )
194    }
195}
196
197pub struct TestEventStream {
198    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
199}
200
201impl std::marker::Unpin for TestEventStream {}
202
203impl futures::stream::FusedStream for TestEventStream {
204    fn is_terminated(&self) -> bool {
205        self.event_receiver.is_terminated()
206    }
207}
208
209impl futures::Stream for TestEventStream {
210    type Item = Result<TestEvent, fidl::Error>;
211
212    fn poll_next(
213        mut self: std::pin::Pin<&mut Self>,
214        cx: &mut std::task::Context<'_>,
215    ) -> std::task::Poll<Option<Self::Item>> {
216        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
217            &mut self.event_receiver,
218            cx
219        )?) {
220            Some(buf) => std::task::Poll::Ready(Some(TestEvent::decode(buf))),
221            None => std::task::Poll::Ready(None),
222        }
223    }
224}
225
226#[derive(Debug)]
227pub enum TestEvent {}
228
229impl TestEvent {
230    /// Decodes a message buffer as a [`TestEvent`].
231    fn decode(
232        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
233    ) -> Result<TestEvent, fidl::Error> {
234        let (bytes, _handles) = buf.split_mut();
235        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
236        debug_assert_eq!(tx_header.tx_id, 0);
237        match tx_header.ordinal {
238            _ => Err(fidl::Error::UnknownOrdinal {
239                ordinal: tx_header.ordinal,
240                protocol_name: <TestMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
241            }),
242        }
243    }
244}
245
246/// A Stream of incoming requests for test.fidl.connector/Test.
247pub struct TestRequestStream {
248    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
249    is_terminated: bool,
250}
251
252impl std::marker::Unpin for TestRequestStream {}
253
254impl futures::stream::FusedStream for TestRequestStream {
255    fn is_terminated(&self) -> bool {
256        self.is_terminated
257    }
258}
259
260impl fidl::endpoints::RequestStream for TestRequestStream {
261    type Protocol = TestMarker;
262    type ControlHandle = TestControlHandle;
263
264    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
265        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
266    }
267
268    fn control_handle(&self) -> Self::ControlHandle {
269        TestControlHandle { inner: self.inner.clone() }
270    }
271
272    fn into_inner(
273        self,
274    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
275    {
276        (self.inner, self.is_terminated)
277    }
278
279    fn from_inner(
280        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
281        is_terminated: bool,
282    ) -> Self {
283        Self { inner, is_terminated }
284    }
285}
286
287impl futures::Stream for TestRequestStream {
288    type Item = Result<TestRequest, fidl::Error>;
289
290    fn poll_next(
291        mut self: std::pin::Pin<&mut Self>,
292        cx: &mut std::task::Context<'_>,
293    ) -> std::task::Poll<Option<Self::Item>> {
294        let this = &mut *self;
295        if this.inner.check_shutdown(cx) {
296            this.is_terminated = true;
297            return std::task::Poll::Ready(None);
298        }
299        if this.is_terminated {
300            panic!("polled TestRequestStream after completion");
301        }
302        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
303            |bytes, handles| {
304                match this.inner.channel().read_etc(cx, bytes, handles) {
305                    std::task::Poll::Ready(Ok(())) => {}
306                    std::task::Poll::Pending => return std::task::Poll::Pending,
307                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
308                        this.is_terminated = true;
309                        return std::task::Poll::Ready(None);
310                    }
311                    std::task::Poll::Ready(Err(e)) => {
312                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
313                            e.into(),
314                        ))))
315                    }
316                }
317
318                // A message has been received from the channel
319                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
320
321                std::task::Poll::Ready(Some(match header.ordinal {
322                    0x23aa1eb7d24346cf => {
323                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
324                        let mut req = fidl::new_empty!(
325                            fidl::encoding::EmptyPayload,
326                            fidl::encoding::DefaultFuchsiaResourceDialect
327                        );
328                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<fidl::encoding::EmptyPayload>(&header, _body_bytes, handles, &mut req)?;
329                        let control_handle = TestControlHandle { inner: this.inner.clone() };
330                        Ok(TestRequest::Ping {
331                            responder: TestPingResponder {
332                                control_handle: std::mem::ManuallyDrop::new(control_handle),
333                                tx_id: header.tx_id,
334                            },
335                        })
336                    }
337                    0x5a1b547382bf672e => {
338                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
339                        let mut req = fidl::new_empty!(
340                            fidl::encoding::EmptyPayload,
341                            fidl::encoding::DefaultFuchsiaResourceDialect
342                        );
343                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<fidl::encoding::EmptyPayload>(&header, _body_bytes, handles, &mut req)?;
344                        let control_handle = TestControlHandle { inner: this.inner.clone() };
345                        Ok(TestRequest::Disconnect {
346                            responder: TestDisconnectResponder {
347                                control_handle: std::mem::ManuallyDrop::new(control_handle),
348                                tx_id: header.tx_id,
349                            },
350                        })
351                    }
352                    _ => Err(fidl::Error::UnknownOrdinal {
353                        ordinal: header.ordinal,
354                        protocol_name: <TestMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
355                    }),
356                }))
357            },
358        )
359    }
360}
361
362#[derive(Debug)]
363pub enum TestRequest {
364    Ping { responder: TestPingResponder },
365    Disconnect { responder: TestDisconnectResponder },
366}
367
368impl TestRequest {
369    #[allow(irrefutable_let_patterns)]
370    pub fn into_ping(self) -> Option<(TestPingResponder)> {
371        if let TestRequest::Ping { responder } = self {
372            Some((responder))
373        } else {
374            None
375        }
376    }
377
378    #[allow(irrefutable_let_patterns)]
379    pub fn into_disconnect(self) -> Option<(TestDisconnectResponder)> {
380        if let TestRequest::Disconnect { responder } = self {
381            Some((responder))
382        } else {
383            None
384        }
385    }
386
387    /// Name of the method defined in FIDL
388    pub fn method_name(&self) -> &'static str {
389        match *self {
390            TestRequest::Ping { .. } => "ping",
391            TestRequest::Disconnect { .. } => "disconnect",
392        }
393    }
394}
395
396#[derive(Debug, Clone)]
397pub struct TestControlHandle {
398    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
399}
400
401impl fidl::endpoints::ControlHandle for TestControlHandle {
402    fn shutdown(&self) {
403        self.inner.shutdown()
404    }
405    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
406        self.inner.shutdown_with_epitaph(status)
407    }
408
409    fn is_closed(&self) -> bool {
410        self.inner.channel().is_closed()
411    }
412    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
413        self.inner.channel().on_closed()
414    }
415
416    #[cfg(target_os = "fuchsia")]
417    fn signal_peer(
418        &self,
419        clear_mask: zx::Signals,
420        set_mask: zx::Signals,
421    ) -> Result<(), zx_status::Status> {
422        use fidl::Peered;
423        self.inner.channel().signal_peer(clear_mask, set_mask)
424    }
425}
426
427impl TestControlHandle {}
428
429#[must_use = "FIDL methods require a response to be sent"]
430#[derive(Debug)]
431pub struct TestPingResponder {
432    control_handle: std::mem::ManuallyDrop<TestControlHandle>,
433    tx_id: u32,
434}
435
436/// Set the the channel to be shutdown (see [`TestControlHandle::shutdown`])
437/// if the responder is dropped without sending a response, so that the client
438/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
439impl std::ops::Drop for TestPingResponder {
440    fn drop(&mut self) {
441        self.control_handle.shutdown();
442        // Safety: drops once, never accessed again
443        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
444    }
445}
446
447impl fidl::endpoints::Responder for TestPingResponder {
448    type ControlHandle = TestControlHandle;
449
450    fn control_handle(&self) -> &TestControlHandle {
451        &self.control_handle
452    }
453
454    fn drop_without_shutdown(mut self) {
455        // Safety: drops once, never accessed again due to mem::forget
456        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
457        // Prevent Drop from running (which would shut down the channel)
458        std::mem::forget(self);
459    }
460}
461
462impl TestPingResponder {
463    /// Sends a response to the FIDL transaction.
464    ///
465    /// Sets the channel to shutdown if an error occurs.
466    pub fn send(self, mut gen: u32) -> Result<(), fidl::Error> {
467        let _result = self.send_raw(gen);
468        if _result.is_err() {
469            self.control_handle.shutdown();
470        }
471        self.drop_without_shutdown();
472        _result
473    }
474
475    /// Similar to "send" but does not shutdown the channel if an error occurs.
476    pub fn send_no_shutdown_on_err(self, mut gen: u32) -> Result<(), fidl::Error> {
477        let _result = self.send_raw(gen);
478        self.drop_without_shutdown();
479        _result
480    }
481
482    fn send_raw(&self, mut gen: u32) -> Result<(), fidl::Error> {
483        self.control_handle.inner.send::<TestPingResponse>(
484            (gen,),
485            self.tx_id,
486            0x23aa1eb7d24346cf,
487            fidl::encoding::DynamicFlags::empty(),
488        )
489    }
490}
491
492#[must_use = "FIDL methods require a response to be sent"]
493#[derive(Debug)]
494pub struct TestDisconnectResponder {
495    control_handle: std::mem::ManuallyDrop<TestControlHandle>,
496    tx_id: u32,
497}
498
499/// Set the the channel to be shutdown (see [`TestControlHandle::shutdown`])
500/// if the responder is dropped without sending a response, so that the client
501/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
502impl std::ops::Drop for TestDisconnectResponder {
503    fn drop(&mut self) {
504        self.control_handle.shutdown();
505        // Safety: drops once, never accessed again
506        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
507    }
508}
509
510impl fidl::endpoints::Responder for TestDisconnectResponder {
511    type ControlHandle = TestControlHandle;
512
513    fn control_handle(&self) -> &TestControlHandle {
514        &self.control_handle
515    }
516
517    fn drop_without_shutdown(mut self) {
518        // Safety: drops once, never accessed again due to mem::forget
519        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
520        // Prevent Drop from running (which would shut down the channel)
521        std::mem::forget(self);
522    }
523}
524
525impl TestDisconnectResponder {
526    /// Sends a response to the FIDL transaction.
527    ///
528    /// Sets the channel to shutdown if an error occurs.
529    pub fn send(self) -> Result<(), fidl::Error> {
530        let _result = self.send_raw();
531        if _result.is_err() {
532            self.control_handle.shutdown();
533        }
534        self.drop_without_shutdown();
535        _result
536    }
537
538    /// Similar to "send" but does not shutdown the channel if an error occurs.
539    pub fn send_no_shutdown_on_err(self) -> Result<(), fidl::Error> {
540        let _result = self.send_raw();
541        self.drop_without_shutdown();
542        _result
543    }
544
545    fn send_raw(&self) -> Result<(), fidl::Error> {
546        self.control_handle.inner.send::<fidl::encoding::EmptyPayload>(
547            (),
548            self.tx_id,
549            0x5a1b547382bf672e,
550            fidl::encoding::DynamicFlags::empty(),
551        )
552    }
553}
554
555mod internal {
556    use super::*;
557
558    impl fidl::encoding::ValueTypeMarker for TestPingResponse {
559        type Borrowed<'a> = &'a Self;
560        fn borrow(value: &<Self as fidl::encoding::TypeMarker>::Owned) -> Self::Borrowed<'_> {
561            value
562        }
563    }
564
565    unsafe impl fidl::encoding::TypeMarker for TestPingResponse {
566        type Owned = Self;
567
568        #[inline(always)]
569        fn inline_align(_context: fidl::encoding::Context) -> usize {
570            4
571        }
572
573        #[inline(always)]
574        fn inline_size(_context: fidl::encoding::Context) -> usize {
575            4
576        }
577        #[inline(always)]
578        fn encode_is_copy() -> bool {
579            true
580        }
581
582        #[inline(always)]
583        fn decode_is_copy() -> bool {
584            true
585        }
586    }
587
588    unsafe impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Encode<TestPingResponse, D>
589        for &TestPingResponse
590    {
591        #[inline]
592        unsafe fn encode(
593            self,
594            encoder: &mut fidl::encoding::Encoder<'_, D>,
595            offset: usize,
596            _depth: fidl::encoding::Depth,
597        ) -> fidl::Result<()> {
598            encoder.debug_check_bounds::<TestPingResponse>(offset);
599            unsafe {
600                // Copy the object into the buffer.
601                let buf_ptr = encoder.buf.as_mut_ptr().add(offset);
602                (buf_ptr as *mut TestPingResponse)
603                    .write_unaligned((self as *const TestPingResponse).read());
604                // Zero out padding regions. Unlike `fidl_struct_impl_noncopy!`, this must be
605                // done second because the memcpy will write garbage to these bytes.
606            }
607            Ok(())
608        }
609    }
610    unsafe impl<D: fidl::encoding::ResourceDialect, T0: fidl::encoding::Encode<u32, D>>
611        fidl::encoding::Encode<TestPingResponse, D> for (T0,)
612    {
613        #[inline]
614        unsafe fn encode(
615            self,
616            encoder: &mut fidl::encoding::Encoder<'_, D>,
617            offset: usize,
618            depth: fidl::encoding::Depth,
619        ) -> fidl::Result<()> {
620            encoder.debug_check_bounds::<TestPingResponse>(offset);
621            // Zero out padding regions. There's no need to apply masks
622            // because the unmasked parts will be overwritten by fields.
623            // Write the fields.
624            self.0.encode(encoder, offset + 0, depth)?;
625            Ok(())
626        }
627    }
628
629    impl<D: fidl::encoding::ResourceDialect> fidl::encoding::Decode<Self, D> for TestPingResponse {
630        #[inline(always)]
631        fn new_empty() -> Self {
632            Self { gen: fidl::new_empty!(u32, D) }
633        }
634
635        #[inline]
636        unsafe fn decode(
637            &mut self,
638            decoder: &mut fidl::encoding::Decoder<'_, D>,
639            offset: usize,
640            _depth: fidl::encoding::Depth,
641        ) -> fidl::Result<()> {
642            decoder.debug_check_bounds::<Self>(offset);
643            let buf_ptr = unsafe { decoder.buf.as_ptr().add(offset) };
644            // Verify that padding bytes are zero.
645            // Copy from the buffer into the object.
646            unsafe {
647                std::ptr::copy_nonoverlapping(buf_ptr, self as *mut Self as *mut u8, 4);
648            }
649            Ok(())
650        }
651    }
652}