Skip to main content

fidl_next_codec/wire/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    Constrained, Decode, DecodeError, Encode, EncodeError, FromWire, FromWireRef, IntoNatural,
12    Slot, ValidationError, Wire, wire,
13};
14
15/// The wire type for [`zx::ObjectType`].
16#[derive(Clone, Copy)]
17#[repr(transparent)]
18pub struct ObjectType {
19    inner: wire::Uint32,
20}
21
22impl Constrained for ObjectType {
23    type Constraint = ();
24
25    fn validate(_: Slot<'_, Self>, _: Self::Constraint) -> Result<(), ValidationError> {
26        Ok(())
27    }
28}
29
30// SAFETY: `ObjectType` is a `#[repr(transparent)]` wrapper around `wire::Uint32`, which is `Wire`.
31unsafe impl Wire for ObjectType {
32    type Narrowed<'de> = Self;
33
34    #[inline]
35    fn zero_padding(out: &mut MaybeUninit<Self>) {
36        munge!(let Self { inner } = out);
37        wire::Uint32::zero_padding(inner);
38    }
39}
40
41impl ObjectType {
42    /// Returns an `ObjectType` with the same value as this wire type.
43    pub fn to_object_type(self) -> zx::ObjectType {
44        zx::ObjectType::from_raw(*self.inner)
45    }
46}
47
48impl From<zx::ObjectType> for ObjectType {
49    fn from(value: zx::ObjectType) -> Self {
50        Self { inner: wire::Uint32(value.into_raw()) }
51    }
52}
53
54impl fmt::Debug for ObjectType {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        self.to_object_type().fmt(f)
57    }
58}
59
60// SAFETY: If `decode` returns `Ok`, `slot` is guaranteed to contain a valid decoded `ObjectType`
61// because it delegates to `wire::Uint32::decode` which guarantees the slot is valid.
62unsafe impl<D: ?Sized> Decode<D> for ObjectType {
63    fn decode(
64        slot: Slot<'_, Self>,
65        decoder: &mut D,
66        _: Self::Constraint,
67    ) -> Result<(), DecodeError> {
68        munge!(let Self { inner } = slot);
69        wire::Uint32::decode(inner, decoder, ())
70    }
71}
72
73// SAFETY: `ObjectType` is `#[repr(transparent)]` over `wire::Uint32`. `encode` delegates to
74// the `Encode` implementation for `u32` (via `into_raw()`), which fully initializes the
75// underlying `Uint32`, thus initializing `ObjectType`.
76unsafe impl<E: ?Sized> Encode<ObjectType, E> for zx::ObjectType {
77    fn encode(
78        self,
79        encoder: &mut E,
80        out: &mut MaybeUninit<ObjectType>,
81        constraint: (),
82    ) -> Result<(), EncodeError> {
83        munge!(let ObjectType { inner } = out);
84        self.into_raw().encode(encoder, inner, constraint)
85    }
86}
87
88// SAFETY: Delegates to the `Encode` implementation for `zx::ObjectType`, which is safe.
89unsafe impl<E: ?Sized> Encode<ObjectType, E> for &zx::ObjectType {
90    fn encode(
91        self,
92        encoder: &mut E,
93        out: &mut MaybeUninit<ObjectType>,
94        constraint: (),
95    ) -> Result<(), EncodeError> {
96        Encode::encode(*self, encoder, out, constraint)
97    }
98}
99
100impl FromWire<ObjectType> for zx::ObjectType {
101    fn from_wire(wire: ObjectType) -> Self {
102        Self::from_wire_ref(&wire)
103    }
104}
105
106impl FromWireRef<ObjectType> for zx::ObjectType {
107    fn from_wire_ref(wire: &ObjectType) -> Self {
108        Self::from_raw(*wire.inner)
109    }
110}
111
112impl IntoNatural for ObjectType {
113    type Natural = zx::ObjectType;
114}