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