fidl_next_protocol/
framework_error.rs

1// Copyright 2024 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::hint::unreachable_unchecked;
7use core::mem::MaybeUninit;
8
9use fidl_next_codec::{
10    munge, Decode, DecodeError, Encodable, Encode, EncodeError, EncodeRef, FromWire, FromWireRef,
11    Slot, Wire, WireI32,
12};
13
14/// An internal framework error.
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16#[repr(i32)]
17pub enum FrameworkError {
18    /// The protocol method was not recognized by the receiver.
19    UnknownMethod = -2,
20}
21
22/// An internal framework error.
23#[derive(Clone, Copy)]
24#[repr(transparent)]
25pub struct WireFrameworkError {
26    inner: WireI32,
27}
28
29unsafe impl Wire for WireFrameworkError {
30    type Decoded<'de> = Self;
31
32    #[inline]
33    fn zero_padding(_: &mut MaybeUninit<Self>) {}
34}
35
36impl fmt::Debug for WireFrameworkError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        FrameworkError::from(*self).fmt(f)
39    }
40}
41
42impl From<WireFrameworkError> for FrameworkError {
43    fn from(value: WireFrameworkError) -> Self {
44        match *value.inner {
45            -2 => Self::UnknownMethod,
46            _ => unsafe { unreachable_unchecked() },
47        }
48    }
49}
50
51unsafe impl<D: ?Sized> Decode<D> for WireFrameworkError {
52    fn decode(slot: Slot<'_, Self>, _: &mut D) -> Result<(), DecodeError> {
53        munge!(let Self { inner } = slot);
54        match **inner {
55            -2 => Ok(()),
56            code => Err(DecodeError::InvalidFrameworkError(code)),
57        }
58    }
59}
60
61impl Encodable for FrameworkError {
62    type Encoded = WireFrameworkError;
63}
64
65unsafe impl<E: ?Sized> Encode<E> for FrameworkError {
66    fn encode(
67        self,
68        encoder: &mut E,
69        out: &mut MaybeUninit<Self::Encoded>,
70    ) -> Result<(), EncodeError> {
71        self.encode_ref(encoder, out)
72    }
73}
74
75unsafe impl<E: ?Sized> EncodeRef<E> for FrameworkError {
76    fn encode_ref(
77        &self,
78        _: &mut E,
79        out: &mut MaybeUninit<Self::Encoded>,
80    ) -> Result<(), EncodeError> {
81        munge!(let WireFrameworkError { inner } = out);
82        inner.write(WireI32(match self {
83            Self::UnknownMethod => -2,
84        }));
85
86        Ok(())
87    }
88}
89
90impl FromWire<WireFrameworkError> for FrameworkError {
91    #[inline]
92    fn from_wire(wire: WireFrameworkError) -> Self {
93        Self::from_wire_ref(&wire)
94    }
95}
96
97impl FromWireRef<WireFrameworkError> for FrameworkError {
98    #[inline]
99    fn from_wire_ref(wire: &WireFrameworkError) -> Self {
100        Self::from(*wire)
101    }
102}