Skip to main content

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