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
30unsafe impl Wire for Rights {
31    type Narrowed<'de> = Self;
32
33    #[inline]
34    fn zero_padding(out: &mut MaybeUninit<Self>) {
35        munge!(let Self { inner } = out);
36        wire::Uint32::zero_padding(inner);
37    }
38}
39
40impl Rights {
41    /// Returns a `Rights` with the same value as this wire type.
42    pub fn to_rights(self) -> zx::Rights {
43        zx::Rights::from_bits_retain(*self.inner)
44    }
45}
46
47impl fmt::Debug for Rights {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        self.to_rights().fmt(f)
50    }
51}
52
53unsafe impl<D: ?Sized> Decode<D> for Rights {
54    fn decode(
55        slot: Slot<'_, Self>,
56        decoder: &mut D,
57        _: Self::Constraint,
58    ) -> Result<(), DecodeError> {
59        munge!(let Self { inner } = slot);
60        wire::Uint32::decode(inner, decoder, ())
61    }
62}
63
64unsafe impl<E: ?Sized> Encode<Rights, E> for zx::Rights {
65    fn encode(
66        self,
67        encoder: &mut E,
68        out: &mut MaybeUninit<Rights>,
69        constraint: (),
70    ) -> Result<(), EncodeError> {
71        munge!(let Rights { inner } = out);
72        self.bits().encode(encoder, inner, constraint)
73    }
74}
75
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        Encode::encode(*self, encoder, out, constraint)
84    }
85}
86
87impl FromWire<Rights> for zx::Rights {
88    fn from_wire(wire: Rights) -> Self {
89        Self::from_wire_ref(&wire)
90    }
91}
92
93impl FromWireRef<Rights> for zx::Rights {
94    fn from_wire_ref(wire: &Rights) -> Self {
95        Self::from_bits_retain(*wire.inner)
96    }
97}
98
99impl IntoNatural for Rights {
100    type Natural = zx::Rights;
101}