fidl_fuchsia_diagnostics_crasher/
fidl_fuchsia_diagnostics_crasher.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_fuchsia_diagnostics_crasher_common::*;
11use futures::future::{self, MaybeDone, TryFutureExt};
12use zx_status;
13
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
15pub struct CrasherMarker;
16
17impl fidl::endpoints::ProtocolMarker for CrasherMarker {
18    type Proxy = CrasherProxy;
19    type RequestStream = CrasherRequestStream;
20    #[cfg(target_os = "fuchsia")]
21    type SynchronousProxy = CrasherSynchronousProxy;
22
23    const DEBUG_NAME: &'static str = "fuchsia.diagnostics.crasher.Crasher";
24}
25impl fidl::endpoints::DiscoverableProtocolMarker for CrasherMarker {}
26
27pub trait CrasherProxyInterface: Send + Sync {
28    type CrashResponseFut: std::future::Future<Output = Result<(), fidl::Error>> + Send;
29    fn r#crash(&self, message: &str) -> Self::CrashResponseFut;
30}
31#[derive(Debug)]
32#[cfg(target_os = "fuchsia")]
33pub struct CrasherSynchronousProxy {
34    client: fidl::client::sync::Client,
35}
36
37#[cfg(target_os = "fuchsia")]
38impl fidl::endpoints::SynchronousProxy for CrasherSynchronousProxy {
39    type Proxy = CrasherProxy;
40    type Protocol = CrasherMarker;
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 CrasherSynchronousProxy {
57    pub fn new(channel: fidl::Channel) -> Self {
58        let protocol_name = <CrasherMarker 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(
69        &self,
70        deadline: zx::MonotonicInstant,
71    ) -> Result<CrasherEvent, fidl::Error> {
72        CrasherEvent::decode(self.client.wait_for_event(deadline)?)
73    }
74
75    /// Crashes the component with the specified message.
76    pub fn r#crash(
77        &self,
78        mut message: &str,
79        ___deadline: zx::MonotonicInstant,
80    ) -> Result<(), fidl::Error> {
81        let _response = self.client.send_query::<
82            CrasherCrashRequest,
83            fidl::encoding::FlexibleType<fidl::encoding::EmptyStruct>,
84        >(
85            (message,),
86            0x2dc9841434f9abda,
87            fidl::encoding::DynamicFlags::FLEXIBLE,
88            ___deadline,
89        )?
90        .into_result::<CrasherMarker>("crash")?;
91        Ok(_response)
92    }
93}
94
95#[derive(Debug, Clone)]
96pub struct CrasherProxy {
97    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
98}
99
100impl fidl::endpoints::Proxy for CrasherProxy {
101    type Protocol = CrasherMarker;
102
103    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
104        Self::new(inner)
105    }
106
107    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
108        self.client.into_channel().map_err(|client| Self { client })
109    }
110
111    fn as_channel(&self) -> &::fidl::AsyncChannel {
112        self.client.as_channel()
113    }
114}
115
116impl CrasherProxy {
117    /// Create a new Proxy for fuchsia.diagnostics.crasher/Crasher.
118    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
119        let protocol_name = <CrasherMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
120        Self { client: fidl::client::Client::new(channel, protocol_name) }
121    }
122
123    /// Get a Stream of events from the remote end of the protocol.
124    ///
125    /// # Panics
126    ///
127    /// Panics if the event stream was already taken.
128    pub fn take_event_stream(&self) -> CrasherEventStream {
129        CrasherEventStream { event_receiver: self.client.take_event_receiver() }
130    }
131
132    /// Crashes the component with the specified message.
133    pub fn r#crash(
134        &self,
135        mut message: &str,
136    ) -> fidl::client::QueryResponseFut<(), fidl::encoding::DefaultFuchsiaResourceDialect> {
137        CrasherProxyInterface::r#crash(self, message)
138    }
139}
140
141impl CrasherProxyInterface for CrasherProxy {
142    type CrashResponseFut =
143        fidl::client::QueryResponseFut<(), fidl::encoding::DefaultFuchsiaResourceDialect>;
144    fn r#crash(&self, mut message: &str) -> Self::CrashResponseFut {
145        fn _decode(
146            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
147        ) -> Result<(), fidl::Error> {
148            let _response = fidl::client::decode_transaction_body::<
149                fidl::encoding::FlexibleType<fidl::encoding::EmptyStruct>,
150                fidl::encoding::DefaultFuchsiaResourceDialect,
151                0x2dc9841434f9abda,
152            >(_buf?)?
153            .into_result::<CrasherMarker>("crash")?;
154            Ok(_response)
155        }
156        self.client.send_query_and_decode::<CrasherCrashRequest, ()>(
157            (message,),
158            0x2dc9841434f9abda,
159            fidl::encoding::DynamicFlags::FLEXIBLE,
160            _decode,
161        )
162    }
163}
164
165pub struct CrasherEventStream {
166    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
167}
168
169impl std::marker::Unpin for CrasherEventStream {}
170
171impl futures::stream::FusedStream for CrasherEventStream {
172    fn is_terminated(&self) -> bool {
173        self.event_receiver.is_terminated()
174    }
175}
176
177impl futures::Stream for CrasherEventStream {
178    type Item = Result<CrasherEvent, fidl::Error>;
179
180    fn poll_next(
181        mut self: std::pin::Pin<&mut Self>,
182        cx: &mut std::task::Context<'_>,
183    ) -> std::task::Poll<Option<Self::Item>> {
184        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
185            &mut self.event_receiver,
186            cx
187        )?) {
188            Some(buf) => std::task::Poll::Ready(Some(CrasherEvent::decode(buf))),
189            None => std::task::Poll::Ready(None),
190        }
191    }
192}
193
194#[derive(Debug)]
195pub enum CrasherEvent {
196    #[non_exhaustive]
197    _UnknownEvent {
198        /// Ordinal of the event that was sent.
199        ordinal: u64,
200    },
201}
202
203impl CrasherEvent {
204    /// Decodes a message buffer as a [`CrasherEvent`].
205    fn decode(
206        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
207    ) -> Result<CrasherEvent, fidl::Error> {
208        let (bytes, _handles) = buf.split_mut();
209        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
210        debug_assert_eq!(tx_header.tx_id, 0);
211        match tx_header.ordinal {
212            _ if tx_header.dynamic_flags().contains(fidl::encoding::DynamicFlags::FLEXIBLE) => {
213                Ok(CrasherEvent::_UnknownEvent { ordinal: tx_header.ordinal })
214            }
215            _ => Err(fidl::Error::UnknownOrdinal {
216                ordinal: tx_header.ordinal,
217                protocol_name: <CrasherMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
218            }),
219        }
220    }
221}
222
223/// A Stream of incoming requests for fuchsia.diagnostics.crasher/Crasher.
224pub struct CrasherRequestStream {
225    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
226    is_terminated: bool,
227}
228
229impl std::marker::Unpin for CrasherRequestStream {}
230
231impl futures::stream::FusedStream for CrasherRequestStream {
232    fn is_terminated(&self) -> bool {
233        self.is_terminated
234    }
235}
236
237impl fidl::endpoints::RequestStream for CrasherRequestStream {
238    type Protocol = CrasherMarker;
239    type ControlHandle = CrasherControlHandle;
240
241    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
242        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
243    }
244
245    fn control_handle(&self) -> Self::ControlHandle {
246        CrasherControlHandle { inner: self.inner.clone() }
247    }
248
249    fn into_inner(
250        self,
251    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
252    {
253        (self.inner, self.is_terminated)
254    }
255
256    fn from_inner(
257        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
258        is_terminated: bool,
259    ) -> Self {
260        Self { inner, is_terminated }
261    }
262}
263
264impl futures::Stream for CrasherRequestStream {
265    type Item = Result<CrasherRequest, fidl::Error>;
266
267    fn poll_next(
268        mut self: std::pin::Pin<&mut Self>,
269        cx: &mut std::task::Context<'_>,
270    ) -> std::task::Poll<Option<Self::Item>> {
271        let this = &mut *self;
272        if this.inner.check_shutdown(cx) {
273            this.is_terminated = true;
274            return std::task::Poll::Ready(None);
275        }
276        if this.is_terminated {
277            panic!("polled CrasherRequestStream after completion");
278        }
279        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
280            |bytes, handles| {
281                match this.inner.channel().read_etc(cx, bytes, handles) {
282                    std::task::Poll::Ready(Ok(())) => {}
283                    std::task::Poll::Pending => return std::task::Poll::Pending,
284                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
285                        this.is_terminated = true;
286                        return std::task::Poll::Ready(None);
287                    }
288                    std::task::Poll::Ready(Err(e)) => {
289                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
290                            e.into(),
291                        ))))
292                    }
293                }
294
295                // A message has been received from the channel
296                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
297
298                std::task::Poll::Ready(Some(match header.ordinal {
299                    0x2dc9841434f9abda => {
300                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
301                        let mut req = fidl::new_empty!(
302                            CrasherCrashRequest,
303                            fidl::encoding::DefaultFuchsiaResourceDialect
304                        );
305                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<CrasherCrashRequest>(&header, _body_bytes, handles, &mut req)?;
306                        let control_handle = CrasherControlHandle { inner: this.inner.clone() };
307                        Ok(CrasherRequest::Crash {
308                            message: req.message,
309
310                            responder: CrasherCrashResponder {
311                                control_handle: std::mem::ManuallyDrop::new(control_handle),
312                                tx_id: header.tx_id,
313                            },
314                        })
315                    }
316                    _ if header.tx_id == 0
317                        && header
318                            .dynamic_flags()
319                            .contains(fidl::encoding::DynamicFlags::FLEXIBLE) =>
320                    {
321                        Ok(CrasherRequest::_UnknownMethod {
322                            ordinal: header.ordinal,
323                            control_handle: CrasherControlHandle { inner: this.inner.clone() },
324                            method_type: fidl::MethodType::OneWay,
325                        })
326                    }
327                    _ if header
328                        .dynamic_flags()
329                        .contains(fidl::encoding::DynamicFlags::FLEXIBLE) =>
330                    {
331                        this.inner.send_framework_err(
332                            fidl::encoding::FrameworkErr::UnknownMethod,
333                            header.tx_id,
334                            header.ordinal,
335                            header.dynamic_flags(),
336                            (bytes, handles),
337                        )?;
338                        Ok(CrasherRequest::_UnknownMethod {
339                            ordinal: header.ordinal,
340                            control_handle: CrasherControlHandle { inner: this.inner.clone() },
341                            method_type: fidl::MethodType::TwoWay,
342                        })
343                    }
344                    _ => Err(fidl::Error::UnknownOrdinal {
345                        ordinal: header.ordinal,
346                        protocol_name:
347                            <CrasherMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
348                    }),
349                }))
350            },
351        )
352    }
353}
354
355#[derive(Debug)]
356pub enum CrasherRequest {
357    /// Crashes the component with the specified message.
358    Crash { message: String, responder: CrasherCrashResponder },
359    /// An interaction was received which does not match any known method.
360    #[non_exhaustive]
361    _UnknownMethod {
362        /// Ordinal of the method that was called.
363        ordinal: u64,
364        control_handle: CrasherControlHandle,
365        method_type: fidl::MethodType,
366    },
367}
368
369impl CrasherRequest {
370    #[allow(irrefutable_let_patterns)]
371    pub fn into_crash(self) -> Option<(String, CrasherCrashResponder)> {
372        if let CrasherRequest::Crash { message, responder } = self {
373            Some((message, responder))
374        } else {
375            None
376        }
377    }
378
379    /// Name of the method defined in FIDL
380    pub fn method_name(&self) -> &'static str {
381        match *self {
382            CrasherRequest::Crash { .. } => "crash",
383            CrasherRequest::_UnknownMethod { method_type: fidl::MethodType::OneWay, .. } => {
384                "unknown one-way method"
385            }
386            CrasherRequest::_UnknownMethod { method_type: fidl::MethodType::TwoWay, .. } => {
387                "unknown two-way method"
388            }
389        }
390    }
391}
392
393#[derive(Debug, Clone)]
394pub struct CrasherControlHandle {
395    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
396}
397
398impl fidl::endpoints::ControlHandle for CrasherControlHandle {
399    fn shutdown(&self) {
400        self.inner.shutdown()
401    }
402    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
403        self.inner.shutdown_with_epitaph(status)
404    }
405
406    fn is_closed(&self) -> bool {
407        self.inner.channel().is_closed()
408    }
409    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
410        self.inner.channel().on_closed()
411    }
412
413    #[cfg(target_os = "fuchsia")]
414    fn signal_peer(
415        &self,
416        clear_mask: zx::Signals,
417        set_mask: zx::Signals,
418    ) -> Result<(), zx_status::Status> {
419        use fidl::Peered;
420        self.inner.channel().signal_peer(clear_mask, set_mask)
421    }
422}
423
424impl CrasherControlHandle {}
425
426#[must_use = "FIDL methods require a response to be sent"]
427#[derive(Debug)]
428pub struct CrasherCrashResponder {
429    control_handle: std::mem::ManuallyDrop<CrasherControlHandle>,
430    tx_id: u32,
431}
432
433/// Set the the channel to be shutdown (see [`CrasherControlHandle::shutdown`])
434/// if the responder is dropped without sending a response, so that the client
435/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
436impl std::ops::Drop for CrasherCrashResponder {
437    fn drop(&mut self) {
438        self.control_handle.shutdown();
439        // Safety: drops once, never accessed again
440        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
441    }
442}
443
444impl fidl::endpoints::Responder for CrasherCrashResponder {
445    type ControlHandle = CrasherControlHandle;
446
447    fn control_handle(&self) -> &CrasherControlHandle {
448        &self.control_handle
449    }
450
451    fn drop_without_shutdown(mut self) {
452        // Safety: drops once, never accessed again due to mem::forget
453        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
454        // Prevent Drop from running (which would shut down the channel)
455        std::mem::forget(self);
456    }
457}
458
459impl CrasherCrashResponder {
460    /// Sends a response to the FIDL transaction.
461    ///
462    /// Sets the channel to shutdown if an error occurs.
463    pub fn send(self) -> Result<(), fidl::Error> {
464        let _result = self.send_raw();
465        if _result.is_err() {
466            self.control_handle.shutdown();
467        }
468        self.drop_without_shutdown();
469        _result
470    }
471
472    /// Similar to "send" but does not shutdown the channel if an error occurs.
473    pub fn send_no_shutdown_on_err(self) -> Result<(), fidl::Error> {
474        let _result = self.send_raw();
475        self.drop_without_shutdown();
476        _result
477    }
478
479    fn send_raw(&self) -> Result<(), fidl::Error> {
480        self.control_handle.inner.send::<fidl::encoding::FlexibleType<fidl::encoding::EmptyStruct>>(
481            fidl::encoding::Flexible::new(()),
482            self.tx_id,
483            0x2dc9841434f9abda,
484            fidl::encoding::DynamicFlags::FLEXIBLE,
485        )
486    }
487}
488
489mod internal {
490    use super::*;
491}