fdomain_client/fidl_next/
wire_types.rs

1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use super::{HandleEncoder, WireHandle, WireOptionalHandle};
6use crate::{Channel, Event, EventPair, Handle, HandleBased, Socket};
7use fidl_next_codec::{Encode, EncodeError, EncodeOption, FromWire, FromWireOption};
8use std::mem::MaybeUninit;
9
10macro_rules! handle_type {
11    ($name:ident) => {
12        unsafe impl<E: HandleEncoder + ?Sized> Encode<WireHandle, E> for $name {
13            fn encode(
14                self,
15                encoder: &mut E,
16                out: &mut MaybeUninit<WireHandle>,
17                constraint: (),
18            ) -> Result<(), EncodeError> {
19                Encode::encode(self.into_handle(), encoder, out, constraint)
20            }
21        }
22
23        impl FromWire<WireHandle> for $name {
24            fn from_wire(wire: WireHandle) -> Self {
25                $name::from_handle(Handle::from_wire(wire))
26            }
27        }
28
29        unsafe impl<E: HandleEncoder + ?Sized> EncodeOption<WireOptionalHandle, E> for $name {
30            fn encode_option(
31                this: Option<Self>,
32                encoder: &mut E,
33                out: &mut MaybeUninit<WireOptionalHandle>,
34                constraint: (),
35            ) -> Result<(), EncodeError> {
36                EncodeOption::encode_option(
37                    this.map(HandleBased::into_handle),
38                    encoder,
39                    out,
40                    constraint,
41                )
42            }
43        }
44
45        impl FromWireOption<WireOptionalHandle> for $name {
46            fn from_wire_option(wire: WireOptionalHandle) -> Option<Self> {
47                Handle::from_wire_option(wire).map(Self::from_handle)
48            }
49        }
50    };
51}
52
53handle_type!(Channel);
54handle_type!(Event);
55handle_type!(EventPair);
56handle_type!(Socket);