fidl_fuchsia_lightsensor/
fidl_fuchsia_lightsensor.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_lightsensor_common::*;
11use futures::future::{self, MaybeDone, TryFutureExt};
12use zx_status;
13
14#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
15pub struct CalibratorMarker;
16
17impl fidl::endpoints::ProtocolMarker for CalibratorMarker {
18    type Proxy = CalibratorProxy;
19    type RequestStream = CalibratorRequestStream;
20    #[cfg(target_os = "fuchsia")]
21    type SynchronousProxy = CalibratorSynchronousProxy;
22
23    const DEBUG_NAME: &'static str = "fuchsia.lightsensor.Calibrator";
24}
25impl fidl::endpoints::DiscoverableProtocolMarker for CalibratorMarker {}
26pub type CalibratorCalibrateResult = Result<Rgbc, Error>;
27
28pub trait CalibratorProxyInterface: Send + Sync {
29    type CalibrateResponseFut: std::future::Future<Output = Result<CalibratorCalibrateResult, fidl::Error>>
30        + Send;
31    fn r#calibrate(&self, data: &Rgbc) -> Self::CalibrateResponseFut;
32}
33#[derive(Debug)]
34#[cfg(target_os = "fuchsia")]
35pub struct CalibratorSynchronousProxy {
36    client: fidl::client::sync::Client,
37}
38
39#[cfg(target_os = "fuchsia")]
40impl fidl::endpoints::SynchronousProxy for CalibratorSynchronousProxy {
41    type Proxy = CalibratorProxy;
42    type Protocol = CalibratorMarker;
43
44    fn from_channel(inner: fidl::Channel) -> Self {
45        Self::new(inner)
46    }
47
48    fn into_channel(self) -> fidl::Channel {
49        self.client.into_channel()
50    }
51
52    fn as_channel(&self) -> &fidl::Channel {
53        self.client.as_channel()
54    }
55}
56
57#[cfg(target_os = "fuchsia")]
58impl CalibratorSynchronousProxy {
59    pub fn new(channel: fidl::Channel) -> Self {
60        let protocol_name = <CalibratorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
61        Self { client: fidl::client::sync::Client::new(channel, protocol_name) }
62    }
63
64    pub fn into_channel(self) -> fidl::Channel {
65        self.client.into_channel()
66    }
67
68    /// Waits until an event arrives and returns it. It is safe for other
69    /// threads to make concurrent requests while waiting for an event.
70    pub fn wait_for_event(
71        &self,
72        deadline: zx::MonotonicInstant,
73    ) -> Result<CalibratorEvent, fidl::Error> {
74        CalibratorEvent::decode(self.client.wait_for_event(deadline)?)
75    }
76
77    /// Calibrates the supplied raw [Rgbc] and returns calibrated [Rgbc].
78    pub fn r#calibrate(
79        &self,
80        mut data: &Rgbc,
81        ___deadline: zx::MonotonicInstant,
82    ) -> Result<CalibratorCalibrateResult, fidl::Error> {
83        let _response = self.client.send_query::<
84            CalibratorCalibrateRequest,
85            fidl::encoding::ResultType<CalibratorCalibrateResponse, Error>,
86        >(
87            (data,),
88            0x7ddb7eaf88039b02,
89            fidl::encoding::DynamicFlags::empty(),
90            ___deadline,
91        )?;
92        Ok(_response.map(|x| x.data))
93    }
94}
95
96#[cfg(target_os = "fuchsia")]
97impl From<CalibratorSynchronousProxy> for zx::Handle {
98    fn from(value: CalibratorSynchronousProxy) -> Self {
99        value.into_channel().into()
100    }
101}
102
103#[cfg(target_os = "fuchsia")]
104impl From<fidl::Channel> for CalibratorSynchronousProxy {
105    fn from(value: fidl::Channel) -> Self {
106        Self::new(value)
107    }
108}
109
110#[derive(Debug, Clone)]
111pub struct CalibratorProxy {
112    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
113}
114
115impl fidl::endpoints::Proxy for CalibratorProxy {
116    type Protocol = CalibratorMarker;
117
118    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
119        Self::new(inner)
120    }
121
122    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
123        self.client.into_channel().map_err(|client| Self { client })
124    }
125
126    fn as_channel(&self) -> &::fidl::AsyncChannel {
127        self.client.as_channel()
128    }
129}
130
131impl CalibratorProxy {
132    /// Create a new Proxy for fuchsia.lightsensor/Calibrator.
133    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
134        let protocol_name = <CalibratorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
135        Self { client: fidl::client::Client::new(channel, protocol_name) }
136    }
137
138    /// Get a Stream of events from the remote end of the protocol.
139    ///
140    /// # Panics
141    ///
142    /// Panics if the event stream was already taken.
143    pub fn take_event_stream(&self) -> CalibratorEventStream {
144        CalibratorEventStream { event_receiver: self.client.take_event_receiver() }
145    }
146
147    /// Calibrates the supplied raw [Rgbc] and returns calibrated [Rgbc].
148    pub fn r#calibrate(
149        &self,
150        mut data: &Rgbc,
151    ) -> fidl::client::QueryResponseFut<
152        CalibratorCalibrateResult,
153        fidl::encoding::DefaultFuchsiaResourceDialect,
154    > {
155        CalibratorProxyInterface::r#calibrate(self, data)
156    }
157}
158
159impl CalibratorProxyInterface for CalibratorProxy {
160    type CalibrateResponseFut = fidl::client::QueryResponseFut<
161        CalibratorCalibrateResult,
162        fidl::encoding::DefaultFuchsiaResourceDialect,
163    >;
164    fn r#calibrate(&self, mut data: &Rgbc) -> Self::CalibrateResponseFut {
165        fn _decode(
166            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
167        ) -> Result<CalibratorCalibrateResult, fidl::Error> {
168            let _response = fidl::client::decode_transaction_body::<
169                fidl::encoding::ResultType<CalibratorCalibrateResponse, Error>,
170                fidl::encoding::DefaultFuchsiaResourceDialect,
171                0x7ddb7eaf88039b02,
172            >(_buf?)?;
173            Ok(_response.map(|x| x.data))
174        }
175        self.client.send_query_and_decode::<CalibratorCalibrateRequest, CalibratorCalibrateResult>(
176            (data,),
177            0x7ddb7eaf88039b02,
178            fidl::encoding::DynamicFlags::empty(),
179            _decode,
180        )
181    }
182}
183
184pub struct CalibratorEventStream {
185    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
186}
187
188impl std::marker::Unpin for CalibratorEventStream {}
189
190impl futures::stream::FusedStream for CalibratorEventStream {
191    fn is_terminated(&self) -> bool {
192        self.event_receiver.is_terminated()
193    }
194}
195
196impl futures::Stream for CalibratorEventStream {
197    type Item = Result<CalibratorEvent, fidl::Error>;
198
199    fn poll_next(
200        mut self: std::pin::Pin<&mut Self>,
201        cx: &mut std::task::Context<'_>,
202    ) -> std::task::Poll<Option<Self::Item>> {
203        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
204            &mut self.event_receiver,
205            cx
206        )?) {
207            Some(buf) => std::task::Poll::Ready(Some(CalibratorEvent::decode(buf))),
208            None => std::task::Poll::Ready(None),
209        }
210    }
211}
212
213#[derive(Debug)]
214pub enum CalibratorEvent {}
215
216impl CalibratorEvent {
217    /// Decodes a message buffer as a [`CalibratorEvent`].
218    fn decode(
219        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
220    ) -> Result<CalibratorEvent, fidl::Error> {
221        let (bytes, _handles) = buf.split_mut();
222        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
223        debug_assert_eq!(tx_header.tx_id, 0);
224        match tx_header.ordinal {
225            _ => Err(fidl::Error::UnknownOrdinal {
226                ordinal: tx_header.ordinal,
227                protocol_name: <CalibratorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
228            }),
229        }
230    }
231}
232
233/// A Stream of incoming requests for fuchsia.lightsensor/Calibrator.
234pub struct CalibratorRequestStream {
235    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
236    is_terminated: bool,
237}
238
239impl std::marker::Unpin for CalibratorRequestStream {}
240
241impl futures::stream::FusedStream for CalibratorRequestStream {
242    fn is_terminated(&self) -> bool {
243        self.is_terminated
244    }
245}
246
247impl fidl::endpoints::RequestStream for CalibratorRequestStream {
248    type Protocol = CalibratorMarker;
249    type ControlHandle = CalibratorControlHandle;
250
251    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
252        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
253    }
254
255    fn control_handle(&self) -> Self::ControlHandle {
256        CalibratorControlHandle { inner: self.inner.clone() }
257    }
258
259    fn into_inner(
260        self,
261    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
262    {
263        (self.inner, self.is_terminated)
264    }
265
266    fn from_inner(
267        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
268        is_terminated: bool,
269    ) -> Self {
270        Self { inner, is_terminated }
271    }
272}
273
274impl futures::Stream for CalibratorRequestStream {
275    type Item = Result<CalibratorRequest, fidl::Error>;
276
277    fn poll_next(
278        mut self: std::pin::Pin<&mut Self>,
279        cx: &mut std::task::Context<'_>,
280    ) -> std::task::Poll<Option<Self::Item>> {
281        let this = &mut *self;
282        if this.inner.check_shutdown(cx) {
283            this.is_terminated = true;
284            return std::task::Poll::Ready(None);
285        }
286        if this.is_terminated {
287            panic!("polled CalibratorRequestStream after completion");
288        }
289        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
290            |bytes, handles| {
291                match this.inner.channel().read_etc(cx, bytes, handles) {
292                    std::task::Poll::Ready(Ok(())) => {}
293                    std::task::Poll::Pending => return std::task::Poll::Pending,
294                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
295                        this.is_terminated = true;
296                        return std::task::Poll::Ready(None);
297                    }
298                    std::task::Poll::Ready(Err(e)) => {
299                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
300                            e.into(),
301                        ))))
302                    }
303                }
304
305                // A message has been received from the channel
306                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
307
308                std::task::Poll::Ready(Some(match header.ordinal {
309                    0x7ddb7eaf88039b02 => {
310                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
311                        let mut req = fidl::new_empty!(
312                            CalibratorCalibrateRequest,
313                            fidl::encoding::DefaultFuchsiaResourceDialect
314                        );
315                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<CalibratorCalibrateRequest>(&header, _body_bytes, handles, &mut req)?;
316                        let control_handle = CalibratorControlHandle { inner: this.inner.clone() };
317                        Ok(CalibratorRequest::Calibrate {
318                            data: req.data,
319
320                            responder: CalibratorCalibrateResponder {
321                                control_handle: std::mem::ManuallyDrop::new(control_handle),
322                                tx_id: header.tx_id,
323                            },
324                        })
325                    }
326                    _ => Err(fidl::Error::UnknownOrdinal {
327                        ordinal: header.ordinal,
328                        protocol_name:
329                            <CalibratorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
330                    }),
331                }))
332            },
333        )
334    }
335}
336
337/// `Calibrator` is responsible for calibrating the raw data that comes from the
338/// product-configured light sensor. It is only intended to be used internally.
339#[derive(Debug)]
340pub enum CalibratorRequest {
341    /// Calibrates the supplied raw [Rgbc] and returns calibrated [Rgbc].
342    Calibrate { data: Rgbc, responder: CalibratorCalibrateResponder },
343}
344
345impl CalibratorRequest {
346    #[allow(irrefutable_let_patterns)]
347    pub fn into_calibrate(self) -> Option<(Rgbc, CalibratorCalibrateResponder)> {
348        if let CalibratorRequest::Calibrate { data, responder } = self {
349            Some((data, responder))
350        } else {
351            None
352        }
353    }
354
355    /// Name of the method defined in FIDL
356    pub fn method_name(&self) -> &'static str {
357        match *self {
358            CalibratorRequest::Calibrate { .. } => "calibrate",
359        }
360    }
361}
362
363#[derive(Debug, Clone)]
364pub struct CalibratorControlHandle {
365    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
366}
367
368impl fidl::endpoints::ControlHandle for CalibratorControlHandle {
369    fn shutdown(&self) {
370        self.inner.shutdown()
371    }
372    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
373        self.inner.shutdown_with_epitaph(status)
374    }
375
376    fn is_closed(&self) -> bool {
377        self.inner.channel().is_closed()
378    }
379    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
380        self.inner.channel().on_closed()
381    }
382
383    #[cfg(target_os = "fuchsia")]
384    fn signal_peer(
385        &self,
386        clear_mask: zx::Signals,
387        set_mask: zx::Signals,
388    ) -> Result<(), zx_status::Status> {
389        use fidl::Peered;
390        self.inner.channel().signal_peer(clear_mask, set_mask)
391    }
392}
393
394impl CalibratorControlHandle {}
395
396#[must_use = "FIDL methods require a response to be sent"]
397#[derive(Debug)]
398pub struct CalibratorCalibrateResponder {
399    control_handle: std::mem::ManuallyDrop<CalibratorControlHandle>,
400    tx_id: u32,
401}
402
403/// Set the the channel to be shutdown (see [`CalibratorControlHandle::shutdown`])
404/// if the responder is dropped without sending a response, so that the client
405/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
406impl std::ops::Drop for CalibratorCalibrateResponder {
407    fn drop(&mut self) {
408        self.control_handle.shutdown();
409        // Safety: drops once, never accessed again
410        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
411    }
412}
413
414impl fidl::endpoints::Responder for CalibratorCalibrateResponder {
415    type ControlHandle = CalibratorControlHandle;
416
417    fn control_handle(&self) -> &CalibratorControlHandle {
418        &self.control_handle
419    }
420
421    fn drop_without_shutdown(mut self) {
422        // Safety: drops once, never accessed again due to mem::forget
423        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
424        // Prevent Drop from running (which would shut down the channel)
425        std::mem::forget(self);
426    }
427}
428
429impl CalibratorCalibrateResponder {
430    /// Sends a response to the FIDL transaction.
431    ///
432    /// Sets the channel to shutdown if an error occurs.
433    pub fn send(self, mut result: Result<&Rgbc, Error>) -> Result<(), fidl::Error> {
434        let _result = self.send_raw(result);
435        if _result.is_err() {
436            self.control_handle.shutdown();
437        }
438        self.drop_without_shutdown();
439        _result
440    }
441
442    /// Similar to "send" but does not shutdown the channel if an error occurs.
443    pub fn send_no_shutdown_on_err(
444        self,
445        mut result: Result<&Rgbc, Error>,
446    ) -> Result<(), fidl::Error> {
447        let _result = self.send_raw(result);
448        self.drop_without_shutdown();
449        _result
450    }
451
452    fn send_raw(&self, mut result: Result<&Rgbc, Error>) -> Result<(), fidl::Error> {
453        self.control_handle
454            .inner
455            .send::<fidl::encoding::ResultType<CalibratorCalibrateResponse, Error>>(
456                result.map(|data| (data,)),
457                self.tx_id,
458                0x7ddb7eaf88039b02,
459                fidl::encoding::DynamicFlags::empty(),
460            )
461    }
462}
463
464#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
465pub struct SensorMarker;
466
467impl fidl::endpoints::ProtocolMarker for SensorMarker {
468    type Proxy = SensorProxy;
469    type RequestStream = SensorRequestStream;
470    #[cfg(target_os = "fuchsia")]
471    type SynchronousProxy = SensorSynchronousProxy;
472
473    const DEBUG_NAME: &'static str = "fuchsia.lightsensor.Sensor";
474}
475impl fidl::endpoints::DiscoverableProtocolMarker for SensorMarker {}
476
477pub trait SensorProxyInterface: Send + Sync {
478    type WatchResponseFut: std::future::Future<Output = Result<LightSensorData, fidl::Error>> + Send;
479    fn r#watch(&self) -> Self::WatchResponseFut;
480}
481#[derive(Debug)]
482#[cfg(target_os = "fuchsia")]
483pub struct SensorSynchronousProxy {
484    client: fidl::client::sync::Client,
485}
486
487#[cfg(target_os = "fuchsia")]
488impl fidl::endpoints::SynchronousProxy for SensorSynchronousProxy {
489    type Proxy = SensorProxy;
490    type Protocol = SensorMarker;
491
492    fn from_channel(inner: fidl::Channel) -> Self {
493        Self::new(inner)
494    }
495
496    fn into_channel(self) -> fidl::Channel {
497        self.client.into_channel()
498    }
499
500    fn as_channel(&self) -> &fidl::Channel {
501        self.client.as_channel()
502    }
503}
504
505#[cfg(target_os = "fuchsia")]
506impl SensorSynchronousProxy {
507    pub fn new(channel: fidl::Channel) -> Self {
508        let protocol_name = <SensorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
509        Self { client: fidl::client::sync::Client::new(channel, protocol_name) }
510    }
511
512    pub fn into_channel(self) -> fidl::Channel {
513        self.client.into_channel()
514    }
515
516    /// Waits until an event arrives and returns it. It is safe for other
517    /// threads to make concurrent requests while waiting for an event.
518    pub fn wait_for_event(
519        &self,
520        deadline: zx::MonotonicInstant,
521    ) -> Result<SensorEvent, fidl::Error> {
522        SensorEvent::decode(self.client.wait_for_event(deadline)?)
523    }
524
525    /// Gets the current [LightSensorData]. Returns immediately on first call;
526    /// subsequent calls return when the value changes.
527    pub fn r#watch(
528        &self,
529        ___deadline: zx::MonotonicInstant,
530    ) -> Result<LightSensorData, fidl::Error> {
531        let _response =
532            self.client.send_query::<fidl::encoding::EmptyPayload, SensorWatchResponse>(
533                (),
534                0x3afa37aef7dc24ff,
535                fidl::encoding::DynamicFlags::empty(),
536                ___deadline,
537            )?;
538        Ok(_response.data)
539    }
540}
541
542#[cfg(target_os = "fuchsia")]
543impl From<SensorSynchronousProxy> for zx::Handle {
544    fn from(value: SensorSynchronousProxy) -> Self {
545        value.into_channel().into()
546    }
547}
548
549#[cfg(target_os = "fuchsia")]
550impl From<fidl::Channel> for SensorSynchronousProxy {
551    fn from(value: fidl::Channel) -> Self {
552        Self::new(value)
553    }
554}
555
556#[derive(Debug, Clone)]
557pub struct SensorProxy {
558    client: fidl::client::Client<fidl::encoding::DefaultFuchsiaResourceDialect>,
559}
560
561impl fidl::endpoints::Proxy for SensorProxy {
562    type Protocol = SensorMarker;
563
564    fn from_channel(inner: ::fidl::AsyncChannel) -> Self {
565        Self::new(inner)
566    }
567
568    fn into_channel(self) -> Result<::fidl::AsyncChannel, Self> {
569        self.client.into_channel().map_err(|client| Self { client })
570    }
571
572    fn as_channel(&self) -> &::fidl::AsyncChannel {
573        self.client.as_channel()
574    }
575}
576
577impl SensorProxy {
578    /// Create a new Proxy for fuchsia.lightsensor/Sensor.
579    pub fn new(channel: ::fidl::AsyncChannel) -> Self {
580        let protocol_name = <SensorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME;
581        Self { client: fidl::client::Client::new(channel, protocol_name) }
582    }
583
584    /// Get a Stream of events from the remote end of the protocol.
585    ///
586    /// # Panics
587    ///
588    /// Panics if the event stream was already taken.
589    pub fn take_event_stream(&self) -> SensorEventStream {
590        SensorEventStream { event_receiver: self.client.take_event_receiver() }
591    }
592
593    /// Gets the current [LightSensorData]. Returns immediately on first call;
594    /// subsequent calls return when the value changes.
595    pub fn r#watch(
596        &self,
597    ) -> fidl::client::QueryResponseFut<
598        LightSensorData,
599        fidl::encoding::DefaultFuchsiaResourceDialect,
600    > {
601        SensorProxyInterface::r#watch(self)
602    }
603}
604
605impl SensorProxyInterface for SensorProxy {
606    type WatchResponseFut = fidl::client::QueryResponseFut<
607        LightSensorData,
608        fidl::encoding::DefaultFuchsiaResourceDialect,
609    >;
610    fn r#watch(&self) -> Self::WatchResponseFut {
611        fn _decode(
612            mut _buf: Result<<fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc, fidl::Error>,
613        ) -> Result<LightSensorData, fidl::Error> {
614            let _response = fidl::client::decode_transaction_body::<
615                SensorWatchResponse,
616                fidl::encoding::DefaultFuchsiaResourceDialect,
617                0x3afa37aef7dc24ff,
618            >(_buf?)?;
619            Ok(_response.data)
620        }
621        self.client.send_query_and_decode::<fidl::encoding::EmptyPayload, LightSensorData>(
622            (),
623            0x3afa37aef7dc24ff,
624            fidl::encoding::DynamicFlags::empty(),
625            _decode,
626        )
627    }
628}
629
630pub struct SensorEventStream {
631    event_receiver: fidl::client::EventReceiver<fidl::encoding::DefaultFuchsiaResourceDialect>,
632}
633
634impl std::marker::Unpin for SensorEventStream {}
635
636impl futures::stream::FusedStream for SensorEventStream {
637    fn is_terminated(&self) -> bool {
638        self.event_receiver.is_terminated()
639    }
640}
641
642impl futures::Stream for SensorEventStream {
643    type Item = Result<SensorEvent, fidl::Error>;
644
645    fn poll_next(
646        mut self: std::pin::Pin<&mut Self>,
647        cx: &mut std::task::Context<'_>,
648    ) -> std::task::Poll<Option<Self::Item>> {
649        match futures::ready!(futures::stream::StreamExt::poll_next_unpin(
650            &mut self.event_receiver,
651            cx
652        )?) {
653            Some(buf) => std::task::Poll::Ready(Some(SensorEvent::decode(buf))),
654            None => std::task::Poll::Ready(None),
655        }
656    }
657}
658
659#[derive(Debug)]
660pub enum SensorEvent {}
661
662impl SensorEvent {
663    /// Decodes a message buffer as a [`SensorEvent`].
664    fn decode(
665        mut buf: <fidl::encoding::DefaultFuchsiaResourceDialect as fidl::encoding::ResourceDialect>::MessageBufEtc,
666    ) -> Result<SensorEvent, fidl::Error> {
667        let (bytes, _handles) = buf.split_mut();
668        let (tx_header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
669        debug_assert_eq!(tx_header.tx_id, 0);
670        match tx_header.ordinal {
671            _ => Err(fidl::Error::UnknownOrdinal {
672                ordinal: tx_header.ordinal,
673                protocol_name: <SensorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
674            }),
675        }
676    }
677}
678
679/// A Stream of incoming requests for fuchsia.lightsensor/Sensor.
680pub struct SensorRequestStream {
681    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
682    is_terminated: bool,
683}
684
685impl std::marker::Unpin for SensorRequestStream {}
686
687impl futures::stream::FusedStream for SensorRequestStream {
688    fn is_terminated(&self) -> bool {
689        self.is_terminated
690    }
691}
692
693impl fidl::endpoints::RequestStream for SensorRequestStream {
694    type Protocol = SensorMarker;
695    type ControlHandle = SensorControlHandle;
696
697    fn from_channel(channel: ::fidl::AsyncChannel) -> Self {
698        Self { inner: std::sync::Arc::new(fidl::ServeInner::new(channel)), is_terminated: false }
699    }
700
701    fn control_handle(&self) -> Self::ControlHandle {
702        SensorControlHandle { inner: self.inner.clone() }
703    }
704
705    fn into_inner(
706        self,
707    ) -> (::std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>, bool)
708    {
709        (self.inner, self.is_terminated)
710    }
711
712    fn from_inner(
713        inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
714        is_terminated: bool,
715    ) -> Self {
716        Self { inner, is_terminated }
717    }
718}
719
720impl futures::Stream for SensorRequestStream {
721    type Item = Result<SensorRequest, fidl::Error>;
722
723    fn poll_next(
724        mut self: std::pin::Pin<&mut Self>,
725        cx: &mut std::task::Context<'_>,
726    ) -> std::task::Poll<Option<Self::Item>> {
727        let this = &mut *self;
728        if this.inner.check_shutdown(cx) {
729            this.is_terminated = true;
730            return std::task::Poll::Ready(None);
731        }
732        if this.is_terminated {
733            panic!("polled SensorRequestStream after completion");
734        }
735        fidl::encoding::with_tls_decode_buf::<_, fidl::encoding::DefaultFuchsiaResourceDialect>(
736            |bytes, handles| {
737                match this.inner.channel().read_etc(cx, bytes, handles) {
738                    std::task::Poll::Ready(Ok(())) => {}
739                    std::task::Poll::Pending => return std::task::Poll::Pending,
740                    std::task::Poll::Ready(Err(zx_status::Status::PEER_CLOSED)) => {
741                        this.is_terminated = true;
742                        return std::task::Poll::Ready(None);
743                    }
744                    std::task::Poll::Ready(Err(e)) => {
745                        return std::task::Poll::Ready(Some(Err(fidl::Error::ServerRequestRead(
746                            e.into(),
747                        ))))
748                    }
749                }
750
751                // A message has been received from the channel
752                let (header, _body_bytes) = fidl::encoding::decode_transaction_header(bytes)?;
753
754                std::task::Poll::Ready(Some(match header.ordinal {
755                    0x3afa37aef7dc24ff => {
756                        header.validate_request_tx_id(fidl::MethodType::TwoWay)?;
757                        let mut req = fidl::new_empty!(
758                            fidl::encoding::EmptyPayload,
759                            fidl::encoding::DefaultFuchsiaResourceDialect
760                        );
761                        fidl::encoding::Decoder::<fidl::encoding::DefaultFuchsiaResourceDialect>::decode_into::<fidl::encoding::EmptyPayload>(&header, _body_bytes, handles, &mut req)?;
762                        let control_handle = SensorControlHandle { inner: this.inner.clone() };
763                        Ok(SensorRequest::Watch {
764                            responder: SensorWatchResponder {
765                                control_handle: std::mem::ManuallyDrop::new(control_handle),
766                                tx_id: header.tx_id,
767                            },
768                        })
769                    }
770                    _ => Err(fidl::Error::UnknownOrdinal {
771                        ordinal: header.ordinal,
772                        protocol_name:
773                            <SensorMarker as fidl::endpoints::ProtocolMarker>::DEBUG_NAME,
774                    }),
775                }))
776            },
777        )
778    }
779}
780
781/// `Sensor` will return calibrated readings from a product-configured light
782/// sensor.
783#[derive(Debug)]
784pub enum SensorRequest {
785    /// Gets the current [LightSensorData]. Returns immediately on first call;
786    /// subsequent calls return when the value changes.
787    Watch { responder: SensorWatchResponder },
788}
789
790impl SensorRequest {
791    #[allow(irrefutable_let_patterns)]
792    pub fn into_watch(self) -> Option<(SensorWatchResponder)> {
793        if let SensorRequest::Watch { responder } = self {
794            Some((responder))
795        } else {
796            None
797        }
798    }
799
800    /// Name of the method defined in FIDL
801    pub fn method_name(&self) -> &'static str {
802        match *self {
803            SensorRequest::Watch { .. } => "watch",
804        }
805    }
806}
807
808#[derive(Debug, Clone)]
809pub struct SensorControlHandle {
810    inner: std::sync::Arc<fidl::ServeInner<fidl::encoding::DefaultFuchsiaResourceDialect>>,
811}
812
813impl fidl::endpoints::ControlHandle for SensorControlHandle {
814    fn shutdown(&self) {
815        self.inner.shutdown()
816    }
817    fn shutdown_with_epitaph(&self, status: zx_status::Status) {
818        self.inner.shutdown_with_epitaph(status)
819    }
820
821    fn is_closed(&self) -> bool {
822        self.inner.channel().is_closed()
823    }
824    fn on_closed(&self) -> fidl::OnSignalsRef<'_> {
825        self.inner.channel().on_closed()
826    }
827
828    #[cfg(target_os = "fuchsia")]
829    fn signal_peer(
830        &self,
831        clear_mask: zx::Signals,
832        set_mask: zx::Signals,
833    ) -> Result<(), zx_status::Status> {
834        use fidl::Peered;
835        self.inner.channel().signal_peer(clear_mask, set_mask)
836    }
837}
838
839impl SensorControlHandle {}
840
841#[must_use = "FIDL methods require a response to be sent"]
842#[derive(Debug)]
843pub struct SensorWatchResponder {
844    control_handle: std::mem::ManuallyDrop<SensorControlHandle>,
845    tx_id: u32,
846}
847
848/// Set the the channel to be shutdown (see [`SensorControlHandle::shutdown`])
849/// if the responder is dropped without sending a response, so that the client
850/// doesn't hang. To prevent this behavior, call `drop_without_shutdown`.
851impl std::ops::Drop for SensorWatchResponder {
852    fn drop(&mut self) {
853        self.control_handle.shutdown();
854        // Safety: drops once, never accessed again
855        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
856    }
857}
858
859impl fidl::endpoints::Responder for SensorWatchResponder {
860    type ControlHandle = SensorControlHandle;
861
862    fn control_handle(&self) -> &SensorControlHandle {
863        &self.control_handle
864    }
865
866    fn drop_without_shutdown(mut self) {
867        // Safety: drops once, never accessed again due to mem::forget
868        unsafe { std::mem::ManuallyDrop::drop(&mut self.control_handle) };
869        // Prevent Drop from running (which would shut down the channel)
870        std::mem::forget(self);
871    }
872}
873
874impl SensorWatchResponder {
875    /// Sends a response to the FIDL transaction.
876    ///
877    /// Sets the channel to shutdown if an error occurs.
878    pub fn send(self, mut data: &LightSensorData) -> Result<(), fidl::Error> {
879        let _result = self.send_raw(data);
880        if _result.is_err() {
881            self.control_handle.shutdown();
882        }
883        self.drop_without_shutdown();
884        _result
885    }
886
887    /// Similar to "send" but does not shutdown the channel if an error occurs.
888    pub fn send_no_shutdown_on_err(self, mut data: &LightSensorData) -> Result<(), fidl::Error> {
889        let _result = self.send_raw(data);
890        self.drop_without_shutdown();
891        _result
892    }
893
894    fn send_raw(&self, mut data: &LightSensorData) -> Result<(), fidl::Error> {
895        self.control_handle.inner.send::<SensorWatchResponse>(
896            (data,),
897            self.tx_id,
898            0x3afa37aef7dc24ff,
899            fidl::encoding::DynamicFlags::empty(),
900        )
901    }
902}
903
904mod internal {
905    use super::*;
906}