fidl_next/fuchsia/wire/
channel.rsuse core::mem::replace;
use crate::zx::sys::zx_handle_t;
use crate::zx::{Channel, Handle};
use munge::munge;
use crate::fuchsia::{HandleDecoder, HandleEncoder, WireHandle, WireOptionalHandle};
use crate::{
Decode, DecodeError, Encodable, EncodableOption, Encode, EncodeError, EncodeOption, Slot,
TakeFrom,
};
#[derive(Debug)]
#[repr(transparent)]
pub struct WireChannel {
handle: WireHandle,
}
impl WireChannel {
pub fn set_encoded_present(slot: Slot<'_, Self>) {
munge!(let Self { handle } = slot);
WireHandle::set_encoded_present(handle);
}
pub fn is_invalid(&self) -> bool {
self.handle.is_invalid()
}
pub fn take(&mut self) -> Channel {
self.handle.take().into()
}
#[inline]
pub fn as_raw_handle(&self) -> zx_handle_t {
self.handle.as_raw_handle()
}
}
unsafe impl<D: HandleDecoder + ?Sized> Decode<D> for WireChannel {
fn decode(mut slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
munge!(let Self { handle } = slot.as_mut());
WireHandle::decode(handle, decoder)
}
}
impl TakeFrom<WireChannel> for Channel {
fn take_from(from: &mut WireChannel) -> Self {
from.take()
}
}
#[derive(Debug)]
#[repr(transparent)]
pub struct WireOptionalChannel {
handle: WireOptionalHandle,
}
impl WireOptionalChannel {
pub fn set_encoded_present(slot: Slot<'_, Self>) {
munge!(let Self { handle } = slot);
WireOptionalHandle::set_encoded_present(handle);
}
pub fn set_encoded_absent(slot: Slot<'_, Self>) {
munge!(let Self { handle } = slot);
WireOptionalHandle::set_encoded_absent(handle);
}
pub fn is_some(&self) -> bool {
!self.handle.is_some()
}
pub fn is_none(&self) -> bool {
self.handle.is_none()
}
pub fn take(&mut self) -> Option<Channel> {
self.handle.take().map(Channel::from)
}
#[inline]
pub fn as_raw_handle(&self) -> Option<zx_handle_t> {
self.handle.as_raw_handle()
}
}
impl Encodable for Channel {
type Encoded<'buf> = WireChannel;
}
impl<E: HandleEncoder + ?Sized> Encode<E> for Channel {
fn encode(
&mut self,
encoder: &mut E,
slot: Slot<'_, Self::Encoded<'_>>,
) -> Result<(), EncodeError> {
let channel = replace(self, Channel::from(Handle::invalid()));
munge!(let WireChannel { handle } = slot);
Handle::from(channel).encode(encoder, handle)
}
}
impl EncodableOption for Channel {
type EncodedOption<'buf> = WireOptionalChannel;
}
impl<E: HandleEncoder + ?Sized> EncodeOption<E> for Channel {
fn encode_option(
this: Option<&mut Self>,
encoder: &mut E,
slot: Slot<'_, Self::EncodedOption<'_>>,
) -> Result<(), EncodeError> {
let channel = this.map(|channel| replace(channel, Channel::from(Handle::invalid())));
munge!(let WireOptionalChannel { handle } = slot);
Handle::encode_option(channel.map(Handle::from).as_mut(), encoder, handle)
}
}
impl TakeFrom<WireOptionalChannel> for Option<Channel> {
fn take_from(from: &mut WireOptionalChannel) -> Self {
from.take()
}
}