fidl_next_codec/fuchsia/
object_type.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 core::fmt;
6use core::mem::MaybeUninit;
7
8use munge::munge;
9
10use crate::{
11    Decode, DecodeError, Encodable, Encode, EncodeError, EncodeRef, FromWire, FromWireRef,
12    IntoNatural, Slot, Wire, WireU32,
13};
14
15/// The wire type for [`zx::ObjectType`].
16#[derive(Clone, Copy)]
17#[repr(transparent)]
18pub struct WireObjectType {
19    inner: WireU32,
20}
21
22unsafe impl Wire for WireObjectType {
23    type Decoded<'de> = Self;
24
25    #[inline]
26    fn zero_padding(out: &mut MaybeUninit<Self>) {
27        munge!(let Self { inner } = out);
28        WireU32::zero_padding(inner);
29    }
30}
31
32impl WireObjectType {
33    /// Returns an `ObjectType` with the same value as this wire type.
34    pub fn to_object_type(self) -> zx::ObjectType {
35        zx::ObjectType::from_raw(*self.inner)
36    }
37}
38
39impl fmt::Debug for WireObjectType {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        self.to_object_type().fmt(f)
42    }
43}
44
45unsafe impl<D: ?Sized> Decode<D> for WireObjectType {
46    fn decode(slot: Slot<'_, Self>, decoder: &mut D) -> Result<(), DecodeError> {
47        munge!(let Self { inner } = slot);
48        WireU32::decode(inner, decoder)
49    }
50}
51
52impl Encodable for zx::ObjectType {
53    type Encoded = WireObjectType;
54}
55
56unsafe impl<E: ?Sized> Encode<E> for zx::ObjectType {
57    fn encode(
58        self,
59        encoder: &mut E,
60        out: &mut MaybeUninit<Self::Encoded>,
61    ) -> Result<(), EncodeError> {
62        self.encode_ref(encoder, out)
63    }
64}
65
66unsafe impl<E: ?Sized> EncodeRef<E> for zx::ObjectType {
67    fn encode_ref(
68        &self,
69        encoder: &mut E,
70        out: &mut MaybeUninit<Self::Encoded>,
71    ) -> Result<(), EncodeError> {
72        munge!(let WireObjectType { inner } = out);
73        self.into_raw().encode(encoder, inner)
74    }
75}
76
77impl FromWire<WireObjectType> for zx::ObjectType {
78    fn from_wire(wire: WireObjectType) -> Self {
79        Self::from_wire_ref(&wire)
80    }
81}
82
83impl FromWireRef<WireObjectType> for zx::ObjectType {
84    fn from_wire_ref(wire: &WireObjectType) -> Self {
85        Self::from_raw(*wire.inner)
86    }
87}
88
89impl IntoNatural for WireObjectType {
90    type Natural = zx::ObjectType;
91}