1use crate::{
6 AnyHandle, AsHandleRef, Channel, ChannelMessageStream, ChannelWriter, Error, Handle,
7 HandleBased, HandleInfo, MessageBuf,
8};
9use fidl::epitaph::ChannelEpitaphExt;
10use fidl_fuchsia_fdomain as proto;
11use fuchsia_sync::Mutex;
12use futures::{Stream, StreamExt, TryStream};
13use std::cell::RefCell;
14use std::marker::PhantomData;
15use std::sync::Arc;
16use std::task::Poll;
17
18pub trait FDomainFlexibleIntoResult<T> {
19 fn into_result_fdomain<P: ProtocolMarker>(
20 self,
21 method_name: &'static str,
22 ) -> Result<T, fidl::Error>;
23}
24
25impl<T> FDomainFlexibleIntoResult<T> for fidl::encoding::Flexible<T> {
26 fn into_result_fdomain<P: ProtocolMarker>(
27 self,
28 method_name: &'static str,
29 ) -> Result<T, fidl::Error> {
30 match self {
31 fidl::encoding::Flexible::Ok(ok) => Ok(ok),
32 fidl::encoding::Flexible::FrameworkErr(fidl::encoding::FrameworkErr::UnknownMethod) => {
33 Err(fidl::Error::UnsupportedMethod { method_name, protocol_name: P::DEBUG_NAME })
34 }
35 }
36 }
37}
38
39impl<T, E> FDomainFlexibleIntoResult<Result<T, E>> for fidl::encoding::FlexibleResult<T, E> {
40 fn into_result_fdomain<P: ProtocolMarker>(
41 self,
42 method_name: &'static str,
43 ) -> Result<Result<T, E>, fidl::Error> {
44 match self {
45 fidl::encoding::FlexibleResult::Ok(ok) => Ok(Ok(ok)),
46 fidl::encoding::FlexibleResult::DomainErr(err) => Ok(Err(err)),
47 fidl::encoding::FlexibleResult::FrameworkErr(
48 fidl::encoding::FrameworkErr::UnknownMethod,
49 ) => Err(fidl::Error::UnsupportedMethod { method_name, protocol_name: P::DEBUG_NAME }),
50 }
51 }
52}
53
54#[derive(Debug)]
55pub struct FDomainProxyChannel(Mutex<ChannelMessageStream>, ChannelWriter);
56
57impl FDomainProxyChannel {
58 pub fn on_closed(&self) -> crate::OnFDomainSignals {
59 self.1.as_channel().on_closed()
60 }
61
62 pub fn read_etc(
63 &self,
64 ctx: &mut std::task::Context<'_>,
65 bytes: &mut Vec<u8>,
66 handles: &mut Vec<HandleInfo>,
67 ) -> Poll<Result<(), Option<crate::Error>>> {
68 let Some(got) = std::task::ready!(self.0.lock().poll_next_unpin(ctx)) else {
69 return Poll::Ready(Err(Some(Error::StreamingAborted)));
70 };
71
72 match got {
73 Ok(got) => {
74 *bytes = got.bytes;
75 *handles = got.handles;
76 Poll::Ready(Ok(()))
77 }
78 Err(Error::FDomain(proto::Error::TargetError(i)))
79 if i == fidl::Status::PEER_CLOSED.into_raw() =>
80 {
81 Poll::Ready(Err(None))
82 }
83 Err(e) => Poll::Ready(Err(Some(e))),
84 }
85 }
86}
87
88impl ::fidl::encoding::ProxyChannelBox<FDomainResourceDialect> for FDomainProxyChannel {
89 fn recv_etc_from(
90 &self,
91 ctx: &mut std::task::Context<'_>,
92 buf: &mut MessageBuf,
93 ) -> Poll<Result<(), Option<Error>>> {
94 let Some(got) = std::task::ready!(self.0.lock().poll_next_unpin(ctx)) else {
95 return Poll::Ready(Err(Some(Error::StreamingAborted)));
96 };
97
98 match got {
99 Ok(got) => {
100 *buf = got;
101 Poll::Ready(Ok(()))
102 }
103 Err(Error::FDomain(proto::Error::TargetError(i)))
104 if i == fidl::Status::PEER_CLOSED.into_raw() =>
105 {
106 Poll::Ready(Err(None))
107 }
108 Err(e) => Poll::Ready(Err(Some(e))),
109 }
110 }
111
112 fn write_etc(&self, bytes: &[u8], handles: &mut [HandleInfo]) -> Result<(), Option<Error>> {
113 let mut handle_ops = Vec::new();
114 for handle in handles {
115 handle_ops.push(crate::channel::HandleOp::Move(
116 std::mem::replace(&mut handle.handle, AnyHandle::invalid()).into(),
117 handle.rights,
118 ));
119 }
120 let _ = self.1.fdomain_write_etc(bytes, handle_ops);
121 Ok(())
122 }
123
124 fn is_closed(&self) -> bool {
125 self.0.lock().is_closed()
126 }
127
128 fn unbox(self) -> Channel {
129 self.0.into_inner().rejoin(self.1)
130 }
131
132 fn as_channel(&self) -> &Channel {
133 self.1.as_channel()
134 }
135}
136
137#[derive(Debug, Copy, Clone, Default)]
138pub struct FDomainResourceDialect;
139impl ::fidl::encoding::ResourceDialect for FDomainResourceDialect {
140 type Handle = Handle;
141 type MessageBufEtc = MessageBuf;
142 type ProxyChannel = Channel;
143
144 #[inline]
145 fn with_tls_buf<R>(f: impl FnOnce(&mut ::fidl::encoding::TlsBuf<Self>) -> R) -> R {
146 thread_local!(static TLS_BUF: RefCell<::fidl::encoding::TlsBuf<FDomainResourceDialect>> =
147 RefCell::new(::fidl::encoding::TlsBuf::default()));
148 TLS_BUF.with(|buf| f(&mut buf.borrow_mut()))
149 }
150}
151
152impl ::fidl::encoding::MessageBufFor<FDomainResourceDialect> for MessageBuf {
153 fn new() -> MessageBuf {
154 MessageBuf { bytes: Vec::new(), handles: Vec::new() }
155 }
156
157 fn split_mut(&mut self) -> (&mut Vec<u8>, &mut Vec<HandleInfo>) {
158 (&mut self.bytes, &mut self.handles)
159 }
160}
161
162impl Into<::fidl::TransportError> for Error {
163 fn into(self) -> ::fidl::TransportError {
164 match self {
165 Error::FDomain(proto::Error::TargetError(i)) => {
166 ::fidl::TransportError::Status(fidl::Status::from_raw(i))
167 }
168 Error::SocketWrite(proto::WriteSocketError {
169 error: proto::Error::TargetError(i),
170 ..
171 }) => ::fidl::TransportError::Status(fidl::Status::from_raw(i)),
172 Error::ChannelWrite(proto::WriteChannelError::Error(proto::Error::TargetError(i))) => {
173 ::fidl::TransportError::Status(fidl::Status::from_raw(i))
174 }
175 Error::ChannelWrite(proto::WriteChannelError::OpErrors(ops)) => {
176 let Some(op) = ops.into_iter().find_map(|x| x) else {
177 let err = Box::<dyn std::error::Error + Send + Sync>::from(
178 "Channel write handle operation reported failure with no status!"
179 .to_owned(),
180 );
181 return ::fidl::TransportError::Other(err.into());
182 };
183 let op = *op;
184 Error::FDomain(op).into()
185 }
186 other => ::fidl::TransportError::Other(std::sync::Arc::new(other)),
187 }
188 }
189}
190
191impl ::fidl::encoding::ProxyChannelFor<FDomainResourceDialect> for Channel {
192 type Boxed = FDomainProxyChannel;
193 type Error = Error;
194 type HandleDisposition = HandleInfo;
195
196 fn boxed(self) -> Self::Boxed {
197 let (a, b) = self.stream().unwrap();
198 FDomainProxyChannel(Mutex::new(a), b)
199 }
200
201 fn write_etc(&self, bytes: &[u8], handles: &mut [HandleInfo]) -> Result<(), Option<Error>> {
202 let mut handle_ops = Vec::new();
203 for handle in handles {
204 handle_ops.push(crate::channel::HandleOp::Move(
205 std::mem::replace(&mut handle.handle, AnyHandle::invalid()).into(),
206 handle.rights,
207 ));
208 }
209 let _ = self.fdomain_write_etc(bytes, handle_ops);
210 Ok(())
211 }
212}
213
214impl ::fidl::epitaph::ChannelLike for Channel {
215 fn write_epitaph(&self, bytes: &[u8]) -> Result<(), ::fidl::TransportError> {
216 let _ = self.write(bytes, vec![]);
217 Ok(())
218 }
219}
220
221impl ::fidl::encoding::HandleFor<FDomainResourceDialect> for Handle {
222 type HandleInfo = HandleInfo;
225
226 fn invalid() -> Self {
227 Handle::invalid()
228 }
229
230 fn is_invalid(&self) -> bool {
231 self.client.upgrade().is_none()
232 }
233}
234
235impl ::fidl::encoding::HandleDispositionFor<FDomainResourceDialect> for HandleInfo {
236 fn from_handle(handle: Handle, object_type: fidl::ObjectType, rights: fidl::Rights) -> Self {
237 HandleInfo { handle: AnyHandle::from_handle(handle, object_type), rights }
238 }
239}
240
241impl ::fidl::encoding::HandleInfoFor<FDomainResourceDialect> for HandleInfo {
242 fn consume(
243 &mut self,
244 expected_object_type: fidl::ObjectType,
245 expected_rights: fidl::Rights,
246 ) -> Result<Handle, ::fidl::Error> {
247 let handle_info = std::mem::replace(
248 self,
249 HandleInfo {
250 handle: crate::AnyHandle::Unknown(Handle::invalid(), fidl::ObjectType::NONE),
251 rights: fidl::Rights::empty(),
252 },
253 );
254 let received_object_type = handle_info.handle.object_type();
255 if expected_object_type != fidl::ObjectType::NONE
256 && received_object_type != fidl::ObjectType::NONE
257 && expected_object_type != received_object_type
258 {
259 return Err(fidl::Error::IncorrectHandleSubtype {
260 expected: fidl::ObjectType::NONE,
264 received: fidl::ObjectType::NONE,
265 });
266 }
267
268 let received_rights = handle_info.rights;
269 if expected_rights != fidl::Rights::SAME_RIGHTS
270 && received_rights != fidl::Rights::SAME_RIGHTS
271 && expected_rights != received_rights
272 {
273 if !received_rights.contains(expected_rights) {
274 return Err(fidl::Error::MissingExpectedHandleRights {
275 missing_rights: fidl::Rights::empty(),
277 });
278 }
279
280 }
284 Ok(handle_info.handle.into())
285 }
286
287 fn drop_in_place(&mut self) {
288 *self = HandleInfo {
289 handle: crate::AnyHandle::Unknown(Handle::invalid(), fidl::ObjectType::NONE),
290 rights: fidl::Rights::empty(),
291 };
292 }
293}
294
295impl ::fidl::encoding::EncodableAsHandle for crate::Event {
296 type Dialect = FDomainResourceDialect;
297}
298
299impl ::fidl::encoding::EncodableAsHandle for crate::EventPair {
300 type Dialect = FDomainResourceDialect;
301}
302
303impl ::fidl::encoding::EncodableAsHandle for crate::Socket {
304 type Dialect = FDomainResourceDialect;
305}
306
307impl ::fidl::encoding::EncodableAsHandle for crate::Channel {
308 type Dialect = FDomainResourceDialect;
309}
310
311impl ::fidl::encoding::EncodableAsHandle for crate::Handle {
312 type Dialect = FDomainResourceDialect;
313}
314
315impl<T: ProtocolMarker> ::fidl::encoding::EncodableAsHandle for ClientEnd<T> {
316 type Dialect = FDomainResourceDialect;
317}
318
319impl<T: ProtocolMarker> ::fidl::encoding::EncodableAsHandle for ServerEnd<T> {
320 type Dialect = FDomainResourceDialect;
321}
322
323pub trait ProtocolMarker: Sized + Send + Sync + 'static {
326 type Proxy: Proxy<Protocol = Self>;
329
330 type RequestStream: RequestStream<Protocol = Self>;
332
333 const DEBUG_NAME: &'static str;
338}
339
340pub trait DiscoverableProtocolMarker: ProtocolMarker {
353 const PROTOCOL_NAME: &'static str = <Self as ProtocolMarker>::DEBUG_NAME;
355}
356
357pub trait Proxy: Sized + Send + Sync {
359 type Protocol: ProtocolMarker<Proxy = Self>;
361
362 fn from_channel(inner: Channel) -> Self;
364
365 fn into_channel(self) -> Result<Channel, Self>;
371
372 fn as_channel(&self) -> &Channel;
378
379 fn domain(&self) -> Arc<crate::Client> {
384 self.as_channel().domain()
385 }
386}
387
388pub trait RequestStream: Sized + Send + Stream + TryStream<Error = fidl::Error> + Unpin {
390 type Protocol: ProtocolMarker<RequestStream = Self>;
392
393 type ControlHandle: ControlHandle;
395
396 fn control_handle(&self) -> Self::ControlHandle;
399
400 fn from_channel(inner: Channel) -> Self;
402
403 fn into_inner(self) -> (std::sync::Arc<fidl::ServeInner<FDomainResourceDialect>>, bool);
405
406 fn from_inner(
408 inner: std::sync::Arc<fidl::ServeInner<FDomainResourceDialect>>,
409 is_terminated: bool,
410 ) -> Self;
411}
412
413pub trait ControlHandle {
416 fn shutdown(&self);
419
420 fn shutdown_with_epitaph(&self, status: zx_status::Status);
423
424 fn is_closed(&self) -> bool;
426
427 fn on_closed(&self) -> crate::OnFDomainSignals;
430}
431
432pub trait Responder {
435 type ControlHandle: ControlHandle;
437
438 fn control_handle(&self) -> &Self::ControlHandle;
440
441 fn drop_without_shutdown(self);
446}
447
448pub type Request<Marker> = <<Marker as ProtocolMarker>::RequestStream as futures::TryStream>::Ok;
450
451#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
453pub struct ClientEnd<T: ProtocolMarker> {
454 inner: Channel,
455 phantom: PhantomData<T>,
456}
457
458impl<T: ProtocolMarker> ClientEnd<T> {
459 pub fn new(inner: Channel) -> Self {
461 ClientEnd { inner, phantom: PhantomData }
462 }
463
464 pub fn channel(&self) -> &Channel {
466 &self.inner
467 }
468
469 pub fn into_channel(self) -> Channel {
471 self.inner
472 }
473}
474
475impl<'c, T: ProtocolMarker> ClientEnd<T> {
476 pub fn into_proxy(self) -> T::Proxy {
478 T::Proxy::from_channel(self.inner)
479 }
480}
481
482impl<T: ProtocolMarker> From<ClientEnd<T>> for Handle {
483 fn from(client: ClientEnd<T>) -> Handle {
484 client.into_channel().into()
485 }
486}
487
488impl<T: ProtocolMarker> From<Handle> for ClientEnd<T> {
489 fn from(handle: Handle) -> Self {
490 ClientEnd { inner: handle.into(), phantom: PhantomData }
491 }
492}
493
494impl<T: ProtocolMarker> From<Channel> for ClientEnd<T> {
495 fn from(chan: Channel) -> Self {
496 ClientEnd { inner: chan, phantom: PhantomData }
497 }
498}
499
500impl<T: ProtocolMarker> AsHandleRef for ClientEnd<T> {
501 fn as_handle_ref(&self) -> crate::HandleRef<'_> {
502 AsHandleRef::as_handle_ref(&self.inner)
503 }
504
505 fn object_type() -> fidl::ObjectType {
506 <Channel as AsHandleRef>::object_type()
507 }
508}
509
510impl<T: ProtocolMarker> HandleBased for ClientEnd<T> {
511 fn close(self) -> impl Future<Output = Result<(), Error>> {
512 let h = <Self as Into<Handle>>::into(self);
513 Handle::close(h)
514 }
515
516 fn duplicate_handle(&self, rights: fidl::Rights) -> impl Future<Output = Result<Self, Error>> {
517 let fut = self.as_handle_ref().duplicate(rights);
518 async move { fut.await.map(|handle| Self::from(handle)) }
519 }
520
521 fn replace_handle(self, rights: fidl::Rights) -> impl Future<Output = Result<Self, Error>> {
522 let h = <Self as Into<Handle>>::into(self);
523 async move { h.replace(rights).await.map(|handle| Self::from(handle)) }
524 }
525
526 fn into_handle(self) -> Handle {
527 self.into()
528 }
529
530 fn from_handle(handle: Handle) -> Self {
531 Self::from(handle)
532 }
533
534 fn into_handle_based<H: HandleBased>(self) -> H {
535 H::from_handle(self.into_handle())
536 }
537
538 fn from_handle_based<H: HandleBased>(h: H) -> Self {
539 Self::from_handle(h.into_handle())
540 }
541
542 fn invalidate(&mut self) {
543 self.inner.invalidate();
544 }
545}
546
547#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
549pub struct ServerEnd<T: ProtocolMarker> {
550 inner: Channel,
551 phantom: PhantomData<T>,
552}
553
554impl<T: ProtocolMarker> ServerEnd<T> {
555 pub fn new(inner: Channel) -> ServerEnd<T> {
557 ServerEnd { inner, phantom: PhantomData }
558 }
559
560 pub fn channel(&self) -> &Channel {
562 &self.inner
563 }
564
565 pub fn into_channel(self) -> Channel {
567 self.inner
568 }
569
570 pub fn into_stream(self) -> T::RequestStream
572 where
573 T: ProtocolMarker,
574 {
575 T::RequestStream::from_channel(self.inner)
576 }
577
578 pub fn into_stream_and_control_handle(
581 self,
582 ) -> (T::RequestStream, <T::RequestStream as RequestStream>::ControlHandle)
583 where
584 T: ProtocolMarker,
585 {
586 let stream = self.into_stream();
587 let control_handle = stream.control_handle();
588 (stream, control_handle)
589 }
590
591 pub fn close_with_epitaph(self, status: fidl::Status) -> Result<(), fidl::Error> {
593 self.inner.close_with_epitaph(status)
594 }
595}
596
597impl<T: ProtocolMarker> From<ServerEnd<T>> for Handle {
598 fn from(server: ServerEnd<T>) -> Handle {
599 server.into_channel().into()
600 }
601}
602
603impl<T: ProtocolMarker> From<Handle> for ServerEnd<T> {
604 fn from(handle: Handle) -> Self {
605 ServerEnd { inner: handle.into(), phantom: PhantomData }
606 }
607}
608
609impl<T: ProtocolMarker> From<Channel> for ServerEnd<T> {
610 fn from(chan: Channel) -> Self {
611 ServerEnd { inner: chan, phantom: PhantomData }
612 }
613}
614
615impl<T: ProtocolMarker> AsHandleRef for ServerEnd<T> {
616 fn as_handle_ref(&self) -> crate::HandleRef<'_> {
617 AsHandleRef::as_handle_ref(&self.inner)
618 }
619
620 fn object_type() -> fidl::ObjectType {
621 <Channel as AsHandleRef>::object_type()
622 }
623}
624
625impl<T: ProtocolMarker> HandleBased for ServerEnd<T> {
626 fn close(self) -> impl Future<Output = Result<(), Error>> {
627 let h = <Self as Into<Handle>>::into(self);
628 Handle::close(h)
629 }
630
631 fn duplicate_handle(&self, rights: fidl::Rights) -> impl Future<Output = Result<Self, Error>> {
632 let fut = self.as_handle_ref().duplicate(rights);
633 async move { fut.await.map(|handle| Self::from(handle)) }
634 }
635
636 fn replace_handle(self, rights: fidl::Rights) -> impl Future<Output = Result<Self, Error>> {
637 let h = <Self as Into<Handle>>::into(self);
638 async move { h.replace(rights).await.map(|handle| Self::from(handle)) }
639 }
640
641 fn into_handle(self) -> Handle {
642 self.into()
643 }
644
645 fn from_handle(handle: Handle) -> Self {
646 Self::from(handle)
647 }
648
649 fn into_handle_based<H: HandleBased>(self) -> H {
650 H::from_handle(self.into_handle())
651 }
652
653 fn from_handle_based<H: HandleBased>(h: H) -> Self {
654 Self::from_handle(h.into_handle())
655 }
656
657 fn invalidate(&mut self) {
658 self.inner.invalidate();
659 }
660}