fidl_next_codec/wire/fuchsia/
rights.rs1use 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#[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 {
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 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
60unsafe 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
73unsafe 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
88unsafe 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}