fidl_next_codec/fuchsia/
channel.rs1use core::mem::MaybeUninit;
6
7use crate::fuchsia::{HandleDecoder, HandleEncoder, WireHandle, WireOptionalHandle};
8use crate::{
9 munge, Decode, DecodeError, Encodable, EncodableOption, Encode, EncodeError, EncodeOption,
10 Slot, TakeFrom, ZeroPadding,
11};
12
13use zx::sys::zx_handle_t;
14use zx::{Channel, Handle};
15
16#[derive(Debug)]
18#[repr(transparent)]
19pub struct WireChannel {
20 handle: WireHandle,
21}
22
23unsafe impl ZeroPadding for WireChannel {
24 #[inline]
25 fn zero_padding(out: &mut MaybeUninit<Self>) {
26 munge!(let Self { handle } = out);
27 WireHandle::zero_padding(handle);
28 }
29}
30
31impl WireChannel {
32 pub fn set_encoded_present(out: &mut MaybeUninit<Self>) {
34 munge!(let Self { handle } = out);
35 WireHandle::set_encoded_present(handle);
36 }
37
38 pub fn is_invalid(&self) -> bool {
40 self.handle.is_invalid()
41 }
42
43 pub fn take(&self) -> Channel {
45 self.handle.take().into()
46 }
47
48 #[inline]
50 pub fn as_raw_handle(&self) -> zx_handle_t {
51 self.handle.as_raw_handle()
52 }
53}
54
55unsafe impl<D: HandleDecoder + ?Sized> Decode<D> for WireChannel {
56 fn decode(mut slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
57 munge!(let Self { handle } = slot.as_mut());
58 WireHandle::decode(handle, decoder)
59 }
60}
61
62impl TakeFrom<WireChannel> for Channel {
63 fn take_from(from: &WireChannel) -> Self {
64 from.take()
65 }
66}
67
68#[derive(Debug)]
70#[repr(transparent)]
71pub struct WireOptionalChannel {
72 handle: WireOptionalHandle,
73}
74
75unsafe impl ZeroPadding for WireOptionalChannel {
76 #[inline]
77 fn zero_padding(out: &mut MaybeUninit<Self>) {
78 munge!(let Self { handle } = out);
79 WireOptionalHandle::zero_padding(handle);
80 }
81}
82
83impl WireOptionalChannel {
84 pub fn set_encoded_present(out: &mut MaybeUninit<Self>) {
86 munge!(let Self { handle } = out);
87 WireOptionalHandle::set_encoded_present(handle);
88 }
89
90 pub fn set_encoded_absent(out: &mut MaybeUninit<Self>) {
92 munge!(let Self { handle } = out);
93 WireOptionalHandle::set_encoded_absent(handle);
94 }
95
96 pub fn is_some(&self) -> bool {
98 !self.handle.is_some()
99 }
100
101 pub fn is_none(&self) -> bool {
103 self.handle.is_none()
104 }
105
106 pub fn take(&self) -> Option<Channel> {
108 self.handle.take().map(Channel::from)
109 }
110
111 #[inline]
113 pub fn as_raw_handle(&self) -> Option<zx_handle_t> {
114 self.handle.as_raw_handle()
115 }
116}
117
118impl Encodable for Channel {
119 type Encoded = WireChannel;
120}
121
122unsafe impl<E: HandleEncoder + ?Sized> Encode<E> for Channel {
123 fn encode(
124 self,
125 encoder: &mut E,
126 out: &mut MaybeUninit<Self::Encoded>,
127 ) -> Result<(), EncodeError> {
128 munge!(let WireChannel { handle } = out);
129 Handle::from(self).encode(encoder, handle)
130 }
131}
132
133impl EncodableOption for Channel {
134 type EncodedOption = WireOptionalChannel;
135}
136
137unsafe impl<E: HandleEncoder + ?Sized> EncodeOption<E> for Channel {
138 fn encode_option(
139 this: Option<Self>,
140 encoder: &mut E,
141 out: &mut MaybeUninit<Self::EncodedOption>,
142 ) -> Result<(), EncodeError> {
143 munge!(let WireOptionalChannel { handle } = out);
144 Handle::encode_option(this.map(Handle::from), encoder, handle)
145 }
146}
147
148unsafe impl<D: HandleDecoder + ?Sized> Decode<D> for WireOptionalChannel {
149 fn decode(mut slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
150 munge!(let Self { handle } = slot.as_mut());
151 WireOptionalHandle::decode(handle, decoder)
152 }
153}
154
155impl TakeFrom<WireOptionalChannel> for Option<Channel> {
156 fn take_from(from: &WireOptionalChannel) -> Self {
157 from.take()
158 }
159}