Skip to main content

fidl_next_codec/wire/fuchsia/
status.rs

1// Copyright 2026 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::Status`].
16#[derive(Clone, Copy)]
17#[repr(transparent)]
18pub struct Status {
19    inner: wire::Int32,
20}
21
22impl Constrained for Status {
23    type Constraint = ();
24
25    fn validate(_: Slot<'_, Self>, _: Self::Constraint) -> Result<(), ValidationError> {
26        Ok(())
27    }
28}
29
30// SAFETY:
31// - Lifetime erasure: `Status` has no lifetimes, so `Narrowed` is `Self`.
32// - Padding: `Status` is transparent over `Int32`, which has no padding.
33unsafe impl Wire for Status {
34    type Narrowed<'de> = Self;
35
36    #[inline]
37    fn zero_padding(out: &mut MaybeUninit<Self>) {
38        munge!(let Self { inner } = out);
39        wire::Int32::zero_padding(inner);
40    }
41}
42
43impl Status {
44    /// Returns a `Status` with the same value as this wire type.
45    pub fn to_status(self) -> zx::Status {
46        zx::Status::from_raw(*self.inner)
47    }
48}
49
50impl fmt::Debug for Status {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        self.to_status().fmt(f)
53    }
54}
55
56// SAFETY: `decode` delegates to `Int32::decode`, which initializes the underlying `Int32`
57// and ensures `slot` contains a valid decoded `Status`.
58unsafe impl<D: ?Sized> Decode<D> for Status {
59    fn decode(
60        slot: Slot<'_, Self>,
61        decoder: &mut D,
62        _: Self::Constraint,
63    ) -> Result<(), DecodeError> {
64        munge!(let Self { inner } = slot);
65        wire::Int32::decode(inner, decoder, ())
66    }
67}
68
69// SAFETY: `encode` delegates to the `Encode` implementation of the raw `i32` status value,
70// which initializes all non-padding bytes of `out`.
71unsafe impl<E: ?Sized> Encode<Status, E> for zx::Status {
72    fn encode(
73        self,
74        encoder: &mut E,
75        out: &mut MaybeUninit<Status>,
76        constraint: (),
77    ) -> Result<(), EncodeError> {
78        munge!(let Status { inner } = out);
79        self.into_raw().encode(encoder, inner, constraint)
80    }
81}
82
83// SAFETY: `encode` delegates to `zx::Status`'s `Encode` implementation, which initializes
84// all non-padding bytes of `out`.
85unsafe impl<E: ?Sized> Encode<Status, E> for &zx::Status {
86    fn encode(
87        self,
88        encoder: &mut E,
89        out: &mut MaybeUninit<Status>,
90        constraint: (),
91    ) -> Result<(), EncodeError> {
92        Encode::encode(*self, encoder, out, constraint)
93    }
94}
95
96impl FromWire<Status> for zx::Status {
97    fn from_wire(wire: Status) -> Self {
98        Self::from_wire_ref(&wire)
99    }
100}
101
102impl FromWireRef<Status> for zx::Status {
103    fn from_wire_ref(wire: &Status) -> Self {
104        Self::from_raw(*wire.inner)
105    }
106}
107
108impl IntoNatural for Status {
109    type Natural = zx::Status;
110}